diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java index 526cd25..60b41a3 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java @@ -69,14 +69,19 @@ public class ChatPlugin extends Bot.Listener { public void packetReceived (ClientboundSystemChatPacket packet) { final Component component = packet.getContent(); - try { - final String key = ((TranslatableComponent) component).key(); + if ( + component instanceof TextComponent t_component && + t_component.content().length() > 15_000 + ) return; + + if (component instanceof TranslatableComponent t_component) { + final String key = t_component.key(); if ( key.equals("advMode.setCommand.success") || key.equals("advMode.notAllowed") ) return; - } catch (ClassCastException ignored) {} + } PlayerMessage playerMessage = null; @@ -86,20 +91,15 @@ public class ChatPlugin extends Bot.Listener { } boolean isCommandSuggestions = false; - try { - final String id = ((TextComponent) component).content(); + if (component instanceof TextComponent t_component) { + final String id = t_component.content(); if (id.equals(bot.commandSuggestion.id)) isCommandSuggestions = true; - } catch (Exception ignored) {} - - final String string = ComponentUtilities.stringify(component); - final String ansi = ComponentUtilities.stringifyAnsi(component); + } for (Listener listener : listeners) { - if (string.length() < 25_000) { - if (!isCommandSuggestions) listener.systemMessageReceived(component, string, ansi); - listener.systemMessageReceived(component, isCommandSuggestions, string, ansi); - } + if (!isCommandSuggestions) listener.systemMessageReceived(component); + listener.systemMessageReceived(component, isCommandSuggestions); if (playerMessage != null) listener.playerMessageReceived(playerMessage); } @@ -140,13 +140,6 @@ public class ChatPlugin extends Bot.Listener { final String translation = getTranslationByChatType(packet.getChatType()); - String string = null; - String ansi = null; - if (translation != null && unsignedContent != null) { - string = ComponentUtilities.stringify(unsignedContent); - ansi = ComponentUtilities.stringifyAnsi(unsignedContent); - } - for (Listener listener : listeners) { listener.playerMessageReceived(playerMessage); @@ -163,13 +156,9 @@ public class ChatPlugin extends Bot.Listener { component = component.args(playerMessage.displayName, playerMessage.contents); } - if (string.length() > 25_000) continue; - - listener.systemMessageReceived(component, string, ansi); + listener.systemMessageReceived(component); } else { - if (string.length() > 25_000) continue; - - listener.systemMessageReceived(unsignedContent, string, ansi); + listener.systemMessageReceived(unsignedContent); } } } @@ -203,10 +192,8 @@ public class ChatPlugin extends Bot.Listener { translatableComponent = translatableComponent.args(name, content); } - final String string = ComponentUtilities.stringify(component); - final String ansi = ComponentUtilities.stringifyAnsi(component); for (Listener listener : listeners) { - listener.systemMessageReceived(translatableComponent, string, ansi); + listener.systemMessageReceived(translatableComponent); } for (ChatParser parser : chatParsers) { @@ -225,15 +212,10 @@ public class ChatPlugin extends Bot.Listener { final PlayerMessage playerMessage = new PlayerMessage(parsedFromMessage.sender, packet.getName(), parsedFromMessage.contents); - final String string = ComponentUtilities.stringify(component); - final String ansi = ComponentUtilities.stringifyAnsi(component); - for (Listener listener : listeners) { listener.playerMessageReceived(playerMessage); - if (string.length() > 25_000) continue; - - listener.systemMessageReceived(component, string, ansi); + listener.systemMessageReceived(component); } } } catch (Exception e) { @@ -349,7 +331,7 @@ public class ChatPlugin extends Bot.Listener { public static class Listener { public void playerMessageReceived (PlayerMessage message) {} - public void systemMessageReceived (Component component, String string, String ansi) {} - public void systemMessageReceived (Component component, boolean isCommandSuggestions, String string, String ansi) {} + public void systemMessageReceived (Component component) {} + public void systemMessageReceived (Component component, boolean isCommandSuggestions) {} } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSpyPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSpyPlugin.java index 26240b8..bb3849c 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSpyPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSpyPlugin.java @@ -22,7 +22,7 @@ public class CommandSpyPlugin extends ChatPlugin.Listener { } @Override - public void systemMessageReceived(Component component, String string, String ansi) { + public void systemMessageReceived(Component component) { TextComponent textComponent; try { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSuggestionPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSuggestionPlugin.java index 1262e0c..f4913bd 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSuggestionPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandSuggestionPlugin.java @@ -20,7 +20,7 @@ public class CommandSuggestionPlugin extends ChatPlugin.Listener { } @Override - public void systemMessageReceived(Component component, boolean isCommandSuggestions, String string, String ansi) { + public void systemMessageReceived(Component component, boolean isCommandSuggestions) { if (!isCommandSuggestions) return; try { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java index 8ddc654..bdb3a9d 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java @@ -59,7 +59,10 @@ public class DiscordPlugin { bot.chat.addListener(new ChatPlugin.Listener() { @Override - public void systemMessageReceived (Component component, String string, String ansi) { + public void systemMessageReceived (Component component) { + final String string = ComponentUtilities.stringify(component); + final String ansi = ComponentUtilities.stringifyAnsi(component); + if (string.length() > 2048) { sendMessage(CodeBlockUtilities.escape(string), channelId); } else { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java index 451aed8..b2376ce 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java @@ -78,8 +78,14 @@ public class LoggerPlugin extends ChatPlugin.Listener { } @Override - public void systemMessageReceived(Component component, String string, String ansi) { - log(ansi, false, logToConsole); + public void systemMessageReceived(Component component) { + final String string = ComponentUtilities.stringify(component); + + if (logToConsole) { + final String ansi = ComponentUtilities.stringifyAnsi(component); + + log(ansi, false, true); + } log(string, true, false); } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java index 09f9817..68b8c01 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java @@ -14,6 +14,7 @@ import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.packet.Packet; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Configuration; +import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; import net.kyori.adventure.text.Component; import org.cloudburstmc.math.vector.Vector3i; @@ -46,7 +47,9 @@ public class SelfCarePlugin extends Bot.Listener { bot.chat.addListener(new ChatPlugin.Listener() { @Override - public void systemMessageReceived(Component component, String string, String ansi) { + public void systemMessageReceived(Component component) { + final String string = ComponentUtilities.stringify(component); + if (string.equals("Successfully enabled CommandSpy")) cspy = true; else if (string.equals("Successfully disabled CommandSpy")) cspy = false; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java index 6fa4ea7..47dcd58 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java @@ -3,10 +3,13 @@ package land.chipmunk.chayapak.chomens_bot.util; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; import land.chipmunk.chayapak.chomens_bot.Main; import java.io.*; -import java.util.ConcurrentModificationException; +import java.util.*; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; public class PersistentDataUtilities { public static final File file = new File("persistent.json"); @@ -15,8 +18,35 @@ public class PersistentDataUtilities { public static JsonObject jsonObject = new JsonObject(); + private static final Map queue = new LinkedHashMap<>(); + + private static final ScheduledFuture future; + static { init(); + + future = Main.executor.scheduleAtFixedRate(() -> { + if (queue.size() == 0) return; + + final Map.Entry entry = queue.entrySet().iterator().next(); // is this the best way to get the first item of the map? + + final String property = entry.getKey(); + final JsonElement value = entry.getValue(); + + if (jsonObject.has(property)) jsonObject.remove(property); + jsonObject.add(property, value); + + write(jsonObject.toString()); + + queue.remove(property); + }, 0, 100, TimeUnit.MILLISECONDS); + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + future.cancel(false); + } + }); } private static void init () { @@ -39,63 +69,36 @@ public class PersistentDataUtilities { } } - private static synchronized void write () { + private static synchronized void write (String string) { try { - final String stringifiedJsonObject = jsonObject.toString(); - writer.close(); // ? how do i clear the file contents without making a completely new FileWriter // or is this the only way? writer = new FileWriter(file, false); - writer.write(stringifiedJsonObject); + writer.write(string); writer.flush(); - } catch (IOException | ConcurrentModificationException ignored) {} + } catch (IOException ignored) {} } - public static synchronized void put (String property, JsonElement value) { - Main.executorService.submit(() -> { - if (jsonObject.has(property)) jsonObject.remove(property); - jsonObject.add(property, value); - - write(); - }); + public static void put (String property, JsonElement value) { + queue.put(property, value); } - public static synchronized void put (String property, String value) { - Main.executorService.submit(() -> { - if (jsonObject.has(property)) jsonObject.remove(property); - jsonObject.addProperty(property, value); - - write(); - }); + public static void put (String property, String value) { + queue.put(property, new JsonPrimitive(value)); } - public static synchronized void put (String property, boolean value) { - Main.executorService.submit(() -> { - if (jsonObject.has(property)) jsonObject.remove(property); - jsonObject.addProperty(property, value); - - write(); - }); + public static void put (String property, boolean value) { + queue.put(property, new JsonPrimitive(value)); } - public static synchronized void put (String property, int value) { - Main.executorService.submit(() -> { - if (jsonObject.has(property)) jsonObject.remove(property); - jsonObject.addProperty(property, value); - - write(); - }); + public static void put (String property, int value) { + queue.put(property, new JsonPrimitive(value)); } - public static synchronized void put (String property, char value) { - Main.executorService.submit(() -> { - if (jsonObject.has(property)) jsonObject.remove(property); - jsonObject.addProperty(property, value); - - write(); - }); + public static void put (String property, char value) { + queue.put(property, new JsonPrimitive(value)); } }