From 1d9eed961627e1563593fa61448f2fb8fba20932 Mon Sep 17 00:00:00 2001 From: Aleksey Terzi Date: Sat, 4 Jun 2022 11:34:59 -0600 Subject: [PATCH] Add scheduled task to save regular changes for SingleBlockAPIView used in JukeAlert --- .../chunkmeta/api/SingleBlockAPIView.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/api/SingleBlockAPIView.java b/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/api/SingleBlockAPIView.java index 605cd0de3..5964e8110 100644 --- a/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/api/SingleBlockAPIView.java +++ b/plugins/civmodcore-paper/paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/api/SingleBlockAPIView.java @@ -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 extends APIView { + private static final long REGULAR_SAVE_INTERVAL_MILLISECONDS = 60L * 1000L; + + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); private final GlobalLocationTracker tracker; + private ScheduledFuture regularSaveRunnable; SingleBlockAPIView(JavaPlugin plugin, short pluginID, GlobalLocationTracker 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 extends APIView { @Override public void disable() { + if (this.regularSaveRunnable != null) + this.regularSaveRunnable.cancel(false); + tracker.persist(); }