From 7bd4005cc350f20a1694851aa406be1e7930dd05 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 21 Jul 2023 17:06:19 +0700 Subject: [PATCH] MORE STUFF --- .../util/PersistentDataUtilities.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) 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 47dcd583..ddf7c9c4 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 @@ -26,19 +26,24 @@ public class PersistentDataUtilities { init(); future = Main.executor.scheduleAtFixedRate(() -> { - if (queue.size() == 0) return; + // TODO: thread-safe + try { + 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 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(); + final String property = entry.getKey(); + final JsonElement value = entry.getValue(); - if (jsonObject.has(property)) jsonObject.remove(property); - jsonObject.add(property, value); + if (jsonObject.has(property)) jsonObject.remove(property); + jsonObject.add(property, value); - write(jsonObject.toString()); + write(jsonObject.toString()); - queue.remove(property); + queue.remove(property); + } catch (Exception e) { + e.printStackTrace(); + } }, 0, 100, TimeUnit.MILLISECONDS); Runtime.getRuntime().addShutdownHook(new Thread() { @@ -69,7 +74,7 @@ public class PersistentDataUtilities { } } - private static synchronized void write (String string) { + private static void write (String string) { try { writer.close(); @@ -82,23 +87,23 @@ public class PersistentDataUtilities { } catch (IOException ignored) {} } - public static void put (String property, JsonElement value) { + public static synchronized void put (String property, JsonElement value) { queue.put(property, value); } - public static void put (String property, String value) { + public static synchronized void put (String property, String value) { queue.put(property, new JsonPrimitive(value)); } - public static void put (String property, boolean value) { + public static synchronized void put (String property, boolean value) { queue.put(property, new JsonPrimitive(value)); } - public static void put (String property, int value) { + public static synchronized void put (String property, int value) { queue.put(property, new JsonPrimitive(value)); } - public static void put (String property, char value) { + public static synchronized void put (String property, char value) { queue.put(property, new JsonPrimitive(value)); } }