mirror of
https://github.com/CivMC/Civ.git
synced 2026-07-18 00:20:44 +00:00
Add scheduled task to save regular changes for SingleBlockAPIView used in JukeAlert
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user