Skip to content

Properly handle equipment updates in containers - #734

Open
roccodev wants to merge 3 commits into
ViaVersion:masterfrom
roccodev:fix/container-equipment-v2
Open

roccodev wants to merge 3 commits into
ViaVersion:masterfrom
roccodev:fix/container-equipment-v2

Conversation

@roccodev

@roccodev roccodev commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

As mentioned on Discord, properly handles what was reported in #733.

Test this by running /item replace entity @p armor.chest with [anything] while the player (on 1.8) has a chest/container open. Other interactions that would update the item like damaging armor would work too.

In brief:

This makes it so equipment changes while inside foreign containers are tracked and sent when the container is closed. This also handles equipment sent as set slot packets with window ID -2, which is not present in 1.8.

To test the -2 case, you can apply this patch to ViaBackwards, which is an alternative version of the one linked above (tested working in 1.21.1):

ViaBackwards patch for -2 set slot
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
--- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/BlockItemPacketRewriter1_21_2.java	(revision 04f836b9b9149b828ad23bcf8f7e0c9cf3a78162)
+++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/BlockItemPacketRewriter1_21_2.java	(date 1792477547810)
@@ -230,22 +230,11 @@
             wrapper.user().<BackwardsStorables1_21_2>storables(protocol).inventoryStateIdStorage().setStateId(stateId);
 
             final short slotId = wrapper.passthrough(Types.SHORT);
-            final Item item = handleItemToClient(wrapper.user(), wrapper.read(itemType()));
+            passthroughClientboundItem(wrapper);
 
             if (containerId == 0 && slotId >= 5 && slotId <= 8) {
-                wrapper.write(mappedItemType(), item.copy());
-
-                // 1.21.2+ servers use set slot packets to update equipment while inside containers, 1.21 only expects
-                // it for hotbar/offhand
-                final EntityTracker tracker = wrapper.user().getEntityTracker(protocol);
-                final PacketWrapper equipment = PacketWrapper.create(ClientboundPackets1_21.SET_EQUIPMENT, wrapper.user());
-                equipment.write(Types.VAR_INT, tracker.clientEntityId());
-                // helmet-boots is 5-8 in container but 5-2 in equipment
-                equipment.write(Types.BYTE, (byte) (10 - slotId));
-                equipment.write(mappedItemType(), item);
-                equipment.send(Protocol1_21_2To1_21.class);
-            } else {
-                wrapper.write(mappedItemType(), item);
+                wrapper.set(Types.BYTE, 0, (byte) -2);
+                wrapper.set(Types.SHORT, 0, (short) (44 - slotId));
             }
         });
         protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_SET_DATA, wrapper -> {

Comment on lines +422 to +436
handler(wrapper -> {
if (wrapper.get(Types.VAR_INT, 0) == tracker(wrapper.user()).clientEntityId()) {
// Vanilla 1.8.9 doesn't properly handle this and Forge handles it differently, use set
// slot packets instead
final Item item = wrapper.get(Types.ITEM1_8, 0);
final int slot = 4 - wrapper.get(Types.SHORT, 0);
wrapper.cancel();
final ContainerEquipmentTracker equipment = wrapper.user().get(ContainerEquipmentTracker.class);
final WindowTracker window = wrapper.user().get(WindowTracker.class);
equipment.setEquipment(slot, item);
if (window.openWindowId() == 0) {
equipment.sendAsSlot(wrapper.user(), slot);
}
}
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably be a check whether wrapper is cancelled + a slot check (to prevent offhand/main handmessing with equipment)

handler(wrapper -> {
    if (wrapper.isCancelled()) {
        return;
    }
    if (wrapper.get(Types.VAR_INT, 0) == tracker(wrapper.user()).clientEntityId()) {
        // Vanilla 1.8.9 doesn't properly handle this and Forge handles it differently, use set
        // slot packets instead
        wrapper.cancel();
        final short equipmentSlot = wrapper.get(Types.SHORT, 0);
        if (equipmentSlot < 1 || equipmentSlot > 4) {
            return;
        }
        final Item item = wrapper.get(Types.ITEM1_8, 0);
        final int slot = 4 - equipmentSlot;
        final ContainerEquipmentTracker equipment = wrapper.user().get(ContainerEquipmentTracker.class);
        final WindowTracker window = wrapper.user().get(WindowTracker.class);
        equipment.setEquipment(slot, item);
        if (window.openWindowId() == 0) {
            equipment.sendAsSlot(wrapper.user(), slot);
        }
    }
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, worth noting though that back then Florian didn't really like this part that touches equipment packets, so it might be removed eventually

iirc it was because this isn't possible in vanilla and is instead just something viabackwards uses, the alternative is to change the viabackwards patch to use -2 set slots which I wouldn't be completely against (esp. if it makes this patch cleaner)

Comment on lines +99 to +103

if (windowId != 0) {
final ContainerEquipmentTracker equipment = wrapper.user().get(ContainerEquipmentTracker.class);
equipment.sendAll(wrapper.user());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the equipment flushing should happen after the container close packet
also the 1.8 client does not care about window id for the close container packet, so the window id check is not needed (this also needs a fix in WindowTracker then)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it still not be good to skip equipment checking/flushing if the server thinks it's closing the player inventory?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it still not be good to skip equipment checking/flushing if the server thinks it's closing the player inventory?

well technically a modded server could close a custom window using window id 0 and then viarewind will never flush equipment

} else if (slot >= 9 && slot <= 35) {
containerSlot = size + slot - 9;
} else { // armor/offhand: no slot in a foreign container
if (slot <= 39) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (slot <= 39) {
if (slot >= 36 && slot <= 39) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, though i guess this just catches invalid (< 0) inputs

@roccodev
roccodev force-pushed the fix/container-equipment-v2 branch from 70ac883 to ef03076 Compare November 24, 2026 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants