From aad94ff3d6eeb8aa3775d1b1d7512439e1f361e4 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 30 Jun 2023 18:45:54 +0700 Subject: [PATCH] IMPROve the persistent data --- .../util/PersistentDataUtilities.java | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 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 894a290..7b13963 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,12 +3,8 @@ 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.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; public class PersistentDataUtilities { public static File file = new File("persistent.json"); @@ -17,10 +13,6 @@ public class PersistentDataUtilities { public static JsonObject jsonObject = new JsonObject(); - private static final ScheduledExecutorService executor = Main.executor; - - private static ScheduledFuture future = null; - static { init(); } @@ -43,16 +35,6 @@ public class PersistentDataUtilities { } catch (IOException e) { e.printStackTrace(); } - - future = executor.scheduleAtFixedRate(PersistentDataUtilities::write, 0, 10, TimeUnit.SECONDS); - - Runtime.getRuntime().addShutdownHook( - new Thread(() -> { - future.cancel(false); - - write(); - }) - ); } // ? how do i clear the file contents without making a completely new FileWriter @@ -70,28 +52,38 @@ public class PersistentDataUtilities { } } - public static synchronized void put (String property, JsonElement value) { + public static void put (String property, JsonElement value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.add(property, value); + + write(); } - public static synchronized void put (String property, String value) { + public static void put (String property, String value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); + + write(); } - public static synchronized void put (String property, boolean value) { + public static void put (String property, boolean value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); + + write(); } - public static synchronized void put (String property, int value) { + public static void put (String property, int value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); + + write(); } - public static synchronized void put (String property, char value) { + public static void put (String property, char value) { if (jsonObject.has(property)) jsonObject.remove(property); jsonObject.addProperty(property, value); + + write(); } }