From c90840265a4b5ea9d42fb5251ebd9723a9666b64 Mon Sep 17 00:00:00 2001
From: ChomeNS <95471003+ChomeNS@users.noreply.github.com>
Date: Wed, 23 Apr 2025 16:08:05 +0700
Subject: [PATCH] refactor: improve language stuff in ComponentUtilities

---
 build-number.txt                              |  2 +-
 .../chomens_bot/util/ComponentUtilities.java  | 24 ++++++++++++-------
 2 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/build-number.txt b/build-number.txt
index 9d57e9a..bae2229 100644
--- a/build-number.txt
+++ b/build-number.txt
@@ -1 +1 @@
-2927
\ No newline at end of file
+2928
\ No newline at end of file
diff --git a/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java b/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java
index 58603a2..77584f8 100644
--- a/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java
+++ b/src/main/java/me/chayapak1/chomens_bot/util/ComponentUtilities.java
@@ -13,6 +13,7 @@ import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -20,10 +21,18 @@ import java.util.regex.Pattern;
 public class ComponentUtilities {
     // component parsing
     // rewritten from chipmunkbot, a lot of stuff has changed, and also ANSI and section signs support, etc...
-    public static final Map<String, String> LANGUAGE = loadJsonStringMap("language.json");
-    public static final Map<String, String> VOICE_CHAT_LANGUAGE = loadJsonStringMap("voiceChatLanguage.json");
+    public static final Map<String, String> LANGUAGE = new HashMap<>();
+
+    private static final List<String> LANGUAGES = List.of("language.json", "voiceChatLanguage.json");
+
     public static final Map<String, String> KEYBINDINGS = loadJsonStringMap("keybinds.json");
 
+    static {
+        for (final String language : LANGUAGES) {
+            LANGUAGE.putAll(loadJsonStringMap(language));
+        }
+    }
+
     private static Map<String, String> loadJsonStringMap (final String name) {
         final Map<String, String> map = new HashMap<>();
 
@@ -41,13 +50,12 @@ public class ComponentUtilities {
 
     public static String getOrReturnFallback (final TranslatableComponent component) {
         final String key = component.key();
+        final String fallback = component.fallback();
 
-        final String minecraftKey = LANGUAGE.get(key);
-        final String voiceChatKey = VOICE_CHAT_LANGUAGE.get(key);
-
-        if (minecraftKey != null) return minecraftKey;
-        else if (voiceChatKey != null) return voiceChatKey;
-        else return component.fallback() != null ? component.fallback() : key;
+        return LANGUAGE.getOrDefault(
+                key,
+                fallback != null ? fallback : key
+        );
     }
 
     public static String stringify (final Component message) { return new ComponentParser().stringify(message, ComponentParser.ParseType.PLAIN); }