diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UrbanCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UrbanCommand.java index 7fe5aad..f8bdacc 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UrbanCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/UrbanCommand.java @@ -50,7 +50,7 @@ public class UrbanCommand extends Command { final JsonArray list = jsonObject.getAsJsonArray("list"); - if (list.size() == 0) context.sendOutput(Component.text("No results found").color(NamedTextColor.RED)); + if (list.isEmpty()) context.sendOutput(Component.text("No results found").color(NamedTextColor.RED)); for (JsonElement element : list) { final JsonObject definitionObject = element.getAsJsonObject(); 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 c8c8bb1..683d124 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 @@ -216,7 +216,7 @@ public class ChatPlugin extends Bot.Listener { } private void sendChatTick () { - if (queue.size() == 0) return; + if (queue.isEmpty()) return; final String message = queue.get(0); 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 03f7a09..eac3ce6 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 @@ -146,7 +146,7 @@ public class DiscordPlugin { Component embedsComponent = Component.empty(); if (messageEvent.getEmbeds().size() > 0) { - if (messageEvent.getAttachments().size() == 0) embedsComponent = embedsComponent.append(Component.space()); + if (messageEvent.getAttachments().isEmpty()) embedsComponent = embedsComponent.append(Component.space()); for (MessageEmbed embed : messageEvent.getEmbeds()) { final Component hoverEvent = Component.translatable( """ diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java index 19e67f4..2ee28e8 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java @@ -85,7 +85,7 @@ public class MusicPlayerPlugin extends Bot.Listener { public void onTick() { try { if (currentSong == null) { - if (songQueue.size() == 0) return; // this line + if (songQueue.isEmpty()) return; // this line addBossBar(); @@ -136,7 +136,7 @@ public class MusicPlayerPlugin extends Bot.Listener { songQueue.remove(0); - if (songQueue.size() == 0) { + if (songQueue.isEmpty()) { stopPlaying(); removeBossBar(); bot.chat.tellraw( @@ -168,7 +168,7 @@ public class MusicPlayerPlugin extends Bot.Listener { songQueue.remove(0); stopPlaying(); } - if (songQueue.size() == 0) return; + if (songQueue.isEmpty()) return; currentSong = songQueue.get(0); currentSong.setTime(0); currentSong.play(); 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 44a47c1..ba0399c 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,9 +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 land.chipmunk.chayapak.chomens_bot.Main; import java.io.*; +import java.util.ArrayList; import java.util.ConcurrentModificationException; +import java.util.List; +import java.util.concurrent.TimeUnit; public class PersistentDataUtilities { public static final File file = new File("persistent.json"); @@ -14,7 +18,7 @@ public class PersistentDataUtilities { public static JsonObject jsonObject = new JsonObject(); - private static boolean isWriting = false; + private static final List queue = new ArrayList<>(); static { init(); @@ -35,27 +39,37 @@ public class PersistentDataUtilities { } writer = new FileWriter(file, false); + + Main.executor.scheduleAtFixedRate(() -> { + if (queue.isEmpty()) return; + + if (queue.size() > 50) queue.clear(); + + write(queue.get(0)); + + queue.remove(0); + }, 0, 50, TimeUnit.MILLISECONDS); + + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + while (true) { + if (queue.isEmpty()) break; + } + })); } catch (IOException e) { e.printStackTrace(); } } - public static synchronized void write () { + private static void write (String object) { try { - if (isWriting) return; - - isWriting = true; - 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(jsonObject.toString()); + writer.write(object); writer.flush(); - - isWriting = false; } catch (IOException | ConcurrentModificationException e) { e.printStackTrace(); } @@ -65,34 +79,34 @@ public class PersistentDataUtilities { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.add(property, value); - write(); + queue.add(jsonObject.toString()); } public static void put (String property, String value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); - write(); + queue.add(jsonObject.toString()); } public static void put (String property, boolean value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); - write(); + queue.add(jsonObject.toString()); } public static void put (String property, int value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); - write(); + queue.add(jsonObject.toString()); } public static void put (String property, char value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); - write(); + queue.add(jsonObject.toString()); } }