diff --git a/src/main/java/cc/woverflow/hytils/handlers/cache/PatternHandler.java b/src/main/java/cc/woverflow/hytils/handlers/cache/PatternHandler.java index 9d19bfdc..080e9b8e 100644 --- a/src/main/java/cc/woverflow/hytils/handlers/cache/PatternHandler.java +++ b/src/main/java/cc/woverflow/hytils/handlers/cache/PatternHandler.java @@ -21,6 +21,7 @@ import cc.polyfrost.oneconfig.utils.JsonUtils; import cc.polyfrost.oneconfig.utils.Multithreading; import cc.polyfrost.oneconfig.utils.NetworkUtils; +import cc.woverflow.hytils.HytilsReborn; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -35,16 +36,24 @@ public class PatternHandler { public void initialize() { Multithreading.runAsync(() -> { try { + JsonObject cached = HytilsReborn.INSTANCE.getLanguageHandler().getRegexJson(); + if (cached != null) { + processJson(cached); + return; + } final String gotten = NetworkUtils.getString("https://data.woverflow.cc/regex.json"); if (gotten != null) { - JsonObject jsonObject = JsonUtils.parseString(gotten).getAsJsonObject(); - for (JsonElement element : jsonObject.getAsJsonArray("game_end")) { - gameEnd.add(Pattern.compile(element.getAsString())); - } + processJson(JsonUtils.parseString(gotten).getAsJsonObject()); } } catch (Exception e) { e.printStackTrace(); } }); } + + private void processJson(JsonObject jsonObject) { + for (JsonElement element : jsonObject.getAsJsonArray("game_end")) { + gameEnd.add(Pattern.compile(element.getAsString())); + } + } } diff --git a/src/main/java/cc/woverflow/hytils/handlers/chat/ChatHandler.java b/src/main/java/cc/woverflow/hytils/handlers/chat/ChatHandler.java index a5cf06a1..74b6a3ac 100644 --- a/src/main/java/cc/woverflow/hytils/handlers/chat/ChatHandler.java +++ b/src/main/java/cc/woverflow/hytils/handlers/chat/ChatHandler.java @@ -143,11 +143,15 @@ public void handleChat(ClientChatReceivedEvent event) { HytilsReborn.INSTANCE.getAutoQueue().onMessageReceived(event); for (ChatReceiveModule module : this.receiveModules) { - if (module.isEnabled()) { - module.onMessageReceived(event); - if (event.isCanceled()) { - return; + try { + if (module.isEnabled()) { + module.onMessageReceived(event); + if (event.isCanceled()) { + return; + } } + } catch (Exception e) { + HytilsReborn.INSTANCE.getLogger().error("An error occurred while handling a chat message with module " + module.getClass().getSimpleName(), e); } } } @@ -167,11 +171,15 @@ public String handleSentMessage(@NotNull String message) { } for (ChatSendModule module : this.sendModules) { - if (module.isEnabled()) { - message = module.onMessageSend(message); - if (message == null) { - return null; + try { + if (module.isEnabled()) { + message = module.onMessageSend(message); + if (message == null) { + return null; + } } + } catch (Exception e) { + HytilsReborn.INSTANCE.getLogger().error("An error occurred while handling a sent message with module " + module.getClass().getSimpleName(), e); } } diff --git a/src/main/java/cc/woverflow/hytils/handlers/language/LanguageHandler.java b/src/main/java/cc/woverflow/hytils/handlers/language/LanguageHandler.java index 7dbeca5c..a1991831 100644 --- a/src/main/java/cc/woverflow/hytils/handlers/language/LanguageHandler.java +++ b/src/main/java/cc/woverflow/hytils/handlers/language/LanguageHandler.java @@ -24,6 +24,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import net.minecraft.client.Minecraft; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; @@ -44,6 +45,8 @@ public class LanguageHandler { put("FRENCH", "fr"); }}; + private JsonObject regex = null; + private LanguageData current = fallback; public LanguageHandler() { @@ -61,13 +64,17 @@ private void initialize() { userLanguage = "ENGLISH"; } final String language = languageMappings.getOrDefault(userLanguage, "en"); - JsonObject finalJsonObject = NetworkUtils.getJsonElement("https://data.woverflow.cc/regex.json").getAsJsonObject(); - if (!finalJsonObject.entrySet().isEmpty()) { - current = gson.fromJson(finalJsonObject.has(language) ? finalJsonObject.getAsJsonObject(language).toString() : finalJsonObject.getAsJsonObject("en").toString(), LanguageData.class); + regex = NetworkUtils.getJsonElement("https://data.woverflow.cc/regex.json").getAsJsonObject(); + if (!regex.entrySet().isEmpty()) { + current = gson.fromJson(regex.has(language) ? regex.getAsJsonObject(language).toString() : regex.getAsJsonObject("en").toString(), LanguageData.class); } current.initialize(); } + public @Nullable JsonObject getRegexJson() { + return regex; + } + public LanguageData getCurrent() { return current; }