Skip to content

Commit

Permalink
Update ViaNBT
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytv committed Dec 31, 2023
1 parent 83fa0c6 commit 651bf05
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
import com.viaversion.viaversion.libs.gson.JsonIOException;
import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.gson.JsonSyntaxException;
import com.viaversion.viaversion.libs.opennbt.NBTIO;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag;
import com.viaversion.viaversion.libs.opennbt.tag.io.NBTIO;
import com.viaversion.viaversion.libs.opennbt.tag.io.TagReader;
import com.viaversion.viaversion.util.GsonUtil;
import java.io.File;
import java.io.FileReader;
Expand All @@ -35,14 +36,16 @@

public final class VBMappingDataLoader {

private static final TagReader<CompoundTag> TAG_READER = NBTIO.reader(CompoundTag.class).named();

public static @Nullable CompoundTag loadNBT(final String name) {
final InputStream resource = getResource(name);
if (resource == null) {
return null;
}

try (final InputStream stream = resource) {
return NBTIO.readTag(stream);
return TAG_READER.read(stream);
} catch (final IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -65,7 +68,7 @@ public final class VBMappingDataLoader {

ViaBackwards.getPlatform().getLogger().info("Loading " + name + " from plugin folder");
try {
final CompoundTag fileData = NBTIO.readFile(file, false, false);
final CompoundTag fileData = TAG_READER.read(file.toPath(), false);
return mergeTags(packedData, fileData);
} catch (final IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void saveListTag(CompoundTag displayTag, ListTag original, String name
// Clone all tag entries
ListTag listTag = new ListTag();
for (Tag tag : original.getValue()) {
listTag.add(tag.clone());
listTag.add(tag.copy());
}

displayTag.put(backupName, listTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant)
ListTag enchantments = tag.get(key);
ListTag remappedEnchantments = new ListTag(CompoundTag.class);
List<Tag> lore = new ArrayList<>();
for (Tag enchantmentEntry : enchantments.clone()) {
for (Tag enchantmentEntry : enchantments.copy()) {
Tag idTag = ((CompoundTag) enchantmentEntry).get("id");
if (idTag == null) continue;

Expand Down Expand Up @@ -114,7 +114,7 @@ public void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnchant)
}

if (!storedEnchant && tag.remove(nbtTagName + "|dummyEnchant") != null) {
for (Tag enchantment : enchantments.clone()) {
for (Tag enchantment : enchantments.copy()) {
short id = ((NumberTag) ((CompoundTag) enchantment).get("id")).asShort();
short level = ((NumberTag) ((CompoundTag) enchantment).get("lvl")).asShort();
if (id == 0 && level == 0) {
Expand All @@ -133,7 +133,7 @@ public void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnchant)
CompoundTag display = tag.get("display");
// A few null checks just to be safe, though they shouldn't actually be
ListTag lore = display != null ? display.get("Lore") : null;
for (Tag enchantment : remappedEnchantments.clone()) {
for (Tag enchantment : remappedEnchantments.copy()) {
enchantments.add(enchantment);
if (lore != null && lore.size() != 0) {
lore.remove(lore.get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ private void rewriteCanPlaceToClient(CompoundTag tag, String tagName) {
if (blockTag == null) return;

ListTag newCanPlaceOn = new ListTag(StringTag.class);
tag.put(extraNbtTag + "|" + tagName, blockTag.clone());
tag.put(extraNbtTag + "|" + tagName, blockTag.copy());
for (Tag oldTag : blockTag) {
Object value = oldTag.getValue();
String[] newValues = value instanceof String ?
Expand All @@ -611,7 +611,7 @@ private void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnch) {
ListTag newEnchantments = new ListTag(CompoundTag.class);
List<Tag> lore = new ArrayList<>();
boolean hasValidEnchants = false;
for (Tag enchantmentEntryTag : enchantments.clone()) {
for (Tag enchantmentEntryTag : enchantments.copy()) {
CompoundTag enchantmentEntry = (CompoundTag) enchantmentEntryTag;
Tag idTag = enchantmentEntry.get("id");
if (!(idTag instanceof StringTag)) {
Expand Down Expand Up @@ -708,7 +708,7 @@ private void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnch) {
} else if (loreTag.size() != 0) {
ListTag oldLore = new ListTag(StringTag.class);
for (Tag value : loreTag) {
oldLore.add(value.clone());
oldLore.add(value.copy());
}
tag.put(extraNbtTag + "|OldLore", oldLore);

Expand Down Expand Up @@ -835,7 +835,7 @@ private void rewriteCanPlaceToServer(CompoundTag tag, String tagName) {
if (!(tag.get(tagName) instanceof ListTag)) return;
ListTag blockTag = tag.remove(extraNbtTag + "|" + tagName);
if (blockTag != null) {
tag.put(tagName, blockTag.clone());
tag.put(tagName, blockTag.copy());
} else if ((blockTag = tag.get(tagName)) != null) {
ListTag newCanPlaceOn = new ListTag(StringTag.class);
for (Tag oldTag : blockTag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static Item copyItem(Item item) {
return null;
}
Item copy = new DataItem(item);
copy.setTag(copy.tag() == null ? null : copy.tag().clone());
copy.setTag(copy.tag() == null ? null : copy.tag().copy());
return copy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
import com.viaversion.viabackwards.api.data.VBMappingDataLoader;
import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap;
import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap;
import com.viaversion.viaversion.libs.opennbt.NBTIO;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.Protocol1_19To1_18_2;
import java.io.IOException;
import org.checkerframework.checker.nullness.qual.Nullable;

public final class BackwardsMappings extends com.viaversion.viabackwards.api.data.BackwardsMappings {
Expand All @@ -41,15 +39,11 @@ public BackwardsMappings() {
protected void loadExtras(final CompoundTag data) {
super.loadExtras(data);

try {
final ListTag chatTypes = NBTIO.readTag(VBMappingDataLoader.getResource("chat-types-1.19.1.nbt")).get("values");
for (final Tag chatType : chatTypes) {
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
final NumberTag idTag = chatTypeCompound.get("id");
defaultChatTypes.put(idTag.asInt(), chatTypeCompound);
}
} catch (final IOException e) {
e.printStackTrace();
final ListTag chatTypes = VBMappingDataLoader.loadNBT("chat-types-1.19.1.nbt").get("values");
for (final Tag chatType : chatTypes) {
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
final NumberTag idTag = chatTypeCompound.get("id");
defaultChatTypes.put(idTag.asInt(), chatTypeCompound);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void register() {
final CompoundTag dimensionCompound = (CompoundTag) dimension;
final StringTag nameTag = dimensionCompound.get("name");
final CompoundTag dimensionData = dimensionCompound.get("element");
dimensionRegistryStorage.addDimension(nameTag.getValue(), dimensionData.clone());
dimensionRegistryStorage.addDimension(nameTag.getValue(), dimensionData.copy());

if (!found && nameTag.getValue().equals(dimensionKey)) {
wrapper.write(Type.NAMED_COMPOUND_TAG, dimensionData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class DimensionRegistryStorage implements StorableObject {

public @Nullable CompoundTag dimension(final String dimensionKey) {
final CompoundTag compoundTag = dimensions.get(dimensionKey);
return compoundTag != null ? compoundTag.clone() : null;
return compoundTag != null ? compoundTag.copy() : null;
}

public void addDimension(final String dimensionKey, final CompoundTag dimension) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
package com.viaversion.viabackwards.protocol.protocol1_19_4to1_20.data;

import com.viaversion.viabackwards.api.data.VBMappingDataLoader;
import com.viaversion.viaversion.libs.opennbt.NBTIO;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.protocols.protocol1_20to1_19_4.Protocol1_20To1_19_4;
import java.io.IOException;

public class BackwardsMappings extends com.viaversion.viabackwards.api.data.BackwardsMappings {

Expand All @@ -35,14 +33,10 @@ public BackwardsMappings() {
protected void loadExtras(CompoundTag data) {
super.loadExtras(data);

try {
trimPatternRegistry = NBTIO.readTag(VBMappingDataLoader.getResource("trim_pattern-1.19.4.nbt"));
} catch (final IOException e) {
e.printStackTrace();
}
trimPatternRegistry = VBMappingDataLoader.loadNBT("trim_pattern-1.19.4.nbt");
}

public CompoundTag getTrimPatternRegistry() {
return trimPatternRegistry;
return trimPatternRegistry.copy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void register() {
if (registry.contains("minecraft:trim_pattern")) {
values = ((CompoundTag) registry.get("minecraft:trim_pattern")).get("value");
} else {
final CompoundTag trimPatternRegistry = Protocol1_19_4To1_20.MAPPINGS.getTrimPatternRegistry().clone();
final CompoundTag trimPatternRegistry = Protocol1_19_4To1_20.MAPPINGS.getTrimPatternRegistry().copy();
registry.put("minecraft:trim_pattern", trimPatternRegistry);
values = trimPatternRegistry.get("value");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void register() {

// Replace with 1.19 chat types
// Ensures that the client has a chat type for system message, with and without overlay
registry.put("minecraft:chat_type", EntityPackets.CHAT_REGISTRY.clone());
registry.put("minecraft:chat_type", EntityPackets.CHAT_REGISTRY.copy());
});
handler(entityRewriter.worldTrackerHandlerByKey());
}
Expand Down
1 change: 0 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ rootProject.name = "viabackwards-parent"

dependencyResolutionManagement {
repositories {
mavenLocal()
maven("https://repo.viaversion.com")
maven("https://repo.papermc.io/repository/maven-public/")
maven("https://oss.sonatype.org/content/repositories/snapshots/")
Expand Down

0 comments on commit 651bf05

Please sign in to comment.