@@ -21,10 +21,13 @@ import dev.slne.surf.surfapi.core.api.font.toSmallCaps
2121import dev.slne.surf.surfapi.core.api.messages.adventure.clickOpensUrl
2222import dev.slne.surf.surfapi.core.api.messages.adventure.playSound
2323import dev.slne.surf.surfapi.core.api.messages.adventure.sendText
24+ import io.papermc.paper.threadedregions.scheduler.ScheduledTask
2425import net.kyori.adventure.text.format.TextDecoration
26+ import org.bukkit.Bukkit
2527import org.bukkit.Sound
2628import org.bukkit.entity.EntityType
2729import org.bukkit.entity.Player
30+ import java.util.concurrent.TimeUnit
2831
2932object SurfNpcHook {
3033 lateinit var survivalNpc: Npc
@@ -35,6 +38,8 @@ object SurfNpcHook {
3538 lateinit var spawnEventNpc: Npc
3639 lateinit var shopNpc: Npc
3740
41+ var eventServerDisplayName: String = " Event"
42+
3843 fun initialize () {
3944 createSurvivalNpc()
4045 createEventNpc()
@@ -89,13 +94,6 @@ object SurfNpcHook {
8994 }
9095 }
9196
92- private val eventServerDisplayName by lazy {
93- val server = surfCoreApi.getServerByName(lobbyConfig.eventServerName)
94- ? : error(" Event server with name ${lobbyConfig.eventServerName} not found" )
95-
96- server.displayName
97- }
98-
9997 private fun createEventNpc () {
10098 eventNpc = npc {
10199 displayName = {
@@ -278,4 +276,25 @@ object SurfNpcHook {
278276 }
279277 } ? : error(" Event server with name ${lobbyConfig.eventServerName} not found" )
280278 }
279+
280+ private lateinit var syncTask: ScheduledTask
281+
282+ fun startSyncTask () {
283+ syncTask = Bukkit .getAsyncScheduler().runAtFixedRate(plugin, {
284+ val server =
285+ surfCoreApi.getServerByName(lobbyConfig.eventServerName) ? : return @runAtFixedRate
286+
287+ if (eventServerDisplayName != server.displayName) {
288+ eventServerDisplayName = server.displayName
289+
290+ eventNpc.refresh()
291+ }
292+ }, 0L , 30L , TimeUnit .SECONDS )
293+ }
294+
295+ fun stopSyncTask () {
296+ if (::syncTask.isInitialized) {
297+ syncTask.cancel()
298+ }
299+ }
281300}
0 commit comments