31
31
import com .viaversion .viaversion .api .protocol .packet .PacketWrapper ;
32
32
import com .viaversion .viaversion .api .protocol .remapper .PacketHandlers ;
33
33
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 ;
36
34
import com .viaversion .viaversion .libs .opennbt .tag .builtin .CompoundTag ;
37
35
import com .viaversion .viaversion .libs .opennbt .tag .builtin .IntTag ;
38
36
import com .viaversion .viaversion .libs .opennbt .tag .builtin .StringTag ;
42
40
import com .viaversion .viaversion .protocols .protocol1_20_2to1_20 .packet .ServerboundPackets1_20_2 ;
43
41
import com .viaversion .viaversion .protocols .protocol1_20_2to1_20 .rewriter .RecipeRewriter1_20_2 ;
44
42
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 ;
45
44
import com .viaversion .viaversion .rewriter .BlockRewriter ;
46
45
import com .viaversion .viaversion .util .Key ;
47
46
import com .viaversion .viaversion .util .MathUtil ;
48
- import java .util .Arrays ;
49
- import java .util .List ;
50
47
import org .checkerframework .checker .nullness .qual .Nullable ;
51
48
52
49
public final class BlockItemPacketRewriter1_20_2 extends ItemRewriter <ClientboundPackets1_20_2 , ServerboundPackets1_19_4 , Protocol1_20To1_20_2 > {
53
50
54
- private final Object2IntMap <String > effects = new Object2IntOpenHashMap <>();
55
-
56
51
public BlockItemPacketRewriter1_20_2 (final Protocol1_20To1_20_2 protocol ) {
57
52
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
- }
98
53
}
99
54
100
55
@ Override
@@ -170,8 +125,13 @@ public void register() {
170
125
map (Type .UNSIGNED_BYTE ); // Window id
171
126
map (Type .VAR_INT ); // State id
172
127
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
175
135
});
176
136
}
177
137
});
@@ -181,7 +141,7 @@ public void register() {
181
141
map (Type .UNSIGNED_BYTE ); // Window id
182
142
map (Type .VAR_INT ); // State id
183
143
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 ))));
185
145
}
186
146
});
187
147
protocol .registerClientbound (ClientboundPackets1_20_2 .ADVANCEMENTS , wrapper -> {
@@ -199,7 +159,7 @@ public void register() {
199
159
if (wrapper .passthrough (Type .BOOLEAN )) {
200
160
wrapper .passthrough (Type .COMPONENT ); // Title
201
161
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
203
163
wrapper .passthrough (Type .VAR_INT ); // Frame type
204
164
final int flags = wrapper .passthrough (Type .INT ); // Flags
205
165
if ((flags & 1 ) != 0 ) {
@@ -227,7 +187,7 @@ public void register() {
227
187
byte slot ;
228
188
do {
229
189
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 ) ));
231
191
} while ((slot & 0xFFFFFF80 ) != 0 );
232
192
});
233
193
}
@@ -246,11 +206,11 @@ public void register() {
246
206
final int length = wrapper .passthrough (Type .VAR_INT );
247
207
for (int i = 0 ; i < length ; i ++) {
248
208
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 ) ));
250
210
}
251
211
252
212
// 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 ) ));
254
214
});
255
215
}
256
216
});
@@ -259,9 +219,9 @@ public void register() {
259
219
wrapper .passthrough (Type .VAR_INT ); // Container id
260
220
final int size = wrapper .passthrough (Type .VAR_INT );
261
221
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
265
225
266
226
wrapper .passthrough (Type .BOOLEAN ); // Trade disabled
267
227
wrapper .passthrough (Type .INT ); // Number of tools uses
@@ -276,7 +236,7 @@ public void register() {
276
236
@ Override
277
237
public void register () {
278
238
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
280
240
}
281
241
});
282
242
protocol .registerClientbound (ClientboundPackets1_20_2 .SPAWN_PARTICLE , new PacketHandlers () {
@@ -299,7 +259,7 @@ public void register() {
299
259
final int data = wrapper .read (Type .VAR_INT );
300
260
wrapper .write (Type .VAR_INT , protocol .getMappingData ().getNewBlockStateId (data ));
301
261
} 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 ) ));
303
263
}
304
264
});
305
265
}
@@ -389,6 +349,30 @@ protected void handleIngredient(final PacketWrapper wrapper) throws Exception {
389
349
}.register (ClientboundPackets1_20_2 .DECLARE_RECIPES );
390
350
}
391
351
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
+
392
376
private @ Nullable CompoundTag handleBlockEntity (@ Nullable final CompoundTag tag ) {
393
377
if (tag == null ) {
394
378
return null ;
@@ -397,13 +381,13 @@ protected void handleIngredient(final PacketWrapper wrapper) throws Exception {
397
381
final StringTag primaryEffect = tag .remove ("primary_effect" );
398
382
if (primaryEffect != null ) {
399
383
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 )));
401
385
}
402
386
403
387
final StringTag secondaryEffect = tag .remove ("secondary_effect" );
404
388
if (secondaryEffect != null ) {
405
389
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 )));
407
391
}
408
392
return tag ;
409
393
}
0 commit comments