From 2517a5d23bea2eeebb15a524e88a374c1de65a38 Mon Sep 17 00:00:00 2001 From: Tofaa <82680183+Tofaa2@users.noreply.github.com> Date: Sat, 6 Jan 2024 04:21:50 +0300 Subject: [PATCH] fix major entityId bug --- .idea/gradle.xml | 1 - README.md | 15 +++----- .../java/me/tofaa/entitylib/EntityLib.java | 34 +++++++++++++++---- .../tofaa/entitylib/entity/WrapperEntity.java | 7 ++-- .../entitylib/entity/WrapperLivingEntity.java | 4 +-- .../java/me/tofaa/entitylib/extras/Color.java | 1 + 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/.idea/gradle.xml b/.idea/gradle.xml index f6ba79e..c4adff3 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -9,7 +9,6 @@ diff --git a/README.md b/README.md index a78d880..b752fc5 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ class Example { PacketEventsAPI api = ;// create PacketEventsAPI instance EntityLib.init(api); // If failed, it will throw an exception. - WrapperEntity entity = EntityLib.createEntity(UUID, EntityType); + 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 @@ -84,16 +84,9 @@ class Example { 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 provide() { - return 0; - } - }; - - // You can also create the EntityLib default provider by calling EntityIdProvider.simple(); + // If the entityId provider for WrapperEntities is not working for you or needs changing, you can get it from EntityLib#getEntityIdProvider() + // You can also set it to a custom provider if needed by calling EntityLib#setEntityIdProvider(EntityIdProvider) + int randomEntityId = EntityLib.getEntityIdProvider().provide(); } } diff --git a/src/main/java/me/tofaa/entitylib/EntityLib.java b/src/main/java/me/tofaa/entitylib/EntityLib.java index fdd6d65..160039a 100644 --- a/src/main/java/me/tofaa/entitylib/EntityLib.java +++ b/src/main/java/me/tofaa/entitylib/EntityLib.java @@ -10,6 +10,7 @@ import com.github.retrooper.packetevents.protocol.player.InteractionHand; import com.github.retrooper.packetevents.protocol.player.User; import com.github.retrooper.packetevents.wrapper.PacketWrapper; import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientInteractEntity; +import me.tofaa.entitylib.entity.EntityIdProvider; import me.tofaa.entitylib.entity.EntityInteractionProcessor; import me.tofaa.entitylib.entity.WrapperEntity; import me.tofaa.entitylib.entity.WrapperLivingEntity; @@ -47,6 +48,7 @@ public final class EntityLib { private static boolean initialized = false; private static PacketEventsAPI packetEvents; private static MetaConverterRegistry metaRegistry; + private static EntityIdProvider entityIdProvider = EntityIdProvider.simple(); /** * Initialize EntityLib. @@ -125,23 +127,28 @@ public final class EntityLib { * @param entityType the entity type * @return the created entity, or null if the entity could not be created */ - public static @Nullable WrapperEntity createEntity(UUID uuid, EntityType entityType) { + public static @Nullable WrapperEntity createEntity(int entityId, UUID uuid, EntityType entityType) { checkInit(); - int id = WrapperEntity.ID_PROVIDER.provide(); - EntityMeta meta = createMeta(id, entityType); + if (entities.containsKey(uuid)) throw new RuntimeException("An entity with that uuid already exists"); + if (entitiesById.containsKey(entityId)) throw new RuntimeException("An entity with that id already exists"); + EntityMeta meta = createMeta(entityId, entityType); if (meta == null) return null; WrapperEntity entity; if (meta instanceof LivingEntityMeta) { - entity = new WrapperLivingEntity(uuid, entityType, meta); + entity = new WrapperLivingEntity(entityId, uuid, entityType, meta); } else { - entity = new WrapperEntity(uuid, entityType, meta); + entity = new WrapperEntity(entityId, uuid, entityType, meta); } entities.put(uuid, entity); - entitiesById.put(id, entity); + entitiesById.put(entityId, entity); return entity; } + public static @Nullable WrapperEntity createEntity(@NotNull UUID uuid, EntityType entityType) { + return createEntity(entityIdProvider.provide(), uuid, entityType); + } + /** * @param entityId the entity id * @return the metadata of the entity with the given id. If the entity does not exist, this method will return null. @@ -225,6 +232,21 @@ public final class EntityLib { EntityLib.interactionProcessor = interactionProcessor; } + /** + * @return the entity id provider + */ + public static EntityIdProvider getEntityIdProvider() { + return entityIdProvider; + } + + /** + * Sets the entity id provider to the given one. + * @param entityIdProvider the entity id provider. The default implementation can be found at {@link EntityIdProvider#simple()} + */ + public static void setEntityIdProvider(EntityIdProvider entityIdProvider) { + EntityLib.entityIdProvider = entityIdProvider; + } + /** * Another internal method to verify the server version is supported. Safe to use externally as its purpose is simple and to avoid code duplication */ diff --git a/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java b/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java index e03b9d2..d2a8c67 100644 --- a/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java +++ b/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java @@ -15,9 +15,6 @@ import org.jetbrains.annotations.NotNull; import java.util.*; public class WrapperEntity { - - public static EntityIdProvider ID_PROVIDER = EntityIdProvider.simple(); - private final EntityType entityType; private final int entityId; private final Optional uuid; @@ -28,11 +25,11 @@ public class WrapperEntity { private boolean spawned; private Vector3d velocity = Vector3d.zero(); - public WrapperEntity(@NotNull UUID uuid, EntityType entityType, EntityMeta meta) { + public WrapperEntity(int entityId, @NotNull UUID uuid, EntityType entityType, EntityMeta meta) { this.uuid = Optional.of(uuid); this.entityType = entityType; - this.entityId = ID_PROVIDER.provide(); this.meta = meta; + this.entityId = entityId; } public void refresh() { diff --git a/src/main/java/me/tofaa/entitylib/entity/WrapperLivingEntity.java b/src/main/java/me/tofaa/entitylib/entity/WrapperLivingEntity.java index 9f4d02c..e9d6133 100644 --- a/src/main/java/me/tofaa/entitylib/entity/WrapperLivingEntity.java +++ b/src/main/java/me/tofaa/entitylib/entity/WrapperLivingEntity.java @@ -10,8 +10,8 @@ public class WrapperLivingEntity extends WrapperEntity{ private final WrapperEntityEquipment equipment; - public WrapperLivingEntity(@NotNull UUID uuid, EntityType entityType, EntityMeta meta) { - super(uuid, entityType, meta); + public WrapperLivingEntity(int entityId, @NotNull UUID uuid, EntityType entityType, EntityMeta meta) { + super(entityId, uuid, entityType, meta); this.equipment = new WrapperEntityEquipment(this); } diff --git a/src/main/java/me/tofaa/entitylib/extras/Color.java b/src/main/java/me/tofaa/entitylib/extras/Color.java index 8feba87..1403d39 100644 --- a/src/main/java/me/tofaa/entitylib/extras/Color.java +++ b/src/main/java/me/tofaa/entitylib/extras/Color.java @@ -1,5 +1,6 @@ package me.tofaa.entitylib.extras; +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerAttachEntity; import net.kyori.adventure.util.RGBLike; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Range;