Skip to content

Commit c6ea04f

Browse files
committed
Update ViaNBT
1 parent 79a2881 commit c6ea04f

File tree

24 files changed

+130
-183
lines changed

24 files changed

+130
-183
lines changed

common/src/main/java/com/viaversion/viabackwards/api/rewriters/EnchantmentRewriter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public void handleToServer(Item item) {
7878

7979
public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) {
8080
String key = storedEnchant ? "StoredEnchantments" : "Enchantments";
81-
ListTag enchantments = tag.getListTag(key);
82-
List<Tag> loreToAdd = new ArrayList<>();
81+
ListTag<CompoundTag> enchantments = tag.getListTag(key, CompoundTag.class);
82+
List<StringTag> loreToAdd = new ArrayList<>();
8383
boolean changed = false;
8484

85-
Iterator<Tag> iterator = enchantments.iterator();
85+
Iterator<CompoundTag> iterator = enchantments.iterator();
8686
while (iterator.hasNext()) {
87-
CompoundTag enchantmentEntry = (CompoundTag) iterator.next();
87+
CompoundTag enchantmentEntry = iterator.next();
8888
StringTag idTag = enchantmentEntry.getStringTag("id");
8989

9090
String enchantmentId = idTag.getValue();
@@ -123,9 +123,9 @@ public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant)
123123
tag.put("display", display = new CompoundTag());
124124
}
125125

126-
ListTag loreTag = display.getListTag("Lore");
126+
ListTag<StringTag> loreTag = display.getListTag("Lore", StringTag.class);
127127
if (loreTag == null) {
128-
display.put("Lore", loreTag = new ListTag(StringTag.class));
128+
display.put("Lore", loreTag = new ListTag<>(StringTag.class));
129129
} else {
130130
// Save original lore
131131
itemRewriter.saveListTag(display, loreTag, "Lore");

common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriter.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,10 @@ public ItemRewriter(T protocol, Type<Item> itemType, Type<Item[]> itemArrayType)
6060
name.setValue(newValue);
6161
}
6262

63-
ListTag lore = display.getListTag("Lore");
63+
ListTag<StringTag> lore = display.getListTag("Lore", StringTag.class);
6464
if (lore != null) {
6565
boolean changed = false;
66-
for (Tag loreEntryTag : lore) {
67-
if (!(loreEntryTag instanceof StringTag)) {
68-
continue;
69-
}
70-
71-
StringTag loreEntry = (StringTag) loreEntryTag;
66+
for (StringTag loreEntry : lore) {
7267
String newValue = protocol.getTranslatableRewriter().processText(loreEntry.getValue()).toString();
7368
if (!changed && !newValue.equals(loreEntry.getValue())) {
7469
// Backup original lore before doing any modifications

common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriterBase.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,11 @@ protected void saveStringTag(CompoundTag displayTag, StringTag original, String
6262
}
6363
}
6464

65-
protected void saveListTag(CompoundTag displayTag, ListTag original, String name) {
65+
protected void saveListTag(CompoundTag displayTag, ListTag<?> original, String name) {
6666
// Multiple places might try to backup data
6767
String backupName = nbtTagName + "|o" + name;
6868
if (!displayTag.contains(backupName)) {
69-
// Clone all tag entries
70-
ListTag listTag = new ListTag();
71-
for (Tag tag : original.getValue()) {
72-
listTag.add(tag.copy());
73-
}
74-
75-
displayTag.put(backupName, listTag);
69+
displayTag.put(backupName, original.copy());
7670
}
7771
}
7872

@@ -103,7 +97,7 @@ protected void restoreStringTag(CompoundTag tag, String tagName) {
10397
protected void restoreListTag(CompoundTag tag, String tagName) {
10498
Tag original = tag.remove(nbtTagName + "|o" + tagName);
10599
if (original instanceof ListTag) {
106-
tag.put(tagName, new ListTag(((ListTag) original).getValue()));
100+
tag.put(tagName, ((ListTag<?>) original).copy());
107101
}
108102
}
109103

common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEnchantmentRewriter.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,18 @@ public void registerEnchantment(int id, String replacementLore) {
4747

4848
public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) {
4949
String key = storedEnchant ? "StoredEnchantments" : "ench";
50-
ListTag enchantments = tag.getListTag(key);
51-
ListTag remappedEnchantments = new ListTag(CompoundTag.class);
52-
List<Tag> lore = new ArrayList<>();
53-
for (Tag enchantmentEntry : enchantments.copy()) {
54-
if (!(enchantmentEntry instanceof CompoundTag)) {
55-
continue;
56-
}
57-
58-
CompoundTag entryTag = (CompoundTag) enchantmentEntry;
59-
NumberTag idTag = entryTag.getNumberTag("id");
50+
ListTag<CompoundTag> enchantments = tag.getListTag(key, CompoundTag.class);
51+
ListTag<CompoundTag> remappedEnchantments = new ListTag<>(CompoundTag.class);
52+
List<StringTag> lore = new ArrayList<>();
53+
for (CompoundTag enchantmentEntry : enchantments.copy()) {
54+
NumberTag idTag = enchantmentEntry.getNumberTag("id");
6055
if (idTag == null) continue;
6156

6257
short newId = idTag.asShort();
6358
String enchantmentName = enchantmentMappings.get(newId);
6459
if (enchantmentName != null) {
6560
enchantments.remove(enchantmentEntry);
66-
NumberTag levelTag = entryTag.getNumberTag("lvl");
61+
NumberTag levelTag = enchantmentEntry.getNumberTag("lvl");
6762
short level = levelTag != null ? levelTag.asShort() : 1;
6863
if (hideLevelForEnchants != null && hideLevelForEnchants.contains(newId)) {
6964
lore.add(new StringTag(enchantmentName));
@@ -99,9 +94,9 @@ public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant)
9994
if (display == null) {
10095
tag.put("display", display = new CompoundTag());
10196
}
102-
ListTag loreTag = display.getListTag("Lore");
97+
ListTag<StringTag> loreTag = display.getListTag("Lore", StringTag.class);
10398
if (loreTag == null) {
104-
display.put("Lore", loreTag = new ListTag(StringTag.class));
99+
display.put("Lore", loreTag = new ListTag<>(StringTag.class));
105100
}
106101

107102
lore.addAll(loreTag.getValue());
@@ -111,21 +106,15 @@ public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant)
111106

112107
public void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnchant) {
113108
String key = storedEnchant ? "StoredEnchantments" : "ench";
114-
ListTag remappedEnchantments = tag.remove(nbtTagName + "|" + key);
115-
ListTag enchantments = tag.getListTag(key);
109+
ListTag<CompoundTag> enchantments = tag.getListTag(key, CompoundTag.class);
116110
if (enchantments == null) {
117-
enchantments = new ListTag(CompoundTag.class);
111+
enchantments = new ListTag<>(CompoundTag.class);
118112
}
119113

120114
if (!storedEnchant && tag.remove(nbtTagName + "|dummyEnchant") != null) {
121-
for (Tag enchantment : enchantments.copy()) {
122-
if (!(enchantment instanceof CompoundTag)) {
123-
continue;
124-
}
125-
126-
CompoundTag entryTag = (CompoundTag) enchantment;
127-
NumberTag idTag = entryTag.getNumberTag("id");
128-
NumberTag levelTag = entryTag.getNumberTag("lvl");
115+
for (CompoundTag enchantment : enchantments.copy()) {
116+
NumberTag idTag = enchantment.getNumberTag("id");
117+
NumberTag levelTag = enchantment.getNumberTag("lvl");
129118
short id = idTag != null ? idTag.asShort() : 0;
130119
short level = levelTag != null ? levelTag.asShort() : 0;
131120
if (id == 0 && level == 0) {
@@ -143,8 +132,9 @@ public void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnchant)
143132

144133
CompoundTag display = tag.getCompoundTag("display");
145134
// A few null checks just to be safe, though they shouldn't actually be
146-
ListTag lore = display != null ? display.getListTag("Lore") : null;
147-
for (Tag enchantment : remappedEnchantments.copy()) {
135+
ListTag<StringTag> lore = display != null ? display.getListTag("Lore", StringTag.class) : null;
136+
ListTag<CompoundTag> remappedEnchantments = tag.remove(nbtTagName + "|" + key);
137+
for (CompoundTag enchantment : remappedEnchantments.copy()) {
148138
enchantments.add(enchantment);
149139
if (lore != null && !lore.isEmpty()) {
150140
lore.remove(lore.get(0));

common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/BannerHandler.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,11 @@ else if (blockId >= WALL_BANNER_START && blockId <= WALL_BANNER_STOP) {
4848
}
4949

5050
// Invert colors
51-
ListTag patternsTag = tag.getListTag("Patterns");
51+
ListTag<CompoundTag> patternsTag = tag.getListTag("Patterns", CompoundTag.class);
5252
if (patternsTag != null) {
53-
for (Tag pattern : patternsTag) {
54-
if (!(pattern instanceof CompoundTag)) continue;
55-
56-
CompoundTag patternTag = (CompoundTag) pattern;
57-
NumberTag colorTag = patternTag.getNumberTag("Color");
58-
patternTag.putInt("Color", 15 - colorTag.asInt()); // Invert color id
53+
for (CompoundTag pattern : patternsTag) {
54+
NumberTag colorTag = pattern.getNumberTag("Color");
55+
pattern.putInt("Color", 15 - colorTag.asInt()); // Invert color id
5956
}
6057
}
6158

common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public PistonHandler() {
4747
addEntries(entry.getKey(), entry.getValue());
4848
}
4949
} else {
50-
ListTag blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").get("blockstates");
50+
ListTag<StringTag> blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class);
5151
for (int id = 0; id < blockStates.size(); id++) {
5252
StringTag state = blockStates.get(id);
5353
String key = state.getValue();

common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,10 @@ private int itemIdToRaw(int oldId, Item item, CompoundTag tag) {
597597

598598
private void rewriteCanPlaceToClient(CompoundTag tag, String tagName) {
599599
// The tag was manually created incorrectly so ignore rewriting it
600-
ListTag blockTag = tag.getListTag(tagName);
600+
ListTag<?> blockTag = tag.getListTag(tagName);
601601
if (blockTag == null) return;
602602

603-
ListTag newCanPlaceOn = new ListTag(StringTag.class);
603+
ListTag<StringTag> newCanPlaceOn = new ListTag<>(StringTag.class);
604604
tag.put(extraNbtTag + "|" + tagName, blockTag.copy());
605605
for (Tag oldTag : blockTag) {
606606
Object value = oldTag.getValue();
@@ -611,7 +611,7 @@ private void rewriteCanPlaceToClient(CompoundTag tag, String tagName) {
611611
newCanPlaceOn.add(new StringTag(newValue));
612612
}
613613
} else {
614-
newCanPlaceOn.add(oldTag);
614+
newCanPlaceOn.add(new StringTag(oldTag.getValue().toString()));
615615
}
616616
}
617617
tag.put(tagName, newCanPlaceOn);
@@ -620,19 +620,14 @@ private void rewriteCanPlaceToClient(CompoundTag tag, String tagName) {
620620
//TODO un-ugly all of this
621621
private void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnch) {
622622
String key = storedEnch ? "StoredEnchantments" : "Enchantments";
623-
ListTag enchantments = tag.getListTag(key);
623+
ListTag<CompoundTag> enchantments = tag.getListTag(key, CompoundTag.class);
624624
if (enchantments == null) return;
625625

626-
ListTag noMapped = new ListTag(CompoundTag.class);
627-
ListTag newEnchantments = new ListTag(CompoundTag.class);
628-
List<Tag> lore = new ArrayList<>();
626+
ListTag<CompoundTag> noMapped = new ListTag<>(CompoundTag.class);
627+
ListTag<CompoundTag> newEnchantments = new ListTag<>(CompoundTag.class);
628+
List<StringTag> lore = new ArrayList<>();
629629
boolean hasValidEnchants = false;
630-
for (Tag enchantmentEntryTag : enchantments.copy()) {
631-
if (!(enchantmentEntryTag instanceof CompoundTag)) {
632-
continue;
633-
}
634-
635-
CompoundTag enchantmentEntry = (CompoundTag) enchantmentEntryTag;
630+
for (CompoundTag enchantmentEntry : enchantments.copy()) {
636631
StringTag idTag = enchantmentEntry.getStringTag("id");
637632
if (idTag == null) {
638633
continue;
@@ -720,13 +715,13 @@ private void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnch) {
720715
tag.put("display", display = new CompoundTag());
721716
}
722717

723-
ListTag loreTag = display.getListTag("Lore");
718+
ListTag<StringTag> loreTag = display.getListTag("Lore", StringTag.class);
724719
if (loreTag == null) {
725-
display.put("Lore", loreTag = new ListTag(StringTag.class));
720+
display.put("Lore", loreTag = new ListTag<>(StringTag.class));
726721
tag.put(extraNbtTag + "|DummyLore", new ByteTag());
727722
} else if (!loreTag.isEmpty()) {
728-
ListTag oldLore = new ListTag(StringTag.class);
729-
for (Tag value : loreTag) {
723+
ListTag<StringTag> oldLore = new ListTag<>(StringTag.class);
724+
for (StringTag value : loreTag) {
730725
oldLore.add(value.copy());
731726
}
732727
tag.put(extraNbtTag + "|OldLore", oldLore);
@@ -854,11 +849,11 @@ public Item handleItemToServer(Item item) {
854849
private void rewriteCanPlaceToServer(CompoundTag tag, String tagName) {
855850
if (tag.getListTag(tagName) == null) return;
856851

857-
ListTag blockTag = tag.remove(extraNbtTag + "|" + tagName);
852+
ListTag<?> blockTag = tag.remove(extraNbtTag + "|" + tagName);
858853
if (blockTag != null) {
859854
tag.put(tagName, blockTag.copy());
860855
} else if ((blockTag = tag.getListTag(tagName)) != null) {
861-
ListTag newCanPlaceOn = new ListTag(StringTag.class);
856+
ListTag<StringTag> newCanPlaceOn = new ListTag<>(StringTag.class);
862857
for (Tag oldTag : blockTag) {
863858
Object value = oldTag.getValue();
864859
String oldId = Key.stripMinecraftNamespace(value.toString());
@@ -884,10 +879,10 @@ private void rewriteCanPlaceToServer(CompoundTag tag, String tagName) {
884879

885880
private void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnch) {
886881
String key = storedEnch ? "StoredEnchantments" : "Enchantments";
887-
ListTag enchantments = tag.getListTag(storedEnch ? key : "ench");
882+
ListTag<CompoundTag> enchantments = tag.getListTag(storedEnch ? key : "ench", CompoundTag.class);
888883
if (enchantments == null) return;
889884

890-
ListTag newEnchantments = new ListTag(CompoundTag.class);
885+
ListTag<CompoundTag> newEnchantments = new ListTag<>(CompoundTag.class);
891886
boolean dummyEnchant = false;
892887
if (!storedEnch) {
893888
Tag hideFlags = tag.remove(extraNbtTag + "|OldHideFlags");
@@ -900,12 +895,7 @@ private void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnch) {
900895
}
901896
}
902897

903-
for (Tag enchEntry : enchantments) {
904-
if (!(enchEntry instanceof CompoundTag)) {
905-
continue;
906-
}
907-
908-
CompoundTag entryTag = (CompoundTag) enchEntry;
898+
for (CompoundTag entryTag : enchantments) {
909899
NumberTag idTag = entryTag.getNumberTag("id");
910900
NumberTag levelTag = entryTag.getNumberTag("lvl");
911901
CompoundTag enchantmentEntry = new CompoundTag();
@@ -925,26 +915,28 @@ private void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnch) {
925915
newEnchantments.add(enchantmentEntry);
926916
}
927917

928-
Tag noMapped = tag.remove(extraNbtTag + "|Enchantments");
929-
if (noMapped instanceof ListTag) {
930-
for (Tag value : ((ListTag) noMapped)) {
918+
ListTag<CompoundTag> noMapped = tag.getListTag(extraNbtTag + "|Enchantments", CompoundTag.class);
919+
if (noMapped != null) {
920+
for (CompoundTag value : noMapped) {
931921
newEnchantments.add(value);
932922
}
923+
tag.remove(extraNbtTag + "|Enchantments");
933924
}
934925

935926
CompoundTag display = tag.getCompoundTag("display");
936927
if (display == null) {
937928
tag.put("display", display = new CompoundTag());
938929
}
939930

940-
Tag oldLore = tag.remove(extraNbtTag + "|OldLore");
941-
if (oldLore instanceof ListTag) {
942-
ListTag lore = display.getListTag("Lore");
931+
ListTag<StringTag> oldLore = tag.getListTag(extraNbtTag + "|OldLore", StringTag.class);
932+
if (oldLore != null) {
933+
ListTag<StringTag> lore = display.getListTag("Lore", StringTag.class);
943934
if (lore == null) {
944-
tag.put("Lore", lore = new ListTag());
935+
tag.put("Lore", lore = new ListTag<>(StringTag.class));
945936
}
946937

947-
lore.setValue(((ListTag) oldLore).getValue());
938+
lore.setValue(oldLore.getValue());
939+
tag.remove(extraNbtTag + "|OldLore");
948940
} else if (tag.remove(extraNbtTag + "|DummyLore") != null) {
949941
display.remove("Lore");
950942
if (display.isEmpty()) {
@@ -969,14 +961,11 @@ private void invertShieldAndBannerId(Item item, CompoundTag tag) {
969961
blockEntityTag.putInt("Base", 15 - base.asInt()); // Invert color id
970962
}
971963

972-
ListTag patterns = blockEntityTag.getListTag("Patterns");
964+
ListTag<CompoundTag> patterns = blockEntityTag.getListTag("Patterns", CompoundTag.class);
973965
if (patterns != null) {
974-
for (Tag pattern : patterns) {
975-
if (!(pattern instanceof CompoundTag)) continue;
976-
977-
CompoundTag patternTag = (CompoundTag) pattern;
978-
NumberTag colorTag = patternTag.getNumberTag("Color");
979-
patternTag.putInt("Color", 15 - colorTag.asInt()); // Invert color id
966+
for (CompoundTag pattern : patterns) {
967+
NumberTag colorTag = pattern.getNumberTag("Color");
968+
pattern.putInt("Color", 15 - colorTag.asInt()); // Invert color id
980969
}
981970
}
982971
}

common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -473,17 +473,14 @@ public Item handleItemToClient(Item item) {
473473
CompoundTag tag = item.tag();
474474
CompoundTag display;
475475
if (tag != null && (display = tag.getCompoundTag("display")) != null) {
476-
ListTag lore = display.getListTag("Lore");
476+
ListTag<StringTag> lore = display.getListTag("Lore", StringTag.class);
477477
if (lore != null) {
478478
saveListTag(display, lore, "Lore");
479479

480-
for (Tag loreEntry : lore) {
481-
if (!(loreEntry instanceof StringTag)) continue;
482-
483-
StringTag loreEntryTag = (StringTag) loreEntry;
484-
String value = loreEntryTag.getValue();
480+
for (StringTag loreEntry : lore) {
481+
String value = loreEntry.getValue();
485482
if (value != null && !value.isEmpty()) {
486-
loreEntryTag.setValue(ComponentUtil.jsonToLegacy(value));
483+
loreEntry.setValue(ComponentUtil.jsonToLegacy(value));
487484
}
488485
}
489486
}
@@ -502,13 +499,10 @@ public Item handleItemToServer(Item item) {
502499
CompoundTag display;
503500
if (tag != null && (display = tag.getCompoundTag("display")) != null) {
504501
// Transform to json if no backup tag is found (else process that in the super method)
505-
ListTag lore = display.getListTag("Lore");
502+
ListTag<StringTag> lore = display.getListTag("Lore", StringTag.class);
506503
if (lore != null && !hasBackupTag(display, "Lore")) {
507-
for (Tag loreEntry : lore) {
508-
if (loreEntry instanceof StringTag) {
509-
StringTag loreEntryTag = (StringTag) loreEntry;
510-
loreEntryTag.setValue(ComponentUtil.legacyToJsonString(loreEntryTag.getValue()));
511-
}
504+
for (StringTag loreEntry : lore) {
505+
loreEntry.setValue(ComponentUtil.legacyToJsonString(loreEntry.getValue()));
512506
}
513507
}
514508
}

0 commit comments

Comments
 (0)