From 3ad53b6fb4759497de2220313e12549969232a35 Mon Sep 17 00:00:00 2001
From: Tofaa <82680183+Tofaa2@users.noreply.github.com>
Date: Mon, 22 Jan 2024 22:53:27 +0300
Subject: [PATCH] implement riding
---
.idea/workspace.xml | 52 +++----
.../tofaa/entitylib/entity/WrapperEntity.java | 131 +++++++++++++++++-
.../tofaa/entitylib/TestDisplayCommand.java | 14 +-
test-plugin/src/main/resources/plugin.yml | 1 +
4 files changed, 167 insertions(+), 31 deletions(-)
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 1314bd3..fa195ae 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -6,8 +6,9 @@
-
+
+
@@ -70,30 +71,30 @@
- {
+ "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"
}
-}]]>
+}
@@ -212,7 +213,8 @@
-
+
+
diff --git a/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java b/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java
index 52f6cb3..f4d6c28 100644
--- a/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java
+++ b/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java
@@ -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 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());
}
diff --git a/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java b/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java
index 4bbadab..0a97087 100644
--- a/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java
+++ b/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java
@@ -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
diff --git a/test-plugin/src/main/resources/plugin.yml b/test-plugin/src/main/resources/plugin.yml
index 86f70f1..7467116 100644
--- a/test-plugin/src/main/resources/plugin.yml
+++ b/test-plugin/src/main/resources/plugin.yml
@@ -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