From fcb472347ef7f871aefd43750ced1d0283cbd375 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Sat, 14 Sep 2024 10:45:19 +0200 Subject: [PATCH] Add EntityTrackerBase#hasClientEntityId and don't use -1 as not set indicator (#4143) --- .../api/data/entity/EntityTracker.java | 14 +++++++++++--- .../data/entity/EntityTrackerBase.java | 18 +++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java b/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java index 11f194e49..8ba1feb81 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java @@ -102,11 +102,19 @@ public interface EntityTracker { @Nullable StoredEntityData entityDataIfPresent(int id); /** - * Returns the client entity id or -1 if unset. + * Returns whether the client entity id has been set. * - * @return client entity id or -1 if unset + * @return whether the client entity id has been set */ - int clientEntityId(); + boolean hasClientEntityId(); + + /** + * Returns the client entity id. Should be wrapped around {@link #hasClientEntityId()}. + * + * @return client entity id + * @throws IllegalStateException if the client entity id has not been set + */ + int clientEntityId() throws IllegalStateException; /** * Sets the client entity id. diff --git a/common/src/main/java/com/viaversion/viaversion/data/entity/EntityTrackerBase.java b/common/src/main/java/com/viaversion/viaversion/data/entity/EntityTrackerBase.java index 06a74bfd2..110ca5ec8 100644 --- a/common/src/main/java/com/viaversion/viaversion/data/entity/EntityTrackerBase.java +++ b/common/src/main/java/com/viaversion/viaversion/data/entity/EntityTrackerBase.java @@ -37,7 +37,7 @@ public class EntityTrackerBase implements EntityTracker, ClientEntityIdChangeLis protected final Int2ObjectMap entities = new Int2ObjectOpenHashMap<>(); private final UserConnection connection; private final EntityType playerType; - private int clientEntityId = -1; + private Integer clientEntityId; private int currentWorldSectionHeight = -1; private int currentMinY; private String currentWorld; @@ -102,7 +102,15 @@ public class EntityTrackerBase implements EntityTracker, ClientEntityIdChangeLis } @Override - public int clientEntityId() { + public boolean hasClientEntityId() { + return clientEntityId != null; + } + + @Override + public int clientEntityId() throws IllegalStateException { + if (clientEntityId == null) { + throw new IllegalStateException("Client entity id not set"); + } return clientEntityId; } @@ -110,7 +118,7 @@ public class EntityTrackerBase implements EntityTracker, ClientEntityIdChangeLis public void setClientEntityId(int clientEntityId) { Preconditions.checkNotNull(playerType); final TrackedEntity oldEntity; - if (this.clientEntityId != -1 && (oldEntity = entities.remove(this.clientEntityId)) != null) { + if (this.clientEntityId != null && (oldEntity = entities.remove(this.clientEntityId.intValue())) != null) { entities.put(clientEntityId, oldEntity); } else { entities.put(clientEntityId, new TrackedEntityImpl(playerType)); @@ -121,8 +129,8 @@ public class EntityTrackerBase implements EntityTracker, ClientEntityIdChangeLis @Override public boolean trackClientEntity() { - if (clientEntityId != -1) { - entities.put(clientEntityId, new TrackedEntityImpl(playerType)); + if (clientEntityId != null) { + entities.put(clientEntityId.intValue(), new TrackedEntityImpl(playerType)); return true; } return false;