Skip to content

Commit a0b0ed4

Browse files
committed
Make <1.21.2 container id types consistent with Vanilla again
... that's to say, to make them inconsistent thanks to special ids: Byte: - CONTAINER_SET_SLOT - CONTAINER_CLICK - PLACE_GHOST_RECIPE - PLACE_RECIPE - CONTAINER_BUTTON_CLICK - CONTAINER_CLOSE (serverbound) - CONTAINER_ACK (serverbound) Unsigned byte (unchanged): - OPEN_SCREEN - CONTAINER_SET_CONTENT - CONTAINER_SET_DATA - HORSE_SCREEN_OPEN - CONTAINER_CLOSE (clientbound) - CONTAINER_ACK (clientbound) Short (unchanged): - SET_CREATIVE_MODE_SLOT This doesn't change much in practice, but means it matches the Vanilla reading
1 parent e9ec29a commit a0b0ed4

File tree

14 files changed

+63
-59
lines changed

14 files changed

+63
-59
lines changed

common/src/main/java/com/viaversion/viaversion/protocols/v1_11_1to1_12/rewriter/ItemPacketRewriter1_12.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void registerPackets() {
4545
protocol.registerServerbound(ServerboundPackets1_12.CONTAINER_CLICK, new PacketHandlers() {
4646
@Override
4747
public void register() {
48-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
48+
map(Types.BYTE); // 0 - Window ID
4949
map(Types.SHORT); // 1 - Slot
5050
map(Types.BYTE); // 2 - Button
5151
map(Types.SHORT); // 3 - Action number
@@ -58,11 +58,11 @@ public void register() {
5858
handleItemToServer(wrapper.user(), item);
5959
return;
6060
}
61-
byte button = wrapper.get(Types.BYTE, 0);
61+
byte button = wrapper.get(Types.BYTE, 1);
6262
int mode = wrapper.get(Types.VAR_INT, 0);
6363
// QUICK_MOVE PATCH (Shift + (click/double click))
6464
if (mode == 1 && button == 0 && item == null) {
65-
short windowId = wrapper.get(Types.UNSIGNED_BYTE, 0);
65+
short windowId = wrapper.get(Types.BYTE, 0);
6666
short slotId = wrapper.get(Types.SHORT, 0);
6767
short actionId = wrapper.get(Types.SHORT, 1);
6868
InventoryQuickMoveProvider provider = Via.getManager().getProviders().get(InventoryQuickMoveProvider.class);

common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/Protocol1_12_2To1_13.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public void register() {
353353
registerClientbound(ClientboundPackets1_12_1.PLACE_GHOST_RECIPE, new PacketHandlers() {
354354
@Override
355355
public void register() {
356-
map(Types.UNSIGNED_BYTE);
356+
map(Types.BYTE);
357357
handler(wrapper -> wrapper.write(Types.STRING, "viaversion:legacy/" + wrapper.read(Types.VAR_INT)));
358358
}
359359
});
@@ -594,7 +594,7 @@ public String transform(PacketWrapper wrapper, String inputValue) {
594594
registerServerbound(ServerboundPackets1_13.PLACE_RECIPE, new PacketHandlers() {
595595
@Override
596596
public void register() {
597-
map(Types.UNSIGNED_BYTE); // Window id
597+
map(Types.BYTE); // Window id
598598

599599
handler(wrapper -> {
600600
String s = wrapper.read(Types.STRING);

common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/rewriter/ItemPacketRewriter1_13.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void registerPackets() {
5959
protocol.registerClientbound(ClientboundPackets1_12_1.CONTAINER_SET_SLOT, new PacketHandlers() {
6060
@Override
6161
public void register() {
62-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
62+
map(Types.BYTE); // 0 - Window ID
6363
map(Types.SHORT); // 1 - Slot ID
6464
map(Types.ITEM1_8, Types.ITEM1_13); // 2 - Slot Value
6565

@@ -185,7 +185,7 @@ public void register() {
185185
protocol.registerServerbound(ServerboundPackets1_13.CONTAINER_CLICK, new PacketHandlers() {
186186
@Override
187187
public void register() {
188-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
188+
map(Types.BYTE); // 0 - Window ID
189189
map(Types.SHORT); // 1 - Slot
190190
map(Types.BYTE); // 2 - Button
191191
map(Types.SHORT); // 3 - Action number

common/src/main/java/com/viaversion/viaversion/protocols/v1_13_1to1_13_2/rewriter/ItemPacketRewriter1_13_2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void register(Protocol1_13_1To1_13_2 protocol) {
3030
protocol.registerClientbound(ClientboundPackets1_13.CONTAINER_SET_SLOT, new PacketHandlers() {
3131
@Override
3232
public void register() {
33-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
33+
map(Types.BYTE); // 0 - Window ID
3434
map(Types.SHORT); // 1 - Slot ID
3535
map(Types.ITEM1_13, Types.ITEM1_13_2); // 2 - Slot Value
3636
}
@@ -116,7 +116,7 @@ public void register() {
116116
protocol.registerServerbound(ServerboundPackets1_13.CONTAINER_CLICK, new PacketHandlers() {
117117
@Override
118118
public void register() {
119-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
119+
map(Types.BYTE); // 0 - Window ID
120120
map(Types.SHORT); // 1 - Slot
121121
map(Types.BYTE); // 2 - Button
122122
map(Types.SHORT); // 3 - Action number

common/src/main/java/com/viaversion/viaversion/protocols/v1_13_2to1_14/rewriter/ItemPacketRewriter1_14.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void register() {
194194
// Selecting trade now moves the items, we need to resync the inventory
195195
PacketWrapper resyncPacket = wrapper.create(ServerboundPackets1_13.CONTAINER_CLICK);
196196
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_13_2To1_14.class);
197-
resyncPacket.write(Types.UNSIGNED_BYTE, ((short) tracker.getLatestTradeWindowId())); // 0 - Window ID
197+
resyncPacket.write(Types.BYTE, ((byte) tracker.getLatestTradeWindowId())); // 0 - Window ID
198198
resyncPacket.write(Types.SHORT, ((short) -999)); // 1 - Slot
199199
resyncPacket.write(Types.BYTE, (byte) 2); // 2 - Button - End left click
200200
resyncPacket.write(Types.SHORT, ((short) ThreadLocalRandom.current().nextInt())); // 3 - Action number

common/src/main/java/com/viaversion/viaversion/protocols/v1_15_2to1_16/rewriter/ItemPacketRewriter1_16.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void registerPackets() {
5151
// clear cursor item to prevent client to try dropping it during navigation between multiple inventories causing arm swing
5252
PacketHandler cursorRemapper = wrapper -> {
5353
PacketWrapper clearPacket = wrapper.create(ClientboundPackets1_16.CONTAINER_SET_SLOT);
54-
clearPacket.write(Types.UNSIGNED_BYTE, (short) -1);
54+
clearPacket.write(Types.BYTE, (byte) -1);
5555
clearPacket.write(Types.SHORT, (short) -1);
5656
clearPacket.write(Types.ITEM1_13_2, null);
5757
clearPacket.send(Protocol1_15_2To1_16.class);

common/src/main/java/com/viaversion/viaversion/protocols/v1_16_4to1_17/rewriter/ItemPacketRewriter1_17.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void registerPackets() {
5656
protocol.registerServerbound(ServerboundPackets1_17.CONTAINER_CLICK, new PacketHandlers() {
5757
@Override
5858
public void register() {
59-
map(Types.UNSIGNED_BYTE); // Window Id
59+
map(Types.BYTE); // Window Id
6060
map(Types.SHORT); // Slot
6161
map(Types.BYTE); // Button
6262
handler(wrapper -> wrapper.write(Types.SHORT, (short) 0)); // Action id - doesn't matter, as the sent out confirmation packet will be cancelled
@@ -110,10 +110,10 @@ public void register() {
110110
// Check extra bit for fast dismissal
111111
if ((id & (1 << 30)) != 0) {
112112
// Decode our requested inventory acknowledgement
113-
short inventoryId = (short) ((id >> 16) & 0xFF);
113+
byte inventoryId = (byte) ((id >> 16) & 0xFF);
114114
short confirmationId = (short) (id & 0xFFFF);
115115
PacketWrapper packet = wrapper.create(ServerboundPackets1_16_2.CONTAINER_ACK);
116-
packet.write(Types.UNSIGNED_BYTE, inventoryId);
116+
packet.write(Types.BYTE, inventoryId);
117117
packet.write(Types.SHORT, confirmationId);
118118
packet.write(Types.BOOLEAN, true); // Accept
119119
packet.sendToServer(Protocol1_16_4To1_17.class);

common/src/main/java/com/viaversion/viaversion/protocols/v1_17to1_17_1/Protocol1_17To1_17_1.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected void registerPackets() {
5050
registerClientbound(ClientboundPackets1_17.CONTAINER_SET_SLOT, new PacketHandlers() {
5151
@Override
5252
public void register() {
53-
map(Types.UNSIGNED_BYTE); // Container id
53+
map(Types.BYTE); // Container id
5454
create(Types.VAR_INT, 0); // Add arbitrary state id
5555
}
5656
});
@@ -73,7 +73,7 @@ public void register() {
7373
registerServerbound(ServerboundPackets1_17.CONTAINER_CLICK, new PacketHandlers() {
7474
@Override
7575
public void register() {
76-
map(Types.UNSIGNED_BYTE); // Container id
76+
map(Types.BYTE); // Container id
7777
read(Types.VAR_INT); // Remove state id
7878
}
7979
});

common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/BlockItemPacketRewriter1_21.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ public void registerPackets() {
7878
registerCooldown(ClientboundPackets1_20_5.COOLDOWN);
7979
registerSetContent1_17_1(ClientboundPackets1_20_5.CONTAINER_SET_CONTENT);
8080
protocol.registerClientbound(ClientboundPackets1_20_5.CONTAINER_SET_SLOT, wrapper -> {
81-
final short containerId = wrapper.passthrough(Types.UNSIGNED_BYTE); // Container id
81+
final byte containerId = wrapper.passthrough(Types.BYTE);
8282
wrapper.passthrough(Types.VAR_INT); // State id
83-
final short slotId = wrapper.passthrough(Types.SHORT); // Slot id
83+
final short slotId = wrapper.passthrough(Types.SHORT);
8484
final Item item = handleItemToClient(wrapper.user(), wrapper.read(itemType()));
8585
wrapper.write(mappedItemType(), item);
8686

common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void registerPackets() {
112112
});
113113

114114
protocol.registerClientbound(ClientboundPackets1_21.CONTAINER_SET_CONTENT, wrapper -> {
115-
updateContainerId(wrapper);
115+
unsignedByteToVarInt(wrapper);
116116
wrapper.passthrough(Types.VAR_INT); // State id
117117
final Item[] items = wrapper.read(itemArrayType());
118118
wrapper.write(mappedItemArrayType(), items);
@@ -122,7 +122,7 @@ public void registerPackets() {
122122
passthroughClientboundItem(wrapper);
123123
});
124124
protocol.registerClientbound(ClientboundPackets1_21.CONTAINER_SET_SLOT, wrapper -> {
125-
updateContainerId(wrapper);
125+
byteToVarInt(wrapper);
126126
final int containerId = wrapper.get(Types.VAR_INT, 0);
127127
if (containerId == -1) { // cursor item
128128
wrapper.setPacketType(ClientboundPackets1_21_2.SET_CURSOR_ITEM);
@@ -142,13 +142,13 @@ public void registerPackets() {
142142
}
143143
passthroughClientboundItem(wrapper);
144144
});
145-
protocol.registerClientbound(ClientboundPackets1_21.CONTAINER_CLOSE, this::updateContainerId);
146-
protocol.registerClientbound(ClientboundPackets1_21.CONTAINER_SET_DATA, this::updateContainerId);
147-
protocol.registerClientbound(ClientboundPackets1_21.HORSE_SCREEN_OPEN, this::updateContainerId);
145+
protocol.registerClientbound(ClientboundPackets1_21.CONTAINER_CLOSE, this::unsignedByteToVarInt);
146+
protocol.registerClientbound(ClientboundPackets1_21.CONTAINER_SET_DATA, this::unsignedByteToVarInt);
147+
protocol.registerClientbound(ClientboundPackets1_21.HORSE_SCREEN_OPEN, this::unsignedByteToVarInt);
148148
protocol.registerClientbound(ClientboundPackets1_21.SET_CARRIED_ITEM, ClientboundPackets1_21_2.SET_HELD_SLOT);
149-
protocol.registerServerbound(ServerboundPackets1_21_2.CONTAINER_CLOSE, this::updateContainerIdServerbound);
149+
protocol.registerServerbound(ServerboundPackets1_21_2.CONTAINER_CLOSE, this::varIntToByte);
150150
protocol.registerServerbound(ServerboundPackets1_21_2.CONTAINER_CLICK, wrapper -> {
151-
updateContainerIdServerbound(wrapper);
151+
varIntToByte(wrapper);
152152
wrapper.passthrough(Types.VAR_INT); // State id
153153
wrapper.passthrough(Types.SHORT); // Slot
154154
wrapper.passthrough(Types.BYTE); // Button
@@ -161,7 +161,7 @@ public void registerPackets() {
161161
passthroughServerboundItem(wrapper);
162162
});
163163
protocol.registerClientbound(ClientboundPackets1_21.PLACE_GHOST_RECIPE, wrapper -> {
164-
this.updateContainerId(wrapper);
164+
this.byteToVarInt(wrapper);
165165

166166
final String recipeKey = wrapper.read(Types.STRING);
167167
final RecipeRewriter1_21_2.Recipe recipe = wrapper.user().get(RecipeRewriter1_21_2.class).recipe(recipeKey);
@@ -174,7 +174,7 @@ public void registerPackets() {
174174
recipe.writeRecipeDisplay(wrapper);
175175
});
176176
protocol.registerServerbound(ServerboundPackets1_21_2.PLACE_RECIPE, wrapper -> {
177-
this.updateContainerIdServerbound(wrapper);
177+
this.varIntToByte(wrapper);
178178
convertServerboundRecipeDisplayId(wrapper);
179179
});
180180
protocol.registerServerbound(ServerboundPackets1_21_2.RECIPE_BOOK_SEEN_RECIPE, this::convertServerboundRecipeDisplayId);
@@ -519,18 +519,19 @@ public Item handleItemToServer(final UserConnection connection, final Item item)
519519
return item;
520520
}
521521

522-
private void updateContainerId(final PacketWrapper wrapper) {
523-
// Container id handling was always a bit whack with most reading them as unsigned bytes, some as bytes, some already as var ints.
524-
// In VV, they're generally read as unsigned bytes to not have to look the type up every time, but we need to make sure they're
525-
// properly converted to ints when used
522+
private void unsignedByteToVarInt(final PacketWrapper wrapper) {
526523
final short containerId = wrapper.read(Types.UNSIGNED_BYTE);
527-
final int intId = (byte) containerId;
528-
wrapper.write(Types.VAR_INT, intId);
524+
wrapper.write(Types.VAR_INT, (int) containerId);
529525
}
530526

531-
private void updateContainerIdServerbound(final PacketWrapper wrapper) {
527+
private void byteToVarInt(final PacketWrapper wrapper) {
528+
final byte containerId = wrapper.read(Types.BYTE);
529+
wrapper.write(Types.VAR_INT, (int) containerId);
530+
}
531+
532+
private void varIntToByte(final PacketWrapper wrapper) {
532533
final int containerId = wrapper.read(Types.VAR_INT);
533-
wrapper.write(Types.UNSIGNED_BYTE, (short) containerId);
534+
wrapper.write(Types.BYTE, (byte) containerId);
534535
}
535536

536537
public static void updateItemData(final Item item) {

common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/ItemPacketRewriter1_9.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void register() {
106106
protocol.registerClientbound(ClientboundPackets1_8.CONTAINER_SET_SLOT, new PacketHandlers() {
107107
@Override
108108
public void register() {
109-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
109+
map(Types.BYTE); // 0 - Window ID
110110
map(Types.SHORT); // 1 - Slot ID
111111
map(Types.ITEM1_8); // 2 - Slot Value
112112
handler(wrapper -> {
@@ -121,7 +121,7 @@ public void register() {
121121
EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_8To1_9.class);
122122

123123
short slotID = wrapper.get(Types.SHORT, 0);
124-
byte windowId = wrapper.get(Types.UNSIGNED_BYTE, 0).byteValue();
124+
byte windowId = wrapper.get(Types.BYTE, 0);
125125

126126
// Store item in slot
127127
inventoryTracker.setItemId(windowId, slotID, stack == null ? 0 : stack.identifier());
@@ -154,7 +154,7 @@ public void register() {
154154

155155
handler(wrapper -> {
156156
Item[] stacks = wrapper.get(Types.ITEM1_8_SHORT_ARRAY, 0);
157-
Short windowId = wrapper.get(Types.UNSIGNED_BYTE, 0);
157+
short windowId = wrapper.get(Types.UNSIGNED_BYTE, 0);
158158

159159
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
160160
EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_8To1_9.class);
@@ -207,7 +207,8 @@ public void register() {
207207
handler(wrapper -> {
208208
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
209209
inventoryTracker.setInventory(null);
210-
inventoryTracker.resetInventory(wrapper.get(Types.UNSIGNED_BYTE, 0));
210+
short windowId = wrapper.get(Types.UNSIGNED_BYTE, 0);
211+
inventoryTracker.resetInventory(windowId);
211212
});
212213
}
213214
});
@@ -245,7 +246,7 @@ public void register() {
245246
short slotID = wrapper.get(Types.SHORT, 0);
246247

247248
// Update item in slot
248-
inventoryTracker.setItemId((short) 0, slotID, stack == null ? 0 : stack.identifier());
249+
inventoryTracker.setItemId(0, slotID, stack == null ? 0 : stack.identifier());
249250

250251
// Sync shield item in offhand with main hand
251252
entityTracker.syncShieldWithSword();
@@ -260,7 +261,7 @@ public void register() {
260261
if (throwItem) {
261262
// Send a packet wiping the slot
262263
wrapper.create(ClientboundPackets1_9.CONTAINER_SET_SLOT, w -> {
263-
w.write(Types.UNSIGNED_BYTE, (short) 0);
264+
w.write(Types.BYTE, (byte) 0);
264265
w.write(Types.SHORT, slot);
265266
w.write(Types.ITEM1_8, null);
266267
}).send(Protocol1_8To1_9.class);
@@ -275,7 +276,7 @@ public void register() {
275276

276277
@Override
277278
public void register() {
278-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
279+
map(Types.BYTE); // 0 - Window ID
279280
map(Types.SHORT); // 1 - Slot ID
280281
map(Types.BYTE); // 2 - Button
281282
map(Types.SHORT); // 3 - Action
@@ -285,10 +286,10 @@ public void register() {
285286
Item stack = wrapper.get(Types.ITEM1_8, 0);
286287

287288
if (Via.getConfig().isShowShieldWhenSwordInHand()) {
288-
Short windowId = wrapper.get(Types.UNSIGNED_BYTE, 0);
289-
byte mode = wrapper.get(Types.BYTE, 1);
289+
byte windowId = wrapper.get(Types.BYTE, 0);
290+
byte mode = wrapper.get(Types.BYTE, 2);
290291
short hoverSlot = wrapper.get(Types.SHORT, 0);
291-
byte button = wrapper.get(Types.BYTE, 0);
292+
byte button = wrapper.get(Types.BYTE, 1);
292293

293294
// Move items in inventory to track the sword location
294295
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
@@ -299,7 +300,7 @@ public void register() {
299300
});
300301
// Brewing patch and elytra throw patch
301302
handler(wrapper -> {
302-
final short windowID = wrapper.get(Types.UNSIGNED_BYTE, 0);
303+
final byte windowID = wrapper.get(Types.BYTE, 0);
303304
final short slot = wrapper.get(Types.SHORT, 0);
304305
boolean throwItem = (slot == 45 && windowID == 0);
305306
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
@@ -315,7 +316,7 @@ public void register() {
315316
if (throwItem) {
316317
// Send a packet wiping the slot
317318
wrapper.create(ClientboundPackets1_9.CONTAINER_SET_SLOT, w -> {
318-
w.write(Types.UNSIGNED_BYTE, windowID);
319+
w.write(Types.BYTE, windowID);
319320
w.write(Types.SHORT, slot);
320321
w.write(Types.ITEM1_8, null);
321322
}).scheduleSend(Protocol1_8To1_9.class);
@@ -334,13 +335,13 @@ public void register() {
334335

335336
@Override
336337
public void register() {
337-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
338+
map(Types.BYTE); // 0 - Window ID
338339

339340
// Inventory tracking
340341
handler(wrapper -> {
341342
InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class);
342343
inventoryTracker.setInventory(null);
343-
inventoryTracker.resetInventory(wrapper.get(Types.UNSIGNED_BYTE, 0));
344+
inventoryTracker.resetInventory(wrapper.get(Types.BYTE, 0));
344345
});
345346
}
346347
});

common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/EntityTracker1_9.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public boolean hasSwordInHand() {
128128

129129
// Get item in new selected slot
130130
int inventorySlot = this.heldItemSlot + 36; // Hotbar slot index to inventory slot
131-
int itemIdentifier = inventoryTracker.getItemId((short) 0, (short) inventorySlot);
131+
int itemIdentifier = inventoryTracker.getItemId(0, (short) inventorySlot);
132132

133133
return Protocol1_8To1_9.isSword(itemIdentifier);
134134
}

0 commit comments

Comments
 (0)