From d3721307b1cdf842a5ed5f7291dc8a7536f874a5 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Sat, 27 Jul 2024 19:58:29 +0200 Subject: [PATCH] Cleanup 1.20.2->1.20 item and recipe rewriter (#840) --- .../BlockItemPacketRewriter1_20_2.java | 184 ++---------------- 1 file changed, 12 insertions(+), 172 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_2to1_20/rewriter/BlockItemPacketRewriter1_20_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_2to1_20/rewriter/BlockItemPacketRewriter1_20_2.java index a86a7233..f2baf774 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_2to1_20/rewriter/BlockItemPacketRewriter1_20_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_2to1_20/rewriter/BlockItemPacketRewriter1_20_2.java @@ -54,7 +54,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; public final class BlockItemPacketRewriter1_20_2 extends BackwardsItemRewriter { public BlockItemPacketRewriter1_20_2(final Protocol1_20_2To1_20 protocol) { - super(protocol, Types.ITEM1_20_2, Types.ITEM1_20_2_ARRAY); + super(protocol, Types.ITEM1_20_2, Types.ITEM1_20_2_ARRAY, Types.ITEM1_13_2, Types.ITEM1_13_2_ARRAY); } @Override @@ -65,6 +65,13 @@ public final class BlockItemPacketRewriter1_20_2 extends BackwardsItemRewriter { wrapper.cancel(); @@ -145,32 +152,6 @@ public final class BlockItemPacketRewriter1_20_2 extends BackwardsItemRewriter { - final Item[] items = wrapper.read(Types.ITEM1_20_2_ARRAY); - for (final Item item : items) { - handleItemToClient(wrapper.user(), item); - } - - wrapper.write(Types.ITEM1_13_2_ARRAY, items); - wrapper.write(Types.ITEM1_13_2, handleItemToClient(wrapper.user(), wrapper.read(Types.ITEM1_20_2))); // Carried item - }); - } - }); - protocol.registerClientbound(ClientboundPackets1_20_2.CONTAINER_SET_SLOT, new PacketHandlers() { - @Override - public void register() { - map(Types.UNSIGNED_BYTE); // Window id - map(Types.VAR_INT); // State id - map(Types.SHORT); // Slot id - handler(wrapper -> wrapper.write(Types.ITEM1_13_2, handleItemToClient(wrapper.user(), wrapper.read(Types.ITEM1_20_2)))); - } - }); protocol.registerClientbound(ClientboundPackets1_20_2.UPDATE_ADVANCEMENTS, wrapper -> { wrapper.passthrough(Types.BOOLEAN); // Reset/clear final int size = wrapper.passthrough(Types.VAR_INT); @@ -216,159 +197,18 @@ public final class BlockItemPacketRewriter1_20_2 extends BackwardsItemRewriter { - // Affected items - final int length = wrapper.passthrough(Types.VAR_INT); - for (int i = 0; i < length; i++) { - wrapper.passthrough(Types.SHORT); // Slot - wrapper.write(Types.ITEM1_20_2, handleItemToServer(wrapper.user(), wrapper.read(Types.ITEM1_13_2))); - } - - // Carried item - wrapper.write(Types.ITEM1_20_2, handleItemToServer(wrapper.user(), wrapper.read(Types.ITEM1_13_2))); - }); - } - }); - protocol.registerClientbound(ClientboundPackets1_20_2.MERCHANT_OFFERS, wrapper -> { - wrapper.passthrough(Types.VAR_INT); // Container id - final int size = wrapper.passthrough(Types.VAR_INT); - for (int i = 0; i < size; i++) { - wrapper.write(Types.ITEM1_13_2, handleItemToClient(wrapper.user(), wrapper.read(Types.ITEM1_20_2))); // Input - wrapper.write(Types.ITEM1_13_2, handleItemToClient(wrapper.user(), wrapper.read(Types.ITEM1_20_2))); // Output - wrapper.write(Types.ITEM1_13_2, handleItemToClient(wrapper.user(), wrapper.read(Types.ITEM1_20_2))); // Second Item - - wrapper.passthrough(Types.BOOLEAN); // Trade disabled - wrapper.passthrough(Types.INT); // Number of tools uses - wrapper.passthrough(Types.INT); // Maximum number of trade uses - wrapper.passthrough(Types.INT); // XP - wrapper.passthrough(Types.INT); // Special price - wrapper.passthrough(Types.FLOAT); // Price multiplier - wrapper.passthrough(Types.INT); // Demand - } - }); - protocol.registerServerbound(ServerboundPackets1_19_4.SET_CREATIVE_MODE_SLOT, new PacketHandlers() { - @Override - public void register() { - map(Types.SHORT); // 0 - Slot - handler(wrapper -> wrapper.write(Types.ITEM1_20_2, handleItemToServer(wrapper.user(), wrapper.read(Types.ITEM1_13_2)))); // 1 - Clicked Item - } - }); - protocol.registerClientbound(ClientboundPackets1_20_2.LEVEL_PARTICLES, new PacketHandlers() { - @Override - public void register() { - map(Types.VAR_INT); // 0 - Particle ID - map(Types.BOOLEAN); // 1 - Long Distance - map(Types.DOUBLE); // 2 - X - map(Types.DOUBLE); // 3 - Y - map(Types.DOUBLE); // 4 - Z - map(Types.FLOAT); // 5 - Offset X - map(Types.FLOAT); // 6 - Offset Y - map(Types.FLOAT); // 7 - Offset Z - map(Types.FLOAT); // 8 - Particle Data - map(Types.INT); // 9 - Particle Count - handler(wrapper -> { - final int id = wrapper.get(Types.VAR_INT, 0); - final ParticleMappings mappings = Protocol1_20_2To1_20.MAPPINGS.getParticleMappings(); - if (mappings.isBlockParticle(id)) { - final int data = wrapper.read(Types.VAR_INT); - wrapper.write(Types.VAR_INT, protocol.getMappingData().getNewBlockStateId(data)); - } else if (mappings.isItemParticle(id)) { - wrapper.write(Types.ITEM1_13_2, handleItemToClient(wrapper.user(), wrapper.read(Types.ITEM1_20_2))); - } - }); - } - }); new RecipeRewriter1_20_2<>(protocol) { @Override - public void handleCraftingShapeless(final PacketWrapper wrapper) { - wrapper.passthrough(Types.STRING); // Group - wrapper.passthrough(Types.VAR_INT); // Crafting book category - handleIngredients(wrapper); - - final Item result = wrapper.read(itemType()); - rewrite(wrapper.user(), result); - wrapper.write(Types.ITEM1_13_2, result); + protected Type mappedItemType() { + return BlockItemPacketRewriter1_20_2.this.mappedItemType(); } @Override - public void handleSmelting(final PacketWrapper wrapper) { - wrapper.passthrough(Types.STRING); // Group - wrapper.passthrough(Types.VAR_INT); // Crafting book category - handleIngredient(wrapper); - - final Item result = wrapper.read(itemType()); - rewrite(wrapper.user(), result); - wrapper.write(Types.ITEM1_13_2, result); - - wrapper.passthrough(Types.FLOAT); // EXP - wrapper.passthrough(Types.VAR_INT); // Cooking time + protected Type mappedItemArrayType() { + return BlockItemPacketRewriter1_20_2.this.mappedItemArrayType(); } - @Override - public void handleCraftingShaped(final PacketWrapper wrapper) { - final int ingredients = wrapper.passthrough(Types.VAR_INT) * wrapper.passthrough(Types.VAR_INT); - wrapper.passthrough(Types.STRING); // Group - wrapper.passthrough(Types.VAR_INT); // Crafting book category - for (int i = 0; i < ingredients; i++) { - handleIngredient(wrapper); - } - - final Item result = wrapper.read(itemType()); - rewrite(wrapper.user(), result); - wrapper.write(Types.ITEM1_13_2, result); - - wrapper.passthrough(Types.BOOLEAN); // Show notification - } - - @Override - public void handleStonecutting(final PacketWrapper wrapper) { - wrapper.passthrough(Types.STRING); // Group - handleIngredient(wrapper); - - final Item result = wrapper.read(itemType()); - rewrite(wrapper.user(), result); - wrapper.write(Types.ITEM1_13_2, result); - } - - @Override - public void handleSmithing(final PacketWrapper wrapper) { - handleIngredient(wrapper); // Base - handleIngredient(wrapper); // Addition - - final Item result = wrapper.read(itemType()); - rewrite(wrapper.user(), result); - wrapper.write(Types.ITEM1_13_2, result); - } - - @Override - public void handleSmithingTransform(final PacketWrapper wrapper) { - handleIngredient(wrapper); // Template - handleIngredient(wrapper); // Base - handleIngredient(wrapper); // Additions - - final Item result = wrapper.read(itemType()); - rewrite(wrapper.user(), result); - wrapper.write(Types.ITEM1_13_2, result); - } - - @Override - protected void handleIngredient(final PacketWrapper wrapper) { - final Item[] items = wrapper.read(itemArrayType()); - wrapper.write(Types.ITEM1_13_2_ARRAY, items); - for (final Item item : items) { - rewrite(wrapper.user(), item); - } - } }.register(ClientboundPackets1_20_2.UPDATE_RECIPES); }