Skip to content

Commit 0c64f7d

Browse files
feat: refine nametag position adjustment based on NPC pose for improved accuracy during state changes
1 parent 477f5b6 commit 0c64f7d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,16 @@ fun createSpawnSittingArmorStandPacket(npc: Npc, npcLocation: BukkitLocation) =
5151
null
5252
)
5353

54-
fun createMoveNametagDownPacket(npc: Npc, npcLocation: BukkitLocation) =
55-
WrapperPlayServerEntityTeleport(
56-
npc.nameTagId,
57-
SpigotConversionUtil.fromBukkitLocation(npcLocation.clone().subtract(0.0, 1.0, 0.0)),
58-
false
59-
)
60-
61-
fun createCorrectNameTagPacket(nameTagId: Int, npcLocation: BukkitLocation) =
54+
fun createCorrectNameTagPacket(nameTagId: Int, npcLocation: BukkitLocation, npcPose: NpcPose) =
6255
WrapperPlayServerEntityTeleport(
6356
nameTagId,
64-
SpigotConversionUtil.fromBukkitLocation(npcLocation),
57+
SpigotConversionUtil.fromBukkitLocation(
58+
calculateNametagLocation(npcPose, npcLocation).add(
59+
0.0,
60+
2.0,
61+
0.0
62+
)
63+
),
6564
false
6665
)
6766

@@ -197,3 +196,24 @@ fun createEntityAnimation(entityId: Int, animation: NpcAnimationType) =
197196
NpcAnimationType.HIT_MAGIC -> WrapperPlayServerEntityAnimation.EntityAnimationType.MAGIC_CRITICAL_HIT
198197
}
199198
)
199+
200+
201+
private fun calculateNametagLocation(
202+
npcPose: NpcPose,
203+
npcLocation: BukkitLocation
204+
): BukkitLocation =
205+
when (npcPose) {
206+
NpcPose.SNEAKING -> {
207+
npcLocation.clone().subtract(0.0, 0.2, 0.0)
208+
}
209+
210+
NpcPose.SITTING -> {
211+
npcLocation.clone().subtract(0.0, 0.62, 0.0)
212+
}
213+
214+
NpcPose.SWIMMING, NpcPose.FALL_FLYING, NpcPose.SLEEPING -> {
215+
npcLocation.clone().subtract(0.0, 1.62, 0.0)
216+
}
217+
218+
else -> npcLocation.clone()
219+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,16 +320,15 @@ class BukkitNpc(
320320

321321
if (pose == NpcPose.SITTING) {
322322
user.sendPacket(createSpawnSittingArmorStandPacket(this, location.toLocation()))
323-
user.sendPacket(createMoveNametagDownPacket(this, location.toLocation()))
324323
user.sendPacket(createSittingArmorStandMetadataPacket(this))
325324
user.sendPacket(createMountSittingArmorStandPacket(this))
326325
} else {
327326
user.sendPacket(createDestroySittingArmorStandPacket(this))
328-
user.sendPacket(createCorrectNameTagPacket(nameTagId, location.toLocation()))
329327
refresh()
330328
}
331329

332330
user.sendPacket(createPoseChangePacket(id, pose))
331+
user.sendPacket(createCorrectNameTagPacket(nameTagId, location.toLocation(), pose))
333332
}
334333
}
335334

0 commit comments

Comments
 (0)