@@ -7,12 +7,16 @@ import com.github.retrooper.packetevents.protocol.player.GameMode
77import com.github.retrooper.packetevents.protocol.player.UserProfile
88import com.github.retrooper.packetevents.util.Vector3d
99import com.github.retrooper.packetevents.wrapper.play.server.*
10+ import dev.slne.surf.npc.api.npc.Npc
11+ import dev.slne.surf.npc.api.npc.NpcPose
1012import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
13+ import dev.slne.surf.npc.bukkit.util.toEntityPose
1114import io.github.retrooper.packetevents.util.SpigotConversionUtil
1215import net.kyori.adventure.text.Component
1316import net.kyori.adventure.text.format.NamedTextColor
14- import org.bukkit.Location
1517import java.util.*
18+ import com.github.retrooper.packetevents.protocol.world.Location as PacketLocation
19+ import org.bukkit.Location as BukkitLocation
1620
1721fun createPlayerInfoPacket (profile : UserProfile , displayName : Component , listed : Boolean = false) =
1822 WrapperPlayServerPlayerInfoUpdate (
@@ -27,25 +31,76 @@ fun createPlayerInfoPacket(profile: UserProfile, displayName: Component, listed:
2731 )
2832 )
2933
30- fun createEntityMetadataPacket (npcEntityId : Int , skinParts : Byte = 0x7F.toByte()) = WrapperPlayServerEntityMetadata (
34+ fun createEntityMetadataPacket (npcEntityId : Int , skinParts : Byte = 0x7F.toByte()) =
35+ WrapperPlayServerEntityMetadata (
36+ npcEntityId,
37+ listOf (
38+ EntityData (17 , EntityDataTypes .BYTE , skinParts),
39+ EntityData (0 , EntityDataTypes .BYTE , 0x02 .toByte()),
40+ )
41+ )
42+
43+ fun createSpawnSittingArmorStandPacket (npc : Npc , npcLocation : BukkitLocation ) =
44+ WrapperPlayServerSpawnEntity (
45+ npc.npcSittingId,
46+ npc.npcSittingUuid,
47+ EntityTypes .ARMOR_STAND ,
48+ SpigotConversionUtil .fromBukkitLocation(npcLocation.clone().subtract(0.0 , 2.0 , 0.0 )),
49+ 0f ,
50+ 0 ,
51+ null
52+ )
53+
54+ fun createCorrectNameTagPacket (nameTagId : Int , npcLocation : BukkitLocation , npcPose : NpcPose ) =
55+ WrapperPlayServerEntityTeleport (
56+ nameTagId,
57+ SpigotConversionUtil .fromBukkitLocation(
58+ calculateNametagLocation(npcPose, npcLocation).add(
59+ 0.0 ,
60+ 2.0 ,
61+ 0.0
62+ )
63+ ),
64+ false
65+ )
66+
67+ fun createSittingArmorStandMetadataPacket (npc : Npc ) = WrapperPlayServerEntityMetadata (
68+ npc.npcSittingId,
69+ listOf (
70+ EntityData (0 , EntityDataTypes .BYTE , 0x20 .toByte()),
71+ )
72+ )
73+
74+ fun createMountSittingArmorStandPacket (npc : Npc ) = WrapperPlayServerSetPassengers (
75+ npc.npcSittingId,
76+ intArrayOf(npc.id)
77+ )
78+
79+ fun createDestroySittingArmorStandPacket (npc : Npc ) =
80+ WrapperPlayServerDestroyEntities (npc.npcSittingId)
81+
82+ fun createPoseChangePacket (npcEntityId : Int , pose : NpcPose ) = WrapperPlayServerEntityMetadata (
3183 npcEntityId,
3284 listOf (
33- EntityData (17 , EntityDataTypes .BYTE , skinParts),
34- EntityData (0 , EntityDataTypes .BYTE , 0x02 .toByte()),
85+ EntityData (
86+ 6 ,
87+ EntityDataTypes .ENTITY_POSE ,
88+ pose.toEntityPose()
89+ )
3590 )
3691)
3792
3893fun createPlayerSpawnPacket (
3994 entityId : Int ,
4095 uuid : UUID ,
41- location : Location ,
96+ location : BukkitLocation ,
4297 yaw : Float ,
4398 pitch : Float
4499) = WrapperPlayServerSpawnEntity (
45100 entityId,
46101 uuid,
47102 EntityTypes .PLAYER ,
48- com.github.retrooper.packetevents.protocol.world. Location (
103+ PacketLocation (
49104 Vector3d (
50105 location.x,
51106 location.y,
@@ -60,12 +115,12 @@ fun createPlayerSpawnPacket(
60115fun createNametagSpawnPacket (
61116 entityId : Int ,
62117 uuid : UUID ,
63- location : Location
118+ location : BukkitLocation
64119) = WrapperPlayServerSpawnEntity (
65120 entityId,
66121 uuid,
67122 EntityTypes .TEXT_DISPLAY ,
68- com.github.retrooper.packetevents.protocol.world. Location (
123+ PacketLocation (
69124 Vector3d (
70125 location.x,
71126 location.y + 2 ,
@@ -122,21 +177,43 @@ fun createRotationPackets(entityId: Int, yaw: Float, pitch: Float) = Pair(
122177 WrapperPlayServerEntityHeadLook (entityId, yaw)
123178)
124179
125- fun createTeleportPacket (entityId : Int , location : Location , onGround : Boolean = false) =
180+ fun createTeleportPacket (entityId : Int , location : BukkitLocation , onGround : Boolean = false) =
126181 WrapperPlayServerEntityTeleport (
127182 entityId,
128183 SpigotConversionUtil .fromBukkitLocation(location),
129184 onGround
130185 )
131186
132- fun createEntityAnimation (entityId : Int , animation : NpcAnimationType ) = WrapperPlayServerEntityAnimation (
133- entityId,
134- when (animation) {
135- NpcAnimationType .SWING_ARM_MAIN -> WrapperPlayServerEntityAnimation .EntityAnimationType .SWING_MAIN_ARM
136- NpcAnimationType .SWING_ARM_OFF -> WrapperPlayServerEntityAnimation .EntityAnimationType .SWING_OFF_HAND
137- NpcAnimationType .GET_DAMAGE -> WrapperPlayServerEntityAnimation .EntityAnimationType .HURT
138- NpcAnimationType .LEAVE_BED -> WrapperPlayServerEntityAnimation .EntityAnimationType .WAKE_UP
139- NpcAnimationType .HIT_CRITICAL -> WrapperPlayServerEntityAnimation .EntityAnimationType .CRITICAL_HIT
140- NpcAnimationType .HIT_MAGIC -> WrapperPlayServerEntityAnimation .EntityAnimationType .MAGIC_CRITICAL_HIT
187+ fun createEntityAnimation (entityId : Int , animation : NpcAnimationType ) =
188+ WrapperPlayServerEntityAnimation (
189+ entityId,
190+ when (animation) {
191+ NpcAnimationType .SWING_ARM_MAIN -> WrapperPlayServerEntityAnimation .EntityAnimationType .SWING_MAIN_ARM
192+ NpcAnimationType .SWING_ARM_OFF -> WrapperPlayServerEntityAnimation .EntityAnimationType .SWING_OFF_HAND
193+ NpcAnimationType .GET_DAMAGE -> WrapperPlayServerEntityAnimation .EntityAnimationType .HURT
194+ NpcAnimationType .LEAVE_BED -> WrapperPlayServerEntityAnimation .EntityAnimationType .WAKE_UP
195+ NpcAnimationType .HIT_CRITICAL -> WrapperPlayServerEntityAnimation .EntityAnimationType .CRITICAL_HIT
196+ NpcAnimationType .HIT_MAGIC -> WrapperPlayServerEntityAnimation .EntityAnimationType .MAGIC_CRITICAL_HIT
197+ }
198+ )
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()
141219 }
142- )
0 commit comments