From 1faf1f3f49fa75ba06661edfb4fd8456035c6c6c Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 16 Feb 2023 12:27:31 +0100 Subject: [PATCH] Reduce RecipeRewriter extends chain --- .../packets/InventoryPackets.java | 3 +- .../Protocol1_13To1_12_2.java | 87 +++++++++--------- .../data/RecipeRewriter1_13_2.java | 75 ---------------- .../data/RecipeRewriter1_14.java | 47 ---------- .../packets/InventoryPackets.java | 3 +- .../packets/InventoryPackets.java | 4 +- .../packets/InventoryPackets.java | 4 +- .../packets/InventoryPackets.java | 4 +- .../packets/InventoryPackets.java | 4 +- .../packets/InventoryPackets.java | 4 +- .../data/RecipeRewriter1_19_3.java | 30 ++----- .../packets/InventoryPackets.java | 4 +- .../data/RecipeRewriter1_19_4.java} | 25 +++--- .../viaversion/rewriter/RecipeRewriter.java | 88 ++++++++++++++++++- 14 files changed, 158 insertions(+), 224 deletions(-) delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/RecipeRewriter1_13_2.java delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/RecipeRewriter1_14.java rename common/src/main/java/com/viaversion/viaversion/protocols/{protocol1_16to1_15_2/data/RecipeRewriter1_16.java => protocol1_19_4to1_19_3/data/RecipeRewriter1_19_4.java} (57%) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java index 25234ca8a..8f0991b5d 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java @@ -22,7 +22,6 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2; import com.viaversion.viaversion.rewriter.ItemRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter; @@ -72,7 +71,7 @@ public class InventoryPackets extends ItemRewriter recipeRewriter = new RecipeRewriter1_13_2<>(protocol); + RecipeRewriter recipeRewriter = new RecipeRewriter<>(protocol); protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, wrapper -> { int size = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < size; i++) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index 5a45c087d..5d57dba91 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -401,56 +401,53 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol entry : RecipeData.recipes.entrySet()) { - wrapper.write(Type.STRING, entry.getKey()); // Id - wrapper.write(Type.STRING, entry.getValue().getType()); - switch (entry.getValue().getType()) { - case "crafting_shapeless": { - wrapper.write(Type.STRING, entry.getValue().getGroup()); - wrapper.write(Type.VAR_INT, entry.getValue().getIngredients().length); - for (Item[] ingredient : entry.getValue().getIngredients()) { - Item[] clone = ingredient.clone(); // Clone because array and item is mutable - for (int i = 0; i < clone.length; i++) { - if (clone[i] == null) continue; - clone[i] = new DataItem(clone[i]); - } - wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone); - } - wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult())); - break; - } - case "crafting_shaped": { - wrapper.write(Type.VAR_INT, entry.getValue().getWidth()); - wrapper.write(Type.VAR_INT, entry.getValue().getHeight()); - wrapper.write(Type.STRING, entry.getValue().getGroup()); - for (Item[] ingredient : entry.getValue().getIngredients()) { - Item[] clone = ingredient.clone(); // Clone because array and item is mutable - for (int i = 0; i < clone.length; i++) { - if (clone[i] == null) continue; - clone[i] = new DataItem(clone[i]); - } - wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone); - } - wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult())); - break; - } - case "smelting": { - wrapper.write(Type.STRING, entry.getValue().getGroup()); - Item[] clone = entry.getValue().getIngredient().clone(); // Clone because array and item is mutable + wrapper.create(ClientboundPackets1_13.DECLARE_RECIPES, wrapper1 -> { + wrapper1.write(Type.VAR_INT, RecipeData.recipes.size()); + for (Map.Entry entry : RecipeData.recipes.entrySet()) { + wrapper1.write(Type.STRING, entry.getKey()); // Id + wrapper1.write(Type.STRING, entry.getValue().getType()); + switch (entry.getValue().getType()) { + case "crafting_shapeless": { + wrapper1.write(Type.STRING, entry.getValue().getGroup()); + wrapper1.write(Type.VAR_INT, entry.getValue().getIngredients().length); + for (Item[] ingredient : entry.getValue().getIngredients()) { + Item[] clone = ingredient.clone(); // Clone because array and item is mutable for (int i = 0; i < clone.length; i++) { if (clone[i] == null) continue; clone[i] = new DataItem(clone[i]); } - wrapper.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone); - wrapper.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult())); - wrapper.write(Type.FLOAT, entry.getValue().getExperience()); - wrapper.write(Type.VAR_INT, entry.getValue().getCookingTime()); - break; + wrapper1.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone); } + wrapper1.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult())); + break; + } + case "crafting_shaped": { + wrapper1.write(Type.VAR_INT, entry.getValue().getWidth()); + wrapper1.write(Type.VAR_INT, entry.getValue().getHeight()); + wrapper1.write(Type.STRING, entry.getValue().getGroup()); + for (Item[] ingredient : entry.getValue().getIngredients()) { + Item[] clone = ingredient.clone(); // Clone because array and item is mutable + for (int i = 0; i < clone.length; i++) { + if (clone[i] == null) continue; + clone[i] = new DataItem(clone[i]); + } + wrapper1.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone); + } + wrapper1.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult())); + break; + } + case "smelting": { + wrapper1.write(Type.STRING, entry.getValue().getGroup()); + Item[] clone = entry.getValue().getIngredient().clone(); // Clone because array and item is mutable + for (int i = 0; i < clone.length; i++) { + if (clone[i] == null) continue; + clone[i] = new DataItem(clone[i]); + } + wrapper1.write(Type.FLAT_ITEM_ARRAY_VAR_INT, clone); + wrapper1.write(Type.FLAT_ITEM, new DataItem(entry.getValue().getResult())); + wrapper1.write(Type.FLOAT, entry.getValue().getExperience()); + wrapper1.write(Type.VAR_INT, entry.getValue().getCookingTime()); + break; } } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/RecipeRewriter1_13_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/RecipeRewriter1_13_2.java deleted file mode 100644 index fa1c64ca2..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/RecipeRewriter1_13_2.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2023 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data; - -import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.protocol.Protocol; -import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.rewriter.RecipeRewriter; - -/** - * For 1.13.2, not 1.13 (1.13 reads recipe type and id in swapped order)! - */ -public class RecipeRewriter1_13_2 extends RecipeRewriter { - - public RecipeRewriter1_13_2(Protocol protocol) { - super(protocol); - recipeHandlers.put("crafting_shapeless", this::handleCraftingShapeless); - recipeHandlers.put("crafting_shaped", this::handleCraftingShaped); - recipeHandlers.put("smelting", this::handleSmelting); - } - - public void handleSmelting(PacketWrapper wrapper) throws Exception { - wrapper.passthrough(Type.STRING); // Group - Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (Item item : items) { - rewrite(item); - } - - rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result - - wrapper.passthrough(Type.FLOAT); // EXP - wrapper.passthrough(Type.VAR_INT); // Cooking time - } - - public void handleCraftingShaped(PacketWrapper wrapper) throws Exception { - int ingredientsNo = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT); - wrapper.passthrough(Type.STRING); // Group - for (int j = 0; j < ingredientsNo; j++) { - Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (Item item : items) { - rewrite(item); - } - } - rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result - } - - public void handleCraftingShapeless(PacketWrapper wrapper) throws Exception { - wrapper.passthrough(Type.STRING); // Group - int ingredientsNo = wrapper.passthrough(Type.VAR_INT); - for (int j = 0; j < ingredientsNo; j++) { - Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (Item item : items) { - rewrite(item); - } - } - rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/RecipeRewriter1_14.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/RecipeRewriter1_14.java deleted file mode 100644 index 08ba61c49..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/RecipeRewriter1_14.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2023 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data; - -import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.protocol.Protocol; -import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2; - -public class RecipeRewriter1_14 extends RecipeRewriter1_13_2 { - - public RecipeRewriter1_14(Protocol protocol) { - super(protocol); - recipeHandlers.put("stonecutting", this::handleStonecutting); - - recipeHandlers.put("blasting", this::handleSmelting); - recipeHandlers.put("smoking", this::handleSmelting); - recipeHandlers.put("campfire_cooking", this::handleSmelting); - } - - public void handleStonecutting(PacketWrapper wrapper) throws Exception { - wrapper.passthrough(Type.STRING); - Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (Item item : items) { - rewrite(item); - } - - rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java index fa05b2ad5..cc90b2ed1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java @@ -34,7 +34,6 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; @@ -191,7 +190,7 @@ public class InventoryPackets extends ItemRewriter recipeRewriter = new RecipeRewriter1_13_2<>(protocol); + RecipeRewriter recipeRewriter = new RecipeRewriter<>(protocol); protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, wrapper -> { int size = wrapper.passthrough(Type.VAR_INT); int deleted = 0; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/InventoryPackets.java index 6fbbf872f..2156ead57 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/InventoryPackets.java @@ -20,9 +20,9 @@ package com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; -import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4; import com.viaversion.viaversion.rewriter.ItemRewriter; +import com.viaversion.viaversion.rewriter.RecipeRewriter; public class InventoryPackets extends ItemRewriter { @@ -39,7 +39,7 @@ public class InventoryPackets extends ItemRewriter(protocol).registerDefaultHandler(ClientboundPackets1_14.DECLARE_RECIPES); + new RecipeRewriter<>(protocol).register(ClientboundPackets1_14.DECLARE_RECIPES); registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM); registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/InventoryPackets.java index a920d47ff..efa1e51a9 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/InventoryPackets.java @@ -21,8 +21,8 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16; import com.viaversion.viaversion.rewriter.ItemRewriter; +import com.viaversion.viaversion.rewriter.RecipeRewriter; public class InventoryPackets extends ItemRewriter { @@ -52,7 +52,7 @@ public class InventoryPackets extends ItemRewriter(protocol).registerDefaultHandler(ClientboundPackets1_16.DECLARE_RECIPES); + new RecipeRewriter<>(protocol).register(ClientboundPackets1_16.DECLARE_RECIPES); registerClickWindow(ServerboundPackets1_16_2.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM); registerCreativeInvAction(ServerboundPackets1_16_2.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java index 597e953d4..1b2053a96 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java @@ -30,13 +30,13 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.UUIDIntArrayType; -import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; import com.viaversion.viaversion.rewriter.ItemRewriter; +import com.viaversion.viaversion.rewriter.RecipeRewriter; import java.util.UUID; public class InventoryPackets extends ItemRewriter { @@ -125,7 +125,7 @@ public class InventoryPackets extends ItemRewriter(protocol).registerDefaultHandler(ClientboundPackets1_15.DECLARE_RECIPES); + new RecipeRewriter<>(protocol).register(ClientboundPackets1_15.DECLARE_RECIPES); registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM); registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/InventoryPackets.java index 3faef58e6..603c6cd21 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/InventoryPackets.java @@ -23,12 +23,12 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.InventoryAcknowledgements; import com.viaversion.viaversion.rewriter.ItemRewriter; +import com.viaversion.viaversion.rewriter.RecipeRewriter; public final class InventoryPackets extends ItemRewriter { @@ -46,7 +46,7 @@ public final class InventoryPackets extends ItemRewriter(protocol).registerDefaultHandler(ClientboundPackets1_16_2.DECLARE_RECIPES); + new RecipeRewriter<>(protocol).register(ClientboundPackets1_16_2.DECLARE_RECIPES); registerCreativeInvAction(ServerboundPackets1_17.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/InventoryPackets.java index a62dd7c21..999d94544 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/InventoryPackets.java @@ -20,11 +20,11 @@ package com.viaversion.viaversion.protocols.protocol1_18to1_17_1.packets; import com.viaversion.viaversion.api.data.ParticleMappings; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16; import com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.Protocol1_18To1_17_1; import com.viaversion.viaversion.rewriter.ItemRewriter; +import com.viaversion.viaversion.rewriter.RecipeRewriter; public final class InventoryPackets extends ItemRewriter { @@ -97,7 +97,7 @@ public final class InventoryPackets extends ItemRewriter(protocol).registerDefaultHandler(ClientboundPackets1_17_1.DECLARE_RECIPES); + new RecipeRewriter<>(protocol).register(ClientboundPackets1_17_1.DECLARE_RECIPES); registerClickWindow1_17_1(ServerboundPackets1_17.CLICK_WINDOW); registerCreativeInvAction(ServerboundPackets1_17.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_3to1_19_1/data/RecipeRewriter1_19_3.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_3to1_19_1/data/RecipeRewriter1_19_3.java index 1d40313a1..ba9a51478 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_3to1_19_1/data/RecipeRewriter1_19_3.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_3to1_19_1/data/RecipeRewriter1_19_3.java @@ -17,17 +17,17 @@ */ package com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.data; -import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16; +import com.viaversion.viaversion.rewriter.RecipeRewriter; -public class RecipeRewriter1_19_3 extends RecipeRewriter1_16 { +public class RecipeRewriter1_19_3 extends RecipeRewriter { public RecipeRewriter1_19_3(final Protocol protocol) { super(protocol); + // Existed before, but now have serialization data recipeHandlers.put("crafting_special_armordye", this::handleSimpleRecipe); recipeHandlers.put("crafting_special_bookcloning", this::handleSimpleRecipe); recipeHandlers.put("crafting_special_mapcloning", this::handleSimpleRecipe); @@ -47,13 +47,7 @@ public class RecipeRewriter1_19_3 extends Recip public void handleCraftingShapeless(final PacketWrapper wrapper) throws Exception { wrapper.passthrough(Type.STRING); // Group wrapper.passthrough(Type.VAR_INT); // Crafting book category - final int ingredients = wrapper.passthrough(Type.VAR_INT); - for (int j = 0; j < ingredients; j++) { - final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (final Item item : items) { - rewrite(item); - } - } + handleIngredients(wrapper); rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result } @@ -62,11 +56,8 @@ public class RecipeRewriter1_19_3 extends Recip final int ingredients = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT); wrapper.passthrough(Type.STRING); // Group wrapper.passthrough(Type.VAR_INT); // Crafting book category - for (int j = 0; j < ingredients; j++) { - final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (final Item item : items) { - rewrite(item); - } + for (int i = 0; i < ingredients; i++) { + handleIngredient(wrapper); } rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result } @@ -75,16 +66,9 @@ public class RecipeRewriter1_19_3 extends Recip public void handleSmelting(final PacketWrapper wrapper) throws Exception { wrapper.passthrough(Type.STRING); // Group wrapper.passthrough(Type.VAR_INT); // Crafting book category - final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients - for (final Item item : items) { - rewrite(item); - } + handleIngredient(wrapper); rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result wrapper.passthrough(Type.FLOAT); // EXP wrapper.passthrough(Type.VAR_INT); // Cooking time } - - public void handleSimpleRecipe(final PacketWrapper wrapper) throws Exception { - wrapper.passthrough(Type.VAR_INT); // Crafting book category - } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_3to1_19_1/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_3to1_19_1/packets/InventoryPackets.java index bad446d15..b2f4afccb 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_3to1_19_1/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_3to1_19_1/packets/InventoryPackets.java @@ -20,13 +20,13 @@ package com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.packets; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type; import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.ClientboundPackets1_19_1; import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.Protocol1_19_3To1_19_1; import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ServerboundPackets1_19_3; import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.rewriter.ItemRewriter; +import com.viaversion.viaversion.rewriter.RecipeRewriter; public final class InventoryPackets extends ItemRewriter { @@ -57,7 +57,7 @@ public final class InventoryPackets extends ItemRewriter recipeRewriter = new RecipeRewriter1_16<>(protocol); + final RecipeRewriter recipeRewriter = new RecipeRewriter<>(protocol); protocol.registerClientbound(ClientboundPackets1_19_1.DECLARE_RECIPES, wrapper -> { final int size = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < size; i++) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/RecipeRewriter1_16.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/data/RecipeRewriter1_19_4.java similarity index 57% rename from common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/RecipeRewriter1_16.java rename to common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/data/RecipeRewriter1_19_4.java index 7469f1219..e660dfc7c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/RecipeRewriter1_16.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/data/RecipeRewriter1_19_4.java @@ -15,31 +15,26 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data; +package com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.data; -import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14; +import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.data.RecipeRewriter1_19_3; -public class RecipeRewriter1_16 extends RecipeRewriter1_14 { +public class RecipeRewriter1_19_4 extends RecipeRewriter1_19_3 { - public RecipeRewriter1_16(Protocol protocol) { + public RecipeRewriter1_19_4(final Protocol protocol) { super(protocol); - recipeHandlers.put("smithing", this::handleSmithing); } - public void handleSmithing(PacketWrapper wrapper) throws Exception { - Item[] baseIngredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); - for (Item item : baseIngredients) { - rewrite(item); - } - Item[] ingredients = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); - for (Item item : ingredients) { - rewrite(item); - } + @Override + public void handleCraftingShapeless(final PacketWrapper wrapper) throws Exception { + wrapper.passthrough(Type.STRING); // Group + wrapper.passthrough(Type.VAR_INT); // Crafting book category + handleIngredients(wrapper); rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + wrapper.passthrough(Type.BOOLEAN); // Show notification } } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/RecipeRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/RecipeRewriter.java index 14ee41574..b0252536a 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/RecipeRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/RecipeRewriter.java @@ -26,13 +26,30 @@ import java.util.HashMap; import java.util.Map; import org.checkerframework.checker.nullness.qual.Nullable; -public abstract class RecipeRewriter { +public class RecipeRewriter { protected final Protocol protocol; protected final Map recipeHandlers = new HashMap<>(); - protected RecipeRewriter(Protocol protocol) { + public RecipeRewriter(Protocol protocol) { this.protocol = protocol; + recipeHandlers.put("crafting_shapeless", this::handleCraftingShapeless); + recipeHandlers.put("crafting_shaped", this::handleCraftingShaped); + recipeHandlers.put("smelting", this::handleSmelting); + + // Added in 1.14 + recipeHandlers.put("stonecutting", this::handleStonecutting); + recipeHandlers.put("blasting", this::handleSmelting); + recipeHandlers.put("smoking", this::handleSmelting); + recipeHandlers.put("campfire_cooking", this::handleSmelting); + + // Added in 1.16 + recipeHandlers.put("smithing", this::handleSmithing); + + // Added in 1.19.4 + recipeHandlers.put("smithing_transform", this::handleSmithingTransform); + recipeHandlers.put("smithing_trim", this::handleSmithingTransform); + recipeHandlers.put("crafting_decorated_pot", this::handleSimpleRecipe); } public void handleRecipeType(PacketWrapper wrapper, String type) throws Exception { @@ -42,7 +59,12 @@ public abstract class RecipeRewriter { } } - public void registerDefaultHandler(C packetType) { + /** + * Registers a packet handler to rewrite recipe types, for 1.14+. + * + * @param packetType packet type + */ + public void register(C packetType) { protocol.registerClientbound(packetType, wrapper -> { int size = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < size; i++) { @@ -53,12 +75,72 @@ public abstract class RecipeRewriter { }); } + public void handleCraftingShaped(PacketWrapper wrapper) throws Exception { + int ingredientsNo = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT); + wrapper.passthrough(Type.STRING); // Group + for (int i = 0; i < ingredientsNo; i++) { + handleIngredient(wrapper); + } + rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + } + + public void handleCraftingShapeless(PacketWrapper wrapper) throws Exception { + wrapper.passthrough(Type.STRING); // Group + handleIngredients(wrapper); + rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + } + + public void handleSmelting(PacketWrapper wrapper) throws Exception { + wrapper.passthrough(Type.STRING); // Group + handleIngredient(wrapper); + rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + wrapper.passthrough(Type.FLOAT); // EXP + wrapper.passthrough(Type.VAR_INT); // Cooking time + } + + public void handleStonecutting(PacketWrapper wrapper) throws Exception { + wrapper.passthrough(Type.STRING); + handleIngredient(wrapper); + rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + } + + public void handleSmithing(PacketWrapper wrapper) throws Exception { + handleIngredient(wrapper); // Base + handleIngredient(wrapper); // Addition + rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + } + + public void handleSimpleRecipe(final PacketWrapper wrapper) throws Exception { + wrapper.passthrough(Type.VAR_INT); // Crafting book category + } + + public void handleSmithingTransform(final PacketWrapper wrapper) throws Exception { + handleIngredient(wrapper); // Template + handleIngredient(wrapper); // Base + handleIngredient(wrapper); // Additions + rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result + } + protected void rewrite(@Nullable Item item) { if (protocol.getItemRewriter() != null) { protocol.getItemRewriter().handleItemToClient(item); } } + protected void handleIngredient(final PacketWrapper wrapper) throws Exception { + final Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); + for (final Item item : items) { + rewrite(item); + } + } + + protected void handleIngredients(final PacketWrapper wrapper) throws Exception { + final int ingredients = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < ingredients; i++) { + handleIngredient(wrapper); + } + } + @FunctionalInterface public interface RecipeConsumer {