From a041610fe20af04346a681876be77eb680f2b4f7 Mon Sep 17 00:00:00 2001 From: _tomcraft <936063+tomcraft@users.noreply.github.com> Date: Fri, 13 Nov 2020 14:27:29 +0100 Subject: [PATCH] Prevent 1.12- clients to creative-pick server-side unknown block/item (#2180) --- .../Protocol1_10To1_9_3_4.java | 4 +++ .../packets/InventoryPackets.java | 25 +++++++++++++++++++ .../Protocol1_11_1To1_11.java | 11 ++++++++ .../packets/InventoryPackets.java | 25 +++++++++++++++++++ .../packets/InventoryPackets.java | 16 +++++++++++- .../packets/InventoryPackets.java | 14 ++++++++++- .../protocol1_9to1_8/ItemRewriter.java | 8 ++++++ 7 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_10to1_9_3/packets/InventoryPackets.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/packets/InventoryPackets.java diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_10to1_9_3/Protocol1_10To1_9_3_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_10to1_9_3/Protocol1_10To1_9_3_4.java index d11e90337..1525d5a32 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_10to1_9_3/Protocol1_10To1_9_3_4.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_10to1_9_3/Protocol1_10To1_9_3_4.java @@ -10,6 +10,7 @@ import us.myles.ViaVersion.api.remapper.ValueTransformer; 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_10to1_9_3.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_10to1_9_3.storage.ResourcePackTracker; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3; @@ -43,6 +44,9 @@ public class Protocol1_10To1_9_3_4 extends Protocol {}, InventoryPackets::toServerItem); + itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM); + } + + public static void toServerItem(Item item) { + if (item == null) return; + boolean newItem = item.getIdentifier() >= 213 && item.getIdentifier() <= 217; + if (newItem) { // Replace server-side unknown items + item.setIdentifier((short) 1); + item.setData((short) 0); + } + } + +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/Protocol1_11_1To1_11.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/Protocol1_11_1To1_11.java index 9d16e420f..5b89e0983 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/Protocol1_11_1To1_11.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/Protocol1_11_1To1_11.java @@ -1,8 +1,19 @@ package us.myles.ViaVersion.protocols.protocol1_11_1to1_11; import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.protocols.protocol1_11_1to1_11.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3; public class Protocol1_11_1To1_11 extends Protocol { + + public Protocol1_11_1To1_11() { + super(ClientboundPackets1_9_3.class, ClientboundPackets1_9_3.class, ServerboundPackets1_9_3.class, ServerboundPackets1_9_3.class); + } + + @Override + protected void registerPackets() { + InventoryPackets.register(this); + } + } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/packets/InventoryPackets.java new file mode 100644 index 000000000..6446f9684 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/packets/InventoryPackets.java @@ -0,0 +1,25 @@ +package us.myles.ViaVersion.protocols.protocol1_11_1to1_11.packets; + +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.ViaVersion.api.rewriters.ItemRewriter; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_11_1to1_11.Protocol1_11_1To1_11; +import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3; + +public class InventoryPackets { + + public static void register(Protocol1_11_1To1_11 protocol) { + ItemRewriter itemRewriter = new ItemRewriter(protocol, item -> {}, InventoryPackets::toServerItem); + itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM); + } + + public static void toServerItem(Item item) { + if (item == null) return; + boolean newItem = item.getIdentifier() == 452; + if (newItem) { // Replace server-side unknown items + item.setIdentifier((short) 1); + item.setData((short) 0); + } + } + +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/packets/InventoryPackets.java index fa87fc5db..0f66861b5 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11to1_10/packets/InventoryPackets.java @@ -1,6 +1,8 @@ package us.myles.ViaVersion.protocols.protocol1_11to1_10.packets; import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.ItemRewriter; @@ -13,7 +15,7 @@ import us.myles.ViaVersion.protocols.protocol1_11to1_10.Protocol1_11To1_10; public class InventoryPackets { public static void register(Protocol1_11To1_10 protocol) { - ItemRewriter itemRewriter = new ItemRewriter(protocol, EntityIdRewriter::toClientItem, EntityIdRewriter::toServerItem); + ItemRewriter itemRewriter = new ItemRewriter(protocol, EntityIdRewriter::toClientItem, InventoryPackets::toServerItem); itemRewriter.registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM); itemRewriter.registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY); @@ -53,4 +55,16 @@ public class InventoryPackets { itemRewriter.registerClickWindow(ServerboundPackets1_9_3.CLICK_WINDOW, Type.ITEM); itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM); } + + public static void toServerItem(Item item) { + EntityIdRewriter.toServerItem(item); + if (item == null) return; + boolean newItem = item.getIdentifier() >= 218 && item.getIdentifier() <= 234; + newItem |= item.getIdentifier() == 449 || item.getIdentifier() == 450; + if (newItem) { // Replace server-side unknown items + item.setIdentifier((short) 1); + item.setData((short) 0); + } + } + } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java index a18c36980..4306e48cc 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java @@ -7,6 +7,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.ItemRewriter; import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.protocol1_11to1_10.EntityIdRewriter; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.BedRewriter; import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.Protocol1_12To1_11_1; @@ -16,7 +17,7 @@ import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQui public class InventoryPackets { public static void register(Protocol1_12To1_11_1 protocol) { - ItemRewriter itemRewriter = new ItemRewriter(protocol, BedRewriter::toClientItem, BedRewriter::toServerItem); + ItemRewriter itemRewriter = new ItemRewriter(protocol, BedRewriter::toClientItem, InventoryPackets::toServerItem); itemRewriter.registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM); itemRewriter.registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY); @@ -98,4 +99,15 @@ public class InventoryPackets { // Creative Inventory Action itemRewriter.registerCreativeInvAction(ServerboundPackets1_12.CREATIVE_INVENTORY_ACTION, Type.ITEM); } + + public static void toServerItem(Item item) { + BedRewriter.toServerItem(item); + if (item == null) return; + boolean newItem = item.getIdentifier() >= 235 && item.getIdentifier() <= 252; + newItem |= item.getIdentifier() == 453; + if (newItem) { // Replace server-side unknown items + item.setIdentifier((short) 1); + item.setData((short) 0); + } + } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java index b219002c9..e14ea7699 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java @@ -186,6 +186,14 @@ public class ItemRewriter { item.setTag(tag); item.setData((short) data); } + + boolean newItem = item.getIdentifier() >= 198 && item.getIdentifier() <= 212; + newItem |= item.getIdentifier() == 397 && item.getData() == 5; + newItem |= item.getIdentifier() >= 432 && item.getIdentifier() <= 448; + if (newItem) { // Replace server-side unknown items + item.setIdentifier((short) 1); + item.setData((short) 0); + } } }