From 6911d061a74c752ffb77276fac37cb423145159b Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Wed, 8 May 2019 12:14:41 +0200 Subject: [PATCH] abstract EntityTracker --- .../protocol1_9to1_8/BlockListener.java | 4 +- .../bungee/handlers/BungeeServerHandler.java | 6 +- .../bungee/listeners/ElytraPatch.java | 4 +- .../ViaVersion/api/storage/EntityTracker.java | 46 ++++++++++ .../protocol1_11to1_10/MetadataRewriter.java | 4 +- .../Protocol1_11To1_10.java | 14 +-- .../storage/EntityTracker.java | 56 ------------ .../storage/EntityTracker1_11.java | 37 ++++++++ .../Protocol1_12To1_11_1.java | 12 +-- .../storage/EntityTracker.java | 39 --------- .../storage/EntityTracker1_12.java | 13 +++ .../Protocol1_13_1To1_13.java | 4 +- .../packets/EntityPackets.java | 12 +-- .../Protocol1_13To1_12_2.java | 6 +- .../packets/EntityPackets.java | 12 +-- .../storage/EntityTracker.java | 39 --------- .../storage/EntityTracker1_13.java | 13 +++ .../MetadataRewriter.java | 6 +- .../Protocol1_14To1_13_2.java | 4 +- .../packets/EntityPackets.java | 16 ++-- .../packets/InventoryPackets.java | 6 +- .../packets/WorldPackets.java | 8 +- ...ityTracker.java => EntityTracker1_14.java} | 37 +++----- .../protocol1_9to1_8/Protocol1_9To1_8.java | 2 +- .../protocol1_9to1_8/chat/ChatRewriter.java | 4 +- .../packets/EntityPackets.java | 21 ++--- .../packets/InventoryPackets.java | 4 +- .../packets/PlayerPackets.java | 24 +++--- .../packets/SpawnPackets.java | 55 ++++++------ .../packets/WorldPackets.java | 10 +-- .../providers/CommandBlockProvider.java | 4 +- .../providers/EntityIdProvider.java | 4 +- ...tityTracker.java => EntityTracker1_9.java} | 85 +++++++++---------- .../protocol1_9to1_8/BlockListener.java | 4 +- .../velocity/listeners/ElytraPatch.java | 4 +- 35 files changed, 288 insertions(+), 331 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java rename common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/{EntityTracker.java => EntityTracker1_14.java} (74%) rename common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/{EntityTracker.java => EntityTracker1_9.java} (84%) diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/protocol1_9to1_8/BlockListener.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/protocol1_9to1_8/BlockListener.java index 7ef92f824..0523a409d 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/protocol1_9to1_8/BlockListener.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/listeners/protocol1_9to1_8/BlockListener.java @@ -8,7 +8,7 @@ import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.bukkit.listeners.ViaBukkitListener; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; public class BlockListener extends ViaBukkitListener { @@ -21,7 +21,7 @@ public class BlockListener extends ViaBukkitListener { if (isOnPipe(e.getPlayer())) { Block b = e.getBlockPlaced(); getUserConnection(e.getPlayer()) - .get(EntityTracker.class) + .get(EntityTracker1_9.class) .addBlockInteraction(new Position((long) b.getX(), (long) b.getY(), (long) b.getZ())); } } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeServerHandler.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeServerHandler.java index 9808f1c2b..f6be5f922 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeServerHandler.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeServerHandler.java @@ -26,7 +26,7 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.EntityIdProvider; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -124,7 +124,7 @@ public class BungeeServerHandler implements Listener { if (e.getServer() != null) { if (!e.getServer().getInfo().getName().equals(storage.getCurrentServer())) { // Clear auto-team - EntityTracker oldEntityTracker = user.get(EntityTracker.class); + EntityTracker1_9 oldEntityTracker = user.get(EntityTracker1_9.class); if (oldEntityTracker != null) { if (oldEntityTracker.isAutoTeam() && oldEntityTracker.isTeamExists()) { oldEntityTracker.sendTeamPacket(false, true); @@ -215,7 +215,7 @@ public class BungeeServerHandler implements Listener { protocol.init(user); } - EntityTracker newTracker = user.get(EntityTracker.class); + EntityTracker1_9 newTracker = user.get(EntityTracker1_9.class); if (newTracker != null) { if (Via.getConfig().isAutoTeam()) { String currentTeam = null; diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/ElytraPatch.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/ElytraPatch.java index 8265f8804..b9dc05121 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/ElytraPatch.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/listeners/ElytraPatch.java @@ -13,7 +13,7 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import java.util.Collections; @@ -29,7 +29,7 @@ public class ElytraPatch implements Listener { try { if (user.get(ProtocolInfo.class).getPipeline().contains(Protocol1_9To1_8.class)) { - int entityId = user.get(EntityTracker.class).getProvidedEntityId(); + int entityId = user.get(EntityTracker1_9.class).getProvidedEntityId(); PacketWrapper wrapper = new PacketWrapper(0x39, null, user); diff --git a/common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java new file mode 100644 index 000000000..247d57d27 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java @@ -0,0 +1,46 @@ +package us.myles.ViaVersion.api.storage; + +import com.google.common.base.Optional; +import lombok.Getter; +import lombok.Setter; +import us.myles.ViaVersion.api.data.ExternalJoinGameListener; +import us.myles.ViaVersion.api.data.StoredObject; +import us.myles.ViaVersion.api.data.UserConnection; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public abstract class EntityTracker extends StoredObject implements ExternalJoinGameListener { + private final Map clientEntityTypes = new ConcurrentHashMap<>(); + @Getter + @Setter + private int clientEntityId; + private final T playerType; + + protected EntityTracker(UserConnection user, T playerType) { + super(user); + this.playerType = playerType; + } + + public void removeEntity(int entityId) { + clientEntityTypes.remove(entityId); + } + + public void addEntity(int entityId, T type) { + clientEntityTypes.put(entityId, type); + } + + public boolean hasEntity(int entityId) { + return clientEntityTypes.containsKey(entityId); + } + + public Optional getEntity(int entityId) { + return Optional.fromNullable(clientEntityTypes.get(entityId)); + } + + @Override + public void onExternalJoinGame(int playerEntityId) { + clientEntityId = playerEntityId; + clientEntityTypes.put(playerEntityId, playerType); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/MetadataRewriter.java index 224f63dc7..54824713f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/MetadataRewriter.java @@ -9,7 +9,7 @@ import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; import us.myles.ViaVersion.api.type.Type; -import us.myles.ViaVersion.protocols.protocol1_11to1_10.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_11to1_10.storage.EntityTracker1_11; import java.util.ArrayList; import java.util.List; @@ -177,7 +177,7 @@ public class MetadataRewriter { // Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true if ((data & 0x20) == 0x20 && ((byte) meta.getValue() & 0x01) == 0x01 && ((String) customName.get().getValue()).length() != 0 && (boolean) customNameVisible.get().getValue()) { - EntityTracker tracker = connection.get(EntityTracker.class); + EntityTracker1_11 tracker = connection.get(EntityTracker1_11.class); if (!tracker.isHologram(entityId)) { tracker.addHologram(entityId); try { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java index 9fb7345c1..30562d7e6 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java @@ -17,7 +17,7 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_11to1_10.packets.InventoryPackets; -import us.myles.ViaVersion.protocols.protocol1_11to1_10.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_11to1_10.storage.EntityTracker1_11; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; @@ -52,7 +52,7 @@ public class Protocol1_11To1_10 extends Protocol { Entity1_11Types.EntityType entType = Entity1_11Types.getTypeFromId(type, true); // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_11.class).addEntity(entityId, entType); } }); } @@ -88,7 +88,7 @@ public class Protocol1_11To1_10 extends Protocol { wrapper.set(Type.VAR_INT, 1, entType.getId()); // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_11.class).addEntity(entityId, entType); MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user()); } }); @@ -148,7 +148,7 @@ public class Protocol1_11To1_10 extends Protocol { public void handle(PacketWrapper wrapper) throws Exception { int entityId = wrapper.get(Type.VAR_INT, 0); - Optional type = wrapper.user().get(EntityTracker.class).get(entityId); + Optional type = wrapper.user().get(EntityTracker1_11.class).getEntity(entityId); if (!type.isPresent()) return; @@ -175,7 +175,7 @@ public class Protocol1_11To1_10 extends Protocol { public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); if (Via.getConfig().isHologramPatch()) { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_11 tracker = wrapper.user().get(EntityTracker1_11.class); if (tracker.isHologram(entityID)) { Double newValue = wrapper.get(Type.DOUBLE, 1); newValue -= (Via.getConfig().getHologramYOffset()); @@ -197,7 +197,7 @@ public class Protocol1_11To1_10 extends Protocol { @Override public void handle(PacketWrapper wrapper) throws Exception { for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) - wrapper.user().get(EntityTracker.class).removeEntity(entity); + wrapper.user().get(EntityTracker1_11.class).removeEntity(entity); } }); } @@ -411,7 +411,7 @@ public class Protocol1_11To1_10 extends Protocol { @Override public void init(UserConnection userConnection) { - userConnection.put(new EntityTracker(userConnection)); + userConnection.put(new EntityTracker1_11(userConnection)); if (!userConnection.has(ClientWorld.class)) userConnection.put(new ClientWorld(userConnection)); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker.java deleted file mode 100644 index d86c2dfc9..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker.java +++ /dev/null @@ -1,56 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_11to1_10.storage; - -import com.google.common.base.Optional; -import com.google.common.collect.Sets; -import us.myles.ViaVersion.api.data.ExternalJoinGameListener; -import us.myles.ViaVersion.api.data.StoredObject; -import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_11Types; - -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -public class EntityTracker extends StoredObject implements ExternalJoinGameListener { - private final Map clientEntityTypes = new ConcurrentHashMap<>(); - private final Set holograms = Sets.newConcurrentHashSet(); - - public EntityTracker(UserConnection user) { - super(user); - } - - public void removeEntity(int entityId) { - clientEntityTypes.remove(entityId); - if (isHologram(entityId)) - removeHologram(entityId); - } - - public void addEntity(int entityId, Entity1_11Types.EntityType type) { - clientEntityTypes.put(entityId, type); - } - - public boolean has(int entityId) { - return clientEntityTypes.containsKey(entityId); - } - - public Optional get(int id) { - return Optional.fromNullable(clientEntityTypes.get(id)); - } - - public void addHologram(int entId) { - holograms.add(entId); - } - - public boolean isHologram(int entId) { - return holograms.contains(entId); - } - - public void removeHologram(int entId) { - holograms.remove(entId); - } - - @Override - public void onExternalJoinGame(int playerEntityId) { - clientEntityTypes.put(playerEntityId, Entity1_11Types.EntityType.PLAYER); - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java new file mode 100644 index 000000000..64cf63717 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java @@ -0,0 +1,37 @@ +package us.myles.ViaVersion.protocols.protocol1_11to1_10.storage; + +import com.google.common.collect.Sets; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.storage.EntityTracker; + +import java.util.Set; + +import static us.myles.ViaVersion.api.entities.Entity1_11Types.EntityType; + +public class EntityTracker1_11 extends EntityTracker { + private final Set holograms = Sets.newConcurrentHashSet(); + + public EntityTracker1_11(UserConnection user) { + super(user, EntityType.PLAYER); + } + + @Override + public void removeEntity(int entityId) { + super.removeEntity(entityId); + + if (isHologram(entityId)) + removeHologram(entityId); + } + + public void addHologram(int entId) { + holograms.add(entId); + } + + public boolean isHologram(int entId) { + return holograms.contains(entId); + } + + public void removeHologram(int entId) { + holograms.remove(entId); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java index f3476a84c..4b7142890 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java @@ -21,7 +21,7 @@ import us.myles.ViaVersion.api.type.types.version.Types1_12; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider; -import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.EntityTracker1_12; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; @@ -51,7 +51,7 @@ public class Protocol1_12To1_11_1 extends Protocol { Entity1_12Types.EntityType entType = Entity1_12Types.getTypeFromId(type, true); // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_12.class).addEntity(entityId, entType); } }); } @@ -84,7 +84,7 @@ public class Protocol1_12To1_11_1 extends Protocol { Entity1_12Types.EntityType entType = Entity1_12Types.getTypeFromId(type, false); // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_12.class).addEntity(entityId, entType); MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_12.METADATA_LIST, 0), wrapper.user()); } }); @@ -198,7 +198,7 @@ public class Protocol1_12To1_11_1 extends Protocol { @Override public void handle(PacketWrapper wrapper) throws Exception { for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) - wrapper.user().get(EntityTracker.class).removeEntity(entity); + wrapper.user().get(EntityTracker1_12.class).removeEntity(entity); } }); } @@ -240,7 +240,7 @@ public class Protocol1_12To1_11_1 extends Protocol { public void handle(PacketWrapper wrapper) throws Exception { int entityId = wrapper.get(Type.VAR_INT, 0); - Optional type = wrapper.user().get(EntityTracker.class).get(entityId); + Optional type = wrapper.user().get(EntityTracker1_12.class).getEntity(entityId); if (!type.isPresent()) return; @@ -427,7 +427,7 @@ public class Protocol1_12To1_11_1 extends Protocol { @Override public void init(UserConnection userConnection) { - userConnection.put(new EntityTracker(userConnection)); + userConnection.put(new EntityTracker1_12(userConnection)); if (!userConnection.has(ClientWorld.class)) userConnection.put(new ClientWorld(userConnection)); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker.java deleted file mode 100644 index 94de6b333..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker.java +++ /dev/null @@ -1,39 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage; - -import com.google.common.base.Optional; -import us.myles.ViaVersion.api.data.ExternalJoinGameListener; -import us.myles.ViaVersion.api.data.StoredObject; -import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_12Types; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class EntityTracker extends StoredObject implements ExternalJoinGameListener { - private final Map clientEntityTypes = new ConcurrentHashMap<>(); - - public EntityTracker(UserConnection user) { - super(user); - } - - public void removeEntity(int entityId) { - clientEntityTypes.remove(entityId); - } - - public void addEntity(int entityId, Entity1_12Types.EntityType type) { - clientEntityTypes.put(entityId, type); - } - - public boolean has(int entityId) { - return clientEntityTypes.containsKey(entityId); - } - - public Optional get(int id) { - return Optional.fromNullable(clientEntityTypes.get(id)); - } - - @Override - public void onExternalJoinGame(int playerEntityId) { - clientEntityTypes.put(playerEntityId, Entity1_12Types.EntityType.PLAYER); - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java new file mode 100644 index 000000000..c49d701db --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java @@ -0,0 +1,13 @@ +package us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.storage.EntityTracker; + +import static us.myles.ViaVersion.api.entities.Entity1_12Types.EntityType; + +public class EntityTracker1_12 extends EntityTracker { + + public EntityTracker1_12(UserConnection user) { + super(user, EntityType.PLAYER); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java index e06278f9d..f5ca05f4c 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java @@ -12,7 +12,7 @@ import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets.WorldPackets; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class Protocol1_13_1To1_13 extends Protocol { @@ -205,7 +205,7 @@ public class Protocol1_13_1To1_13 extends Protocol { @Override public void init(UserConnection userConnection) { - userConnection.put(new EntityTracker(userConnection)); + userConnection.put(new EntityTracker1_13(userConnection)); if (!userConnection.has(ClientWorld.class)) userConnection.put(new ClientWorld(userConnection)); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java index c4134183b..1f0c11234 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java @@ -12,7 +12,7 @@ import us.myles.ViaVersion.api.type.types.version.Types1_13; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.MetadataRewriter; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; public class EntityPackets { @@ -47,7 +47,7 @@ public class EntityPackets { } } // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); } }); } @@ -79,7 +79,7 @@ public class EntityPackets { Entity1_13Types.EntityType entType = Entity1_13Types.getTypeFromId(type, false); // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } @@ -107,7 +107,7 @@ public class EntityPackets { Entity1_13Types.EntityType entType = Entity1_13Types.EntityType.PLAYER; // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); @@ -123,7 +123,7 @@ public class EntityPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) - wrapper.user().get(EntityTracker.class).removeEntity(entity); + wrapper.user().get(EntityTracker1_13.class).removeEntity(entity); } }); } @@ -141,7 +141,7 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { int entityId = wrapper.get(Type.VAR_INT, 0); - Optional type = wrapper.user().get(EntityTracker.class).get(entityId); + Optional type = wrapper.user().get(EntityTracker1_13.class).getEntity(entityId); MetadataRewriter.handleMetadata(entityId, type.orNull(), wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index 3aac71e72..a22651d40 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -30,7 +30,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityP import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.PaintingProvider; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.BlockConnectionStorage; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.BlockStorage; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Particle1_13Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; @@ -385,7 +385,7 @@ public class Protocol1_13To1_12_2 extends Protocol { public void handle(PacketWrapper wrapper) throws Exception { // Store the player int entityId = wrapper.get(Type.INT, 0); - wrapper.user().get(EntityTracker.class).addEntity(entityId, Entity1_13Types.EntityType.PLAYER); + wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, Entity1_13Types.EntityType.PLAYER); ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); int dimensionId = wrapper.get(Type.INT, 1); @@ -1134,7 +1134,7 @@ public class Protocol1_13To1_12_2 extends Protocol { @Override public void init(UserConnection userConnection) { - userConnection.put(new EntityTracker(userConnection)); + userConnection.put(new EntityTracker1_13(userConnection)); userConnection.put(new TabCompleteTracker(userConnection)); if (!userConnection.has(ClientWorld.class)) userConnection.put(new ClientWorld(userConnection)); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java index 98c6b5c47..2b848ac14 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java @@ -12,7 +12,7 @@ import us.myles.ViaVersion.api.type.types.version.Types1_13; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.MetadataRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; public class EntityPackets { public static void register(Protocol protocol) { @@ -72,7 +72,7 @@ public class EntityPackets { } // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); } }); } @@ -110,7 +110,7 @@ public class EntityPackets { // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } @@ -138,7 +138,7 @@ public class EntityPackets { Entity1_13Types.EntityType entType = Entity1_13Types.EntityType.PLAYER; // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); @@ -154,7 +154,7 @@ public class EntityPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) - wrapper.user().get(EntityTracker.class).removeEntity(entity); + wrapper.user().get(EntityTracker1_13.class).removeEntity(entity); } }); } @@ -171,7 +171,7 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { int entityId = wrapper.get(Type.VAR_INT, 0); - Optional type = wrapper.user().get(EntityTracker.class).get(entityId); + Optional type = wrapper.user().get(EntityTracker1_13.class).getEntity(entityId); MetadataRewriter.handleMetadata(entityId, type.orNull(), wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker.java deleted file mode 100644 index eddf6c396..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker.java +++ /dev/null @@ -1,39 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage; - -import com.google.common.base.Optional; -import us.myles.ViaVersion.api.data.ExternalJoinGameListener; -import us.myles.ViaVersion.api.data.StoredObject; -import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_13Types; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class EntityTracker extends StoredObject implements ExternalJoinGameListener { - private final Map clientEntityTypes = new ConcurrentHashMap<>(); - - public EntityTracker(UserConnection user) { - super(user); - } - - public void removeEntity(int entityId) { - clientEntityTypes.remove(entityId); - } - - public void addEntity(int entityId, Entity1_13Types.EntityType type) { - clientEntityTypes.put(entityId, type); - } - - public boolean has(int entityId) { - return clientEntityTypes.containsKey(entityId); - } - - public Optional get(int id) { - return Optional.fromNullable(clientEntityTypes.get(id)); - } - - @Override - public void onExternalJoinGame(int playerEntityId) { - clientEntityTypes.put(playerEntityId, Entity1_13Types.EntityType.PLAYER); - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java new file mode 100644 index 000000000..454bcd18e --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java @@ -0,0 +1,13 @@ +package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.storage.EntityTracker; + +import static us.myles.ViaVersion.api.entities.Entity1_13Types.EntityType; + +public class EntityTracker1_13 extends EntityTracker { + + public EntityTracker1_13(UserConnection user) { + super(user, EntityType.PLAYER); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java index 52899f59b..f2015af0e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/MetadataRewriter.java @@ -11,7 +11,7 @@ import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_14; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.Particle; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.InventoryPackets; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; import java.util.ArrayList; import java.util.List; @@ -23,7 +23,7 @@ public class MetadataRewriter { try { metadata.setMetaType(MetaType1_14.byId(metadata.getMetaType().getTypeID())); - EntityTracker tracker = connection.get(EntityTracker.class); + EntityTracker1_14 tracker = connection.get(EntityTracker1_14.class); if (metadata.getMetaType() == MetaType1_14.Slot) { InventoryPackets.toClient((Item) metadata.getValue()); @@ -196,7 +196,7 @@ public class MetadataRewriter { return (entityFlags & 0x80) != 0; } - public static int recalculatePlayerPose(int entityId, EntityTracker tracker) { + public static int recalculatePlayerPose(int entityId, EntityTracker1_14 tracker) { byte flags = tracker.getEntityFlags(entityId); // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize int pose = 0; // standing diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java index d572f45ea..9b2a480f3 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java @@ -13,7 +13,7 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.PlayerPackets; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.WorldPackets; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class Protocol1_14To1_13_2 extends Protocol { @@ -321,7 +321,7 @@ public class Protocol1_14To1_13_2 extends Protocol { @Override public void init(UserConnection userConnection) { - userConnection.put(new EntityTracker(userConnection)); + userConnection.put(new EntityTracker1_14(userConnection)); if (!userConnection.has(ClientWorld.class)) userConnection.put(new ClientWorld(userConnection)); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java index 537455bdb..876480c02 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java @@ -17,7 +17,7 @@ import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.MetadataRewriter; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.EntityTypeRewriter; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; import java.util.LinkedList; import java.util.List; @@ -99,7 +99,7 @@ public class EntityPackets { wrapper.set(Type.VAR_INT, 1, typeId); // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, type1_14); + wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, type1_14); } }); } @@ -137,7 +137,7 @@ public class EntityPackets { wrapper.set(Type.VAR_INT, 1, type); // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, entType); MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } @@ -178,7 +178,7 @@ public class EntityPackets { Entity1_14Types.EntityType entType = Entity1_14Types.EntityType.PLAYER; // Register Type ID - wrapper.user().get(EntityTracker.class).addEntity(entityId, entType); + wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, entType); MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } }); @@ -195,7 +195,7 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { short animation = wrapper.passthrough(Type.UNSIGNED_BYTE); if (animation == 2) { //Leave bed - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_14 tracker = wrapper.user().get(EntityTracker1_14.class); int entityId = wrapper.get(Type.VAR_INT, 0); tracker.setSleeping(entityId, false); @@ -222,7 +222,7 @@ public class EntityPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_14 tracker = wrapper.user().get(EntityTracker1_14.class); int entityId = wrapper.get(Type.VAR_INT, 0); tracker.setSleeping(entityId, true); @@ -248,7 +248,7 @@ public class EntityPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) - wrapper.user().get(EntityTracker.class).removeEntity(entity); + wrapper.user().get(EntityTracker1_14.class).removeEntity(entity); } }); } @@ -265,7 +265,7 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { int entityId = wrapper.get(Type.VAR_INT, 0); - Optional type = wrapper.user().get(EntityTracker.class).get(entityId); + Optional type = wrapper.user().get(EntityTracker1_14.class).getEntity(entityId); MetadataRewriter.handleMetadata(entityId, type.orNull(), wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java index e200c8e6f..fa153eaab 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java @@ -15,7 +15,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.InventoryNameRewriter; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; @@ -153,7 +153,7 @@ public class InventoryPackets { wrapper.read(Type.STRING); // Remove channel int windowId = wrapper.read(Type.INT); - wrapper.user().get(EntityTracker.class).setLatestTradeWindowId(windowId); + wrapper.user().get(EntityTracker1_14.class).setLatestTradeWindowId(windowId); wrapper.write(Type.VAR_INT, windowId); int size = wrapper.passthrough(Type.UNSIGNED_BYTE); @@ -292,7 +292,7 @@ public class InventoryPackets { public void handle(PacketWrapper wrapper) throws Exception { // Selecting trade now moves the items, we need to resync the inventory PacketWrapper resyncPacket = wrapper.create(0x08); - resyncPacket.write(Type.UNSIGNED_BYTE, ((short) wrapper.user().get(EntityTracker.class).getLatestTradeWindowId())); // 0 - Window ID + resyncPacket.write(Type.UNSIGNED_BYTE, ((short) wrapper.user().get(EntityTracker1_14.class).getLatestTradeWindowId())); // 0 - Window ID resyncPacket.write(Type.SHORT, ((short) -999)); // 1 - Slot resyncPacket.write(Type.BYTE, (byte) 2); // 2 - Button - End left click resyncPacket.write(Type.SHORT, ((short) ThreadLocalRandom.current().nextInt())); // 3 - Action number diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index 43b1e9156..6806b55b3 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -18,7 +18,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.MetadataRewriter; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; @@ -195,7 +195,7 @@ public class WorldPackets { lightPacket.write(Type.BYTE_ARRAY, Bytes.asList(section.getBlockLight()).toArray(new Byte[0])); } - EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_14 entityTracker = wrapper.user().get(EntityTracker1_14.class); int diffX = Math.abs(entityTracker.getChunkCenterX() - chunk.getX()); int diffZ = Math.abs(entityTracker.getChunkCenterZ() - chunk.getZ()); if (entityTracker.isForceSendCenterChunk() @@ -291,7 +291,7 @@ public class WorldPackets { Entity1_14Types.EntityType entType = Entity1_14Types.EntityType.PLAYER; // Register Type ID - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_14 tracker = wrapper.user().get(EntityTracker1_14.class); tracker.addEntity(entityId, entType); tracker.setClientEntityId(entityId); } @@ -342,7 +342,7 @@ public class WorldPackets { ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); int dimensionId = wrapper.get(Type.INT, 0); clientWorld.setEnvironment(dimensionId); - EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_14 entityTracker = wrapper.user().get(EntityTracker1_14.class); // The client may reset the center chunk if dimension is changed entityTracker.setForceSendCenterChunk(true); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java similarity index 74% rename from common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java rename to common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java index 1cd3e2dc6..5b8a74ee1 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java @@ -1,13 +1,10 @@ package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage; -import com.google.common.base.Optional; import lombok.Getter; import lombok.Setter; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.data.ExternalJoinGameListener; -import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_14Types; +import us.myles.ViaVersion.api.storage.EntityTracker; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.WorldPackets; @@ -15,8 +12,9 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.WorldPackets; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public class EntityTracker extends StoredObject implements ExternalJoinGameListener { - private final Map clientEntityTypes = new ConcurrentHashMap<>(); +import static us.myles.ViaVersion.api.entities.Entity1_14Types.EntityType; + +public class EntityTracker1_14 extends EntityTracker { private final Map insentientData = new ConcurrentHashMap<>(); // 0x1 = sleeping, 0x2 = riptide private final Map sleepingAndRiptideData = new ConcurrentHashMap<>(); @@ -26,29 +24,24 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe private int latestTradeWindowId; @Getter @Setter - private int clientEntityId; - @Getter - @Setter private boolean forceSendCenterChunk = true; @Getter @Setter private int chunkCenterX, chunkCenterZ; - public EntityTracker(UserConnection user) { - super(user); + public EntityTracker1_14(UserConnection user) { + super(user, EntityType.PLAYER); } + @Override public void removeEntity(int entityId) { - clientEntityTypes.remove(entityId); + super.removeEntity(entityId); + insentientData.remove(entityId); sleepingAndRiptideData.remove(entityId); playerEntityFlags.remove(entityId); } - public void addEntity(int entityId, Entity1_14Types.EntityType type) { - clientEntityTypes.put(entityId, type); - } - public byte getInsentientData(int entity) { Byte val = insentientData.get(entity); return val == null ? 0 : val; @@ -89,18 +82,10 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe } } - public boolean has(int entityId) { - return clientEntityTypes.containsKey(entityId); - } - - public Optional get(int id) { - return Optional.fromNullable(clientEntityTypes.get(id)); - } - @Override public void onExternalJoinGame(int playerEntityId) { - clientEntityId = playerEntityId; - clientEntityTypes.put(playerEntityId, Entity1_14Types.EntityType.PLAYER); + super.onExternalJoinGame(playerEntityId); + PacketWrapper setViewDistance = new PacketWrapper(0x41, null, getUser()); setViewDistance.write(Type.VAR_INT, WorldPackets.SERVERSIDE_VIEW_DISTANCE); try { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java index 974a08940..15411563d 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java @@ -116,7 +116,7 @@ public class Protocol1_9To1_8 extends Protocol { @Override public void init(UserConnection userConnection) { // Entity tracker - userConnection.put(new EntityTracker(userConnection)); + userConnection.put(new EntityTracker1_9(userConnection)); // Chunk tracker userConnection.put(new ClientChunks(userConnection)); // Movement tracker diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/chat/ChatRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/chat/ChatRewriter.java index c26b460ef..4215a01a1 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/chat/ChatRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/chat/ChatRewriter.java @@ -3,7 +3,7 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.chat; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; public class ChatRewriter { /** @@ -15,7 +15,7 @@ public class ChatRewriter { public static void toClient(JsonObject obj, UserConnection user) { //Check gamemode change if (obj.get("translate") != null && obj.get("translate").getAsString().equals("gameMode.changed")) { - String gameMode = user.get(EntityTracker.class).getGameMode().getText(); + String gameMode = user.get(EntityTracker1_9.class).getGameMode().getText(); JsonObject gameModeObject = new JsonObject(); gameModeObject.addProperty("text", gameMode); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java index 4792a00e7..c244fcca5 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java @@ -1,5 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.packets; +import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Pair; @@ -19,7 +20,7 @@ import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import java.util.*; @@ -44,7 +45,7 @@ public class EntityPackets { map(Type.BOOLEAN, new ValueTransformer(Type.NOTHING) { @Override public Void transform(PacketWrapper wrapper, Boolean inputValue) throws Exception { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); if (!inputValue) { int passenger = wrapper.get(Type.INT, 0); int vehicle = wrapper.get(Type.INT, 1); @@ -89,7 +90,7 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); if (Via.getConfig().isHologramPatch()) { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); if (tracker.getKnownHolograms().contains(entityID)) { Double newValue = wrapper.get(Type.DOUBLE, 1); newValue += (Via.getConfig().getHologramYOffset()); @@ -157,7 +158,7 @@ public class EntityPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); int entityID = wrapper.get(Type.VAR_INT, 0); Item stack = wrapper.get(Type.ITEM, 0); @@ -184,10 +185,10 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - Entity1_10Types.EntityType type = tracker.getClientEntityTypes().get(entityID); - if (type != null) { - MetadataRewriter.transform(type, metadataList); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + Optional type = tracker.getEntity(entityID); + if (type.isPresent()) { + MetadataRewriter.transform(type.get(), metadataList); } else { // Buffer tracker.addMetadataToBuffer(entityID, metadataList); @@ -202,7 +203,7 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); tracker.handleMetadata(entityID, metadataList); } }); @@ -283,7 +284,7 @@ public class EntityPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { if (!Via.getConfig().isMinimizeCooldown()) return; - if (wrapper.get(Type.VAR_INT, 0) != wrapper.user().get(EntityTracker.class).getProvidedEntityId()) { + if (wrapper.get(Type.VAR_INT, 0) != wrapper.user().get(EntityTracker1_9.class).getProvidedEntityId()) { return; } int propertiesToRead = wrapper.read(Type.INT); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/InventoryPackets.java index f0d9b77fd..e28a36afe 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/InventoryPackets.java @@ -10,7 +10,7 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.InventoryTracker; public class InventoryPackets { @@ -320,7 +320,7 @@ public class InventoryPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); if (entityTracker.isBlocking()) { entityTracker.setBlocking(false); entityTracker.setSecondHand(null); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/PlayerPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/PlayerPackets.java index 642704f5e..3bada15be 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/PlayerPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/PlayerPackets.java @@ -21,7 +21,7 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.chat.GameMode; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.CommandBlockProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MainHandProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; public class PlayerPackets { public static void register(Protocol protocol) { @@ -131,7 +131,7 @@ public class PlayerPackets { if (mode == 0 || mode == 3 || mode == 4) { String[] players = wrapper.passthrough(Type.STRING_ARRAY); // Players - final EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); + final EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); String myName = wrapper.user().get(ProtocolInfo.class).getUsername(); String teamName = wrapper.get(Type.STRING, 0); for (String player : players) { @@ -153,7 +153,7 @@ public class PlayerPackets { } if (mode == 1) { // Remove team - final EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); + final EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); String teamName = wrapper.get(Type.STRING, 0); if (entityTracker.isAutoTeam() && teamName.equals(entityTracker.getCurrentTeam())) { @@ -179,10 +179,10 @@ public class PlayerPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - int entityID = wrapper.get(Type.INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.getClientEntityTypes().put(entityID, Entity1_10Types.EntityType.PLAYER); - tracker.setEntityID(entityID); + int entityId = wrapper.get(Type.INT, 0); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + tracker.addEntity(entityId, Entity1_10Types.EntityType.PLAYER); + tracker.setClientEntityId(entityId); } }); map(Type.UNSIGNED_BYTE); // 1 - Player Gamemode @@ -195,7 +195,7 @@ public class PlayerPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); tracker.setGameMode(GameMode.getById(wrapper.get(Type.UNSIGNED_BYTE, 0))); //Set player gamemode } }); @@ -214,7 +214,7 @@ public class PlayerPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); if (Via.getConfig().isAutoTeam()) { entityTracker.setAutoTeam(true); // Workaround for packet order issue @@ -364,7 +364,7 @@ public class PlayerPackets { cc.getLoadedChunks().clear(); int gamemode = wrapper.get(Type.UNSIGNED_BYTE, 0); - wrapper.user().get(EntityTracker.class).setGameMode(GameMode.getById(gamemode)); + wrapper.user().get(EntityTracker1_9.class).setGameMode(GameMode.getById(gamemode)); } }); @@ -392,7 +392,7 @@ public class PlayerPackets { public void handle(PacketWrapper wrapper) throws Exception { if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 3) { //Change gamemode int gamemode = wrapper.get(Type.FLOAT, 0).intValue(); - wrapper.user().get(EntityTracker.class).setGameMode(GameMode.getById(gamemode)); + wrapper.user().get(EntityTracker1_9.class).setGameMode(GameMode.getById(gamemode)); } } }); @@ -565,7 +565,7 @@ public class PlayerPackets { int action = wrapper.get(Type.VAR_INT, 0); if (action == 2) { // cancel any blocking >.> - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); if (tracker.isBlocking()) { tracker.setSecondHand(null); tracker.setBlocking(false); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java index 587fe0d12..41c503c18 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java @@ -1,5 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.packets; +import com.google.common.base.Optional; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.entities.Entity1_10Types; @@ -18,7 +19,7 @@ import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata.MetadataRewriter; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import java.util.ArrayList; import java.util.List; @@ -42,7 +43,7 @@ public class SpawnPackets { @Override public void write(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); // 1 - UUID } }); @@ -54,8 +55,8 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); int typeID = wrapper.get(Type.BYTE, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.getClientEntityTypes().put(entityID, Entity1_10Types.getTypeFromId(typeID, true)); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, true)); tracker.sendMetadataBuffer(entityID); } }); @@ -129,8 +130,8 @@ public class SpawnPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.getClientEntityTypes().put(entityID, Entity1_10Types.EntityType.EXPERIENCE_ORB); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + tracker.addEntity(entityID, Entity1_10Types.EntityType.EXPERIENCE_ORB); tracker.sendMetadataBuffer(entityID); } }); @@ -155,8 +156,8 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { // Currently only lightning uses this int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.getClientEntityTypes().put(entityID, Entity1_10Types.EntityType.LIGHTNING); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + tracker.addEntity(entityID, Entity1_10Types.EntityType.LIGHTNING); tracker.sendMetadataBuffer(entityID); } }); @@ -177,7 +178,7 @@ public class SpawnPackets { @Override public void write(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); // 1 - UUID } }); @@ -189,8 +190,8 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); int typeID = wrapper.get(Type.UNSIGNED_BYTE, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.getClientEntityTypes().put(entityID, Entity1_10Types.getTypeFromId(typeID, false)); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, false)); tracker.sendMetadataBuffer(entityID); } }); @@ -213,10 +214,10 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - Entity1_10Types.EntityType type = tracker.getClientEntityTypes().get(entityID); - if (type != null) { - MetadataRewriter.transform(type, metadataList); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + Optional type = tracker.getEntity(entityID); + if (type.isPresent()) { + MetadataRewriter.transform(type.get(), metadataList); } else { Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityID); metadataList.clear(); @@ -229,7 +230,7 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); tracker.handleMetadata(entityID, metadataList); } }); @@ -247,8 +248,8 @@ public class SpawnPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.getClientEntityTypes().put(entityID, Entity1_10Types.EntityType.PAINTING); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + tracker.addEntity(entityID, Entity1_10Types.EntityType.PAINTING); tracker.sendMetadataBuffer(entityID); } }); @@ -258,7 +259,7 @@ public class SpawnPackets { @Override public void write(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); // 1 - UUID } }); @@ -281,8 +282,8 @@ public class SpawnPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.getClientEntityTypes().put(entityID, Entity1_10Types.EntityType.PLAYER); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + tracker.addEntity(entityID, Entity1_10Types.EntityType.PLAYER); tracker.sendMetadataBuffer(entityID); } }); @@ -319,10 +320,10 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - Entity1_10Types.EntityType type = tracker.getClientEntityTypes().get(entityID); - if (type != null) { - MetadataRewriter.transform(type, metadataList); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + Optional type = tracker.getEntity(entityID); + if (type.isPresent()) { + MetadataRewriter.transform(type.get(), metadataList); } else { Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityID); metadataList.clear(); @@ -336,7 +337,7 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); tracker.handleMetadata(entityID, metadataList); } }); @@ -356,7 +357,7 @@ public class SpawnPackets { Integer[] entities = wrapper.get(Type.VAR_INT_ARRAY, 0); for (Integer entity : entities) { // EntityTracker - wrapper.user().get(EntityTracker.class).removeEntity(entity); + wrapper.user().get(EntityTracker1_9.class).removeEntity(entity); } } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java index 9fcc90631..b9076a2f9 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -22,7 +22,7 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.CommandBlockProv import us.myles.ViaVersion.protocols.protocol1_9to1_8.sounds.Effect; import us.myles.ViaVersion.protocols.protocol1_9to1_8.sounds.SoundEffect; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.PlaceBlockTracker; import us.myles.ViaVersion.protocols.protocol1_9to1_8.types.Chunk1_9to1_8Type; @@ -99,7 +99,7 @@ public class WorldPackets { wrapper.set(Type.STRING, 0, newname); wrapper.write(Type.VAR_INT, catid); // Write Category ID if (effect != null && effect.isBreaksound()) { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); int x = wrapper.passthrough(Type.INT); //Position X int y = wrapper.passthrough(Type.INT); //Position Y int z = wrapper.passthrough(Type.INT); //Position Z @@ -268,7 +268,7 @@ public class WorldPackets { public void handle(PacketWrapper wrapper) throws Exception { int status = wrapper.get(Type.UNSIGNED_BYTE, 0); if (status == 5 || status == 4 || status == 3) { - EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); if (entityTracker.isBlocking()) { entityTracker.setBlocking(false); entityTracker.setSecondHand(null); @@ -297,7 +297,7 @@ public class WorldPackets { Item item = Protocol1_9To1_8.getHandItem(wrapper.user()); // Blocking patch if (Via.getConfig().isShieldBlocking()) { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); if (item != null && Protocol1_9To1_8.isSword(item.getId())) { if (hand == 0) { @@ -386,7 +386,7 @@ public class WorldPackets { x++; break; } - EntityTracker tracker = wrapper.user().get(EntityTracker.class); + EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); tracker.addBlockInteraction(new Position(x, y, z)); } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/CommandBlockProvider.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/CommandBlockProvider.java index a2e6fb785..166d3097a 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/CommandBlockProvider.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/CommandBlockProvider.java @@ -9,7 +9,7 @@ import us.myles.ViaVersion.api.platform.providers.Provider; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.CommandBlockStorage; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; public class CommandBlockProvider implements Provider { @@ -41,7 +41,7 @@ public class CommandBlockProvider implements Provider { return; PacketWrapper wrapper = new PacketWrapper(0x1B, null, user); // Entity status - wrapper.write(Type.INT, user.get(EntityTracker.class).getProvidedEntityId()); // Entity ID + wrapper.write(Type.INT, user.get(EntityTracker1_9.class).getProvidedEntityId()); // Entity ID wrapper.write(Type.BYTE, (byte) 26); // Hardcoded op permission level wrapper.send(Protocol1_9To1_8.class); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java index b1a47ff72..6111e1b8f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java @@ -2,11 +2,11 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.providers; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.providers.Provider; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; public class EntityIdProvider implements Provider { public int getEntityId(UserConnection user) throws Exception { - return user.get(EntityTracker.class).getEntityID(); + return user.get(EntityTracker1_9.class).getClientEntityId(); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java similarity index 84% rename from common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java rename to common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java index f5ce676cc..4e5405cc3 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java @@ -10,14 +10,13 @@ import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.boss.BossBar; import us.myles.ViaVersion.api.boss.BossColor; import us.myles.ViaVersion.api.boss.BossStyle; -import us.myles.ViaVersion.api.data.ExternalJoinGameListener; -import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_10Types; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; +import us.myles.ViaVersion.api.storage.EntityTracker; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.protocols.base.ProtocolInfo; @@ -31,10 +30,11 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; +import static us.myles.ViaVersion.api.entities.Entity1_10Types.EntityType; + @Getter -public class EntityTracker extends StoredObject implements ExternalJoinGameListener { +public class EntityTracker1_9 extends EntityTracker { private final Map uuidMap = new ConcurrentHashMap<>(); - private final Map clientEntityTypes = new ConcurrentHashMap<>(); private final Map> metadataBuffer = new ConcurrentHashMap<>(); private final Map vehicleMap = new ConcurrentHashMap<>(); private final Map bossBarMap = new ConcurrentHashMap<>(); @@ -46,8 +46,6 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe @Setter private boolean autoTeam = false; @Setter - private int entityID = -1; - @Setter private Position currentlyDigging = null; private boolean teamExists = false; @Setter @@ -55,8 +53,8 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe @Setter private String currentTeam; - public EntityTracker(UserConnection user) { - super(user); + public EntityTracker1_9(UserConnection user) { + super(user, EntityType.PLAYER); } public UUID getEntityUUID(int id) { @@ -70,7 +68,7 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe } public void setSecondHand(Item item) { - setSecondHand(entityID, item); + setSecondHand(getClientEntityId(), item); } public void setSecondHand(int entityID, Item item) { @@ -85,15 +83,17 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe } } - public void removeEntity(Integer entityID) { - clientEntityTypes.remove(entityID); - vehicleMap.remove(entityID); - uuidMap.remove(entityID); - validBlocking.remove(entityID); - knownHolograms.remove(entityID); - metadataBuffer.remove(entityID); + @Override + public void removeEntity(int entityId) { + super.removeEntity(entityId); - BossBar bar = bossBarMap.remove(entityID); + vehicleMap.remove(entityId); + uuidMap.remove(entityId); + validBlocking.remove(entityId); + knownHolograms.remove(entityId); + metadataBuffer.remove(entityId); + + BossBar bar = bossBarMap.remove(entityId); if (bar != null) { bar.hide(); // Send to provider @@ -117,8 +117,8 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe blockInteractions.put(p, 0); } - public void handleMetadata(int entityID, List metadataList) { - Entity1_10Types.EntityType type = clientEntityTypes.get(entityID); + public void handleMetadata(int entityId, List metadataList) { + Entity1_10Types.EntityType type = getEntity(entityId).orNull(); if (type == null) { return; } @@ -156,16 +156,16 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe if (metadata.getId() == 0) { // Byte byte data = (byte) metadata.getValue(); - if (entityID != getProvidedEntityId() && Via.getConfig().isShieldBlocking()) { + if (entityId != getProvidedEntityId() && Via.getConfig().isShieldBlocking()) { if ((data & 0x10) == 0x10) { - if (validBlocking.contains(entityID)) { + if (validBlocking.contains(entityId)) { Item shield = new Item((short) 442, (byte) 1, (short) 0, null); - setSecondHand(entityID, shield); + setSecondHand(entityId, shield); } else { - setSecondHand(entityID, null); + setSecondHand(entityId, null); } } else { - setSecondHand(entityID, null); + setSecondHand(entityId, null); } } } @@ -183,13 +183,13 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe byte data = (byte) metadata.getValue(); // Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true if ((data & 0x20) == 0x20 && ((byte) meta.getValue() & 0x01) == 0x01 - && ((String) getMetaByIndex(metadataList, 2).getValue()).length() != 0 && (boolean) getMetaByIndex(metadataList, 3).getValue()) { - if (!knownHolograms.contains(entityID)) { - knownHolograms.add(entityID); + && !((String) getMetaByIndex(metadataList, 2).getValue()).isEmpty() && (boolean) getMetaByIndex(metadataList, 3).getValue()) { + if (!knownHolograms.contains(entityId)) { + knownHolograms.add(entityId); try { // Send movement PacketWrapper wrapper = new PacketWrapper(0x25, null, getUser()); - wrapper.write(Type.VAR_INT, entityID); + wrapper.write(Type.VAR_INT, entityId); wrapper.write(Type.SHORT, (short) 0); wrapper.write(Type.SHORT, (short) (128D * (Via.getConfig().getHologramYOffset() * 32D))); wrapper.write(Type.SHORT, (short) 0); @@ -206,12 +206,12 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe if (Via.getConfig().isBossbarPatch()) { if (type == Entity1_10Types.EntityType.ENDER_DRAGON || type == Entity1_10Types.EntityType.WITHER) { if (metadata.getId() == 2) { - BossBar bar = bossBarMap.get(entityID); + BossBar bar = bossBarMap.get(entityId); String title = (String) metadata.getValue(); title = title.isEmpty() ? (type == Entity1_10Types.EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither") : title; if (bar == null) { bar = Via.getAPI().createBossBar(title, BossColor.PINK, BossStyle.SOLID); - bossBarMap.put(entityID, bar); + bossBarMap.put(entityId, bar); bar.addPlayer(uuid); bar.show(); @@ -221,14 +221,14 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe bar.setTitle(title); } } else if (metadata.getId() == 6 && !Via.getConfig().isBossbarAntiflicker()) { // If anti flicker is enabled, don't update health - BossBar bar = bossBarMap.get(entityID); + BossBar bar = bossBarMap.get(entityId); // Make health range between 0 and 1 float maxHealth = type == Entity1_10Types.EntityType.ENDER_DRAGON ? 200.0f : 300.0f; float health = Math.max(0.0f, Math.min(((float) metadata.getValue()) / maxHealth, 1.0f)); if (bar == null) { String title = type == Entity1_10Types.EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither"; bar = Via.getAPI().createBossBar(title, health, BossColor.PINK, BossStyle.SOLID); - bossBarMap.put(entityID, bar); + bossBarMap.put(entityId, bar); bar.addPlayer(uuid); bar.show(); // Send to provider @@ -287,22 +287,22 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe } } - public void sendMetadataBuffer(int entityID) { - List metadataList = metadataBuffer.get(entityID); + public void sendMetadataBuffer(int entityId) { + List metadataList = metadataBuffer.get(entityId); if (metadataList != null) { PacketWrapper wrapper = new PacketWrapper(0x39, null, getUser()); - wrapper.write(Type.VAR_INT, entityID); + wrapper.write(Type.VAR_INT, entityId); wrapper.write(Types1_9.METADATA_LIST, metadataList); - MetadataRewriter.transform(getClientEntityTypes().get(entityID), metadataList); - handleMetadata(entityID, metadataList); - if (metadataList.size() > 0) { + MetadataRewriter.transform(getEntity(entityId).orNull(), metadataList); + handleMetadata(entityId, metadataList); + if (!metadataList.isEmpty()) { try { wrapper.send(Protocol1_9To1_8.class); } catch (Exception e) { e.printStackTrace(); } } - metadataBuffer.remove(entityID); + metadataBuffer.remove(entityId); } } @@ -310,12 +310,7 @@ public class EntityTracker extends StoredObject implements ExternalJoinGameListe try { return Via.getManager().getProviders().get(EntityIdProvider.class).getEntityId(getUser()); } catch (Exception e) { - return entityID; + return getClientEntityId(); } } - - @Override - public void onExternalJoinGame(int playerEntityId) { - clientEntityTypes.put(playerEntityId, Entity1_10Types.EntityType.PLAYER); - } } diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/BlockListener.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/BlockListener.java index 09167c581..b2d5f678e 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/BlockListener.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/listeners/protocol1_9to1_8/BlockListener.java @@ -8,7 +8,7 @@ import org.spongepowered.api.world.Location; import us.myles.ViaVersion.SpongePlugin; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import us.myles.ViaVersion.sponge.listeners.ViaSpongeListener; public class BlockListener extends ViaSpongeListener { @@ -22,7 +22,7 @@ public class BlockListener extends ViaSpongeListener { if (isOnPipe(player.getUniqueId())) { Location loc = e.getTransactions().get(0).getFinal().getLocation().get(); getUserConnection(player.getUniqueId()) - .get(EntityTracker.class) + .get(EntityTracker1_9.class) .addBlockInteraction(new Position((long) loc.getX(), (long) loc.getY(), (long) loc.getZ())); } } diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/listeners/ElytraPatch.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/listeners/ElytraPatch.java index 4c6fabe0f..194a86600 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/listeners/ElytraPatch.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/listeners/ElytraPatch.java @@ -12,7 +12,7 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_9; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import java.util.Collections; @@ -28,7 +28,7 @@ public class ElytraPatch { try { if (user.get(ProtocolInfo.class).getPipeline().contains(Protocol1_9To1_8.class)) { - int entityId = user.get(EntityTracker.class).getProvidedEntityId(); + int entityId = user.get(EntityTracker1_9.class).getProvidedEntityId(); PacketWrapper wrapper = new PacketWrapper(0x39, null, user);