Add scheduled task to save regular changes for SingleBlockAPIView used in JukeAlert

This commit is contained in:
Aleksey Terzi
2022-06-04 11:34:59 -06:00
parent cf41d4d13d
commit 1d9eed9616

View File

@@ -7,14 +7,30 @@ import org.bukkit.plugin.java.JavaPlugin;
import vg.civcraft.mc.civmodcore.world.locations.global.GlobalLocationTracker;
import vg.civcraft.mc.civmodcore.world.locations.global.LocationTrackable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class SingleBlockAPIView <T extends LocationTrackable> extends APIView {
private static final long REGULAR_SAVE_INTERVAL_MILLISECONDS = 60L * 1000L;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final GlobalLocationTracker<T> tracker;
private ScheduledFuture<?> regularSaveRunnable;
SingleBlockAPIView(JavaPlugin plugin, short pluginID, GlobalLocationTracker<T> tracker) {
super(plugin, pluginID);
this.tracker = tracker;
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, tracker::initFromDB);
registerRegularSaveRunnable();
}
private void registerRegularSaveRunnable() {
this.regularSaveRunnable = scheduler.scheduleWithFixedDelay(() -> {
tracker.persist();
}, REGULAR_SAVE_INTERVAL_MILLISECONDS, REGULAR_SAVE_INTERVAL_MILLISECONDS, TimeUnit.MILLISECONDS);
}
public T get(Location loc) {
@@ -43,6 +59,9 @@ public class SingleBlockAPIView <T extends LocationTrackable> extends APIView {
@Override
public void disable() {
if (this.regularSaveRunnable != null)
this.regularSaveRunnable.cancel(false);
tracker.persist();
}