Skip to content

Commit

Permalink
Merge pull request DevotedMC#19 from Aleksey-Terzi/fix_connections
Browse files Browse the repository at this point in the history
Improve log entries saving and fix kill log entry
  • Loading branch information
wingzero54 authored Jul 14, 2022
2 parents 2a95922 + 3e9ce17 commit e24b212
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
5 changes: 4 additions & 1 deletion paper/src/main/java/com/untamedears/jukealert/JukeAlert.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public TaskChainFactory getTaskChainFactory() {

@Override
public void onDisable() {
snitchManager.shutDown();
snitchManager.shutdown();
if (this.taskChainFactory != null) {
this.taskChainFactory.shutdown(10, TimeUnit.SECONDS);
this.taskChainFactory = null;
Expand Down Expand Up @@ -102,7 +102,10 @@ public void onEnable() {
Bukkit.shutdown();
return;
}

snitchManager = new SnitchManager(api);
snitchManager.enable();

settingsManager = new JASettingsManager();
commandManager = new JACommandManager(this);
registerJukeAlertEvents();
Expand Down
57 changes: 54 additions & 3 deletions paper/src/main/java/com/untamedears/jukealert/SnitchManager.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package com.untamedears.jukealert;

import com.untamedears.jukealert.database.JukeAlertDAO;
import com.untamedears.jukealert.model.Snitch;
import com.untamedears.jukealert.model.SnitchQTEntry;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;

import com.untamedears.jukealert.model.actions.ActionCacheState;
import com.untamedears.jukealert.model.actions.abstr.LoggablePlayerAction;
import org.bukkit.Location;
import org.bukkit.block.Block;
import vg.civcraft.mc.civmodcore.world.locations.SparseQuadTree;
Expand All @@ -17,16 +26,44 @@
import vg.civcraft.mc.namelayer.group.Group;

public class SnitchManager {
private static final long SAVE_LOGS_INTERVAL_MS = 60L * 1000L; // 1 min

private SingleBlockAPIView<Snitch> api;
private Map<UUID, SparseQuadTree<SnitchQTEntry>> quadTreesByWorld;
private record LogItem(int internalActionId, Snitch snitch, LoggablePlayerAction action){}

private final SingleBlockAPIView<Snitch> api;
private final Map<UUID, SparseQuadTree<SnitchQTEntry>> quadTreesByWorld;
private final ScheduledExecutorService scheduler;
private final ConcurrentLinkedQueue<LogItem> logs;

public SnitchManager(SingleBlockAPIView<Snitch> api) {
this.api = api;
this.quadTreesByWorld = new TreeMap<>();
this.scheduler = Executors.newScheduledThreadPool(1);
this.logs = new ConcurrentLinkedQueue<>();
}

public void enable() {
scheduler.scheduleWithFixedDelay(() -> {
saveLogsToDB();
}, SAVE_LOGS_INTERVAL_MS, SAVE_LOGS_INTERVAL_MS, TimeUnit.MILLISECONDS);
}

public void shutDown() {
public void shutdown() {
scheduler.shutdown();

try {
if (!scheduler.awaitTermination(60, TimeUnit.SECONDS))
scheduler.shutdownNow();
} catch (InterruptedException ex) {
ex.printStackTrace();
}

JukeAlert.getInstance().getLogger().info("SnitchManager scheduler and its tasks are shutdown.");

saveLogsToDB();

JukeAlert.getInstance().getLogger().info("Snitch logs are saved.");

api.disable();
}

Expand Down Expand Up @@ -105,4 +142,18 @@ public Set<Snitch> getSnitchesCovering(Location location) {
return result;
}

public void saveLog(int internalActionId, Snitch snitch, LoggablePlayerAction action) {
logs.add(new LogItem(internalActionId, snitch, action));
}

private void saveLogsToDB() {
JukeAlertDAO dao = JukeAlert.getInstance().getDAO();

LogItem logItem;
while ((logItem = logs.poll()) != null) {
final int actionID = dao.insertLog(logItem.internalActionId, logItem.snitch, logItem.action.getPersistence());
logItem.action.setID(actionID);
logItem.action.setCacheState(ActionCacheState.NORMAL);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -572,23 +572,6 @@ public int insertLog(final int actionTypeID,
return NOT_YET_INSERTED_ID;
}

/**
* Inserts a new snitch log to the database asynchronously.
*
* @param actionTypeID The internal ID of the action type.
* @param snitch The snitch to save the log to.
* @param action The action to store in the database.
*/
public void insertLogAsync(final int actionTypeID,
@Nonnull final Snitch snitch,
@Nonnull final LoggablePlayerAction action) {
Bukkit.getScheduler().runTaskAsynchronously(JukeAlert.getInstance(), () -> {
final int actionID = insertLog(actionTypeID, snitch, action.getPersistence());
action.setID(actionID);
action.setCacheState(ActionCacheState.NORMAL);
});
}

/**
* Deletes a particular log from the database.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public IClickable getGUIRepresentation() {

@Override
protected String getChatRepresentationIdentifier() {
return "Killed " + getVictim();
return "Killed " + getVictimName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public void acceptAction(final SnitchAction action) {
this.pendingActions.put(log, internalActionID);
return;
}
JukeAlert.getInstance().getDAO().insertLogAsync(internalActionID, getSnitch(), log);

JukeAlert.getInstance().getSnitchManager().saveLog(internalActionID, getSnitch(), log);
}

/** {@inheritDoc} */
Expand Down

0 comments on commit e24b212

Please sign in to comment.