Skip to content

Commit c42a61e

Browse files
Merge pull request #18 from SLNE-Development/fix/fix-dimension-change-despawns-npc
feat: add WorldChangeListener to handle NPC respawning on world change
2 parents a8e6946 + 09a4d8c commit c42a61e

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

surf-npc-bukkit/src/main/kotlin/dev/slne/surf/npc/bukkit/BukkitMain.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import dev.slne.surf.npc.api.npc.property.NpcPropertyType
77
import dev.slne.surf.npc.bukkit.command.NpcCommand
88
import dev.slne.surf.npc.bukkit.listener.ConnectionListener
99
import dev.slne.surf.npc.bukkit.listener.NpcListener
10+
import dev.slne.surf.npc.bukkit.listener.WorldChangeListener
1011
import dev.slne.surf.npc.bukkit.npc.property.impl.*
1112
import dev.slne.surf.npc.core.property.propertyTypeRegistry
1213
import dev.slne.surf.npc.core.service.storageService
13-
import org.bukkit.Bukkit
14+
import dev.slne.surf.surfapi.bukkit.api.event.register
1415
import org.bukkit.plugin.java.JavaPlugin
1516

1617
class BukkitMain : SuspendingJavaPlugin() {
@@ -19,7 +20,8 @@ class BukkitMain : SuspendingJavaPlugin() {
1920
NpcListener(),
2021
PacketListenerPriority.NORMAL
2122
)
22-
Bukkit.getPluginManager().registerEvents(ConnectionListener(), this)
23+
ConnectionListener().register()
24+
WorldChangeListener().register()
2325

2426
propertyTypeRegistry.register(BooleanPropertyType(NpcPropertyType.Types.BOOLEAN))
2527
propertyTypeRegistry.register(ComponentPropertyType(NpcPropertyType.Types.COMPONENT))

surf-npc-bukkit/src/main/kotlin/dev/slne/surf/npc/bukkit/controller/BukkitNpcController.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,6 @@ class BukkitNpcController : NpcController, Services.Fallback {
221221
return NpcRespawnResult.Failure(NpcRespawnFailureReason.NOT_EXIST)
222222
}
223223

224-
if (npc.viewers.contains(uuid)) {
225-
return NpcRespawnResult.Failure(NpcRespawnFailureReason.ALREADY_SPAWNED)
226-
}
227-
228224
npc.despawn(uuid)
229225
npc.spawn(uuid)
230226

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dev.slne.surf.npc.bukkit.listener
2+
3+
import dev.slne.surf.npc.api.npc.location.NpcLocation
4+
import dev.slne.surf.npc.api.npc.property.NpcProperty
5+
import dev.slne.surf.npc.core.controller.npcController
6+
import org.bukkit.event.EventHandler
7+
import org.bukkit.event.Listener
8+
import org.bukkit.event.player.PlayerChangedWorldEvent
9+
10+
class WorldChangeListener : Listener {
11+
@EventHandler
12+
fun onWorldChanged(event: PlayerChangedWorldEvent) {
13+
val player = event.player
14+
val world = player.world
15+
16+
npcController.getNpcs().mapNotNull {
17+
val npcLocation = it.getPropertyValue(NpcProperty.Internal.LOCATION, NpcLocation::class)
18+
if (npcLocation?.world == world.name) it to npcLocation else null
19+
}.forEach { (npc, _) ->
20+
npcController.reShowNpc(npc, player.uniqueId)
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)