Skip to content

Commit

Permalink
try catch chat handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Wyvest committed Nov 10, 2022
1 parent fc380f5 commit 3e508c5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()));
}
}
}
24 changes: 16 additions & 8 deletions src/main/java/cc/woverflow/hytils/handlers/chat/ChatHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,6 +45,8 @@ public class LanguageHandler {
put("FRENCH", "fr");
}};

private JsonObject regex = null;

private LanguageData current = fallback;

public LanguageHandler() {
Expand All @@ -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;
}
Expand Down

0 comments on commit 3e508c5

Please sign in to comment.