@@ -8,7 +8,11 @@ import org.bukkit.event.EventPriority
88import org.bukkit.event.Listener
99import org.bukkit.event.entity.EntityDamageByEntityEvent
1010import org.bukkit.event.entity.EntityDeathEvent
11+ import org.bukkit.event.entity.EntitySpawnEvent
12+ import org.bukkit.event.player.PlayerJoinEvent
13+ import org.bukkit.event.player.PlayerQuitEvent
1114import org.bukkit.plugin.Plugin
15+ import org.simplemc.simplehealthbars2.healthbar.Healthbar
1216import org.simplemc.simplehealthbars2.healthbar.MobHealthbar
1317import org.simplemc.simplehealthbars2.healthbar.PlayerHealthbar
1418import java.util.UUID
@@ -18,13 +22,34 @@ class DamageListener(
1822 private val playerHealthbars : Map <String ?, PlayerHealthbar ?>,
1923 private val mobHealthbars : Map <String ?, MobHealthbar ?>,
2024) : Listener, AutoCloseable {
21- private data class RemoveHealthbarTask (val taskId : Int , val task : () -> Unit )
25+ private data class RemoveHealthbarTask (val taskId : Int? , val removeAction : () -> Unit )
2226
2327 private val scheduler = Bukkit .getScheduler()
2428 private val removeHealthbarTasks: MutableMap <UUID , RemoveHealthbarTask ?> = mutableMapOf ()
2529
30+ // <editor-fold desc="Set always on healthbars on spawn/join">
2631 @EventHandler(ignoreCancelled = true , priority = EventPriority .HIGHEST )
27- fun onEntityDamageEvent (event : EntityDamageByEntityEvent ) {
32+ fun onEntitySpawn (event : EntitySpawnEvent ) {
33+ val entity = event.entity as ? LivingEntity
34+ entity?.healthbar?.let { healthbar ->
35+ if (healthbar.durationTicks == null ) {
36+ healthbar(null , entity, 0.0 )
37+ }
38+ }
39+ }
40+
41+ @EventHandler(ignoreCancelled = true , priority = EventPriority .HIGHEST )
42+ fun onPlayerJoin (event : PlayerJoinEvent ) {
43+ event.player.healthbar?.let { healthbar ->
44+ if (healthbar.durationTicks == null ) {
45+ healthbar(null , event.player, 0.0 )
46+ }
47+ }
48+ }
49+ // </editor-fold>
50+
51+ @EventHandler(ignoreCancelled = true , priority = EventPriority .HIGHEST )
52+ fun onEntityDamageByEntityEvent (event : EntityDamageByEntityEvent ) {
2853 val target = event.entity as ? LivingEntity ? : return
2954 val source = event.damager as ? LivingEntity
3055
@@ -33,42 +58,46 @@ class DamageListener(
3358 source?.let { healthbar(target, it, 0.0 ) }
3459 }
3560
61+ @EventHandler(ignoreCancelled = true , priority = EventPriority .LOWEST )
62+ fun onPlayerQuit (event : PlayerQuitEvent ) = clearEntityHealthbar(event.player)
63+
3664 /* *
3765 * Remove the healthbar from dying entities immediately
3866 */
3967 @EventHandler(ignoreCancelled = true , priority = EventPriority .HIGHEST )
40- fun onEntityDeathEvent (event : EntityDeathEvent ) {
41- removeHealthbarTasks[event.entity.uniqueId]?.let {
42- scheduler.cancelTask(it.taskId)
43- it.task()
44- }
45- }
68+ fun onEntityDeathEvent (event : EntityDeathEvent ) = clearEntityHealthbar(event.entity)
4669
4770 private fun healthbar (source : LivingEntity ? , target : LivingEntity , damage : Double ) {
4871 // cancel scheduled healthbar removal and run it now to prepare for new (updated) healthbar
49- removeHealthbarTasks[target.uniqueId]?.let {
50- scheduler.cancelTask(it.taskId)
51- it.task()
72+ clearEntityHealthbar(target)
73+
74+ // update healthbar and schedule its removal task if necessary
75+ val healthbar = target.healthbar
76+ healthbar?.updateHealth(source, target, damage)?.let { removeAction ->
77+ val taskId = healthbar.durationTicks?.let { ticks ->
78+ scheduler.scheduleSyncDelayedTask(plugin, removeAction, ticks)
79+ }
80+ removeHealthbarTasks[target.uniqueId] = RemoveHealthbarTask (taskId, removeAction)
5281 }
82+ }
5383
54- val healthbar = when (target) {
55- is Player -> playerHealthbars[target.world.name] ? : playerHealthbars[null ]
56- else -> mobHealthbars[target.world.name] ? : mobHealthbars[null ]
84+ private val LivingEntity .healthbar: Healthbar ?
85+ get(): Healthbar ? = when (this ) {
86+ is Player -> playerHealthbars[world.name] ? : playerHealthbars[null ]
87+ else -> mobHealthbars[world.name] ? : mobHealthbars[null ]
5788 }
5889
59- // update healthbar and schedule its removal task if available
60- healthbar?.updateHealth(source, target, damage )?.let {
61- val taskId = scheduler.scheduleSyncDelayedTask(plugin, it, healthbar.durationTicks)
62- removeHealthbarTasks[target.uniqueId] = RemoveHealthbarTask (taskId, it )
90+ private fun clearEntityHealthbar ( entity : LivingEntity ) {
91+ removeHealthbarTasks.remove(entity.uniqueId )?.let {
92+ it.taskId?. let { taskId -> scheduler.cancelTask(taskId) }
93+ it.removeAction( )
6394 }
6495 }
6596
6697 /* *
6798 * Remember to remove all healthbars on close
6899 */
69100 override fun close () {
70- removeHealthbarTasks
71- .mapNotNull { (_, removeTask) -> removeTask?.task }
72- .forEach { it() }
101+ removeHealthbarTasks.forEach { (_, removeTask) -> removeTask?.removeAction() }
73102 }
74103}
0 commit comments