3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-09-28 06:31:05 +02:00

Add EntityTrackerBase#hasClientEntityId and don't use -1 as not set indicator (#4143)

Dieser Commit ist enthalten in:
EnZaXD 2024-09-14 10:45:19 +02:00 committet von GitHub
Ursprung e0ce8cc715
Commit fcb472347e
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
2 geänderte Dateien mit 24 neuen und 8 gelöschten Zeilen

Datei anzeigen

@ -102,11 +102,19 @@ public interface EntityTracker {
@Nullable StoredEntityData entityDataIfPresent(int id); @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. * Sets the client entity id.

Datei anzeigen

@ -37,7 +37,7 @@ public class EntityTrackerBase implements EntityTracker, ClientEntityIdChangeLis
protected final Int2ObjectMap<TrackedEntity> entities = new Int2ObjectOpenHashMap<>(); protected final Int2ObjectMap<TrackedEntity> entities = new Int2ObjectOpenHashMap<>();
private final UserConnection connection; private final UserConnection connection;
private final EntityType playerType; private final EntityType playerType;
private int clientEntityId = -1; private Integer clientEntityId;
private int currentWorldSectionHeight = -1; private int currentWorldSectionHeight = -1;
private int currentMinY; private int currentMinY;
private String currentWorld; private String currentWorld;
@ -102,7 +102,15 @@ public class EntityTrackerBase implements EntityTracker, ClientEntityIdChangeLis
} }
@Override @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; return clientEntityId;
} }
@ -110,7 +118,7 @@ public class EntityTrackerBase implements EntityTracker, ClientEntityIdChangeLis
public void setClientEntityId(int clientEntityId) { public void setClientEntityId(int clientEntityId) {
Preconditions.checkNotNull(playerType); Preconditions.checkNotNull(playerType);
final TrackedEntity oldEntity; 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); entities.put(clientEntityId, oldEntity);
} else { } else {
entities.put(clientEntityId, new TrackedEntityImpl(playerType)); entities.put(clientEntityId, new TrackedEntityImpl(playerType));
@ -121,8 +129,8 @@ public class EntityTrackerBase implements EntityTracker, ClientEntityIdChangeLis
@Override @Override
public boolean trackClientEntity() { public boolean trackClientEntity() {
if (clientEntityId != -1) { if (clientEntityId != null) {
entities.put(clientEntityId, new TrackedEntityImpl(playerType)); entities.put(clientEntityId.intValue(), new TrackedEntityImpl(playerType));
return true; return true;
} }
return false; return false;