Skip to content

Commit f868dfa

Browse files
committed
Update ViaNBT
1 parent cad78ea commit f868dfa

File tree

27 files changed

+147
-205
lines changed

27 files changed

+147
-205
lines changed

api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
2626
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
2727
import com.github.steveice10.opennbt.tag.builtin.ListTag;
28+
import com.github.steveice10.opennbt.tag.builtin.StringTag;
2829
import com.github.steveice10.opennbt.tag.builtin.Tag;
2930
import com.viaversion.viaversion.api.Via;
3031
import com.viaversion.viaversion.api.minecraft.RegistryType;
@@ -83,8 +84,8 @@ public void load() {
8384
entityMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "entities");
8485
argumentTypeMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "argumenttypes");
8586

86-
final ListTag unmappedParticles = unmappedIdentifierData.get("particles");
87-
final ListTag mappedParticles = mappedIdentifierData.get("particles");
87+
final ListTag<StringTag> unmappedParticles = unmappedIdentifierData.getListTag("particles", StringTag.class);
88+
final ListTag<StringTag> mappedParticles = mappedIdentifierData.getListTag("particles", StringTag.class);
8889
if (unmappedParticles != null && mappedParticles != null) {
8990
Mappings particleMappings = loadMappings(data, "particles");
9091
if (particleMappings == null) {

api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
2828
import com.github.steveice10.opennbt.tag.builtin.IntTag;
2929
import com.github.steveice10.opennbt.tag.builtin.ListTag;
30+
import com.github.steveice10.opennbt.tag.builtin.StringTag;
3031
import com.github.steveice10.opennbt.tag.io.NBTIO;
3132
import com.github.steveice10.opennbt.tag.io.TagReader;
3233
import com.google.common.annotations.Beta;
@@ -227,8 +228,8 @@ public static void clearCache() {
227228
}
228229

229230
public static FullMappings loadFullMappings(final CompoundTag mappingsTag, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
230-
final ListTag unmappedElements = unmappedIdentifiers.get(key);
231-
final ListTag mappedElements = mappedIdentifiers.get(key);
231+
final ListTag<StringTag> unmappedElements = unmappedIdentifiers.getListTag(key, StringTag.class);
232+
final ListTag<StringTag> mappedElements = mappedIdentifiers.getListTag(key, StringTag.class);
232233
if (unmappedElements == null || mappedElements == null) {
233234
return null;
234235
}
@@ -239,8 +240,8 @@ public static FullMappings loadFullMappings(final CompoundTag mappingsTag, final
239240
}
240241

241242
return new FullMappingsBase(
242-
unmappedElements.getValue().stream().map(t -> (String) t.getValue()).collect(Collectors.toList()),
243-
mappedElements.getValue().stream().map(t -> (String) t.getValue()).collect(Collectors.toList()),
243+
unmappedElements.getValue().stream().map(StringTag::getValue).collect(Collectors.toList()),
244+
mappedElements.getValue().stream().map(StringTag::getValue).collect(Collectors.toList()),
244245
mappings
245246
);
246247
}

common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
2323
import com.github.steveice10.opennbt.tag.builtin.ListTag;
2424
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
25+
import com.github.steveice10.opennbt.tag.builtin.StringTag;
2526
import com.github.steveice10.opennbt.tag.builtin.Tag;
2627
import com.viaversion.viaversion.api.Via;
2728
import com.viaversion.viaversion.api.connection.UserConnection;
@@ -166,9 +167,9 @@ public static void init() {
166167
}
167168

168169
Via.getPlatform().getLogger().info("Loading block connection mappings ...");
169-
ListTag blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").getListTag("blockstates");
170+
ListTag<StringTag> blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class);
170171
for (int id = 0; id < blockStates.size(); id++) {
171-
String key = (String) blockStates.get(id).getValue();
172+
String key = blockStates.get(id).getValue();
172173
KEY_TO_ID.put(key, id);
173174
}
174175

@@ -177,11 +178,10 @@ public static void init() {
177178
if (!Via.getConfig().isReduceBlockStorageMemory()) {
178179
blockConnectionData = new Int2ObjectOpenHashMap<>(2048);
179180

180-
ListTag blockConnectionMappings = MappingDataLoader.loadNBT("blockConnections.nbt").getListTag("data");
181-
for (Tag blockTag : blockConnectionMappings) {
182-
CompoundTag blockCompoundTag = (CompoundTag) blockTag;
181+
ListTag<CompoundTag> blockConnectionMappings = MappingDataLoader.loadNBT("blockConnections.nbt").getListTag("data", CompoundTag.class);
182+
for (CompoundTag blockTag : blockConnectionMappings) {
183183
BlockData blockData = new BlockData();
184-
for (Entry<String, Tag> entry : blockCompoundTag.entrySet()) {
184+
for (Entry<String, Tag> entry : blockTag.entrySet()) {
185185
String key = entry.getKey();
186186
if (key.equals("id") || key.equals("ids")) {
187187
continue;
@@ -198,11 +198,11 @@ public static void init() {
198198
blockData.put(connectionTypeId, attachingFaces);
199199
}
200200

201-
NumberTag idTag = blockCompoundTag.getNumberTag("id");
201+
NumberTag idTag = blockTag.getNumberTag("id");
202202
if (idTag != null) {
203203
blockConnectionData.put(idTag.asInt(), blockData);
204204
} else {
205-
IntArrayTag idsTag = blockCompoundTag.getIntArrayTag("ids");
205+
IntArrayTag idsTag = blockTag.getIntArrayTag("ids");
206206
for (int id : idsTag.getValue()) {
207207
blockConnectionData.put(id, blockData);
208208
}

common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java

Lines changed: 38 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,16 @@ public Item handleItemToClient(Item item) {
295295
blockEntityTag.putInt("Base", 15 - baseTag.asInt());
296296
}
297297

298-
ListTag patternsTag = blockEntityTag.getListTag("Patterns");
298+
ListTag<CompoundTag> patternsTag = blockEntityTag.getListTag("Patterns", CompoundTag.class);
299299
if (patternsTag != null) {
300-
for (Tag pattern : patternsTag) {
301-
if (!(pattern instanceof CompoundTag)) {
302-
continue;
303-
}
304-
305-
CompoundTag patternTag = (CompoundTag) pattern;
306-
NumberTag colorTag = patternTag.getNumberTag("Color");
300+
for (CompoundTag pattern : patternsTag) {
301+
NumberTag colorTag = pattern.getNumberTag("Color");
307302
if (colorTag == null) {
308303
continue;
309304
}
310305

311306
// Invert color id
312-
patternTag.putInt("Color", 15 - colorTag.asInt());
307+
pattern.putInt("Color", 15 - colorTag.asInt());
313308
}
314309
}
315310
}
@@ -324,16 +319,11 @@ public Item handleItemToClient(Item item) {
324319
}
325320
}
326321
// ench is now Enchantments and now uses identifiers
327-
ListTag ench = tag.getListTag("ench");
322+
ListTag<CompoundTag> ench = tag.getListTag("ench", CompoundTag.class);
328323
if (ench != null) {
329-
ListTag enchantments = new ListTag(CompoundTag.class);
330-
for (Tag enchEntry : ench) {
331-
if (!(enchEntry instanceof CompoundTag)) {
332-
continue;
333-
}
334-
335-
CompoundTag entryTag = (CompoundTag) enchEntry;
336-
NumberTag idTag = entryTag.getNumberTag("id");
324+
ListTag<CompoundTag> enchantments = new ListTag<>(CompoundTag.class);
325+
for (CompoundTag enchEntry : ench) {
326+
NumberTag idTag = enchEntry.getNumberTag("id");
337327
if (idTag == null) {
338328
continue;
339329
}
@@ -346,7 +336,7 @@ public Item handleItemToClient(Item item) {
346336
}
347337
enchantmentEntry.putString("id", newId);
348338

349-
NumberTag levelTag = entryTag.getNumberTag("lvl");
339+
NumberTag levelTag = enchEntry.getNumberTag("lvl");
350340
if (levelTag != null) {
351341
enchantmentEntry.putShort("lvl", levelTag.asShort());
352342
}
@@ -357,16 +347,11 @@ public Item handleItemToClient(Item item) {
357347
tag.put("Enchantments", enchantments);
358348
}
359349

360-
ListTag storedEnch = tag.getListTag("StoredEnchantments");
350+
ListTag<CompoundTag> storedEnch = tag.getListTag("StoredEnchantments", CompoundTag.class);
361351
if (storedEnch != null) {
362-
ListTag newStoredEnch = new ListTag(CompoundTag.class);
363-
for (Tag enchEntry : storedEnch) {
364-
if (!(enchEntry instanceof CompoundTag)) {
365-
continue;
366-
}
367-
368-
CompoundTag entryTag = (CompoundTag) enchEntry;
369-
NumberTag idTag = entryTag.getNumberTag("id");
352+
ListTag<CompoundTag> newStoredEnch = new ListTag<>(CompoundTag.class);
353+
for (CompoundTag enchEntry : storedEnch) {
354+
NumberTag idTag = enchEntry.getNumberTag("id");
370355
if (idTag == null) {
371356
continue;
372357
}
@@ -379,7 +364,7 @@ public Item handleItemToClient(Item item) {
379364
}
380365
enchantmentEntry.putString("id", newId);
381366

382-
NumberTag levelTag = entryTag.getNumberTag("lvl");
367+
NumberTag levelTag = enchEntry.getNumberTag("lvl");
383368
if (levelTag != null) {
384369
enchantmentEntry.putShort("lvl", levelTag.asShort());
385370
}
@@ -389,9 +374,9 @@ public Item handleItemToClient(Item item) {
389374
tag.put("StoredEnchantments", newStoredEnch);
390375
}
391376

392-
ListTag canPlaceOnTag = tag.getListTag("CanPlaceOn");
377+
ListTag<?> canPlaceOnTag = tag.getListTag("CanPlaceOn");
393378
if (canPlaceOnTag != null) {
394-
ListTag newCanPlaceOn = new ListTag(StringTag.class);
379+
ListTag<StringTag> newCanPlaceOn = new ListTag<>(StringTag.class);
395380
tag.put(NBT_TAG_NAME + "|CanPlaceOn", canPlaceOnTag.copy());
396381
for (Tag oldTag : canPlaceOnTag) {
397382
Object value = oldTag.getValue();
@@ -412,9 +397,9 @@ public Item handleItemToClient(Item item) {
412397
tag.put("CanPlaceOn", newCanPlaceOn);
413398
}
414399

415-
ListTag canDestroyTag = tag.getListTag("CanDestroy");
400+
ListTag<?> canDestroyTag = tag.getListTag("CanDestroy");
416401
if (canDestroyTag != null) {
417-
ListTag newCanDestroy = new ListTag(StringTag.class);
402+
ListTag<StringTag> newCanDestroy = new ListTag<>(StringTag.class);
418403
tag.put(NBT_TAG_NAME + "|CanDestroy", canDestroyTag.copy());
419404
for (Tag oldTag : canDestroyTag) {
420405
Object value = oldTag.getValue();
@@ -594,16 +579,11 @@ public Item handleItemToServer(Item item) {
594579
blockEntityTag.putInt("Base", 15 - baseTag.asInt()); // invert color id
595580
}
596581

597-
ListTag patternsTag = blockEntityTag.getListTag("Patterns");
582+
ListTag<CompoundTag> patternsTag = blockEntityTag.getListTag("Patterns", CompoundTag.class);
598583
if (patternsTag != null) {
599-
for (Tag pattern : patternsTag) {
600-
if (!(pattern instanceof CompoundTag)) {
601-
continue;
602-
}
603-
604-
CompoundTag patternTag = (CompoundTag) pattern;
605-
NumberTag colorTag = patternTag.getNumberTag("Color");
606-
patternTag.putInt("Color", 15 - colorTag.asInt()); // Invert color id
584+
for (CompoundTag pattern : patternsTag) {
585+
NumberTag colorTag = pattern.getNumberTag("Color");
586+
pattern.putInt("Color", 15 - colorTag.asInt()); // Invert color id
607587
}
608588
}
609589
}
@@ -619,16 +599,11 @@ public Item handleItemToServer(Item item) {
619599
}
620600

621601
// ench is now Enchantments and now uses identifiers
622-
ListTag enchantments = tag.getListTag("Enchantments");
602+
ListTag<CompoundTag> enchantments = tag.getListTag("Enchantments", CompoundTag.class);
623603
if (enchantments != null) {
624-
ListTag ench = new ListTag(CompoundTag.class);
625-
for (Tag enchantmentEntry : enchantments) {
626-
if (!(enchantmentEntry instanceof CompoundTag)) {
627-
continue;
628-
}
629-
630-
CompoundTag entryTag = (CompoundTag) enchantmentEntry;
631-
StringTag idTag = entryTag.getStringTag("id");
604+
ListTag<CompoundTag> ench = new ListTag<>(CompoundTag.class);
605+
for (CompoundTag enchantmentEntry : enchantments) {
606+
StringTag idTag = enchantmentEntry.getStringTag("id");
632607
if (idTag == null) {
633608
continue;
634609
}
@@ -641,7 +616,7 @@ public Item handleItemToServer(Item item) {
641616
}
642617
if (oldId != null) {
643618
enchEntry.putShort("id", oldId);
644-
NumberTag levelTag = entryTag.getNumberTag("lvl");
619+
NumberTag levelTag = enchantmentEntry.getNumberTag("lvl");
645620
if (levelTag != null) {
646621
enchEntry.putShort("lvl", levelTag.asShort());
647622
}
@@ -653,16 +628,11 @@ public Item handleItemToServer(Item item) {
653628
}
654629

655630

656-
ListTag storedEnch = tag.getListTag("StoredEnchantments");
631+
ListTag<CompoundTag> storedEnch = tag.getListTag("StoredEnchantments", CompoundTag.class);
657632
if (storedEnch != null) {
658-
ListTag newStoredEnch = new ListTag(CompoundTag.class);
659-
for (Tag enchantmentEntry : storedEnch) {
660-
if (!(enchantmentEntry instanceof CompoundTag)) {
661-
continue;
662-
}
663-
664-
CompoundTag entryTag = (CompoundTag) enchantmentEntry;
665-
StringTag idTag = entryTag.getStringTag("id");
633+
ListTag<CompoundTag> newStoredEnch = new ListTag<>(CompoundTag.class);
634+
for (CompoundTag enchantmentEntry : storedEnch) {
635+
StringTag idTag = enchantmentEntry.getStringTag("id");
666636
if (idTag == null) {
667637
continue;
668638
}
@@ -679,7 +649,7 @@ public Item handleItemToServer(Item item) {
679649
}
680650

681651
enchEntry.putShort("id", oldId);
682-
NumberTag levelTag = entryTag.getNumberTag("lvl");
652+
NumberTag levelTag = enchantmentEntry.getNumberTag("lvl");
683653
if (levelTag != null) {
684654
enchEntry.putShort("lvl", levelTag.asShort());
685655
}
@@ -690,8 +660,8 @@ public Item handleItemToServer(Item item) {
690660
if (tag.getListTag(NBT_TAG_NAME + "|CanPlaceOn") != null) {
691661
tag.put("CanPlaceOn", tag.remove(NBT_TAG_NAME + "|CanPlaceOn"));
692662
} else if (tag.getListTag("CanPlaceOn") != null) {
693-
ListTag old = tag.getListTag("CanPlaceOn");
694-
ListTag newCanPlaceOn = new ListTag(StringTag.class);
663+
ListTag<?> old = tag.getListTag("CanPlaceOn");
664+
ListTag<StringTag> newCanPlaceOn = new ListTag<>(StringTag.class);
695665
for (Tag oldTag : old) {
696666
Object value = oldTag.getValue();
697667
String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String
@@ -702,16 +672,16 @@ public Item handleItemToServer(Item item) {
702672
newCanPlaceOn.add(new StringTag(newValue));
703673
}
704674
} else {
705-
newCanPlaceOn.add(oldTag);
675+
newCanPlaceOn.add(new StringTag(value.toString()));
706676
}
707677
}
708678
tag.put("CanPlaceOn", newCanPlaceOn);
709679
}
710680
if (tag.getListTag(NBT_TAG_NAME + "|CanDestroy") != null) {
711681
tag.put("CanDestroy", tag.remove(NBT_TAG_NAME + "|CanDestroy"));
712682
} else if (tag.getListTag("CanDestroy") != null) {
713-
ListTag old = tag.getListTag("CanDestroy");
714-
ListTag newCanDestroy = new ListTag(StringTag.class);
683+
ListTag<?> old = tag.getListTag("CanDestroy");
684+
ListTag<StringTag> newCanDestroy = new ListTag<>(StringTag.class);
715685
for (Tag oldTag : old) {
716686
Object value = oldTag.getValue();
717687
String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String
@@ -722,7 +692,7 @@ public Item handleItemToServer(Item item) {
722692
newCanDestroy.add(new StringTag(newValue));
723693
}
724694
} else {
725-
newCanDestroy.add(oldTag);
695+
newCanDestroy.add(new StringTag(oldTag.getValue().toString()));
726696
}
727697
}
728698
tag.put("CanDestroy", newCanDestroy);

common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/blockentities/BannerHandler.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,12 @@ public int transform(UserConnection user, CompoundTag tag) {
6060
Via.getPlatform().getLogger().warning("Why does this block have the banner block entity? :(" + tag);
6161
}
6262

63-
ListTag patterns = tag.getListTag("Patterns");
63+
ListTag<CompoundTag> patterns = tag.getListTag("Patterns", CompoundTag.class);
6464
if (patterns != null) {
65-
for (Tag pattern : patterns) {
66-
if (!(pattern instanceof CompoundTag)) {
67-
continue;
68-
}
69-
70-
CompoundTag patternTag = (CompoundTag) pattern;
71-
NumberTag colorTag = patternTag.getNumberTag("Color");
65+
for (CompoundTag pattern : patterns) {
66+
NumberTag colorTag = pattern.getNumberTag("Color");
7267
if (colorTag != null) {
73-
patternTag.putInt("Color", 15 - colorTag.asInt()); // Invert color id
68+
pattern.putInt("Color", 15 - colorTag.asInt()); // Invert color id
7469
}
7570
}
7671
}

0 commit comments

Comments
 (0)