From 7aee97e064412d0718c1bd0f52401868fecc4ac1 Mon Sep 17 00:00:00 2001 From: BestZige Date: Thu, 3 Jul 2025 17:28:36 +0700 Subject: [PATCH 1/2] chore: add CurrencyLoggerEvent Purpose of Log Centering in Data Analysis --- .../api/event/CurrencyLoggerEvent.java | 52 +++++++++++++++++++ .../coinsengine/currency/CurrencyLogger.java | 20 ++++--- 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/main/java/su/nightexpress/coinsengine/api/event/CurrencyLoggerEvent.java diff --git a/src/main/java/su/nightexpress/coinsengine/api/event/CurrencyLoggerEvent.java b/src/main/java/su/nightexpress/coinsengine/api/event/CurrencyLoggerEvent.java new file mode 100644 index 0000000..74c01fa --- /dev/null +++ b/src/main/java/su/nightexpress/coinsengine/api/event/CurrencyLoggerEvent.java @@ -0,0 +1,52 @@ +package su.nightexpress.coinsengine.api.event; + +import org.bukkit.Bukkit; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * @author BestZige + * Created: 7/3/2025 + */ + +public class CurrencyLoggerEvent extends Event implements Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private final String message; + + private boolean cancelled; + + public CurrencyLoggerEvent(@NotNull String message) { + super(!Bukkit.isPrimaryThread()); + this.message = message; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @NotNull + public String getMessage() { + return this.message; + } +} + diff --git a/src/main/java/su/nightexpress/coinsengine/currency/CurrencyLogger.java b/src/main/java/su/nightexpress/coinsengine/currency/CurrencyLogger.java index f5a77b6..f42a1dd 100644 --- a/src/main/java/su/nightexpress/coinsengine/currency/CurrencyLogger.java +++ b/src/main/java/su/nightexpress/coinsengine/currency/CurrencyLogger.java @@ -3,6 +3,7 @@ import org.jetbrains.annotations.NotNull; import su.nightexpress.coinsengine.CoinsEnginePlugin; import su.nightexpress.coinsengine.api.currency.OperationResult; +import su.nightexpress.coinsengine.api.event.CurrencyLoggerEvent; import su.nightexpress.coinsengine.config.Config; import su.nightexpress.nightcore.util.TimeUtil; @@ -16,7 +17,7 @@ public class CurrencyLogger { - //private final CoinsEnginePlugin plugin; + private final CoinsEnginePlugin plugin; private final BlockingQueue queue; private final DateTimeFormatter timeFormatter; private final BufferedWriter writer; @@ -24,7 +25,7 @@ public class CurrencyLogger { private boolean running; public CurrencyLogger(@NotNull CoinsEnginePlugin plugin) throws IOException { - //this.plugin = plugin; + this.plugin = plugin; this.queue = new LinkedBlockingQueue<>(); this.timeFormatter = DateTimeFormatter.ofPattern(Config.LOGS_DATE_FORMAT.get()); this.writer = new BufferedWriter(new FileWriter(plugin.getDataFolder() + "/" + Config.LOG_FILENAME, true)); @@ -37,8 +38,7 @@ public void shutdown() { try { this.writer.close(); - } - catch (IOException exception) { + } catch (IOException exception) { exception.printStackTrace(); } } @@ -53,13 +53,19 @@ public void write() { OperationResult result = this.queue.poll(500, TimeUnit.MILLISECONDS); if (result != null) { String date = TimeUtil.getLocalDateTimeOf(result.getTimestamp()).format(this.timeFormatter); - this.writer.append("[").append(date).append("] ").append(result.getLog()); + String message = String.format("[%s] %s", date, result.getLog()); + + plugin.getScheduler().runTask(plugin, () -> { + CurrencyLoggerEvent event = new CurrencyLoggerEvent(message); + plugin.getServer().getPluginManager().callEvent(event); + }); + + this.writer.append(message); this.writer.newLine(); this.writer.flush(); } } - } - catch (Exception exception) { + } catch (Exception exception) { exception.printStackTrace(); } } From 6c913adf4b56fadd7a89e6bc1cface081995879c Mon Sep 17 00:00:00 2001 From: BestZige Date: Thu, 3 Jul 2025 23:47:45 +0700 Subject: [PATCH 2/2] chore: fix logger async --- .../coinsengine/api/event/CurrencyLoggerEvent.java | 5 ----- .../coinsengine/currency/CurrencyLogger.java | 14 +++++++------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main/java/su/nightexpress/coinsengine/api/event/CurrencyLoggerEvent.java b/src/main/java/su/nightexpress/coinsengine/api/event/CurrencyLoggerEvent.java index 74c01fa..ff82ce3 100644 --- a/src/main/java/su/nightexpress/coinsengine/api/event/CurrencyLoggerEvent.java +++ b/src/main/java/su/nightexpress/coinsengine/api/event/CurrencyLoggerEvent.java @@ -6,11 +6,6 @@ import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; -/** - * @author BestZige - * Created: 7/3/2025 - */ - public class CurrencyLoggerEvent extends Event implements Cancellable { private static final HandlerList HANDLER_LIST = new HandlerList(); diff --git a/src/main/java/su/nightexpress/coinsengine/currency/CurrencyLogger.java b/src/main/java/su/nightexpress/coinsengine/currency/CurrencyLogger.java index f42a1dd..80417e2 100644 --- a/src/main/java/su/nightexpress/coinsengine/currency/CurrencyLogger.java +++ b/src/main/java/su/nightexpress/coinsengine/currency/CurrencyLogger.java @@ -55,14 +55,14 @@ public void write() { String date = TimeUtil.getLocalDateTimeOf(result.getTimestamp()).format(this.timeFormatter); String message = String.format("[%s] %s", date, result.getLog()); - plugin.getScheduler().runTask(plugin, () -> { - CurrencyLoggerEvent event = new CurrencyLoggerEvent(message); - plugin.getServer().getPluginManager().callEvent(event); - }); + CurrencyLoggerEvent event = new CurrencyLoggerEvent(message); + plugin.getServer().getPluginManager().callEvent(event); - this.writer.append(message); - this.writer.newLine(); - this.writer.flush(); + if (!event.isCancelled()) { + this.writer.append(message); + this.writer.newLine(); + this.writer.flush(); + } } } } catch (Exception exception) {