From 1cd57b72463cec2e8b4acb6489fc0b8485b56c85 Mon Sep 17 00:00:00 2001 From: Tofaa <82680183+Tofaa2@users.noreply.github.com> Date: Tue, 28 Nov 2023 02:45:21 +0300 Subject: [PATCH] Stable release ready? I think --- README.md | 89 ++++++++++++++++++- build.gradle | 2 +- .../{ => entity}/EntityIdProvider.java | 2 +- .../tofaa/entitylib/entity/WrapperEntity.java | 19 +++- .../me/tofaa/entitylib/meta/EntityMeta.java | 10 ++- 5 files changed, 117 insertions(+), 5 deletions(-) rename src/main/java/me/tofaa/entitylib/{ => entity}/EntityIdProvider.java (92%) diff --git a/README.md b/README.md index 8e6fab9..5435714 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,89 @@ # EntityLib -A PacketEvents based utility library for generating and working with EntityMeta and other Entity features +EntityLib is a PacketEvents addon that provides an abstraction over raw entity data and packets to make it easier to work with entities as a whole. + + +## Features +- Full EntityMeta support +- Creation of WrapperEntities +- Keeping track of entities. + + +## Usage + +For more realistic examples, please take a look at the `test-plugin` module. It has an example `Bukkit` plugin that uses EntityLib. + +### Using the EntityMeta api + +```java +import java.sql.Wrapper; + +class Example { + + public static void main(String[] args) { + PacketEventsAPI api = ;// create PacketEventsAPI instance + EntityLib.init(api); // If failed, it will throw an exception. + + // Making a random entity using packet events raw, i strongly recommend using the EntityLib#createEntity method instead + int entityID = 1; + WrapperPlayServerSpawnEntity packet = new WrapperPlayServerSpawnEntity(constructor args); + EntityMeta meta = EntityLib.createMeta(entityId, EntityType); + // You can cast the meta to the correct type depending on the entity type + PigMeta pigMeta = (PigMeta) meta; + + // Once you're done modifying the meta accordingly, you can convert it to a packet, and send it to whoever you want for them to see the changes. + WrapperPlayServerEntityMetadata metaPacket = meta.createPacket(); + User.sendPacket(metaPacket);; + } + +} +``` + +### Creating a WrapperEntity + +```java +import javax.xml.stream.Location; + +class Example { + + public static void main(String[] args) { + PacketEventsAPI api = ;// create PacketEventsAPI instance + EntityLib.init(api); // If failed, it will throw an exception. + + WrapperEntity entity = EntityLib.createEntity(UUID, EntityType); + // You can keep track of the entity yourself or store its entityId or uuid and fetch it using EntityLib#getEntity + + // Handling entity interactions if needed + // By default EntityLib will ignore interaction packets and not handle them. You can enable this if needed + EntityLib.enableEntityInteractions(); // Now we need to create an interaction handler + EntityLib.setInteractionProcessor(EntityInteractionProcessor); + + + // Entities also have access to the EntityMeta api, the EntityMeta api can be used seperately from wrapper entities but also can be used together + EntityMeta meta = entity.getEntityMeta(); + // Depending on the entity type, you can safely cast the meta to the correct type + PigMeta meta1 = (PigMeta) meta; // If the entity type is EntityTypes.PIG + + // adding a viewer to the entity can be done before or after spawn, doesnt matter + entity.addViewer(User); // UUID also applicable + entity.removeViewer(User); // UUID also applicable + + entity.spawn(Location); // Spawns the entity at the given location + entity.remove(); // Removes the entity from the world + entity.rotateHead(float yaw, float pitch); // Rotates the head of the entity + entity.teleport(Location); // Teleports the entity to the given location. + + // If the entityId provider for WrapperEntities is not working for you or needs changing, you can get it from WrapperEntity#ID_PROVIDER + // You can also set it to a custom provider if needed + WrapperEntity.ID_PROVIDER = new EntityIdProvider() { + @Override + public int getEntityId() { + return 0; + } + }; + + // You can also create the EntityLib default provider by calling EntityIdProvider.simple(); + } + +} + +``` \ No newline at end of file diff --git a/build.gradle b/build.gradle index 5d4059e..39741a5 100644 --- a/build.gradle +++ b/build.gradle @@ -36,6 +36,6 @@ allprojects { dependencies { - compileOnlyApi("com.github.retrooper.packetevents:spigot:2.0.2") + compileOnlyApi("com.github.retrooper.packetevents:spigot:2.1.0") } diff --git a/src/main/java/me/tofaa/entitylib/EntityIdProvider.java b/src/main/java/me/tofaa/entitylib/entity/EntityIdProvider.java similarity index 92% rename from src/main/java/me/tofaa/entitylib/EntityIdProvider.java rename to src/main/java/me/tofaa/entitylib/entity/EntityIdProvider.java index b608629..4c342c2 100644 --- a/src/main/java/me/tofaa/entitylib/EntityIdProvider.java +++ b/src/main/java/me/tofaa/entitylib/entity/EntityIdProvider.java @@ -1,4 +1,4 @@ -package me.tofaa.entitylib; +package me.tofaa.entitylib.entity; import java.util.concurrent.atomic.AtomicInteger; diff --git a/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java b/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java index 603bb5c..8b33dcf 100644 --- a/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java +++ b/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java @@ -5,9 +5,9 @@ import com.github.retrooper.packetevents.protocol.player.User; import com.github.retrooper.packetevents.protocol.world.Location; import com.github.retrooper.packetevents.wrapper.PacketWrapper; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerDestroyEntities; +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityRotation; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityTeleport; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnEntity; -import me.tofaa.entitylib.EntityIdProvider; import me.tofaa.entitylib.EntityLib; import me.tofaa.entitylib.meta.EntityMeta; import org.jetbrains.annotations.NotNull; @@ -28,6 +28,7 @@ public class WrapperEntity { private final Set viewers = new HashSet<>(); private Location location; + private boolean onGround; private boolean spawned; public WrapperEntity(@NotNull UUID uuid, EntityType entityType, EntityMeta meta) { @@ -57,6 +58,21 @@ public class WrapperEntity { return true; } + public void rotateHead(float yaw, float pitch) { + sendPacketToViewers( + new WrapperPlayServerEntityRotation(entityId, yaw, pitch, onGround) + ); + } + + + private static double distance(Location to, Location from) { + double x = to.getX() - from.getX(); + double y = to.getY() - from.getY(); + double z = to.getZ() - from.getZ(); + return Math.sqrt(x * x + y * y + z * z); + } + + public void remove() { if (!spawned) return; spawned = false; @@ -65,6 +81,7 @@ public class WrapperEntity { public void teleport(Location location, boolean onGround) { this.location = location; + this.onGround = onGround; sendPacketToViewers( new WrapperPlayServerEntityTeleport(entityId, location, onGround) ); diff --git a/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java b/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java index 363d763..b90cbe1 100644 --- a/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java +++ b/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java @@ -2,7 +2,9 @@ package me.tofaa.entitylib.meta; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.manager.server.VersionComparison; +import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; +import com.github.retrooper.packetevents.protocol.entity.data.EntityMetadataProvider; import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMetadata; import me.tofaa.entitylib.EntityLib; @@ -10,7 +12,9 @@ import me.tofaa.entitylib.exception.InvalidVersionException; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -public class EntityMeta { +import java.util.List; + +public class EntityMeta implements EntityMetadataProvider { public static final byte OFFSET = 0; public static final byte MAX_OFFSET = OFFSET + 8; @@ -193,4 +197,8 @@ public class EntityMeta { setMask((byte)index, mask); } + @Override + public List entityData() { + return metadata.getEntries(); + } }