From 498bec9065208b855f18e6bcf3b067c83c91c633 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 5 Nov 2024 20:18:48 +0100 Subject: [PATCH] Remove 'has recipe error' container data from smithing table Fixes #909 --- .../v1_21_2to1_21/Protocol1_21_2To1_21.java | 1 - .../BlockItemPacketRewriter1_21_2.java | 31 +++++++++++++++++-- .../storage/InventoryStateIdStorage.java | 9 ++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java index 0833efc6..ca9ffe28 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java @@ -86,7 +86,6 @@ public final class Protocol1_21_2To1_21 extends BackwardsProtocol(this).register(ClientboundPackets1_21_2.AWARD_STATS); new AttributeRewriter<>(this).register1_21(ClientboundPackets1_21_2.UPDATE_ATTRIBUTES); - translatableRewriter.registerOpenScreen(ClientboundPackets1_21_2.OPEN_SCREEN); translatableRewriter.registerComponentPacket(ClientboundPackets1_21_2.SET_ACTION_BAR_TEXT); translatableRewriter.registerComponentPacket(ClientboundPackets1_21_2.SET_TITLE_TEXT); translatableRewriter.registerComponentPacket(ClientboundPackets1_21_2.SET_SUBTITLE_TEXT); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/BlockItemPacketRewriter1_21_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/BlockItemPacketRewriter1_21_2.java index 3f4c08d6..80f0f140 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/BlockItemPacketRewriter1_21_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/BlockItemPacketRewriter1_21_2.java @@ -86,6 +86,18 @@ public final class BlockItemPacketRewriter1_21_2 extends BackwardsStructuredItem handleItemToClient(wrapper.user(), item); }); + protocol.registerClientbound(ClientboundPackets1_21_2.OPEN_SCREEN, wrapper -> { + wrapper.passthrough(Types.VAR_INT); // Id + + final int containerType = wrapper.passthrough(Types.VAR_INT); + if (containerType == 21) { + // Track smithing table to remove new data + wrapper.user().get(InventoryStateIdStorage.class).setSmithingTableOpen(true); + } + + protocol.getComponentRewriter().passthroughAndProcess(wrapper); + }); + protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_SET_CONTENT, wrapper -> { updateContainerId(wrapper); @@ -108,11 +120,24 @@ public final class BlockItemPacketRewriter1_21_2 extends BackwardsStructuredItem wrapper.passthrough(Types.SHORT); // Slot id passthroughClientboundItem(wrapper); }); + protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_SET_DATA, wrapper -> { + updateContainerId(wrapper); + + if (wrapper.user().get(InventoryStateIdStorage.class).smithingTableOpen()) { + // Cancel new data for smithing table + wrapper.cancel(); + } + }); + protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_CLOSE, wrapper -> { + updateContainerId(wrapper); + wrapper.user().get(InventoryStateIdStorage.class).setSmithingTableOpen(false); + }); protocol.registerClientbound(ClientboundPackets1_21_2.SET_HELD_SLOT, ClientboundPackets1_21.SET_CARRIED_ITEM); - protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_CLOSE, this::updateContainerId); - protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_SET_DATA, this::updateContainerId); protocol.registerClientbound(ClientboundPackets1_21_2.HORSE_SCREEN_OPEN, this::updateContainerId); - protocol.registerServerbound(ServerboundPackets1_20_5.CONTAINER_CLOSE, this::updateContainerIdServerbound); + protocol.registerServerbound(ServerboundPackets1_20_5.CONTAINER_CLOSE, wrapper -> { + updateContainerIdServerbound(wrapper); + wrapper.user().get(InventoryStateIdStorage.class).setSmithingTableOpen(false); + }); protocol.registerServerbound(ServerboundPackets1_20_5.CONTAINER_CLICK, wrapper -> { updateContainerIdServerbound(wrapper); wrapper.passthrough(Types.VAR_INT); // State id diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/InventoryStateIdStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/InventoryStateIdStorage.java index 0f6064fa..5749ce0a 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/InventoryStateIdStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/storage/InventoryStateIdStorage.java @@ -21,6 +21,7 @@ import com.viaversion.viaversion.api.connection.StorableObject; public final class InventoryStateIdStorage implements StorableObject { + private boolean smithingTableOpen; private int stateId = -1; public int stateId() { @@ -30,4 +31,12 @@ public final class InventoryStateIdStorage implements StorableObject { public void setStateId(final int stateId) { this.stateId = stateId; } + + public boolean smithingTableOpen() { + return smithingTableOpen; + } + + public void setSmithingTableOpen(final boolean smithingTableOpen) { + this.smithingTableOpen = smithingTableOpen; + } }