Skip to content

Commit 8e6727c

Browse files
committed
Handle changed custom potion effects tag
1 parent e73eeeb commit 8e6727c

File tree

1 file changed

+44
-60
lines changed

1 file changed

+44
-60
lines changed

common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20to1_20_2/rewriter/BlockItemPacketRewriter1_20_2.java

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
3232
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
3333
import com.viaversion.viaversion.api.type.Type;
34-
import com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap;
35-
import com.viaversion.viaversion.libs.fastutil.objects.Object2IntOpenHashMap;
3634
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
3735
import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag;
3836
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
@@ -42,59 +40,16 @@
4240
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundPackets1_20_2;
4341
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.rewriter.RecipeRewriter1_20_2;
4442
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.type.ChunkType1_20_2;
43+
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.util.PotionEffects;
4544
import com.viaversion.viaversion.rewriter.BlockRewriter;
4645
import com.viaversion.viaversion.util.Key;
4746
import com.viaversion.viaversion.util.MathUtil;
48-
import java.util.Arrays;
49-
import java.util.List;
5047
import org.checkerframework.checker.nullness.qual.Nullable;
5148

5249
public final class BlockItemPacketRewriter1_20_2 extends ItemRewriter<ClientboundPackets1_20_2, ServerboundPackets1_19_4, Protocol1_20To1_20_2> {
5350

54-
private final Object2IntMap<String> effects = new Object2IntOpenHashMap<>();
55-
5651
public BlockItemPacketRewriter1_20_2(final Protocol1_20To1_20_2 protocol) {
5752
super(protocol, Type.ITEM1_20_2, Type.ITEM1_20_2_VAR_INT_ARRAY);
58-
59-
final List<String> effects = Arrays.asList(
60-
"speed",
61-
"slowness",
62-
"haste",
63-
"mining_fatigue",
64-
"strength",
65-
"instant_health",
66-
"instant_damage",
67-
"jump_boost",
68-
"nausea",
69-
"regeneration",
70-
"resistance",
71-
"fire_resistance",
72-
"water_breathing",
73-
"invisibility",
74-
"blindness",
75-
"night_vision",
76-
"hunger",
77-
"weakness",
78-
"poison",
79-
"wither",
80-
"health_boost",
81-
"absorption",
82-
"saturation",
83-
"glowing",
84-
"levitation",
85-
"luck",
86-
"unluck",
87-
"slow_falling",
88-
"conduit_power",
89-
"dolphins_grace",
90-
"bad_omen",
91-
"hero_of_the_village",
92-
"darkness"
93-
);
94-
for (int i = 0; i < effects.size(); i++) {
95-
final String effect = effects.get(i);
96-
this.effects.put(effect, i + 1); // Effects start at 1 before 1.20.2
97-
}
9853
}
9954

10055
@Override
@@ -170,8 +125,13 @@ public void register() {
170125
map(Type.UNSIGNED_BYTE); // Window id
171126
map(Type.VAR_INT); // State id
172127
handler(wrapper -> {
173-
wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, wrapper.read(Type.ITEM1_20_2_VAR_INT_ARRAY)); // Items
174-
wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.ITEM1_20_2)); // Carried item
128+
final Item[] items = wrapper.read(Type.ITEM1_20_2_VAR_INT_ARRAY);
129+
for (final Item item : items) {
130+
handleItemToClient(item);
131+
}
132+
133+
wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, items);
134+
wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(wrapper.read(Type.ITEM1_20_2))); // Carried item
175135
});
176136
}
177137
});
@@ -181,7 +141,7 @@ public void register() {
181141
map(Type.UNSIGNED_BYTE); // Window id
182142
map(Type.VAR_INT); // State id
183143
map(Type.SHORT); // Slot id
184-
map(Type.ITEM1_20_2, Type.FLAT_VAR_INT_ITEM); // Item
144+
handler(wrapper -> wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(wrapper.read(Type.ITEM1_20_2))));
185145
}
186146
});
187147
protocol.registerClientbound(ClientboundPackets1_20_2.ADVANCEMENTS, wrapper -> {
@@ -199,7 +159,7 @@ public void register() {
199159
if (wrapper.passthrough(Type.BOOLEAN)) {
200160
wrapper.passthrough(Type.COMPONENT); // Title
201161
wrapper.passthrough(Type.COMPONENT); // Description
202-
wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.ITEM1_20_2)); // Icon
162+
wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(wrapper.read(Type.ITEM1_20_2))); // Icon
203163
wrapper.passthrough(Type.VAR_INT); // Frame type
204164
final int flags = wrapper.passthrough(Type.INT); // Flags
205165
if ((flags & 1) != 0) {
@@ -227,7 +187,7 @@ public void register() {
227187
byte slot;
228188
do {
229189
slot = wrapper.passthrough(Type.BYTE);
230-
wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.ITEM1_20_2));
190+
wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(wrapper.read(Type.ITEM1_20_2)));
231191
} while ((slot & 0xFFFFFF80) != 0);
232192
});
233193
}
@@ -246,11 +206,11 @@ public void register() {
246206
final int length = wrapper.passthrough(Type.VAR_INT);
247207
for (int i = 0; i < length; i++) {
248208
wrapper.passthrough(Type.SHORT); // Slot
249-
wrapper.write(Type.ITEM1_20_2, wrapper.read(Type.FLAT_VAR_INT_ITEM));
209+
wrapper.write(Type.ITEM1_20_2, handleItemToServer(wrapper.read(Type.FLAT_VAR_INT_ITEM)));
250210
}
251211

252212
// Carried item
253-
wrapper.write(Type.ITEM1_20_2, wrapper.read(Type.FLAT_VAR_INT_ITEM));
213+
wrapper.write(Type.ITEM1_20_2, handleItemToServer(wrapper.read(Type.FLAT_VAR_INT_ITEM)));
254214
});
255215
}
256216
});
@@ -259,9 +219,9 @@ public void register() {
259219
wrapper.passthrough(Type.VAR_INT); // Container id
260220
final int size = wrapper.passthrough(Type.VAR_INT);
261221
for (int i = 0; i < size; i++) {
262-
wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.ITEM1_20_2)); // Input
263-
wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.ITEM1_20_2)); // Output
264-
wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.ITEM1_20_2)); // Second Item
222+
wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(wrapper.read(Type.ITEM1_20_2))); // Input
223+
wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(wrapper.read(Type.ITEM1_20_2))); // Output
224+
wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(wrapper.read(Type.ITEM1_20_2))); // Second Item
265225

266226
wrapper.passthrough(Type.BOOLEAN); // Trade disabled
267227
wrapper.passthrough(Type.INT); // Number of tools uses
@@ -276,7 +236,7 @@ public void register() {
276236
@Override
277237
public void register() {
278238
map(Type.SHORT); // 0 - Slot
279-
map(Type.FLAT_VAR_INT_ITEM, Type.ITEM1_20_2); // 1 - Clicked Item
239+
handler(wrapper -> wrapper.write(Type.ITEM1_20_2, handleItemToServer(wrapper.read(Type.FLAT_VAR_INT_ITEM)))); // 1 - Clicked Item
280240
}
281241
});
282242
protocol.registerClientbound(ClientboundPackets1_20_2.SPAWN_PARTICLE, new PacketHandlers() {
@@ -299,7 +259,7 @@ public void register() {
299259
final int data = wrapper.read(Type.VAR_INT);
300260
wrapper.write(Type.VAR_INT, protocol.getMappingData().getNewBlockStateId(data));
301261
} else if (mappings.isItemParticle(id)) {
302-
wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.ITEM1_20_2));
262+
wrapper.write(Type.FLAT_VAR_INT_ITEM, handleItemToClient(wrapper.read(Type.ITEM1_20_2)));
303263
}
304264
});
305265
}
@@ -389,6 +349,30 @@ protected void handleIngredient(final PacketWrapper wrapper) throws Exception {
389349
}.register(ClientboundPackets1_20_2.DECLARE_RECIPES);
390350
}
391351

352+
@Override
353+
public @Nullable Item handleItemToClient(@Nullable final Item item) {
354+
if (item == null) {
355+
return null;
356+
}
357+
if (item.tag() != null) {
358+
com.viaversion.viaversion.protocols.protocol1_20_2to1_20.rewriter.BlockItemPacketRewriter1_20_2.to1_20_1Effects(item);
359+
}
360+
361+
return super.handleItemToClient(item);
362+
}
363+
364+
@Override
365+
public @Nullable Item handleItemToServer(@Nullable final Item item) {
366+
if (item == null) {
367+
return null;
368+
}
369+
if (item.tag() != null) {
370+
com.viaversion.viaversion.protocols.protocol1_20_2to1_20.rewriter.BlockItemPacketRewriter1_20_2.to1_20_2Effects(item);
371+
}
372+
373+
return super.handleItemToServer(item);
374+
}
375+
392376
private @Nullable CompoundTag handleBlockEntity(@Nullable final CompoundTag tag) {
393377
if (tag == null) {
394378
return null;
@@ -397,13 +381,13 @@ protected void handleIngredient(final PacketWrapper wrapper) throws Exception {
397381
final StringTag primaryEffect = tag.remove("primary_effect");
398382
if (primaryEffect != null) {
399383
final String effectKey = Key.stripMinecraftNamespace(primaryEffect.getValue());
400-
tag.put("Primary", new IntTag(effects.getInt(effectKey)));
384+
tag.put("Primary", new IntTag(PotionEffects.keyToId(effectKey)));
401385
}
402386

403387
final StringTag secondaryEffect = tag.remove("secondary_effect");
404388
if (secondaryEffect != null) {
405389
final String effectKey = Key.stripMinecraftNamespace(secondaryEffect.getValue());
406-
tag.put("Secondary", new IntTag(effects.getInt(effectKey)));
390+
tag.put("Secondary", new IntTag(PotionEffects.keyToId(effectKey)));
407391
}
408392
return tag;
409393
}

0 commit comments

Comments
 (0)