From 647bbbedb00c73fba726109fb00383171842bb06 Mon Sep 17 00:00:00 2001 From: Marco Neuhaus Date: Wed, 8 Aug 2018 21:45:54 +0200 Subject: [PATCH] add items remapping --- .../MetadataRewriter.java | 9 +- .../Protocol18w32aTO1_13.java | 2 + .../packets/InventoryPackets.java | 137 ++++++++++++++++++ 3 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/packets/InventoryPackets.java diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/MetadataRewriter.java index b2ef64de7..7e666b529 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/MetadataRewriter.java @@ -3,8 +3,10 @@ package us.myles.ViaVersion.protocols.protocol18w32ato1_13; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.Entity1_13Types; +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.protocols.protocol1_13to1_12_2.packets.InventoryPackets; import java.util.ArrayList; import java.util.List; @@ -14,12 +16,15 @@ public class MetadataRewriter { public static void handleMetadata(int entityId, Entity1_13Types.EntityType type, List metadatas, UserConnection connection) { for (Metadata metadata : new ArrayList<>(metadatas)) { try { - if (metadata.getMetaType() == MetaType1_13.BlockID) { + // 1.13 changed item to flat item (no data) + if (metadata.getMetaType() == MetaType1_13.Slot) { + InventoryPackets.toClient((Item) metadata.getValue()); + } else if (metadata.getMetaType() == MetaType1_13.BlockID) { // Convert to new block id int data = (int) metadata.getValue(); metadata.setValue(Protocol18w32aTO1_13.getMapBlockId(data)); } - if(type == null) continue; + if (type == null) continue; if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { // New block format int data = (int) metadata.getValue(); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/Protocol18w32aTO1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/Protocol18w32aTO1_13.java index 812ef1bf6..1665499f5 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/Protocol18w32aTO1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/Protocol18w32aTO1_13.java @@ -14,6 +14,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_13; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.protocol18w32ato1_13.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; @@ -22,6 +23,7 @@ public class Protocol18w32aTO1_13 extends Protocol { @Override protected void registerPackets() { + InventoryPackets.register(this); //Tab complete registerIncoming(State.PLAY, 0x05, 0x05, new PacketRemapper() { @Override diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/packets/InventoryPackets.java new file mode 100644 index 000000000..132c4ef9a --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol18w32ato1_13/packets/InventoryPackets.java @@ -0,0 +1,137 @@ +package us.myles.ViaVersion.protocols.protocol18w32ato1_13.packets; + +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.minecraft.item.Item; +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.type.Type; +import us.myles.ViaVersion.packets.State; + +/** + * Created by Marco Neuhaus on 08.08.2018 for the Project ViaVersion2. + */ +public class InventoryPackets { + + public static void register(Protocol protocol) { + + /* + Outgoing packets + */ + + // Set slot packet + protocol.registerOutgoing(State.PLAY, 0x17, 0x17, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.BYTE); // 0 - Window ID + map(Type.SHORT); // 1 - Slot ID + map(Type.FLAT_ITEM, Type.FLAT_ITEM); // 2 - Slot Value + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item stack = wrapper.get(Type.FLAT_ITEM, 0); + toClient(stack); + } + }); + } + }); + + // Window items packet + protocol.registerOutgoing(State.PLAY, 0x15, 0x15, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.UNSIGNED_BYTE); // 0 - Window ID + map(Type.FLAT_ITEM_ARRAY, Type.FLAT_ITEM_ARRAY); // 1 - Window Values + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item[] stacks = wrapper.get(Type.FLAT_ITEM_ARRAY, 0); + for (Item stack : stacks) + toClient(stack); + } + }); + } + }); + + // Entity Equipment Packet + protocol.registerOutgoing(State.PLAY, 0x42, 0x42, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity ID + map(Type.VAR_INT); // 1 - Slot ID + map(Type.FLAT_ITEM, Type.FLAT_ITEM); // 2 - Item + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item stack = wrapper.get(Type.FLAT_ITEM, 0); + toClient(stack); + } + }); + } + }); + + + /* + Incoming packets + */ + + // Click window packet + protocol.registerIncoming(State.PLAY, 0x08, 0x08, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.UNSIGNED_BYTE); // 0 - Window ID + map(Type.SHORT); // 1 - Slot + map(Type.BYTE); // 2 - Button + map(Type.SHORT); // 3 - Action number + map(Type.VAR_INT); // 4 - Mode + map(Type.FLAT_ITEM, Type.FLAT_ITEM); // 5 - Clicked Item + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item item = wrapper.get(Type.FLAT_ITEM, 0); + toServer(item); + } + }); + } + } + ); + + // Creative Inventory Action + protocol.registerIncoming(State.PLAY, 0x24, 0x24, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.SHORT); // 0 - Slot + map(Type.FLAT_ITEM, Type.FLAT_ITEM); // 1 - Clicked Item + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Item item = wrapper.get(Type.FLAT_ITEM, 0); + toServer(item); + } + }); + } + } + ); + } + + // TODO CLEANUP / SMARTER REWRITE SYSTEM + // TODO Rewrite identifiers + public static void toClient(Item item) { + if (item == null) return; + if(item.getId() >= 443){ + item.setId((short) (item.getId() + 5)); + } + } + + public static void toServer(Item item) { + if (item == null) return; + if(item.getId() >= 448){ + item.setId((short) (item.getId() - 5)); + } + } +}