diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java index cb0413cdf..1c809e59d 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java @@ -1,20 +1,31 @@ package us.myles.ViaVersion.api.rewriters; +import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.storage.EntityTracker; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.logging.Logger; -public abstract class MetadataRewriter { +public abstract class MetadataRewriter extends Rewriter { + private final Class entityTrackerClass; - public final void handleMetadata(int entityId, T type, List metadatas, UserConnection connection) { + protected MetadataRewriter(T protocol, Class entityTrackerClass) { + super(protocol); + this.entityTrackerClass = entityTrackerClass; + protocol.put(this); + } + + public final void handleMetadata(int entityId, List metadatas, UserConnection connection) { + EntityType type = connection.get(entityTrackerClass).getEntity(entityId); Map metadataMap = new HashMap<>(metadatas.size()); for (Metadata metadata : metadatas) { metadataMap.put(metadata.getId(), metadata); @@ -41,9 +52,77 @@ public abstract class MetadataRewriter { } } - protected void handleMetadata(int entityId, T type, Metadata metadata, List metadatas, UserConnection connection) throws Exception {} + public void registerMetadataRewriter(int oldPacketId, int newPacketId, Type> oldMetaType, Type> newMetaType) { + getProtocol().registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity ID + if (oldMetaType != null) { + map(oldMetaType, newMetaType); + } else { + map(newMetaType); + } + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int entityId = wrapper.get(Type.VAR_INT, 0); + List metadata = wrapper.get(newMetaType, 0); + handleMetadata(entityId, metadata, wrapper.user()); + } + }); + } + }); + } - protected void handleMetadata(int entityId, T type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) throws Exception { + public void registerMetadataRewriter(int oldPacketId, int newPacketId, Type> metaType) { + registerMetadataRewriter(oldPacketId, newPacketId, null, metaType); + } + + public void registerExtraTracker(int packetId, EntityType entityType, Type intType) { + getProtocol().registerOutgoing(State.PLAY, packetId, packetId, new PacketRemapper() { + @Override + public void registerMap() { + map(intType); // 0 - Entity id + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + wrapper.user().get(entityTrackerClass).addEntity((int) wrapper.get(intType, 0), entityType); + } + }); + } + }); + } + + public void registerExtraTracker(int packetId, EntityType entityType) { + registerExtraTracker(packetId, entityType, Type.VAR_INT); + } + + public void registerEntityDestroy(int oldPacketId, int newPacketId) { + getProtocol().registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT_ARRAY); // 0 - Entity ids + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + EntityTracker entityTracker = wrapper.user().get(entityTrackerClass); + for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) { + entityTracker.removeEntity(entity); + } + } + }); + } + }); + } + + public void registerEntityDestroy(int packetId) { + registerEntityDestroy(packetId, packetId); + } + + protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { + } + + protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) throws Exception { handleMetadata(entityId, type, metadata, metadatas, connection); } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/Rewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/Rewriter.java new file mode 100644 index 000000000..44da9a3d3 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/Rewriter.java @@ -0,0 +1,15 @@ +package us.myles.ViaVersion.api.rewriters; + +import us.myles.ViaVersion.api.protocol.Protocol; + +public abstract class Rewriter { + protected final T protocol; + + protected Rewriter(T protocol) { + this.protocol = protocol; + } + + public T getProtocol() { + return protocol; + } +} 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 index 931b5592c..fcafa91fe 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java +++ b/common/src/main/java/us/myles/ViaVersion/api/storage/EntityTracker.java @@ -6,15 +6,14 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.EntityType; import java.util.Map; -import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; -public abstract class EntityTracker extends StoredObject implements ExternalJoinGameListener { - private final Map clientEntityTypes = new ConcurrentHashMap<>(); +public abstract class EntityTracker extends StoredObject implements ExternalJoinGameListener { + private final Map clientEntityTypes = new ConcurrentHashMap<>(); + private final EntityType playerType; private int clientEntityId; - private final T playerType; - protected EntityTracker(UserConnection user, T playerType) { + protected EntityTracker(UserConnection user, EntityType playerType) { super(user); this.playerType = playerType; } @@ -23,7 +22,7 @@ public abstract class EntityTracker extends StoredObject i clientEntityTypes.remove(entityId); } - public void addEntity(int entityId, T type) { + public void addEntity(int entityId, EntityType type) { clientEntityTypes.put(entityId, type); } @@ -31,8 +30,8 @@ public abstract class EntityTracker extends StoredObject i return clientEntityTypes.containsKey(entityId); } - public Optional getEntity(int entityId) { - return Optional.ofNullable(clientEntityTypes.get(entityId)); + public EntityType getEntity(int entityId) { + return clientEntityTypes.get(entityId); } @Override 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 26fa37ef7..acbcb13d8 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 @@ -21,8 +21,6 @@ import us.myles.ViaVersion.protocols.protocol1_11to1_10.storage.EntityTracker1_1 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 java.util.Optional; - public class Protocol1_11To1_10 extends Protocol { private static final ValueTransformer toOldByte = new ValueTransformer(Type.UNSIGNED_BYTE) { @Override @@ -33,7 +31,7 @@ public class Protocol1_11To1_10 extends Protocol { @Override protected void registerPackets() { - put(new MetadataRewriter1_11To1_10()); + MetadataRewriter1_11To1_10 metadataRewriter = new MetadataRewriter1_11To1_10(this); InventoryPackets.register(this); @@ -93,7 +91,7 @@ public class Protocol1_11To1_10 extends Protocol { // Register Type ID wrapper.user().get(EntityTracker1_11.class).addEntity(entityId, entType); - get(MetadataRewriter1_11To1_10.class).handleMetadata(entityId, entType, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user()); + metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user()); } } }); @@ -143,25 +141,7 @@ public class Protocol1_11To1_10 extends Protocol { }); // Metadata packet - registerOutgoing(State.PLAY, 0x39, 0x39, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - map(Types1_9.METADATA_LIST); // 1 - Metadata list - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); - - Optional type = wrapper.user().get(EntityTracker1_11.class).getEntity(entityId); - if (!type.isPresent()) - return; - - get(MetadataRewriter1_11To1_10.class).handleMetadata(entityId, type.get(), wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user()); - } - }); - } - }); + metadataRewriter.registerMetadataRewriter(0x39, 0x39, Types1_9.METADATA_LIST); // Entity teleport registerOutgoing(State.PLAY, 0x49, 0x49, new PacketRemapper() { @@ -193,20 +173,7 @@ public class Protocol1_11To1_10 extends Protocol { }); // Destroy entities - registerOutgoing(State.PLAY, 0x30, 0x30, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT_ARRAY); // 0 - Entity IDS - - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) - wrapper.user().get(EntityTracker1_11.class).removeEntity(entity); - } - }); - } - }); + metadataRewriter.registerEntityDestroy(0x30); // Title packet registerOutgoing(State.PLAY, 0x45, 0x45, new PacketRemapper() { @@ -416,7 +383,9 @@ public class Protocol1_11To1_10 extends Protocol { @Override public void init(UserConnection userConnection) { + // Entity tracker 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/metadata/MetadataRewriter1_11To1_10.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/metadata/MetadataRewriter1_11To1_10.java index 531cf1a33..84ae81979 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/metadata/MetadataRewriter1_11To1_10.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/metadata/MetadataRewriter1_11To1_10.java @@ -17,15 +17,20 @@ import java.util.List; import java.util.Map; import java.util.Optional; -public class MetadataRewriter1_11To1_10 extends MetadataRewriter { +public class MetadataRewriter1_11To1_10 extends MetadataRewriter { + + public MetadataRewriter1_11To1_10(Protocol1_11To1_10 protocol) { + super(protocol, EntityTracker1_11.class); + } @Override - protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) { + protected void handleMetadata(int entityId, us.myles.ViaVersion.api.entities.EntityType type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) { if (metadata.getValue() instanceof Item) { // Apply rewrite EntityIdRewriter.toClientItem((Item) metadata.getValue()); } + if (type == null) return; if (type.is(EntityType.ELDER_GUARDIAN) || type.is(EntityType.GUARDIAN)) { // Guardians int oldid = metadata.getId(); if (oldid == 12) { 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 index 0148b89b7..10088b9ba 100644 --- 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 @@ -2,13 +2,12 @@ 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.entities.Entity1_11Types; import us.myles.ViaVersion.api.entities.Entity1_11Types.EntityType; import us.myles.ViaVersion.api.storage.EntityTracker; import java.util.Set; -public class EntityTracker1_11 extends EntityTracker { +public class EntityTracker1_11 extends EntityTracker { private final Set holograms = Sets.newConcurrentHashSet(); public EntityTracker1_11(UserConnection user) { 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 2d81b252c..f9bc869ae 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 @@ -26,13 +26,11 @@ import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4 import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import java.util.Optional; - public class Protocol1_12To1_11_1 extends Protocol { @Override protected void registerPackets() { - put(new MetadataRewriter1_12To1_11_1()); + MetadataRewriter1_12To1_11_1 metadataRewriter = new MetadataRewriter1_12To1_11_1(this); InventoryPackets.register(this); // Outgoing @@ -89,7 +87,7 @@ public class Protocol1_12To1_11_1 extends Protocol { Entity1_12Types.EntityType entType = Entity1_12Types.getTypeFromId(type, false); // Register Type ID wrapper.user().get(EntityTracker1_12.class).addEntity(entityId, entType); - get(MetadataRewriter1_12To1_11_1.class).handleMetadata(entityId, entType, wrapper.get(Types1_12.METADATA_LIST, 0), wrapper.user()); + metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_12.METADATA_LIST, 0), wrapper.user()); } }); } @@ -191,22 +189,11 @@ public class Protocol1_12To1_11_1 extends Protocol { registerOutgoing(State.PLAY, 0x25, 0x26); registerOutgoing(State.PLAY, 0x26, 0x27); registerOutgoing(State.PLAY, 0x27, 0x28); + // New packet at 0x30 // Destroy entities - registerOutgoing(State.PLAY, 0x30, 0x31, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT_ARRAY); // 0 - Entity IDS + metadataRewriter.registerEntityDestroy(0x30, 0x31); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) - wrapper.user().get(EntityTracker1_12.class).removeEntity(entity); - } - }); - } - }); registerOutgoing(State.PLAY, 0x31, 0x32); registerOutgoing(State.PLAY, 0x32, 0x33); // Respawn Packet @@ -233,26 +220,10 @@ public class Protocol1_12To1_11_1 extends Protocol { registerOutgoing(State.PLAY, 0x36, 0x38); registerOutgoing(State.PLAY, 0x37, 0x39); registerOutgoing(State.PLAY, 0x38, 0x3a); + // Metadata packet - registerOutgoing(State.PLAY, 0x39, 0x3b, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - map(Types1_12.METADATA_LIST); // 1 - Metadata list - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); + metadataRewriter.registerMetadataRewriter(0x39, 0x3b, Types1_12.METADATA_LIST); - Optional type = wrapper.user().get(EntityTracker1_12.class).getEntity(entityId); - if (!type.isPresent()) - return; - - get(MetadataRewriter1_12To1_11_1.class).handleMetadata(entityId, type.get(), wrapper.get(Types1_12.METADATA_LIST, 0), wrapper.user()); - } - }); - } - }); registerOutgoing(State.PLAY, 0x3a, 0x3c); registerOutgoing(State.PLAY, 0x3b, 0x3d); // registerOutgoing(State.PLAY, 0x3c, 0x3e); - Handled in InventoryPackets diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/metadata/MetadataRewriter1_12To1_11_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/metadata/MetadataRewriter1_12To1_11_1.java index 32b119c13..cf1154c72 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/metadata/MetadataRewriter1_12To1_11_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/metadata/MetadataRewriter1_12To1_11_1.java @@ -2,15 +2,21 @@ package us.myles.ViaVersion.protocols.protocol1_12to1_11_1.metadata; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_12Types; -import us.myles.ViaVersion.api.entities.Entity1_12Types.EntityType; +import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.BedRewriter; +import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.Protocol1_12To1_11_1; +import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.EntityTracker1_12; import java.util.List; -public class MetadataRewriter1_12To1_11_1 extends MetadataRewriter { +public class MetadataRewriter1_12To1_11_1 extends MetadataRewriter { + + public MetadataRewriter1_12To1_11_1(Protocol1_12To1_11_1 protocol) { + super(protocol, EntityTracker1_12.class); + } @Override protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) { @@ -18,8 +24,10 @@ public class MetadataRewriter1_12To1_11_1 extends MetadataRewriter { +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 4b92c31f0..b588662b3 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 @@ -20,7 +20,7 @@ public class Protocol1_13_1To1_13 extends Protocol { @Override protected void registerPackets() { - put(new MetadataRewriter1_13_1To1_13()); + new MetadataRewriter1_13_1To1_13(this); EntityPackets.register(this); InventoryPackets.register(this); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java index b7a4c02be..d52442819 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/metadata/MetadataRewriter1_13_1To1_13.java @@ -2,17 +2,22 @@ package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.metadata; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_13Types; -import us.myles.ViaVersion.api.entities.Entity1_13Types.EntityType; +import us.myles.ViaVersion.api.entities.EntityType; 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_13; import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets.InventoryPackets; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; import java.util.List; -public class MetadataRewriter1_13_1To1_13 extends MetadataRewriter { +public class MetadataRewriter1_13_1To1_13 extends MetadataRewriter { + + public MetadataRewriter1_13_1To1_13(Protocol1_13_1To1_13 protocol) { + super(protocol, EntityTracker1_13.class); + } @Override protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) { @@ -27,13 +32,13 @@ public class MetadataRewriter1_13_1To1_13 extends MetadataRewriter= 7) { + if (type.isOrHasParent(Entity1_13Types.EntityType.ABSTRACT_ARROW) && metadata.getId() >= 7) { metadata.setId(metadata.getId() + 1); // New shooter UUID } } 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 446c01a4b..417051190 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,11 +12,10 @@ import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.metadata.MetadataRewriter1_13_1To1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; -import java.util.Optional; - public class EntityPackets { public static void register(final Protocol protocol) { + MetadataRewriter1_13_1To1_13 metadataRewriter = protocol.get(MetadataRewriter1_13_1To1_13.class); //spawn entity protocol.registerOutgoing(State.PLAY, 0x0, 0x0, new PacketRemapper() { @@ -81,7 +80,7 @@ public class EntityPackets { // Register Type ID wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); - protocol.get(MetadataRewriter1_13_1To1_13.class).handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); + metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); } @@ -108,45 +107,16 @@ public class EntityPackets { Entity1_13Types.EntityType entType = Entity1_13Types.EntityType.PLAYER; // Register Type ID wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); - protocol.get(MetadataRewriter1_13_1To1_13.class).handleMetadata(entityId, entType, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); + metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); } }); } }); + // Destroy entities - protocol.registerOutgoing(State.PLAY, 0x35, 0x35, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT_ARRAY); // 0 - Entity IDS - - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) - wrapper.user().get(EntityTracker1_13.class).removeEntity(entity); - } - }); - } - }); - + metadataRewriter.registerEntityDestroy(0x35); // Metadata packet - protocol.registerOutgoing(State.PLAY, 0x3F, 0x3F, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - map(Types1_13.METADATA_LIST); // 1 - Metadata list - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); - - Optional type = wrapper.user().get(EntityTracker1_13.class).getEntity(entityId); - protocol.get(MetadataRewriter1_13_1To1_13.class).handleMetadata(entityId, type.orElse(null), wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); - } - }); - } - }); + metadataRewriter.registerMetadataRewriter(0x3F, 0x3F, Types1_13.METADATA_LIST); } - } 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 d5a90cd04..db72dfb65 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 @@ -132,7 +132,7 @@ public class Protocol1_13To1_12_2 extends Protocol { @Override protected void registerPackets() { - put(new MetadataRewriter1_13To1_12_2()); + new MetadataRewriter1_13To1_12_2(this); // Register grouped packet changes EntityPackets.register(this); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java index fdb7b3436..31ba053af 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java @@ -2,21 +2,27 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_13Types; -import us.myles.ViaVersion.api.entities.Entity1_13Types.EntityType; +import us.myles.ViaVersion.api.entities.EntityType; 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_13; import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.Particle; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ParticleRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.WorldPackets; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; import java.util.List; import java.util.Map; -public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { +public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { + + public MetadataRewriter1_13To1_12_2(Protocol1_13To1_12_2 protocol) { + super(protocol, EntityTracker1_13.class); + } @Override protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) throws Exception { @@ -50,18 +56,18 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter 14) metadata.setId(metadata.getId() + 1); } // Handle Minecart inner block - if (type.isOrHasParent(EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { + if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { // New block format int oldId = (int) metadata.getValue(); int combined = (((oldId & 4095) << 4) | (oldId >> 12 & 15)); @@ -70,7 +76,7 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter type = wrapper.user().get(EntityTracker1_13.class).getEntity(entityId); - protocol.get(MetadataRewriter1_13To1_12_2.class).handleMetadata(entityId, type.orElse(null), wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); - } - }); - } - }); + metadataRewriter.registerMetadataRewriter(0x3C, 0x3F, Types1_12.METADATA_LIST, Types1_13.METADATA_LIST); } } 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 index f3556f24f..da5ea486b 100644 --- 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 @@ -1,11 +1,10 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_13Types; import us.myles.ViaVersion.api.entities.Entity1_13Types.EntityType; import us.myles.ViaVersion.api.storage.EntityTracker; -public class EntityTracker1_13 extends EntityTracker { +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_14_1to1_14/Protocol1_14_1To1_14.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java index 298913ccc..6dcf02f30 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java @@ -10,7 +10,7 @@ public class Protocol1_14_1To1_14 extends Protocol { @Override protected void registerPackets() { - put(new MetadataRewriter1_14_1To1_14()); + new MetadataRewriter1_14_1To1_14(this); EntityPackets.register(this); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/metadata/MetadataRewriter1_14_1To1_14.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/metadata/MetadataRewriter1_14_1To1_14.java index 3fb76c9f2..529021971 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/metadata/MetadataRewriter1_14_1To1_14.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/metadata/MetadataRewriter1_14_1To1_14.java @@ -2,18 +2,25 @@ package us.myles.ViaVersion.protocols.protocol1_14_1to1_14.metadata; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_14Types; -import us.myles.ViaVersion.api.entities.Entity1_14Types.EntityType; +import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.rewriters.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_14_1to1_14.Protocol1_14_1To1_14; +import us.myles.ViaVersion.protocols.protocol1_14_1to1_14.storage.EntityTracker1_14_1; import java.util.List; -public class MetadataRewriter1_14_1To1_14 extends MetadataRewriter { +public class MetadataRewriter1_14_1To1_14 extends MetadataRewriter { - public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) { + public MetadataRewriter1_14_1To1_14(Protocol1_14_1To1_14 protocol) { + super(protocol, EntityTracker1_14_1.class); + } + + @Override + public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) { if (type == null) return; - if (type.is(EntityType.VILLAGER) || type.is(EntityType.WANDERING_TRADER)) { + if (type == Entity1_14Types.EntityType.VILLAGER || type == Entity1_14Types.EntityType.WANDERING_TRADER) { if (metadata.getId() >= 15) { metadata.setId(metadata.getId() + 1); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/packets/EntityPackets.java index 60d3447a9..bc7e68d12 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/packets/EntityPackets.java @@ -11,11 +11,10 @@ import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_14_1to1_14.metadata.MetadataRewriter1_14_1To1_14; import us.myles.ViaVersion.protocols.protocol1_14_1to1_14.storage.EntityTracker1_14_1; -import java.util.Optional; - public class EntityPackets { public static void register(final Protocol protocol) { + MetadataRewriter1_14_1To1_14 metadataRewriter = protocol.get(MetadataRewriter1_14_1To1_14.class); // Spawn Mob protocol.registerOutgoing(State.PLAY, 0x03, 0x03, new PacketRemapper() { @@ -46,7 +45,7 @@ public class EntityPackets { // Register Type ID wrapper.user().get(EntityTracker1_14_1.class).addEntity(entityId, entType); - protocol.get(MetadataRewriter1_14_1To1_14.class).handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); + metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } }); } @@ -74,29 +73,13 @@ public class EntityPackets { // Register Type ID wrapper.user().get(EntityTracker1_14_1.class).addEntity(entityId, entType); - protocol.get(MetadataRewriter1_14_1To1_14.class).handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); + metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } }); } }); // Entity Metadata - protocol.registerOutgoing(State.PLAY, 0x43, 0x43, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - map(Types1_14.METADATA_LIST); // 1 - Metadata list - - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); - - Optional type = wrapper.user().get(EntityTracker1_14_1.class).getEntity(entityId); - protocol.get(MetadataRewriter1_14_1To1_14.class).handleMetadata(entityId, type.orElse(null), wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); - } - }); - } - }); + metadataRewriter.registerMetadataRewriter(0x43, 0x43, Types1_14.METADATA_LIST); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/storage/EntityTracker1_14_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/storage/EntityTracker1_14_1.java index bff461a03..8356f3f7a 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/storage/EntityTracker1_14_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_1to1_14/storage/EntityTracker1_14_1.java @@ -1,11 +1,10 @@ package us.myles.ViaVersion.protocols.protocol1_14_1to1_14.storage; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_14Types; import us.myles.ViaVersion.api.entities.Entity1_14Types.EntityType; import us.myles.ViaVersion.api.storage.EntityTracker; -public class EntityTracker1_14_1 extends EntityTracker { +public class EntityTracker1_14_1 extends EntityTracker { public EntityTracker1_14_1(UserConnection user) { super(user, EntityType.PLAYER); 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 b2e23bb5b..ec26d7976 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 @@ -25,7 +25,7 @@ public class Protocol1_14To1_13_2 extends Protocol { @Override protected void registerPackets() { - put(new MetadataRewriter1_14To1_13_2()); + new MetadataRewriter1_14To1_13_2(this); InventoryPackets.register(this); EntityPackets.register(this); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java index 11270f48c..fb877e5e2 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java @@ -3,7 +3,7 @@ package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_14Types; -import us.myles.ViaVersion.api.entities.Entity1_14Types.EntityType; +import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.VillagerData; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; @@ -17,7 +17,11 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1 import java.util.List; -public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { +public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { + + public MetadataRewriter1_14To1_13_2(Protocol1_14To1_13_2 protocol) { + super(protocol, EntityTracker1_14.class); + } @Override protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { @@ -41,19 +45,19 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter 11 && type.isOrHasParent(EntityType.LIVINGENTITY)) { + if (metadata.getId() > 11 && type.isOrHasParent(Entity1_14Types.EntityType.LIVINGENTITY)) { metadata.setId(metadata.getId() + 1); } - if (type.isOrHasParent(EntityType.ABSTRACT_INSENTIENT)) { + if (type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_INSENTIENT)) { if (metadata.getId() == 13) { tracker.setInsentientData(entityId, (byte) ((((Number) metadata.getValue()).byteValue() & ~0x4) - | (tracker.getInsentientData(entityId) & 0x4))); // New attacking metadata + | (tracker.getInsentientData(entityId) & 0x4))); // New attacking metadata metadata.setValue(tracker.getInsentientData(entityId)); } } - if (type.isOrHasParent(EntityType.PLAYER)) { + if (type.isOrHasParent(Entity1_14Types.EntityType.PLAYER)) { if (entityId != tracker.getClientEntityId()) { if (metadata.getId() == 0) { byte flags = ((Number) metadata.getValue()).byteValue(); @@ -66,10 +70,10 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter 16) { @@ -77,13 +81,13 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter= 9) { // New piercing metadata.setId(metadata.getId() + 1); } - } else if (type.is(EntityType.FIREWORKS_ROCKET)) { + } else if (type.is(Entity1_14Types.EntityType.FIREWORKS_ROCKET)) { if (metadata.getId() == 8) { if (metadata.getValue().equals(0)) metadata.setValue(null); // https://bugs.mojang.com/browse/MC-111480 metadata.setMetaType(MetaType1_14.OptVarInt); } - } else if (type.isOrHasParent(EntityType.ABSTRACT_SKELETON)) { + } else if (type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_SKELETON)) { if (metadata.getId() == 14) { tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) - | ((boolean) metadata.getValue() ? 0x4 : 0))); // New attacking + | ((boolean) metadata.getValue() ? 0x4 : 0))); // New attacking metadatas.remove(metadata); // "Is swinging arms" metadatas.add(new Metadata(13, MetaType1_14.Byte, tracker.getInsentientData(entityId))); } - } else if (type.is(EntityType.AREA_EFFECT_CLOUD)) { + } else if (type.is(Entity1_14Types.EntityType.AREA_EFFECT_CLOUD)) { if (metadata.getId() == 10) { Particle particle = (Particle) metadata.getValue(); particle.setId(getNewParticleId(particle.getId())); } } - if (type.isOrHasParent(EntityType.ABSTRACT_ILLAGER_BASE)) { + if (type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_ILLAGER_BASE)) { if (metadata.getId() == 14) { tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) - | (((Number) metadata.getValue()).byteValue() != 0 ? 0x4 : 0))); // New attacking + | (((Number) metadata.getValue()).byteValue() != 0 ? 0x4 : 0))); // New attacking metadatas.remove(metadata); // "Has target (aggressive state)" metadatas.add(new Metadata(13, MetaType1_14.Byte, tracker.getInsentientData(entityId))); } } // TODO Are witch and ravager also abstract illagers? They all inherit the new metadata 14 added in 19w13a - if (type.is(EntityType.WITCH) || type.is(EntityType.RAVAGER) || type.isOrHasParent(EntityType.ABSTRACT_ILLAGER_BASE)) { + if (type.is(Entity1_14Types.EntityType.WITCH) || type.is(Entity1_14Types.EntityType.RAVAGER) || type.isOrHasParent(Entity1_14Types.EntityType.ABSTRACT_ILLAGER_BASE)) { if (metadata.getId() >= 14) { // TODO 19w13 added a new boolean (raid participant - is celebrating) with id 14 metadata.setId(metadata.getId() + 1); } 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 75a35c2cc..5db837868 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 @@ -20,11 +20,11 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1 import java.util.LinkedList; import java.util.List; -import java.util.Optional; public class EntityPackets { public static void register(final Protocol protocol) { + MetadataRewriter1_14To1_13_2 metadataRewriter = protocol.get(MetadataRewriter1_14To1_13_2.class); // Spawn entity protocol.registerOutgoing(State.PLAY, 0x00, 0x00, new PacketRemapper() { @@ -135,7 +135,7 @@ public class EntityPackets { // Register Type ID wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, entType); - protocol.get(MetadataRewriter1_14To1_13_2.class).handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); + metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } }); } @@ -174,7 +174,7 @@ public class EntityPackets { Entity1_14Types.EntityType entType = Entity1_14Types.EntityType.PLAYER; // Register Type ID wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, entType); - protocol.get(MetadataRewriter1_14To1_13_2.class).handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); + metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); } }); } @@ -234,37 +234,9 @@ public class EntityPackets { }); // Destroy entities - protocol.registerOutgoing(State.PLAY, 0x35, 0x37, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT_ARRAY); // 0 - Entity IDS - - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) - wrapper.user().get(EntityTracker1_14.class).removeEntity(entity); - } - }); - } - }); + metadataRewriter.registerEntityDestroy(0x35, 0x37); // Metadata packet - protocol.registerOutgoing(State.PLAY, 0x3F, 0x43, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - map(Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST); // 1 - Metadata list - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); - - Optional type = wrapper.user().get(EntityTracker1_14.class).getEntity(entityId); - protocol.get(MetadataRewriter1_14To1_13_2.class).handleMetadata(entityId, type.orElse(null), wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); - } - }); - } - }); + metadataRewriter.registerMetadataRewriter(0x3F, 0x43, Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java index 7a53c384e..8223d3290 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java @@ -4,7 +4,6 @@ import lombok.Getter; import lombok.Setter; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_14Types; import us.myles.ViaVersion.api.entities.Entity1_14Types.EntityType; import us.myles.ViaVersion.api.storage.EntityTracker; import us.myles.ViaVersion.api.type.Type; @@ -14,7 +13,7 @@ import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.WorldPackets; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public class EntityTracker1_14 extends EntityTracker { +public class EntityTracker1_14 extends EntityTracker { private final Map insentientData = new ConcurrentHashMap<>(); // 0x1 = sleeping, 0x2 = riptide private final Map sleepingAndRiptideData = new ConcurrentHashMap<>(); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/MetadataRewriter.java deleted file mode 100644 index 13765bf1a..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/MetadataRewriter.java +++ /dev/null @@ -1,45 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_15to1_14_4; - -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_15Types; -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_14; -import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets.InventoryPackets; - -import java.util.ArrayList; -import java.util.List; - -public class MetadataRewriter { - - public static void handleMetadata(int entityId, Entity1_15Types.EntityType type, List metadatas, UserConnection connection) { - for (Metadata metadata : new ArrayList<>(metadatas)) { - try { - if (metadata.getMetaType() == MetaType1_14.Slot) { - InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_14.BlockID) { - // Convert to new block id - int data = (int) metadata.getValue(); - metadata.setValue(Protocol1_15To1_14_4.getNewBlockStateId(data)); - } - - if (type == null) continue; - - // Metadata 12 added to abstract_living - if (metadata.getId() > 11 && type.isOrHasParent(Entity1_15Types.EntityType.LIVINGENTITY)) { - metadata.setId(metadata.getId() + 1); //TODO is it 11 or 12? what is it for? - } - - //TODO new boolean with id 17 for enderman? - } catch (Exception e) { - metadatas.remove(metadata); - if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Via.getPlatform().getLogger().warning("An error occurred with entity metadata handler"); - Via.getPlatform().getLogger().warning("Metadata: " + metadata); - e.printStackTrace(); - } - } - } - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java index c2e662238..24abe0fca 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java @@ -9,6 +9,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.MappingData; +import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.metadata.MetadataRewriter1_15To1_14_4; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets.EntityPackets; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets.PlayerPackets; @@ -20,7 +21,8 @@ public class Protocol1_15To1_14_4 extends Protocol { @Override protected void registerPackets() { - MappingData.init(); + new MetadataRewriter1_15To1_14_4(this); + EntityPackets.register(this); PlayerPackets.register(this); WorldPackets.register(this); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/metadata/MetadataRewriter1_15To1_14_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/metadata/MetadataRewriter1_15To1_14_4.java new file mode 100644 index 000000000..8e833f386 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/metadata/MetadataRewriter1_15To1_14_4.java @@ -0,0 +1,41 @@ +package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.metadata; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_15Types; +import us.myles.ViaVersion.api.entities.EntityType; +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_14; +import us.myles.ViaVersion.api.rewriters.MetadataRewriter; +import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4; +import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets.InventoryPackets; +import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.storage.EntityTracker1_15; + +import java.util.List; + +public class MetadataRewriter1_15To1_14_4 extends MetadataRewriter { + + public MetadataRewriter1_15To1_14_4(Protocol1_15To1_14_4 protocol) { + super(protocol, EntityTracker1_15.class); + } + + @Override + public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { + if (metadata.getMetaType() == MetaType1_14.Slot) { + InventoryPackets.toClient((Item) metadata.getValue()); + } else if (metadata.getMetaType() == MetaType1_14.BlockID) { + // Convert to new block id + int data = (int) metadata.getValue(); + metadata.setValue(Protocol1_15To1_14_4.getNewBlockStateId(data)); + } + + if (type == null) return; + + // Metadata 12 added to abstract_living + if (metadata.getId() > 11 && type.isOrHasParent(Entity1_15Types.EntityType.LIVINGENTITY)) { + metadata.setId(metadata.getId() + 1); //TODO is it 11 or 12? what is it for? + } + + //TODO new boolean with id 17 for enderman + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java index 7cc60f332..0d545c849 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java @@ -8,16 +8,17 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_14; import us.myles.ViaVersion.packets.State; -import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.MetadataRewriter; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4; +import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.metadata.MetadataRewriter1_15To1_14_4; import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.storage.EntityTracker1_15; -import java.util.Optional; import java.util.UUID; public class EntityPackets { public static void register(Protocol protocol) { + MetadataRewriter1_15To1_14_4 metadataRewriter = protocol.get(MetadataRewriter1_15To1_14_4.class); + // Spawn entity protocol.registerOutgoing(State.PLAY, 0x00, 0x00, new PacketRemapper() { @Override @@ -113,21 +114,7 @@ public class EntityPackets { }); // Metadata packet - protocol.registerOutgoing(State.PLAY, 0x43, 0x44, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - map(Types1_14.METADATA_LIST); // 1 - Metadata list - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); - Optional type = wrapper.user().get(EntityTracker1_15.class).getEntity(entityId); - MetadataRewriter.handleMetadata(entityId, type.orElse(null), wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user()); - } - }); - } - }); + metadataRewriter.registerMetadataRewriter(0x43, 0x44, Types1_14.METADATA_LIST); } public static int getNewEntityId(int oldId) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/storage/EntityTracker1_15.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/storage/EntityTracker1_15.java index 0b800f638..b6f02a024 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/storage/EntityTracker1_15.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/storage/EntityTracker1_15.java @@ -1,12 +1,12 @@ package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.storage; import us.myles.ViaVersion.api.data.UserConnection; -import us.myles.ViaVersion.api.entities.Entity1_15Types; +import us.myles.ViaVersion.api.entities.Entity1_15Types.EntityType; import us.myles.ViaVersion.api.storage.EntityTracker; -public class EntityTracker1_15 extends EntityTracker { +public class EntityTracker1_15 extends EntityTracker { public EntityTracker1_15(UserConnection user) { - super(user, Entity1_15Types.EntityType.PLAYER); + super(user, EntityType.PLAYER); } } 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 23412139c..d524fa93e 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 @@ -73,7 +73,7 @@ public class Protocol1_9To1_8 extends Protocol { @Override protected void registerPackets() { - put(new MetadataRewriter1_9To1_8()); + new MetadataRewriter1_9To1_8(this); // Disconnect workaround (JSON!) registerOutgoing(State.LOGIN, 0x00, 0x00, new PacketRemapper() { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetaIndex.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetaIndex.java index 3c74899fa..8c6340d5d 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetaIndex.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetaIndex.java @@ -3,6 +3,7 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata; import lombok.Getter; import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.entities.Entity1_10Types; +import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_8; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; @@ -146,14 +147,14 @@ public enum MetaIndex { static { for (MetaIndex index : MetaIndex.values()) - metadataRewrites.put(new Pair<>(index.getClazz(), index.getIndex()), index); + metadataRewrites.put(new Pair<>(index.clazz, index.index), index); } - private Entity1_10Types.EntityType clazz; - private int newIndex; - private MetaType1_9 newType; - private MetaType1_8 oldType; - private int index; + private final Entity1_10Types.EntityType clazz; + private final int newIndex; + private final MetaType1_9 newType; + private final MetaType1_8 oldType; + private final int index; MetaIndex(Entity1_10Types.EntityType type, int index, MetaType1_8 oldType, MetaType1_9 newType) { this.clazz = type; @@ -171,13 +172,13 @@ public enum MetaIndex { this.newType = newType; } - private static Optional getIndex(Entity1_10Types.EntityType type, int index) { + private static Optional getIndex(EntityType type, int index) { Pair pair = new Pair<>(type, index); return Optional.ofNullable(metadataRewrites.get(pair)); } - public static MetaIndex searchIndex(Entity1_10Types.EntityType type, int index) { - Entity1_10Types.EntityType currentType = type; + public static MetaIndex searchIndex(EntityType type, int index) { + EntityType currentType = type; do { Optional optMeta = getIndex(currentType, index); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter1_9To1_8.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter1_9To1_8.java index f5a73f341..1f5f7b72d 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter1_9To1_8.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter1_9To1_8.java @@ -2,7 +2,7 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_10Types; -import us.myles.ViaVersion.api.entities.Entity1_10Types.EntityType; +import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.EulerAngle; import us.myles.ViaVersion.api.minecraft.Vector; import us.myles.ViaVersion.api.minecraft.item.Item; @@ -13,12 +13,17 @@ import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; import us.myles.ViaVersion.api.rewriters.MetadataRewriter; 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.EntityTracker1_9; import java.util.List; import java.util.Map; import java.util.UUID; -public class MetadataRewriter1_9To1_8 extends MetadataRewriter { +public class MetadataRewriter1_9To1_8 extends MetadataRewriter { + + public MetadataRewriter1_9To1_8(Protocol1_9To1_8 protocol) { + super(protocol, EntityTracker1_9.class); + } @Override protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, Map metadataMap, UserConnection connection) throws Exception { @@ -35,7 +40,7 @@ public class MetadataRewriter1_9To1_8 extends MetadataRewriter metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityId = wrapper.get(Type.VAR_INT, 0); EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); - Optional type = tracker.getEntity(entityId); - if (type.isPresent()) { - protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, type.get(), metadataList, wrapper.user()); + if (tracker.hasEntity(entityId)) { + protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user()); } else { // Buffer tracker.addMetadataToBuffer(entityId, metadataList); 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 aa90d0c5b..6c70d74ce 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 @@ -22,7 +22,6 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import java.util.ArrayList; import java.util.List; -import java.util.Optional; public class SpawnPackets { public static final ValueTransformer toNewDouble = new ValueTransformer(Type.DOUBLE) { @@ -215,9 +214,8 @@ public class SpawnPackets { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityId = wrapper.get(Type.VAR_INT, 0); EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); - Optional type = tracker.getEntity(entityId); - if (type.isPresent()) { - protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, type.get(), metadataList, wrapper.user()); + if (tracker.hasEntity(entityId)) { + protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user()); } else { Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId); metadataList.clear(); @@ -321,9 +319,8 @@ public class SpawnPackets { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityId = wrapper.get(Type.VAR_INT, 0); EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); - Optional type = tracker.getEntity(entityId); - if (type.isPresent()) { - protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, type.get(), metadataList, wrapper.user()); + if (tracker.hasEntity(entityId)) { + protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user()); } else { Via.getPlatform().getLogger().warning("Unable to find entity for metadata, entity ID: " + entityId); metadataList.clear(); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java index 6a34ac42c..6e8bfed2c 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java @@ -31,7 +31,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; @Getter -public class EntityTracker1_9 extends EntityTracker { +public class EntityTracker1_9 extends EntityTracker { private final Map uuidMap = new ConcurrentHashMap<>(); private final Map> metadataBuffer = new ConcurrentHashMap<>(); private final Map vehicleMap = new ConcurrentHashMap<>(); @@ -116,7 +116,7 @@ public class EntityTracker1_9 extends EntityTracker { } public void handleMetadata(int entityId, List metadataList) { - EntityType type = getEntity(entityId).orElse(null); + us.myles.ViaVersion.api.entities.EntityType type = getEntity(entityId); if (type == null) { return; } @@ -292,7 +292,7 @@ public class EntityTracker1_9 extends EntityTracker { wrapper.write(Type.VAR_INT, entityId); wrapper.write(Types1_9.METADATA_LIST, metadataList); getUser().get(ProtocolInfo.class).getPipeline().getProtocol(Protocol1_9To1_8.class).get(MetadataRewriter1_9To1_8.class) - .handleMetadata(entityId, getEntity(entityId).orElse(null), metadataList, getUser()); + .handleMetadata(entityId, metadataList, getUser()); handleMetadata(entityId, metadataList); if (!metadataList.isEmpty()) { try {