From d8b7e7d9ff9ba215eabc2af351d8c2a5515af4b8 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Sun, 24 Mar 2024 11:46:08 +0100 Subject: [PATCH] Update mapping data loader (#700) --- .../api/data/BackwardsMappings.java | 4 +- .../api/data/VBMappingDataLoader.java | 69 ++++--------------- .../rewriters/LegacyBlockItemRewriter.java | 2 +- .../api/rewriters/TranslatableRewriter.java | 2 +- .../block_entity_handlers/PistonHandler.java | 2 +- .../data/BiomeMappings.java | 2 +- .../data/BackwardsMappings.java | 2 +- .../data/BackwardsMappings.java | 2 +- .../data/BackwardsMappings.java | 2 +- .../data/BackwardsMappings.java | 2 +- 10 files changed, 22 insertions(+), 67 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java index ca2bb569..4347306c 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java @@ -167,7 +167,7 @@ public class BackwardsMappings extends MappingDataBase { } @Override - protected @Nullable CompoundTag readNBTFile(final String name) { - return VBMappingDataLoader.loadNBTFromDir(name); + protected @Nullable CompoundTag readMappingsFile(final String name) { + return VBMappingDataLoader.INSTANCE.loadNBTFromDir(name); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java index 2c89b3a5..5b23f440 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java @@ -18,37 +18,25 @@ package com.viaversion.viabackwards.api.data; import com.viaversion.viabackwards.ViaBackwards; -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.api.data.MappingDataLoader; 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; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.util.Map; import org.checkerframework.checker.nullness.qual.Nullable; -public final class VBMappingDataLoader { +public class VBMappingDataLoader extends MappingDataLoader { - private static final TagReader TAG_READER = NBTIO.reader(CompoundTag.class).named(); + public static final VBMappingDataLoader INSTANCE = new VBMappingDataLoader(); - public static @Nullable CompoundTag loadNBT(final String name) { - final InputStream resource = getResource(name); - if (resource == null) { - return null; - } + public VBMappingDataLoader() { + super(VBMappingDataLoader.class, "assets/viabackwards/data/"); + } - try (final InputStream stream = resource) { - return TAG_READER.read(stream); - } catch (final IOException e) { - throw new RuntimeException(e); - } + @Override + public File getFile(final String name) { + return new File(ViaBackwards.getPlatform().getDataFolder(), name); } /** @@ -58,7 +46,7 @@ public final class VBMappingDataLoader { * @param name name of the file * @return nbt data from the plugin folder or packed assets */ - public static @Nullable CompoundTag loadNBTFromDir(final String name) { + public @Nullable CompoundTag loadNBTFromDir(final String name) { final CompoundTag packedData = loadNBT(name); final File file = new File(ViaBackwards.getPlatform().getDataFolder(), name); @@ -68,14 +56,14 @@ public final class VBMappingDataLoader { ViaBackwards.getPlatform().getLogger().info("Loading " + name + " from plugin folder"); try { - final CompoundTag fileData = TAG_READER.read(file.toPath(), false); + final CompoundTag fileData = MAPPINGS_READER.read(file.toPath(), false); return mergeTags(packedData, fileData); } catch (final IOException e) { throw new RuntimeException(e); } } - private static CompoundTag mergeTags(final CompoundTag original, final CompoundTag extra) { + private CompoundTag mergeTags(final CompoundTag original, final CompoundTag extra) { for (final Map.Entry entry : extra.entrySet()) { if (entry.getValue() instanceof CompoundTag) { // For compound tags, don't replace the entire tag @@ -90,37 +78,4 @@ public final class VBMappingDataLoader { } return original; } - - public static JsonObject loadData(final String name) { - try (final InputStream stream = getResource(name)) { - if (stream == null) return null; - return GsonUtil.getGson().fromJson(new InputStreamReader(stream), JsonObject.class); - } catch (final IOException e) { - throw new RuntimeException(e); - } - } - - public static JsonObject loadFromDataDir(final String name) { - final File file = new File(ViaBackwards.getPlatform().getDataFolder(), name); - if (!file.exists()) { - return loadData(name); - } - - // Load the file from the platform's directory if present - try (final FileReader reader = new FileReader(file)) { - return GsonUtil.getGson().fromJson(reader, JsonObject.class); - } catch (final JsonSyntaxException e) { - ViaBackwards.getPlatform().getLogger().warning(name + " is badly formatted!"); - e.printStackTrace(); - ViaBackwards.getPlatform().getLogger().warning("Falling back to resource's file!"); - return loadData(name); - } catch (final IOException | JsonIOException e) { - e.printStackTrace(); - } - return null; - } - - public static @Nullable InputStream getResource(final String name) { - return VBMappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viabackwards/data/" + name); - } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java index 65d77327..fa74708c 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java @@ -52,7 +52,7 @@ public abstract class LegacyBlockItemRewriter replacementData; // Raw id -> mapped data static { - JsonObject jsonObject = VBMappingDataLoader.loadFromDataDir("legacy-mappings.json"); + JsonObject jsonObject = VBMappingDataLoader.INSTANCE.loadFromDataDir("legacy-mappings.json"); for (Map.Entry entry : jsonObject.entrySet()) { Int2ObjectMap mappings = new Int2ObjectOpenHashMap<>(8); LEGACY_MAPPINGS.put(entry.getKey(), mappings); diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java index 100a0328..7eda082c 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/TranslatableRewriter.java @@ -34,7 +34,7 @@ public class TranslatableRewriter extends Compo private final Map translatables; public static void loadTranslatables() { - final JsonObject jsonObject = VBMappingDataLoader.loadFromDataDir("translation-mappings.json"); + final JsonObject jsonObject = VBMappingDataLoader.INSTANCE.loadFromDataDir("translation-mappings.json"); for (final Map.Entry entry : jsonObject.entrySet()) { final Map versionMappings = new HashMap<>(); TRANSLATABLES.put(entry.getKey(), versionMappings); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java index 77b624c6..11d1e921 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java @@ -47,7 +47,7 @@ public class PistonHandler implements BackwardsBlockEntityProvider.BackwardsBloc addEntries(entry.getKey(), entry.getValue()); } } else { - ListTag blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class); + ListTag blockStates = MappingDataLoader.INSTANCE.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class); for (int id = 0; id < blockStates.size(); id++) { StringTag state = blockStates.get(id); String key = state.getValue(); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/data/BiomeMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/data/BiomeMappings.java index 978cbe2d..f2b2afd9 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/data/BiomeMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/data/BiomeMappings.java @@ -117,7 +117,7 @@ public final class BiomeMappings { MODERN_TO_LEGACY_ID.put(entry.getKey(), entry.getIntValue()); } - final JsonObject mappings = VBMappingDataLoader.loadFromDataDir("biome-mappings.json"); + final JsonObject mappings = VBMappingDataLoader.INSTANCE.loadFromDataDir("biome-mappings.json"); for (final Map.Entry entry : mappings.entrySet()) { final int legacyBiome = LEGACY_BIOMES.getInt(entry.getValue().getAsString()); if (legacyBiome == -1) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/data/BackwardsMappings.java index 929a234b..681f2ee6 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/data/BackwardsMappings.java @@ -38,7 +38,7 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat protected void loadExtras(final CompoundTag data) { super.loadExtras(data); - final ListTag chatTypes = VBMappingDataLoader.loadNBT("chat-types-1.19.1.nbt").getListTag("values", CompoundTag.class); + final ListTag chatTypes = VBMappingDataLoader.INSTANCE.loadNBT("chat-types-1.19.1.nbt").getListTag("values", CompoundTag.class); for (final CompoundTag chatType : chatTypes) { final NumberTag idTag = chatType.getNumberTag("id"); defaultChatTypes.put(idTag.asInt(), chatType); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/data/BackwardsMappings.java index 1ee5d74c..ab561c9b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/data/BackwardsMappings.java @@ -39,7 +39,7 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat protected void loadExtras(final CompoundTag data) { super.loadExtras(data); - final JsonArray sounds = VBMappingDataLoader.loadData("sounds-1.19.json").getAsJsonArray("sounds"); + final JsonArray sounds = VBMappingDataLoader.INSTANCE.loadData("sounds-1.19.json").getAsJsonArray("sounds"); int i = 0; for (final JsonElement sound : sounds) { mappedSounds.put(sound.getAsString(), i++); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/data/BackwardsMappings.java index 572d99b7..33f03732 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_4to1_20/data/BackwardsMappings.java @@ -33,7 +33,7 @@ public class BackwardsMappings extends com.viaversion.viabackwards.api.data.Back protected void loadExtras(CompoundTag data) { super.loadExtras(data); - trimPatternRegistry = VBMappingDataLoader.loadNBT("trim_pattern-1.19.4.nbt"); + trimPatternRegistry = VBMappingDataLoader.INSTANCE.loadNBT("trim_pattern-1.19.4.nbt"); } public CompoundTag getTrimPatternRegistry() { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/data/BackwardsMappings.java index 59f68129..a16d574c 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/data/BackwardsMappings.java @@ -36,7 +36,7 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat protected void loadExtras(final CompoundTag data) { super.loadExtras(data); - final JsonArray sounds = VBMappingDataLoader.loadData("sounds-1.20.3.json").getAsJsonArray("sounds"); + final JsonArray sounds = VBMappingDataLoader.INSTANCE.loadData("sounds-1.20.3.json").getAsJsonArray("sounds"); this.sounds = new String[sounds.size()]; int i = 0; for (final JsonElement sound : sounds) {