Skip to content

Commit 9e63243

Browse files
Make TranslatableRewriter usable in other projects as well (#762)
1 parent 98fad11 commit 9e63243

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
package com.viaversion.viabackwards.api.rewriters;
1919

20-
import com.viaversion.viabackwards.ViaBackwards;
2120
import com.viaversion.viabackwards.api.BackwardsProtocol;
2221
import com.viaversion.viabackwards.api.data.BackwardsMappingDataLoader;
2322
import com.viaversion.viaversion.api.connection.UserConnection;
@@ -37,10 +36,16 @@ public class TranslatableRewriter<C extends ClientboundPacketType> extends Compo
3736
private final Map<String, String> translatables;
3837

3938
public static void loadTranslatables() {
40-
final JsonObject jsonObject = BackwardsMappingDataLoader.INSTANCE.loadFromDataDir("translation-mappings.json");
39+
if (!TRANSLATABLES.isEmpty()) {
40+
throw new IllegalStateException("Translatables already loaded!");
41+
}
42+
fillTranslatables(BackwardsMappingDataLoader.INSTANCE.loadFromDataDir("translation-mappings.json"), TRANSLATABLES);
43+
}
44+
45+
public static void fillTranslatables(final JsonObject jsonObject, final Map<String, Map<String, String>> translatables) {
4146
for (final Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
4247
final Map<String, String> versionMappings = new HashMap<>();
43-
TRANSLATABLES.put(entry.getKey(), versionMappings);
48+
translatables.put(entry.getKey(), versionMappings);
4449
for (final Map.Entry<String, JsonElement> translationEntry : entry.getValue().getAsJsonObject().entrySet()) {
4550
versionMappings.put(translationEntry.getKey(), translationEntry.getValue().getAsString());
4651
}
@@ -53,7 +58,7 @@ public TranslatableRewriter(final BackwardsProtocol<C, ?, ?, ?> protocol, final
5358

5459
public TranslatableRewriter(final BackwardsProtocol<C, ?, ?, ?> protocol, final ReadType type, final String sectionIdentifier) {
5560
super(protocol, type);
56-
final Map<String, String> translatableMappings = TRANSLATABLES.get(sectionIdentifier);
61+
final Map<String, String> translatableMappings = getTranslatableMappings(sectionIdentifier);
5762
if (translatableMappings == null) {
5863
protocol.getLogger().warning("Missing " + sectionIdentifier + " translatables!");
5964
this.translatables = new HashMap<>();
@@ -81,4 +86,8 @@ protected void handleTranslate(final UserConnection connection, final CompoundTa
8186
public @Nullable String mappedTranslationKey(final String translationKey) {
8287
return translatables.get(translationKey);
8388
}
89+
90+
public Map<String, String> getTranslatableMappings(final String sectionIdentifier) {
91+
return TRANSLATABLES.get(sectionIdentifier);
92+
}
8493
}

0 commit comments

Comments
 (0)