From bfaae59d51f32a1d03c04239ef230f55f6cdf3d3 Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Wed, 10 Nov 2021 19:22:06 +0100 Subject: [PATCH] Implement auto-saving of portal data --- build.gradle.kts | 2 +- src/main/kotlin/PortalsPlugin.kt | 28 ++++++++++++++++++++++++++++ src/main/resources/config.yml | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index f1b5cac..daaffae 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "dev.w1zzrd" -version = "1.0.1" +version = "1.0.2" repositories { mavenCentral() diff --git a/src/main/kotlin/PortalsPlugin.kt b/src/main/kotlin/PortalsPlugin.kt index 82feb91..a47156c 100644 --- a/src/main/kotlin/PortalsPlugin.kt +++ b/src/main/kotlin/PortalsPlugin.kt @@ -1,11 +1,18 @@ import kr.entree.spigradle.annotations.SpigotPlugin +import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin +import org.bukkit.scheduler.BukkitTask import java.io.File +private const val PATH_AUTOSAVE = "autoSaveMinutesInterval" +private const val AUTOSAVE_DEFAULT = 240L + + @SpigotPlugin class PortalsPlugin: JavaPlugin() { private val data = YamlFile.loadFile(File(dataFolder, "data.yml")) private val portalManager = PortalManager(data) { config } + private var autoSaveTask: BukkitTask? = null override fun onEnable() { super.onEnable() @@ -45,6 +52,27 @@ class PortalsPlugin: JavaPlugin() { portalManager.reload() } + private fun startAutoSaver() { + val task = autoSaveTask + if (task != null) { + Bukkit.getScheduler().cancelTask(task.taskId) + } + + val interval = config.getLong(PATH_AUTOSAVE, AUTOSAVE_DEFAULT) + + autoSaveTask = Bukkit.getScheduler().runTaskTimer( + this, + Runnable { + Bukkit.getLogger().info("Triggered auto-save") + portalManager.save() + data.save() + Bukkit.getLogger().info("Auto-save complete") + }, + interval, + interval + ) + } + override fun onDisable() { super.onDisable() diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index cd3ad25..5df3c20 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,2 +1,3 @@ chunkLoadDestination: true +autoSaveMinutesInterval: 240 playerTeleportCooldownTicks: 100 \ No newline at end of file