implement riding

This commit is contained in:
Tofaa 2024-01-22 22:53:27 +03:00
parent b4cdf0e9cd
commit 3ad53b6fb4
4 changed files with 167 additions and 31 deletions

View file

@ -6,8 +6,9 @@
<component name="ChangeListManager">
<list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/display/ItemDisplayMeta.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/display/ItemDisplayMeta.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/resources/plugin.yml" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/resources/plugin.yml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -70,30 +71,30 @@
<option name="showExcludedFiles" value="false" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Gradle.Build EntityLib.executor": "Run",
"Gradle.EntityLib:test-plugin [runServer].executor": "Debug",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false",
"git-widget-placeholder": "master",
"ignore.virus.scanning.warn.message": "true",
"jdk.selected.JAVA_MODULE": "corretto-17",
"kotlin-language-version-configured": "true",
"last_opened_file_path": "D:/Github/EntityLib",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"project.structure.last.edited": "Modules",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.0",
"settings.editor.selected.configurable": "preferences.pluginManager",
"vue.rearranger.settings.migration": "true"
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;Gradle.Build EntityLib.executor&quot;: &quot;Run&quot;,
&quot;Gradle.EntityLib:test-plugin [runServer].executor&quot;: &quot;Debug&quot;,
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;WebServerToolWindowFactoryState&quot;: &quot;false&quot;,
&quot;git-widget-placeholder&quot;: &quot;master&quot;,
&quot;ignore.virus.scanning.warn.message&quot;: &quot;true&quot;,
&quot;jdk.selected.JAVA_MODULE&quot;: &quot;corretto-17&quot;,
&quot;kotlin-language-version-configured&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;D:/Github/EntityLib&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;project.structure.last.edited&quot;: &quot;Modules&quot;,
&quot;project.structure.proportion&quot;: &quot;0.0&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.0&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;preferences.pluginManager&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
}
}]]></component>
}</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="D:\Github\EntityLib\test-plugin" />
@ -212,7 +213,8 @@
<workItem from="1705192736239" duration="496000" />
<workItem from="1705534524814" duration="15424000" />
<workItem from="1705578156456" duration="78000" />
<workItem from="1705636302508" duration="4010000" />
<workItem from="1705636302508" duration="7111000" />
<workItem from="1705951390204" duration="1790000" />
</task>
<servers />
</component>

View file

@ -11,8 +11,6 @@ import me.tofaa.entitylib.Tickable;
import me.tofaa.entitylib.meta.EntityMeta;
import me.tofaa.entitylib.meta.types.ObjectData;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.util.*;
public class WrapperEntity implements Tickable {
@ -26,6 +24,10 @@ public class WrapperEntity implements Tickable {
private boolean spawned;
protected Vector3d velocity = Vector3d.zero();
private int riding = -1;
private Set<Integer> passengers = new HashSet<>();
public WrapperEntity(int entityId, @NotNull UUID uuid, EntityType entityType, EntityMeta meta) {
this.uuid = Optional.of(uuid);
this.entityType = entityType;
@ -38,6 +40,121 @@ public class WrapperEntity implements Tickable {
sendPacketToViewers(meta.createPacket());
}
/**
* Adds a passenger to the entity. The passenger will be visible to all viewers of the entity.
* @param passenger the entity id of the passenger
*/
public void addPassenger(int passenger) {
if (passengers.contains(passenger)) {
throw new IllegalArgumentException("Passenger already exists");
}
passengers.add(passenger);
sendPacketToViewers(createPassengerPacket());
WrapperEntity e = EntityLib.getEntity(passenger);
if (e != null) {
e.riding = this.entityId;
}
}
/**
* Adds multiple passengers to the entity. The passengers will be visible to all viewers of the entity.
* @param passengers the entity ids of the passengers
*/
public void addPassengers(int... passengers) {
for (int passenger : passengers) {
addPassenger(passenger);
}
}
/**
* Adds a passenger to the entity. The passenger will be visible to all viewers of the entity.
* @param passenger the wrapper entity passenger
*/
public void addPassenger(WrapperEntity passenger) {
addPassenger(passenger.getEntityId());
}
/**
* Adds multiple passengers to the entity. The passengers will be visible to all viewers of the entity.
* @param passengers the wrapper entity passengers
*/
public void addPassengers(WrapperEntity... passengers) {
for (WrapperEntity passenger : passengers) {
addPassenger(passenger);
}
}
/**
* Removes a passenger from the entity. The passenger will be removed from the view of all viewers of the entity.
* @param passenger the entity id of the passenger
*/
public void removePassenger(int passenger) {
if (!passengers.contains(passenger)) {
throw new IllegalArgumentException("Passenger does not exist");
}
passengers.remove(passenger);
sendPacketToViewers(createPassengerPacket());
WrapperEntity e = EntityLib.getEntity(passenger);
if (e != null) {
e.riding = -1;
}
}
/**
* Removes multiple passengers from the entity. The passengers will be removed from the view of all viewers of the entity.
* @param passengers the entity ids of the passengers
*/
public void removePassengers(int... passengers) {
for (int passenger : passengers) {
removePassenger(passenger);
}
}
/**
* Removes a passenger from the entity. The passenger will be removed from the view of all viewers of the entity.
* @param passenger the wrapper entity passenger
*/
public void removePassenger(WrapperEntity passenger) {
removePassenger(passenger.getEntityId());
}
/**
* Removes multiple passengers from the entity. The passengers will be removed from the view of all viewers of the entity.
* @param passengers the wrapper entity passengers
*/
public void removePassengers(WrapperEntity... passengers) {
for (WrapperEntity passenger : passengers) {
removePassenger(passenger);
}
}
/**
* @return true if the entity has passengers, false otherwise
*/
public boolean isRiding() {
return riding != -1;
}
/**
* Clears all passengers from the entity. The passengers will be removed from the view of all viewers of the entity.
*/
public void clearPassengers() {
if (passengers.isEmpty()) return;
passengers.clear();
sendPacketToViewers(createPassengerPacket());
}
/**
* @return the entity id of the entity that the entity is riding, -1 if the entity is not riding
*/
public int getRidingId() {
return riding;
}
protected WrapperPlayServerSetPassengers createPassengerPacket() {
return new WrapperPlayServerSetPassengers(entityId, passengers.stream().mapToInt(i -> i).toArray());
}
public boolean spawn(Location location) {
if (spawned) return false;
this.location = location;
@ -100,7 +217,17 @@ public class WrapperEntity implements Tickable {
rotateHead(entity.getLocation());
}
/**
* Desyncs if the entity is riding some other entity. TODO: fix. This is a temporary solution.
* @return the location of the entity
*/
public Location getLocation() {
if (isRiding()) {
WrapperEntity riding = EntityLib.getEntity(this.riding);
assert riding != null;
return riding.getLocation();
}
return new Location(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
}

View file

@ -3,6 +3,8 @@ package me.tofaa.entitylib;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.item.type.ItemTypes;
import com.github.retrooper.packetevents.protocol.world.Location;
import com.github.retrooper.packetevents.util.Vector3d;
import com.github.retrooper.packetevents.util.Vector3f;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.tofaa.entitylib.entity.WrapperEntity;
@ -51,11 +53,11 @@ public class TestDisplayCommand implements CommandExecutor, TabCompleter {
WrapperEntity e = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.BLOCK_DISPLAY);
BlockDisplayMeta meta = (BlockDisplayMeta) e.getMeta();
meta.setHasGlowingEffect(true);
meta.setBlockId(Material.AMETHYST_BLOCK.getId());
meta.setBlockId(SpigotConversionUtil.fromBukkitBlockData(Material.ACACIA_LOG.createBlockData()).getGlobalId());
meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER);
meta.setScale(new Vector3f(2, 2, 2));
e.addViewer(player.getUniqueId());
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
e.spawn(fromPlayer(player));
}
private void text(Player player) {
@ -66,7 +68,7 @@ public class TestDisplayCommand implements CommandExecutor, TabCompleter {
meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER);
meta.setScale(new Vector3f(2, 2, 2));
e.addViewer(player.getUniqueId());
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
e.spawn(fromPlayer(player));
}
private void item(Player player) {
@ -77,7 +79,11 @@ public class TestDisplayCommand implements CommandExecutor, TabCompleter {
.type(ItemTypes.ACACIA_BOAT).build()
);
e.addViewer(player.getUniqueId());
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
e.spawn(fromPlayer(player));
}
private static Location fromPlayer(Player player) {
return new Location(player.getLocation().getX(), player.getLocation().getY() + 2, player.getLocation().getZ(), 0f, 0f);
}
@Nullable

View file

@ -3,6 +3,7 @@ version: 1.0.0
depend:
- packetevents
main: me.tofaa.entitylib.EntityLibPlugin
api-version: 1.20
commands:
testapi:
description: Test PEEntityMeta API