Skip to content

Commit e686bba

Browse files
Merge pull request #11 from SLNE-Development/feat/7-add-entity-animations
Feat/7 add entity animations
2 parents 048fb8d + 681e71f commit e686bba

File tree

12 files changed

+150
-0
lines changed

12 files changed

+150
-0
lines changed

surf-npc-api/src/main/kotlin/dev/slne/surf/npc/api/SurfNpcApi.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.slne.surf.npc.api
22

33
import dev.slne.surf.npc.api.npc.Npc
4+
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
45
import dev.slne.surf.npc.api.npc.location.NpcLocation
56
import dev.slne.surf.npc.api.npc.property.NpcProperty
67
import dev.slne.surf.npc.api.npc.property.NpcPropertyType
@@ -232,6 +233,13 @@ interface SurfNpcApi {
232233
*/
233234
fun getPropertyType(id: String): NpcPropertyType?
234235

236+
/**
237+
* Plays an animation on the specified NPC.
238+
*
239+
* @param npc The NPC on which the animation will be played.
240+
*/
241+
fun playAnimation(npc: Npc, animationType: NpcAnimationType)
242+
235243
companion object {
236244
/**
237245
* The instance of the SurfNpcApi.

surf-npc-api/src/main/kotlin/dev/slne/surf/npc/api/npc/Npc.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.slne.surf.npc.api.npc
22

3+
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
34
import dev.slne.surf.npc.api.npc.property.NpcProperty
45
import it.unimi.dsi.fastutil.objects.Object2ObjectMap
56
import it.unimi.dsi.fastutil.objects.ObjectSet
@@ -158,4 +159,12 @@ interface Npc {
158159
* @return True if the NPC has properties, false otherwise.
159160
*/
160161
fun hasProperties(): Boolean
162+
163+
164+
/**
165+
* Plays an animation on the NPC.
166+
*
167+
* @param animationType The type of animation to play.
168+
*/
169+
fun playAnimation(animationType: NpcAnimationType)
161170
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package dev.slne.surf.npc.api.npc.animation
2+
3+
enum class NpcAnimationType {
4+
SWING_ARM_MAIN,
5+
SWING_ARM_OFF,
6+
GET_DAMAGE,
7+
LEAVE_BED,
8+
HIT_CRITICAL,
9+
HIT_MAGIC
10+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.github.retrooper.packetevents.protocol.player.GameMode
77
import com.github.retrooper.packetevents.protocol.player.UserProfile
88
import com.github.retrooper.packetevents.util.Vector3d
99
import com.github.retrooper.packetevents.wrapper.play.server.*
10+
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
1011
import io.github.retrooper.packetevents.util.SpigotConversionUtil
1112
import net.kyori.adventure.text.Component
1213
import net.kyori.adventure.text.format.NamedTextColor
@@ -127,3 +128,15 @@ fun createTeleportPacket(entityId: Int, location: Location, onGround: Boolean =
127128
SpigotConversionUtil.fromBukkitLocation(location),
128129
onGround
129130
)
131+
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
141+
}
142+
)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dev.slne.surf.npc.bukkit.api
33
import com.google.auto.service.AutoService
44
import dev.slne.surf.npc.api.SurfNpcApi
55
import dev.slne.surf.npc.api.npc.Npc
6+
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
67
import dev.slne.surf.npc.api.npc.location.NpcLocation
78
import dev.slne.surf.npc.api.npc.property.NpcProperty
89
import dev.slne.surf.npc.api.npc.property.NpcPropertyType
@@ -156,4 +157,11 @@ class BukkitSurfNpcApi : SurfNpcApi, Services.Fallback {
156157
override fun getPropertyType(id: String): NpcPropertyType? {
157158
return propertyTypeRegistry.get(id)
158159
}
160+
161+
override fun playAnimation(
162+
npc: Npc,
163+
animationType: NpcAnimationType
164+
) {
165+
npcController.playAnimation(npc, animationType)
166+
}
159167
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ class NpcCommand(commandName: String) : CommandAPICommand(commandName) {
2626
subcommand(NpcReloadFromDiskCommand("loadFromDisk"))
2727
subcommand(NpcSaveToDiskCommand("saveToDisk"))
2828
subcommand(NpcRefreshCommand("refresh"))
29+
subcommand(NpcAnimateCommand("animate"))
2930
}
3031
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dev.slne.surf.npc.bukkit.command.argument
2+
3+
import dev.jorel.commandapi.CommandAPICommand
4+
import dev.jorel.commandapi.arguments.Argument
5+
import dev.jorel.commandapi.arguments.ArgumentSuggestions
6+
import dev.jorel.commandapi.arguments.CustomArgument
7+
import dev.jorel.commandapi.arguments.StringArgument
8+
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
9+
10+
class NpcAnimationTypeArgument(nodeName: String) :
11+
CustomArgument<NpcAnimationType, String>(StringArgument(nodeName), { info ->
12+
NpcAnimationType.valueOf(info.input.uppercase())
13+
}) {
14+
init {
15+
replaceSuggestions(ArgumentSuggestions.stringCollection {
16+
NpcAnimationType.entries.map { it.name.lowercase() }
17+
})
18+
}
19+
}
20+
21+
inline fun CommandAPICommand.npcAnimationTypeArgument(
22+
nodeName: String,
23+
optional: Boolean = false,
24+
block: Argument<*>.() -> Unit = {}
25+
): CommandAPICommand =
26+
withArguments(NpcAnimationTypeArgument(nodeName).setOptional(optional).apply(block))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.slne.surf.npc.bukkit.command.sub
2+
3+
import dev.jorel.commandapi.CommandAPICommand
4+
import dev.jorel.commandapi.kotlindsl.getValue
5+
import dev.jorel.commandapi.kotlindsl.playerExecutor
6+
import dev.slne.surf.npc.api.npc.Npc
7+
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
8+
import dev.slne.surf.npc.bukkit.command.argument.npcAnimationTypeArgument
9+
import dev.slne.surf.npc.bukkit.command.argument.npcArgument
10+
import dev.slne.surf.npc.bukkit.util.PermissionRegistry
11+
import dev.slne.surf.npc.core.controller.npcController
12+
import dev.slne.surf.surfapi.core.api.messages.adventure.sendText
13+
14+
class NpcAnimateCommand(commandName: String): CommandAPICommand(commandName) {
15+
init {
16+
withPermission(PermissionRegistry.COMMAND_NPC_ANIMATE)
17+
npcArgument("npc")
18+
npcAnimationTypeArgument("animationType")
19+
playerExecutor { player, args ->
20+
val npc: Npc by args
21+
val animationType: NpcAnimationType by args
22+
23+
npcController.playAnimation(npc, animationType)
24+
25+
player.sendText {
26+
appendPrefix()
27+
success("Die Animation ")
28+
variableValue(animationType.name)
29+
success(" wurde für den Npc ")
30+
variableValue(npc.uniqueName)
31+
success(" abgespielt.")
32+
}
33+
}
34+
}
35+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.google.auto.service.AutoService
66
import dev.slne.surf.npc.api.event.NpcCreateEvent
77
import dev.slne.surf.npc.api.event.NpcDeleteEvent
88
import dev.slne.surf.npc.api.npc.Npc
9+
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
910
import dev.slne.surf.npc.api.npc.location.NpcLocation
1011
import dev.slne.surf.npc.api.npc.property.NpcProperty
1112
import dev.slne.surf.npc.api.npc.property.NpcPropertyType
@@ -356,4 +357,11 @@ class BukkitNpcController : NpcController, Services.Fallback {
356357
npc.removeProperty(key)
357358
return true
358359
}
360+
361+
override fun playAnimation(
362+
npc: Npc,
363+
animationType: NpcAnimationType
364+
) {
365+
npc.playAnimation(animationType)
366+
}
359367
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.github.shynixn.mccoroutine.folia.launch
99
import dev.slne.surf.npc.api.event.NpcDespawnEvent
1010
import dev.slne.surf.npc.api.event.NpcSpawnEvent
1111
import dev.slne.surf.npc.api.npc.Npc
12+
import dev.slne.surf.npc.api.npc.animation.NpcAnimationType
1213
import dev.slne.surf.npc.api.npc.location.NpcLocation
1314
import dev.slne.surf.npc.api.npc.property.NpcProperty
1415
import dev.slne.surf.npc.api.npc.property.NpcPropertyType
@@ -291,6 +292,27 @@ class BukkitNpc(
291292
return properties.isNotEmpty()
292293
}
293294

295+
override fun playAnimation(animationType: NpcAnimationType) {
296+
val packetEvents = PacketEvents.getAPI()
297+
val playerManager = packetEvents.playerManager
298+
299+
val global =
300+
this.getPropertyValue(NpcProperty.Internal.VISIBILITY_GLOBAL, Boolean::class) ?: false
301+
302+
if (global) {
303+
forEachPlayer {
304+
playerManager.getUser(it).sendPacket(createEntityAnimation(id, animationType))
305+
}
306+
} else {
307+
for (viewer in viewers) {
308+
val player = Bukkit.getPlayer(viewer) ?: continue
309+
val user = playerManager.getUser(player)
310+
311+
user.sendPacket(createEntityAnimation(id, animationType))
312+
}
313+
}
314+
}
315+
294316
override fun <T : Any> getPropertyValue(key: String, clazz: KClass<T>): T? {
295317
val propertyValue = this.getProperty(key)?.value ?: return null
296318

0 commit comments

Comments
 (0)