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..ff82ce3 --- /dev/null +++ b/src/main/java/su/nightexpress/coinsengine/api/event/CurrencyLoggerEvent.java @@ -0,0 +1,47 @@ +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; + +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..80417e2 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()); - this.writer.newLine(); - this.writer.flush(); + String message = String.format("[%s] %s", date, result.getLog()); + + CurrencyLoggerEvent event = new CurrencyLoggerEvent(message); + plugin.getServer().getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + this.writer.append(message); + this.writer.newLine(); + this.writer.flush(); + } } } - } - catch (Exception exception) { + } catch (Exception exception) { exception.printStackTrace(); } }