Skip to content

Commit

Permalink
fix major entityId bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofaa2 committed Jan 6, 2024
1 parent c7983b2 commit 2517a5d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}

}
Expand Down
34 changes: 28 additions & 6 deletions src/main/java/me/tofaa/entitylib/EntityLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -47,6 +48,7 @@ private EntityLib() {}
private static boolean initialized = false;
private static PacketEventsAPI<?> packetEvents;
private static MetaConverterRegistry metaRegistry;
private static EntityIdProvider entityIdProvider = EntityIdProvider.simple();

/**
* Initialize EntityLib.
Expand Down Expand Up @@ -125,23 +127,28 @@ public void onPacketReceive(PacketReceiveEvent event) {
* @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.
Expand Down Expand Up @@ -225,6 +232,21 @@ public static void setInteractionProcessor(EntityInteractionProcessor interactio
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
*/
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/me/tofaa/entitylib/extras/Color.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 2517a5d

Please sign in to comment.