Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,15 +17,15 @@

public class CurrencyLogger {

//private final CoinsEnginePlugin plugin;
private final CoinsEnginePlugin plugin;
private final BlockingQueue<OperationResult> queue;
private final DateTimeFormatter timeFormatter;
private final BufferedWriter writer;

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));
Expand All @@ -37,8 +38,7 @@ public void shutdown() {

try {
this.writer.close();
}
catch (IOException exception) {
} catch (IOException exception) {
exception.printStackTrace();
}
}
Expand All @@ -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();
}
}
Expand Down