diff options
Diffstat (limited to 'src/main/java/com')
3 files changed, 40 insertions, 18 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java index 2b2865e..e0cb622 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java +++ b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java @@ -7,6 +7,7 @@ import com.keuin.kbackupfabric.metadata.BackupMetadata; import com.keuin.kbackupfabric.metadata.MetadataHolder; import com.keuin.kbackupfabric.notification.DistinctNotifiable; import com.keuin.kbackupfabric.notification.NotificationManager; +import com.keuin.kbackupfabric.singleton.MinecraftServerInstance; import com.keuin.kbackupfabric.ui.KBCommands; import com.keuin.kbackupfabric.util.DateUtil; import com.keuin.kbackupfabric.util.PrintUtil; @@ -41,6 +42,9 @@ public final class KBPluginEvents implements ModInitializer, ServerStartCallback @Override public void onStartServer(MinecraftServer server) { + // Set singleton holder, so in other place we can use MinecraftServerInstance to get a reference + MinecraftServerInstance.setInstance(server); + // Buggy: this does not work if (!(server instanceof MinecraftDedicatedServer)) throw new RuntimeException("KBackup is a server-side-only plugin. Please do not use it in client-side."); diff --git a/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java index 32d626e..d143118 100644 --- a/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java +++ b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java @@ -8,27 +8,28 @@ import java.util.logging.Logger; public class AutoBackupDispatcher { private Timer timer = null; - private final Logger logger = Logger.getLogger(AutoBackupDispatcher.class.getName()); + private final Logger logger = Logger.getLogger("KBackup-AutoBackup"); private boolean skipIfNoPlayerLoggedIn; private final PlayerActivityTracker playerActivityTracker; public AutoBackupDispatcher(int intervalSeconds, boolean skipIfNoPlayerLoggedIn, PlayerActivityTracker playerActivityTracker) { + if (intervalSeconds < 1) + throw new IllegalArgumentException("interval is too small"); this.skipIfNoPlayerLoggedIn = skipIfNoPlayerLoggedIn; this.playerActivityTracker = playerActivityTracker; - if (intervalSeconds > 0) - setInterval(intervalSeconds); - } - - public synchronized void setInterval(int intervalSeconds) { + // start timer Optional.ofNullable(timer).ifPresent(Timer::cancel); - Timer newTimer = new Timer("AutoBackupTimer"); - newTimer.schedule(new TimerTask() { + Timer timer = new Timer("AutoBackupTimer"); + timer.schedule(new TimerTask() { @Override public void run() { - toggleBackup(); + if (playerActivityTracker.getCheckpoint() || !skipIfNoPlayerLoggedIn) { + logger.info("Making regular backup..."); + // TODO: perform a backup + } } }, 0L, intervalSeconds * 1000L); - timer = newTimer; + this.timer = timer; } public void setSkipIfNoPlayerLoggedIn(boolean skipIfNoPlayerLoggedIn) { @@ -39,12 +40,4 @@ public class AutoBackupDispatcher { timer.cancel(); timer = null; } - - private void toggleBackup() { - if (playerActivityTracker.getCheckpoint() || !skipIfNoPlayerLoggedIn) { - logger.info("Interval backup event is triggered."); - // TODO: perform a backup - } - } - } diff --git a/src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java b/src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java new file mode 100644 index 0000000..dc1ff52 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java @@ -0,0 +1,25 @@ +package com.keuin.kbackupfabric.singleton; + +import net.minecraft.server.MinecraftServer; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Holds {@link MinecraftServer} instance. + */ +public class MinecraftServerInstance { + private static AtomicReference<MinecraftServer> instance = new AtomicReference<>(); + + public static @NotNull MinecraftServer getInstance() { + return Objects.requireNonNull(instance.get()); + } + + public static void setInstance(@NotNull MinecraftServer instance) { + Objects.requireNonNull(instance); + if (!MinecraftServerInstance.instance.compareAndSet(null, instance)) { + throw new IllegalStateException("instance already exists"); + } + } +} |