Skip to content

Commit

Permalink
Improve spectating client entity id tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Sep 20, 2024
1 parent 546f4b1 commit 8945193
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void register() {
map(Types.UNSIGNED_BYTE); // Game mode
map(Types.BYTE); // Dimension
handler(playerTrackerHandler());
handler(wrapper -> wrapper.user().getClientWorld(protocol.getClass()).setEnvironment(wrapper.get(Types.BYTE, 0)));
handler(getDimensionHandler());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

public class PlayerPacketRewriter1_8 extends RewriterBase<Protocol1_8To1_7_6_10> {
Expand Down Expand Up @@ -164,7 +165,7 @@ public void register() {
wrapper.write(Types.BOOLEAN, playerSession.onGround);

final EntityTracker1_8 tracker = wrapper.user().getEntityTracker(Protocol1_8To1_7_6_10.class);
if (tracker.spectatingClientEntityId != tracker.clientEntityId()) {
if (!Objects.equals(tracker.spectatingClientEntityId, tracker.clientEntityIdOrNull())) {
wrapper.cancel();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@
import com.viaversion.viaversion.libs.fastutil.objects.Object2IntOpenHashMap;
import com.viaversion.viaversion.protocols.v1_8to1_9.packet.ClientboundPackets1_8;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.logging.Level;

public class EntityTracker1_8 extends EntityTrackerBase {
Expand All @@ -50,9 +47,9 @@ public class EntityTracker1_8 extends EntityTrackerBase {
private final Int2ObjectMap<UUID> entityIdToUUID = new Int2ObjectArrayMap<>();
private final Object2IntMap<UUID> entityUUIDToId = new Object2IntOpenHashMap<>();

private List<EntityData> entityData = new ArrayList<>();
private final List<EntityData> entityData = new ArrayList<>();

public int spectatingClientEntityId = -1;
public Integer spectatingClientEntityId;
private int clientEntityGameMode;

public EntityTracker1_8(UserConnection connection) {
Expand Down Expand Up @@ -87,12 +84,16 @@ public void clearEntities() {

@Override
public void setClientEntityId(int entityId) {
if (this.hasClientEntityId() && this.spectatingClientEntityId == this.clientEntityId()) {
if (Objects.equals(this.spectatingClientEntityId, clientEntityIdOrNull())) {
this.spectatingClientEntityId = entityId;
}
super.setClientEntityId(entityId);
}

public Integer clientEntityIdOrNull() {
return this.hasClientEntityId() ? this.clientEntityId() : null;
}

public void addPlayer(final int entityId, final UUID uuid) {
entityUUIDToId.put(uuid, entityId);
entityIdToUUID.put(entityId, uuid);
Expand Down

0 comments on commit 8945193

Please sign in to comment.