diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java index 23d0acb49..430e0c4f3 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java @@ -116,8 +116,8 @@ public abstract class Protocol { incoming.put(pair, protocolPacket); } - public void cancelIncoming(State state, int newPacketID) { - registerIncoming(state, -1, newPacketID, new PacketRemapper() { + public void cancelIncoming(State state, int oldPacketID, int newPacketID) { + registerIncoming(state, oldPacketID, newPacketID, new PacketRemapper() { @Override public void registerMap() { handler(PacketWrapper::cancel); @@ -125,6 +125,10 @@ public abstract class Protocol { }); } + public void cancelIncoming(State state, int newPacketID) { + cancelOutgoing(state, -1, newPacketID); + } + /** * Register an outgoing packet, with simple id transformation. * @@ -158,8 +162,8 @@ public abstract class Protocol { outgoing.put(pair, protocolPacket); } - public void cancelOutgoing(State state, int oldPacketID) { - registerIncoming(state, oldPacketID, -1, new PacketRemapper() { + public void cancelOutgoing(State state, int oldPacketID, int newPacketID) { + registerIncoming(state, oldPacketID, newPacketID, new PacketRemapper() { @Override public void registerMap() { handler(PacketWrapper::cancel); 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 1c809e59d..a9ba901a0 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 @@ -78,23 +78,76 @@ public abstract class MetadataRewriter extends Rewriter { registerMetadataRewriter(oldPacketId, newPacketId, null, metaType); } - public void registerExtraTracker(int packetId, EntityType entityType, Type intType) { - getProtocol().registerOutgoing(State.PLAY, packetId, packetId, new PacketRemapper() { + /** + * Returns a packethandler to track and rewrite an entity. + * + * @param metaType type of the metadata list + * @return handler for tracking and rewriting entities + */ + public PacketHandler getTrackerAndRewriter(Type> metaType) { + return new PacketHandler() { @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 handle(PacketWrapper wrapper) throws Exception { + int entityId = wrapper.get(Type.VAR_INT, 0); + int type = wrapper.get(Type.VAR_INT, 1); + + int newType = getNewEntityId(type); + if (newType != type) { + wrapper.set(Type.VAR_INT, 1, newType); + } + + EntityType entType = getTypeFromId(newType); + // Register Type ID + wrapper.user().get(entityTrackerClass).addEntity(entityId, entType); + + if (metaType != null) { + handleMetadata(entityId, wrapper.get(metaType, 0), wrapper.user()); + } } - }); + }; } - public void registerExtraTracker(int packetId, EntityType entityType) { - registerExtraTracker(packetId, entityType, Type.VAR_INT); + public PacketHandler getTracker() { + return getTrackerAndRewriter(null); + } + + public PacketHandler getTrackerAndRewriter(Type> metaType, EntityType entityType) { + return new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int entityId = wrapper.get(Type.VAR_INT, 0); + // Register Type ID + wrapper.user().get(entityTrackerClass).addEntity(entityId, entityType); + + if (metaType != null) { + handleMetadata(entityId, wrapper.get(metaType, 0), wrapper.user()); + } + } + }; + } + + /** + * Returns a packethandler to track an object entity. + * + * @return handler for tracking and rewriting entities + */ + public PacketHandler getObjectTracker() { + return new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int entityId = wrapper.get(Type.VAR_INT, 0); + byte type = wrapper.get(Type.BYTE, 0); + + int newType = getNewEntityId(type); + if (newType != type) { + wrapper.set(Type.BYTE, 0, (byte) newType); + } + + EntityType entType = getObjectTypeFromId(newType); + // Register Type ID + wrapper.user().get(entityTrackerClass).addEntity(entityId, entType); + } + }; } public void registerEntityDestroy(int oldPacketId, int newPacketId) { @@ -119,6 +172,16 @@ public abstract class MetadataRewriter extends Rewriter { registerEntityDestroy(packetId, packetId); } + protected abstract EntityType getTypeFromId(int type); + + protected EntityType getObjectTypeFromId(int type) { + return getTypeFromId(type); + } + + protected int getNewEntityId(int oldId) { + return oldId; + } + protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { } 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 27f7b7940..14f0cce58 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 @@ -44,19 +44,7 @@ public class Protocol1_11To1_10 extends Protocol { map(Type.BYTE); // 2 - Type // Track Entity - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - - int entityId = wrapper.get(Type.VAR_INT, 0); - byte type = wrapper.get(Type.BYTE, 0); - - Entity1_11Types.EntityType entType = Entity1_11Types.getTypeFromId(type, true); - - // Register Type ID - wrapper.user().get(EntityTracker1_11.class).addEntity(entityId, entType); - } - }); + handler(metadataRewriter.getObjectTracker()); } }); 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 84ae81979..b181854d9 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 @@ -3,6 +3,7 @@ package us.myles.ViaVersion.protocols.protocol1_11to1_10.metadata; 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.Entity1_11Types; import us.myles.ViaVersion.api.entities.Entity1_11Types.EntityType; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; @@ -130,6 +131,16 @@ public class MetadataRewriter1_11To1_10 extends MetadataRewriter metadata) { Optional optType = EntityType.findById(numType); if (!optType.isPresent()) { 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 84af37f9b..4b6fdcb34 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 @@ -8,7 +8,6 @@ import com.google.gson.JsonParser; 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.Entity1_12Types; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.platform.providers.ViaProviders; @@ -43,19 +42,7 @@ public class Protocol1_12To1_11_1 extends Protocol { map(Type.BYTE); // 2 - Type // Track Entity - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - - int entityId = wrapper.get(Type.VAR_INT, 0); - byte type = wrapper.get(Type.BYTE, 0); - - Entity1_12Types.EntityType entType = Entity1_12Types.getTypeFromId(type, true); - - // Register Type ID - wrapper.user().get(EntityTracker1_12.class).addEntity(entityId, entType); - } - }); + handler(metadataRewriter.getObjectTracker()); } }); @@ -77,19 +64,8 @@ public class Protocol1_12To1_11_1 extends Protocol { map(Type.SHORT); // 11 - Velocity Z map(Types1_12.METADATA_LIST); // 12 - Metadata - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); - // Change Type :) - int type = wrapper.get(Type.VAR_INT, 1); - - Entity1_12Types.EntityType entType = Entity1_12Types.getTypeFromId(type, false); - // Register Type ID - wrapper.user().get(EntityTracker1_12.class).addEntity(entityId, entType); - metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_12.METADATA_LIST, 0), wrapper.user()); - } - }); + // Track mob and rewrite metadata + handler(metadataRewriter.getTrackerAndRewriter(Types1_12.METADATA_LIST)); } }); @@ -272,7 +248,7 @@ public class Protocol1_12To1_11_1 extends Protocol { // Incoming // New packet at 0x01 - cancelIncoming(State.PLAY, 0x01); + cancelIncoming(State.PLAY, 0x01, 0x01); registerIncoming(State.PLAY, 0x01, 0x02); registerIncoming(State.PLAY, 0x02, 0x03); @@ -328,12 +304,12 @@ public class Protocol1_12To1_11_1 extends Protocol { registerIncoming(State.PLAY, 0x15, 0x16); // New packet at 0x17 - cancelIncoming(State.PLAY, 0x17); + cancelIncoming(State.PLAY, 0x17, 0x17); registerIncoming(State.PLAY, 0x16, 0x18); // New packet 0x19 - cancelIncoming(State.PLAY, 0x19); + cancelIncoming(State.PLAY, 0x19, 0x19); registerIncoming(State.PLAY, 0x17, 0x1a); // registerIncoming(State.PLAY, 0x18, 0x1b); - 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 cf1154c72..0a85b80a3 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 @@ -33,4 +33,14 @@ public class MetadataRewriter1_12To1_11_1 extends MetadataRewriter optNewType = EntityTypeRewriter.getNewId(type); - type = optNewType.orElse(type); - Entity1_13Types.EntityType entType = Entity1_13Types.getTypeFromId(type, false); - - wrapper.set(Type.VAR_INT, 1, type); - - - // Register Type ID - wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); - - metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); - } - }); + handler(metadataRewriter.getTrackerAndRewriter(Types1_13.METADATA_LIST)); } }); @@ -132,17 +111,7 @@ public class EntityPackets { map(Type.BYTE); // 6 - Pitch map(Types1_12.METADATA_LIST, Types1_13.METADATA_LIST); // 7 - Metadata - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); - - Entity1_13Types.EntityType entType = Entity1_13Types.EntityType.PLAYER; - // Register Type ID - wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); - metadataRewriter.handleMetadata(entityId, wrapper.get(Types1_13.METADATA_LIST, 0), wrapper.user()); - } - }); + handler(metadataRewriter.getTrackerAndRewriter(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER)); } }); 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 529021971..485363222 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 @@ -26,4 +26,9 @@ public class MetadataRewriter1_14_1To1_14 extends MetadataRewriter } } + @Override + protected EntityType getTypeFromId(int type) { + return Entity1_10Types.getTypeFromId(type, false); + } + + @Override + protected EntityType getObjectTypeFromId(int type) { + return Entity1_10Types.getTypeFromId(type, true); + } } 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 dd61f1ad4..9789b56fe 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 @@ -249,7 +249,7 @@ public class EntityPackets { // Update Entity NBT - protocol.cancelOutgoing(State.PLAY, 0x49); + protocol.cancelOutgoing(State.PLAY, 0x49, 0x49); // Combat Event Packet protocol.registerOutgoing(State.PLAY, 0x42, 0x2C, new PacketRemapper() { 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 3294f7bb8..dd824f76e 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 @@ -402,7 +402,7 @@ public class PlayerPackets { /* Removed packets */ // Set Compression - protocol.cancelOutgoing(State.PLAY, 0x46); + protocol.cancelOutgoing(State.PLAY, 0x46, 0x46); /* Packets which do not have any field remapping or handlers */