diff --git a/gradle.properties b/gradle.properties index ee09553..97e7b74 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ kotlin.code.style=official kotlin.stdlib.default.dependency=false org.gradle.parallel=true -version=1.21.11-3.0.7-SNAPSHOT \ No newline at end of file +version=1.21.11-3.0.8-SNAPSHOT \ No newline at end of file diff --git a/src/main/kotlin/dev/slne/surf/lobby/PaperMain.kt b/src/main/kotlin/dev/slne/surf/lobby/PaperMain.kt index 6e7b60a..8a82e36 100644 --- a/src/main/kotlin/dev/slne/surf/lobby/PaperMain.kt +++ b/src/main/kotlin/dev/slne/surf/lobby/PaperMain.kt @@ -23,6 +23,7 @@ class PaperMain : SuspendingJavaPlugin() { override fun onEnable() { if (surfNpcHook) { SurfNpcHook.initialize() + SurfNpcHook.startSyncTask() } if (surfHologramHook) { @@ -50,6 +51,10 @@ class PaperMain : SuspendingJavaPlugin() { } override fun onDisable() { + if (surfNpcHook) { + SurfNpcHook.stopSyncTask() + } + redisApi.disconnect() } diff --git a/src/main/kotlin/dev/slne/surf/lobby/hook/npc/SurfNpcHook.kt b/src/main/kotlin/dev/slne/surf/lobby/hook/npc/SurfNpcHook.kt index 212e4bc..2069705 100644 --- a/src/main/kotlin/dev/slne/surf/lobby/hook/npc/SurfNpcHook.kt +++ b/src/main/kotlin/dev/slne/surf/lobby/hook/npc/SurfNpcHook.kt @@ -22,10 +22,13 @@ import dev.slne.surf.surfapi.core.api.font.toSmallCaps import dev.slne.surf.surfapi.core.api.messages.adventure.clickOpensUrl import dev.slne.surf.surfapi.core.api.messages.adventure.playSound import dev.slne.surf.surfapi.core.api.messages.adventure.sendText +import io.papermc.paper.threadedregions.scheduler.ScheduledTask import net.kyori.adventure.text.format.TextDecoration +import org.bukkit.Bukkit import org.bukkit.Sound import org.bukkit.entity.EntityType import org.bukkit.entity.Player +import java.util.concurrent.TimeUnit object SurfNpcHook { lateinit var survivalNpc: Npc @@ -36,6 +39,8 @@ object SurfNpcHook { lateinit var spawnEventNpc: Npc lateinit var shopNpc: Npc + var eventServerDisplayName: String = "Event" + fun initialize() { createSurvivalNpc() createEventNpc() @@ -90,13 +95,6 @@ object SurfNpcHook { } } - private val eventServerDisplayName by lazy { - val server = surfCoreApi.getServerByName(lobbyConfig.eventServerName) - ?: error("Event server with name ${lobbyConfig.eventServerName} not found") - - server.displayName - } - private fun createEventNpc() { eventNpc = npc { displayName = { @@ -284,4 +282,25 @@ object SurfNpcHook { } } ?: error("Event server with name ${lobbyConfig.eventServerName} not found") } + + private lateinit var syncTask: ScheduledTask + + fun startSyncTask() { + syncTask = Bukkit.getAsyncScheduler().runAtFixedRate(plugin, { + val server = + surfCoreApi.getServerByName(lobbyConfig.eventServerName) ?: return@runAtFixedRate + + if (eventServerDisplayName != server.displayName) { + eventServerDisplayName = server.displayName + + eventNpc.refresh() + } + }, 0L, 30L, TimeUnit.SECONDS) + } + + fun stopSyncTask() { + if (::syncTask.isInitialized) { + syncTask.cancel() + } + } } \ No newline at end of file