Skip to content

Commit f6dcd14

Browse files
committed
Update pre-1.20 container id types
1 parent 1fb2bd4 commit f6dcd14

File tree

8 files changed

+36
-33
lines changed

8 files changed

+36
-33
lines changed

common/src/main/java/com/viaversion/viabackwards/protocol/v1_11to1_10/rewriter/BlockItemPacketRewriter1_11.java

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

@@ -118,7 +118,7 @@ public void register() {
118118
protocol.registerServerbound(ServerboundPackets1_9_3.CONTAINER_CLICK, new PacketHandlers() {
119119
@Override
120120
public void register() {
121-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
121+
map(Types.BYTE); // 0 - Window ID
122122
map(Types.SHORT); // 1 - Slot
123123
map(Types.BYTE); // 2 - Button
124124
map(Types.SHORT); // 3 - Action number

common/src/main/java/com/viaversion/viabackwards/protocol/v1_12to1_11_1/rewriter/BlockItemPacketRewriter1_12.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void register() {
9595
protocol.registerServerbound(ServerboundPackets1_9_3.CONTAINER_CLICK, new PacketHandlers() {
9696
@Override
9797
public void register() {
98-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
98+
map(Types.BYTE); // 0 - Window ID
9999
map(Types.SHORT); // 1 - Slot
100100
map(Types.BYTE); // 2 - Button
101101
map(Types.SHORT); // 3 - Action number
@@ -112,7 +112,7 @@ public void register() {
112112

113113
// Apologize (may happen in some cases, maybe if inventory is full?)
114114
PacketWrapper confirm = wrapper.create(ServerboundPackets1_12.CONTAINER_ACK);
115-
confirm.write(Types.UNSIGNED_BYTE, wrapper.get(Types.UNSIGNED_BYTE, 0));
115+
confirm.write(Types.BYTE, wrapper.get(Types.BYTE, 0));
116116
confirm.write(Types.SHORT, wrapper.get(Types.SHORT, 1));
117117
confirm.write(Types.BOOLEAN, false); // Success - not used
118118

common/src/main/java/com/viaversion/viabackwards/protocol/v1_13_2to1_13_1/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_2To1_13_1 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_2, Types.ITEM1_13); // 2 - Slot Value
3636
}
@@ -117,7 +117,7 @@ public void register() {
117117
protocol.registerServerbound(ServerboundPackets1_13.CONTAINER_CLICK, new PacketHandlers() {
118118
@Override
119119
public void register() {
120-
map(Types.UNSIGNED_BYTE); // 0 - Window ID
120+
map(Types.BYTE); // 0 - Window ID
121121
map(Types.SHORT); // 1 - Slot
122122
map(Types.BYTE); // 2 - Button
123123
map(Types.SHORT); // 3 - Action number

common/src/main/java/com/viaversion/viabackwards/protocol/v1_13to1_12_2/rewriter/BlockItemPacketRewriter1_13.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public void register() {
270270
protocol.registerClientbound(ClientboundPackets1_13.CONTAINER_SET_SLOT, new PacketHandlers() {
271271
@Override
272272
public void register() {
273-
map(Types.UNSIGNED_BYTE);
273+
map(Types.BYTE);
274274
map(Types.SHORT);
275275
map(Types.ITEM1_13, Types.ITEM1_8);
276276

@@ -464,7 +464,7 @@ public void register() {
464464
protocol.registerServerbound(ServerboundPackets1_12_1.CONTAINER_CLICK, new PacketHandlers() {
465465
@Override
466466
public void register() {
467-
map(Types.UNSIGNED_BYTE);
467+
map(Types.BYTE);
468468
map(Types.SHORT);
469469
map(Types.BYTE);
470470
map(Types.SHORT);

common/src/main/java/com/viaversion/viabackwards/protocol/v1_17_1to1_17/Protocol1_17_1To1_17.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected void registerPackets() {
6161
wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
6262
});
6363
registerClientbound(ClientboundPackets1_17_1.CONTAINER_SET_SLOT, wrapper -> {
64-
short containerId = wrapper.passthrough(Types.UNSIGNED_BYTE);
64+
byte containerId = wrapper.passthrough(Types.BYTE);
6565
int stateId = wrapper.read(Types.VAR_INT);
6666
wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId);
6767
});
@@ -88,11 +88,11 @@ protected void registerPackets() {
8888
});
8989

9090
registerServerbound(ServerboundPackets1_17.CONTAINER_CLOSE, wrapper -> {
91-
short containerId = wrapper.passthrough(Types.UNSIGNED_BYTE);
91+
byte containerId = wrapper.passthrough(Types.BYTE);
9292
wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
9393
});
9494
registerServerbound(ServerboundPackets1_17.CONTAINER_CLICK, wrapper -> {
95-
short containerId = wrapper.passthrough(Types.UNSIGNED_BYTE);
95+
byte containerId = wrapper.passthrough(Types.BYTE);
9696
int stateId = wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
9797
wrapper.write(Types.VAR_INT, stateId == Integer.MAX_VALUE ? 0 : stateId);
9898
});

common/src/main/java/com/viaversion/viabackwards/protocol/v1_17_1to1_17/storage/InventoryStateIds.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ public final class InventoryStateIds implements StorableObject {
2525
private final Int2IntMap ids = new Int2IntOpenHashMap();
2626

2727
public InventoryStateIds() {
28-
// Inventory ids are unsigned bytes, so this is safe to do
2928
ids.defaultReturnValue(Integer.MAX_VALUE);
3029
}
3130

32-
public void setStateId(short containerId, int id) {
31+
public void setStateId(int containerId, int id) {
3332
ids.put(containerId, id);
3433
}
3534

36-
public int removeStateId(short containerId) {
35+
public int removeStateId(int containerId) {
3736
return ids.remove(containerId);
3837
}
3938
}

common/src/main/java/com/viaversion/viabackwards/protocol/v1_17to1_16_4/rewriter/BlockItemPacketRewriter1_17.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected void registerPackets() {
9090
protocol.registerServerbound(ServerboundPackets1_16_2.CONTAINER_CLICK, new PacketHandlers() {
9191
@Override
9292
public void register() {
93-
map(Types.UNSIGNED_BYTE);
93+
map(Types.BYTE);
9494
handler(wrapper -> {
9595
short slot = wrapper.passthrough(Types.SHORT); // Slot
9696
byte button = wrapper.passthrough(Types.BYTE); // Button
@@ -138,7 +138,7 @@ public void register() {
138138
});
139139

140140
protocol.registerClientbound(ClientboundPackets1_17.CONTAINER_SET_SLOT, wrapper -> {
141-
short windowId = wrapper.passthrough(Types.UNSIGNED_BYTE);
141+
byte windowId = wrapper.passthrough(Types.BYTE);
142142
short slot = wrapper.passthrough(Types.SHORT);
143143

144144
Item carried = wrapper.read(Types.ITEM1_13_2);
@@ -163,7 +163,7 @@ public void register() {
163163
}
164164

165165
// Handle ping packet replacement
166-
short inventoryId = wrapper.read(Types.UNSIGNED_BYTE);
166+
byte inventoryId = wrapper.read(Types.BYTE);
167167
short confirmationId = wrapper.read(Types.SHORT);
168168
boolean accepted = wrapper.read(Types.BOOLEAN);
169169
if (inventoryId == 0 && accepted) {

common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/BlockItemPacketRewriter1_21_2.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void registerPackets() {
100100
});
101101

102102
protocol.registerClientbound(ClientboundPackets1_21_2.SET_CURSOR_ITEM, ClientboundPackets1_21.CONTAINER_SET_SLOT, wrapper -> {
103-
wrapper.write(Types.UNSIGNED_BYTE, (short) -1); // Player inventory
103+
wrapper.write(Types.BYTE, (byte) -1); // Player inventory
104104
wrapper.write(Types.VAR_INT, wrapper.user().get(InventoryStateIdStorage.class).stateId()); // State id; re-use the last known one
105105
wrapper.write(Types.SHORT, (short) -1); // Cursor
106106
passthroughClientboundItem(wrapper);
@@ -119,7 +119,7 @@ public void registerPackets() {
119119
});
120120

121121
protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_SET_CONTENT, wrapper -> {
122-
updateContainerId(wrapper);
122+
varIntToUnsignedByte(wrapper);
123123

124124
final int stateId = wrapper.passthrough(Types.VAR_INT);
125125
wrapper.user().get(InventoryStateIdStorage.class).setStateId(stateId);
@@ -132,7 +132,7 @@ public void registerPackets() {
132132
passthroughClientboundItem(wrapper);
133133
});
134134
protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_SET_SLOT, wrapper -> {
135-
updateContainerId(wrapper);
135+
varIntToByte(wrapper);
136136

137137
final int stateId = wrapper.passthrough(Types.VAR_INT);
138138
wrapper.user().get(InventoryStateIdStorage.class).setStateId(stateId);
@@ -141,25 +141,25 @@ public void registerPackets() {
141141
passthroughClientboundItem(wrapper);
142142
});
143143
protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_SET_DATA, wrapper -> {
144-
updateContainerId(wrapper);
144+
varIntToUnsignedByte(wrapper);
145145

146146
if (wrapper.user().get(InventoryStateIdStorage.class).smithingTableOpen()) {
147147
// Cancel new data for smithing table
148148
wrapper.cancel();
149149
}
150150
});
151151
protocol.registerClientbound(ClientboundPackets1_21_2.CONTAINER_CLOSE, wrapper -> {
152-
updateContainerId(wrapper);
152+
varIntToUnsignedByte(wrapper);
153153
wrapper.user().get(InventoryStateIdStorage.class).setSmithingTableOpen(false);
154154
});
155155
protocol.registerClientbound(ClientboundPackets1_21_2.SET_HELD_SLOT, ClientboundPackets1_21.SET_CARRIED_ITEM);
156-
protocol.registerClientbound(ClientboundPackets1_21_2.HORSE_SCREEN_OPEN, this::updateContainerId);
156+
protocol.registerClientbound(ClientboundPackets1_21_2.HORSE_SCREEN_OPEN, this::varIntToUnsignedByte);
157157
protocol.registerServerbound(ServerboundPackets1_20_5.CONTAINER_CLOSE, wrapper -> {
158-
updateContainerIdServerbound(wrapper);
158+
byteToVarInt(wrapper);
159159
wrapper.user().get(InventoryStateIdStorage.class).setSmithingTableOpen(false);
160160
});
161161
protocol.registerServerbound(ServerboundPackets1_20_5.CONTAINER_CLICK, wrapper -> {
162-
updateContainerIdServerbound(wrapper);
162+
byteToVarInt(wrapper);
163163
wrapper.passthrough(Types.VAR_INT); // State id
164164
wrapper.passthrough(Types.SHORT); // Slot
165165
wrapper.passthrough(Types.BYTE); // Button
@@ -184,7 +184,7 @@ public void registerPackets() {
184184
});
185185

186186
protocol.registerClientbound(ClientboundPackets1_21_2.SET_PLAYER_INVENTORY, ClientboundPackets1_21.CONTAINER_SET_SLOT, wrapper -> {
187-
wrapper.write(Types.UNSIGNED_BYTE, (short) -2); // Player inventory
187+
wrapper.write(Types.BYTE, (byte) -2); // Player inventory
188188
wrapper.write(Types.VAR_INT, 0); // 0 state id
189189
final int slot = wrapper.read(Types.VAR_INT);
190190
wrapper.write(Types.SHORT, (short) slot);
@@ -268,11 +268,11 @@ public void registerPackets() {
268268
wrapper.cancel();
269269
});
270270
protocol.registerClientbound(ClientboundPackets1_21_2.PLACE_GHOST_RECIPE, wrapper -> {
271-
this.updateContainerId(wrapper);
271+
this.varIntToByte(wrapper);
272272
wrapper.cancel(); // Full recipe display, this doesn't look mappable
273273
});
274274
protocol.registerServerbound(ServerboundPackets1_20_5.PLACE_RECIPE, wrapper -> {
275-
this.updateContainerIdServerbound(wrapper);
275+
this.byteToVarInt(wrapper);
276276

277277
final String recipe = Key.stripMinecraftNamespace(wrapper.read(Types.STRING));
278278
wrapper.write(Types.VAR_INT, Integer.parseInt(recipe));
@@ -283,15 +283,19 @@ public void registerPackets() {
283283
});
284284
}
285285

286-
private void updateContainerId(final PacketWrapper wrapper) {
286+
private void varIntToUnsignedByte(final PacketWrapper wrapper) {
287287
final int containerId = wrapper.read(Types.VAR_INT);
288288
wrapper.write(Types.UNSIGNED_BYTE, (short) containerId);
289289
}
290290

291-
private void updateContainerIdServerbound(final PacketWrapper wrapper) {
292-
final short containerId = wrapper.read(Types.UNSIGNED_BYTE);
293-
final int intId = (byte) containerId;
294-
wrapper.write(Types.VAR_INT, intId);
291+
private void varIntToByte(final PacketWrapper wrapper) {
292+
final int containerId = wrapper.read(Types.VAR_INT);
293+
wrapper.write(Types.BYTE, (byte) containerId);
294+
}
295+
296+
private void byteToVarInt(final PacketWrapper wrapper) {
297+
final byte containerId = wrapper.read(Types.BYTE);
298+
wrapper.write(Types.VAR_INT, (int) containerId);
295299
}
296300

297301
@Override

0 commit comments

Comments
 (0)