fix major entityId bug

This commit is contained in:
Tofaa 2024-01-06 04:21:50 +03:00
parent c7983b261c
commit 2517a5d23b
6 changed files with 37 additions and 25 deletions

View file

@ -9,7 +9,6 @@
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/pathfinding" />
<option value="$PROJECT_DIR$/test-plugin" /> <option value="$PROJECT_DIR$/test-plugin" />
</set> </set>
</option> </option>

View file

@ -84,16 +84,9 @@ class Example {
entity.rotateHead(float yaw, float pitch); // Rotates the head of the entity entity.rotateHead(float yaw, float pitch); // Rotates the head of the entity
entity.teleport(Location); // Teleports the entity to the given location. 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 // 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 // You can also set it to a custom provider if needed by calling EntityLib#setEntityIdProvider(EntityIdProvider)
WrapperEntity.ID_PROVIDER = new EntityIdProvider() { int randomEntityId = EntityLib.getEntityIdProvider().provide();
@Override
public int provide() {
return 0;
}
};
// You can also create the EntityLib default provider by calling EntityIdProvider.simple();
} }
} }

View file

@ -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.protocol.player.User;
import com.github.retrooper.packetevents.wrapper.PacketWrapper; import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientInteractEntity; 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.EntityInteractionProcessor;
import me.tofaa.entitylib.entity.WrapperEntity; import me.tofaa.entitylib.entity.WrapperEntity;
import me.tofaa.entitylib.entity.WrapperLivingEntity; import me.tofaa.entitylib.entity.WrapperLivingEntity;
@ -47,6 +48,7 @@ public final class EntityLib {
private static boolean initialized = false; private static boolean initialized = false;
private static PacketEventsAPI<?> packetEvents; private static PacketEventsAPI<?> packetEvents;
private static MetaConverterRegistry metaRegistry; private static MetaConverterRegistry metaRegistry;
private static EntityIdProvider entityIdProvider = EntityIdProvider.simple();
/** /**
* Initialize EntityLib. * Initialize EntityLib.
@ -125,23 +127,28 @@ public final class EntityLib {
* @param entityType the entity type * @param entityType the entity type
* @return the created entity, or null if the entity could not be created * @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(); checkInit();
int id = WrapperEntity.ID_PROVIDER.provide(); if (entities.containsKey(uuid)) throw new RuntimeException("An entity with that uuid already exists");
EntityMeta meta = createMeta(id, entityType); if (entitiesById.containsKey(entityId)) throw new RuntimeException("An entity with that id already exists");
EntityMeta meta = createMeta(entityId, entityType);
if (meta == null) return null; if (meta == null) return null;
WrapperEntity entity; WrapperEntity entity;
if (meta instanceof LivingEntityMeta) { if (meta instanceof LivingEntityMeta) {
entity = new WrapperLivingEntity(uuid, entityType, meta); entity = new WrapperLivingEntity(entityId, uuid, entityType, meta);
} }
else { else {
entity = new WrapperEntity(uuid, entityType, meta); entity = new WrapperEntity(entityId, uuid, entityType, meta);
} }
entities.put(uuid, entity); entities.put(uuid, entity);
entitiesById.put(id, entity); entitiesById.put(entityId, entity);
return entity; return entity;
} }
public static @Nullable WrapperEntity createEntity(@NotNull UUID uuid, EntityType entityType) {
return createEntity(entityIdProvider.provide(), uuid, entityType);
}
/** /**
* @param entityId the entity id * @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. * @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; 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 * Another internal method to verify the server version is supported. Safe to use externally as its purpose is simple and to avoid code duplication
*/ */

View file

@ -15,9 +15,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.*;
public class WrapperEntity { public class WrapperEntity {
public static EntityIdProvider ID_PROVIDER = EntityIdProvider.simple();
private final EntityType entityType; private final EntityType entityType;
private final int entityId; private final int entityId;
private final Optional<UUID> uuid; private final Optional<UUID> uuid;
@ -28,11 +25,11 @@ public class WrapperEntity {
private boolean spawned; private boolean spawned;
private Vector3d velocity = Vector3d.zero(); 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.uuid = Optional.of(uuid);
this.entityType = entityType; this.entityType = entityType;
this.entityId = ID_PROVIDER.provide();
this.meta = meta; this.meta = meta;
this.entityId = entityId;
} }
public void refresh() { public void refresh() {

View file

@ -10,8 +10,8 @@ public class WrapperLivingEntity extends WrapperEntity{
private final WrapperEntityEquipment equipment; private final WrapperEntityEquipment equipment;
public WrapperLivingEntity(@NotNull UUID uuid, EntityType entityType, EntityMeta meta) { public WrapperLivingEntity(int entityId, @NotNull UUID uuid, EntityType entityType, EntityMeta meta) {
super(uuid, entityType, meta); super(entityId, uuid, entityType, meta);
this.equipment = new WrapperEntityEquipment(this); this.equipment = new WrapperEntityEquipment(this);
} }

View file

@ -1,5 +1,6 @@
package me.tofaa.entitylib.extras; package me.tofaa.entitylib.extras;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerAttachEntity;
import net.kyori.adventure.util.RGBLike; import net.kyori.adventure.util.RGBLike;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Range;