fix major entityId bug
This commit is contained in:
parent
c7983b261c
commit
2517a5d23b
6 changed files with 37 additions and 25 deletions
|
@ -9,7 +9,6 @@
|
|||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/pathfinding" />
|
||||
<option value="$PROJECT_DIR$/test-plugin" />
|
||||
</set>
|
||||
</option>
|
||||
|
|
13
README.md
13
README.md
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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> 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() {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue