From 9e63243003e91248315afae960c0ac31199bbe8a Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Mon, 27 May 2024 19:37:57 +0200 Subject: [PATCH] Make TranslatableRewriter usable in other projects as well (#762) --- .../api/rewriters/TranslatableRewriter.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) 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 1b3ad676..910f333b 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 @@ -17,7 +17,6 @@ */ package com.viaversion.viabackwards.api.rewriters; -import com.viaversion.viabackwards.ViaBackwards; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.data.BackwardsMappingDataLoader; import com.viaversion.viaversion.api.connection.UserConnection; @@ -37,10 +36,16 @@ public class TranslatableRewriter extends Compo private final Map translatables; public static void loadTranslatables() { - final JsonObject jsonObject = BackwardsMappingDataLoader.INSTANCE.loadFromDataDir("translation-mappings.json"); + if (!TRANSLATABLES.isEmpty()) { + throw new IllegalStateException("Translatables already loaded!"); + } + fillTranslatables(BackwardsMappingDataLoader.INSTANCE.loadFromDataDir("translation-mappings.json"), TRANSLATABLES); + } + + public static void fillTranslatables(final JsonObject jsonObject, final Map> translatables) { for (final Map.Entry entry : jsonObject.entrySet()) { final Map versionMappings = new HashMap<>(); - TRANSLATABLES.put(entry.getKey(), versionMappings); + translatables.put(entry.getKey(), versionMappings); for (final Map.Entry translationEntry : entry.getValue().getAsJsonObject().entrySet()) { versionMappings.put(translationEntry.getKey(), translationEntry.getValue().getAsString()); } @@ -53,7 +58,7 @@ public class TranslatableRewriter extends Compo public TranslatableRewriter(final BackwardsProtocol protocol, final ReadType type, final String sectionIdentifier) { super(protocol, type); - final Map translatableMappings = TRANSLATABLES.get(sectionIdentifier); + final Map translatableMappings = getTranslatableMappings(sectionIdentifier); if (translatableMappings == null) { protocol.getLogger().warning("Missing " + sectionIdentifier + " translatables!"); this.translatables = new HashMap<>(); @@ -81,4 +86,8 @@ public class TranslatableRewriter extends Compo public @Nullable String mappedTranslationKey(final String translationKey) { return translatables.get(translationKey); } + + public Map getTranslatableMappings(final String sectionIdentifier) { + return TRANSLATABLES.get(sectionIdentifier); + } }