3
0
Mirror von https://github.com/ViaVersion/ViaBackwards.git synchronisiert 2024-12-27 08:30:10 +01:00

Update mapping data loader (#700)

Dieser Commit ist enthalten in:
EnZaXD 2024-03-24 11:46:08 +01:00 committet von GitHub
Ursprung 51561d143c
Commit d8b7e7d9ff
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
10 geänderte Dateien mit 22 neuen und 67 gelöschten Zeilen

Datei anzeigen

@ -167,7 +167,7 @@ public class BackwardsMappings extends MappingDataBase {
} }
@Override @Override
protected @Nullable CompoundTag readNBTFile(final String name) { protected @Nullable CompoundTag readMappingsFile(final String name) {
return VBMappingDataLoader.loadNBTFromDir(name); return VBMappingDataLoader.INSTANCE.loadNBTFromDir(name);
} }
} }

Datei anzeigen

@ -18,37 +18,25 @@
package com.viaversion.viabackwards.api.data; package com.viaversion.viabackwards.api.data;
import com.viaversion.viabackwards.ViaBackwards; import com.viaversion.viabackwards.ViaBackwards;
import com.viaversion.viaversion.libs.gson.JsonIOException; import com.viaversion.viaversion.api.data.MappingDataLoader;
import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.gson.JsonSyntaxException;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; 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.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.File;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map; import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
public final class VBMappingDataLoader { public class VBMappingDataLoader extends MappingDataLoader {
private static final TagReader<CompoundTag> TAG_READER = NBTIO.reader(CompoundTag.class).named(); public static final VBMappingDataLoader INSTANCE = new VBMappingDataLoader();
public static @Nullable CompoundTag loadNBT(final String name) { public VBMappingDataLoader() {
final InputStream resource = getResource(name); super(VBMappingDataLoader.class, "assets/viabackwards/data/");
if (resource == null) {
return null;
} }
try (final InputStream stream = resource) { @Override
return TAG_READER.read(stream); public File getFile(final String name) {
} catch (final IOException e) { return new File(ViaBackwards.getPlatform().getDataFolder(), name);
throw new RuntimeException(e);
}
} }
/** /**
@ -58,7 +46,7 @@ public final class VBMappingDataLoader {
* @param name name of the file * @param name name of the file
* @return nbt data from the plugin folder or packed assets * @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 CompoundTag packedData = loadNBT(name);
final File file = new File(ViaBackwards.getPlatform().getDataFolder(), 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"); ViaBackwards.getPlatform().getLogger().info("Loading " + name + " from plugin folder");
try { try {
final CompoundTag fileData = TAG_READER.read(file.toPath(), false); final CompoundTag fileData = MAPPINGS_READER.read(file.toPath(), false);
return mergeTags(packedData, fileData); return mergeTags(packedData, fileData);
} catch (final IOException e) { } catch (final IOException e) {
throw new RuntimeException(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<String, Tag> entry : extra.entrySet()) { for (final Map.Entry<String, Tag> entry : extra.entrySet()) {
if (entry.getValue() instanceof CompoundTag) { if (entry.getValue() instanceof CompoundTag) {
// For compound tags, don't replace the entire tag // For compound tags, don't replace the entire tag
@ -90,37 +78,4 @@ public final class VBMappingDataLoader {
} }
return original; 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);
}
} }

Datei anzeigen

@ -52,7 +52,7 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
protected final Int2ObjectMap<MappedLegacyBlockItem> replacementData; // Raw id -> mapped data protected final Int2ObjectMap<MappedLegacyBlockItem> replacementData; // Raw id -> mapped data
static { static {
JsonObject jsonObject = VBMappingDataLoader.loadFromDataDir("legacy-mappings.json"); JsonObject jsonObject = VBMappingDataLoader.INSTANCE.loadFromDataDir("legacy-mappings.json");
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
Int2ObjectMap<MappedLegacyBlockItem> mappings = new Int2ObjectOpenHashMap<>(8); Int2ObjectMap<MappedLegacyBlockItem> mappings = new Int2ObjectOpenHashMap<>(8);
LEGACY_MAPPINGS.put(entry.getKey(), mappings); LEGACY_MAPPINGS.put(entry.getKey(), mappings);

Datei anzeigen

@ -34,7 +34,7 @@ public class TranslatableRewriter<C extends ClientboundPacketType> extends Compo
private final Map<String, String> translatables; private final Map<String, String> translatables;
public static void loadTranslatables() { 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<String, JsonElement> entry : jsonObject.entrySet()) { for (final Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
final Map<String, String> versionMappings = new HashMap<>(); final Map<String, String> versionMappings = new HashMap<>();
TRANSLATABLES.put(entry.getKey(), versionMappings); TRANSLATABLES.put(entry.getKey(), versionMappings);

Datei anzeigen

@ -47,7 +47,7 @@ public class PistonHandler implements BackwardsBlockEntityProvider.BackwardsBloc
addEntries(entry.getKey(), entry.getValue()); addEntries(entry.getKey(), entry.getValue());
} }
} else { } else {
ListTag<StringTag> blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class); ListTag<StringTag> blockStates = MappingDataLoader.INSTANCE.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class);
for (int id = 0; id < blockStates.size(); id++) { for (int id = 0; id < blockStates.size(); id++) {
StringTag state = blockStates.get(id); StringTag state = blockStates.get(id);
String key = state.getValue(); String key = state.getValue();

Datei anzeigen

@ -117,7 +117,7 @@ public final class BiomeMappings {
MODERN_TO_LEGACY_ID.put(entry.getKey(), entry.getIntValue()); 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<String, JsonElement> entry : mappings.entrySet()) { for (final Map.Entry<String, JsonElement> entry : mappings.entrySet()) {
final int legacyBiome = LEGACY_BIOMES.getInt(entry.getValue().getAsString()); final int legacyBiome = LEGACY_BIOMES.getInt(entry.getValue().getAsString());
if (legacyBiome == -1) { if (legacyBiome == -1) {

Datei anzeigen

@ -38,7 +38,7 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat
protected void loadExtras(final CompoundTag data) { protected void loadExtras(final CompoundTag data) {
super.loadExtras(data); super.loadExtras(data);
final ListTag<CompoundTag> chatTypes = VBMappingDataLoader.loadNBT("chat-types-1.19.1.nbt").getListTag("values", CompoundTag.class); final ListTag<CompoundTag> chatTypes = VBMappingDataLoader.INSTANCE.loadNBT("chat-types-1.19.1.nbt").getListTag("values", CompoundTag.class);
for (final CompoundTag chatType : chatTypes) { for (final CompoundTag chatType : chatTypes) {
final NumberTag idTag = chatType.getNumberTag("id"); final NumberTag idTag = chatType.getNumberTag("id");
defaultChatTypes.put(idTag.asInt(), chatType); defaultChatTypes.put(idTag.asInt(), chatType);

Datei anzeigen

@ -39,7 +39,7 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat
protected void loadExtras(final CompoundTag data) { protected void loadExtras(final CompoundTag data) {
super.loadExtras(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; int i = 0;
for (final JsonElement sound : sounds) { for (final JsonElement sound : sounds) {
mappedSounds.put(sound.getAsString(), i++); mappedSounds.put(sound.getAsString(), i++);

Datei anzeigen

@ -33,7 +33,7 @@ public class BackwardsMappings extends com.viaversion.viabackwards.api.data.Back
protected void loadExtras(CompoundTag data) { protected void loadExtras(CompoundTag data) {
super.loadExtras(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() { public CompoundTag getTrimPatternRegistry() {

Datei anzeigen

@ -36,7 +36,7 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat
protected void loadExtras(final CompoundTag data) { protected void loadExtras(final CompoundTag data) {
super.loadExtras(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()]; this.sounds = new String[sounds.size()];
int i = 0; int i = 0;
for (final JsonElement sound : sounds) { for (final JsonElement sound : sounds) {