@@ -7,6 +7,7 @@ import org.bukkit.event.EventHandler
77import org.bukkit.event.EventPriority
88import org.bukkit.event.Listener
99import org.bukkit.event.entity.EntityDamageByEntityEvent
10+ import org.bukkit.event.entity.EntityDamageEvent
1011import org.bukkit.event.entity.EntityDeathEvent
1112import org.bukkit.event.entity.EntitySpawnEvent
1213import org.bukkit.event.player.PlayerJoinEvent
@@ -30,8 +31,8 @@ class DamageListener(
3031 // <editor-fold desc="Set always on healthbars on spawn/join">
3132 @EventHandler(ignoreCancelled = true , priority = EventPriority .HIGHEST )
3233 fun onEntitySpawn (event : EntitySpawnEvent ) {
33- val entity = event.entity as ? LivingEntity
34- entity? .healthbar?.let { healthbar ->
34+ val entity = event.entity as ? LivingEntity ? : return
35+ entity.healthbar?.let { healthbar ->
3536 if (healthbar.durationTicks == null ) {
3637 healthbar(null , entity, 0.0 )
3738 }
@@ -48,14 +49,29 @@ class DamageListener(
4849 }
4950 // </editor-fold>
5051
52+ /* *
53+ * Update healthbars as needed
54+ *
55+ * Damage from other entities may create a healthbar if configured.
56+ * Other damage can only update existing healthbars.
57+ */
5158 @EventHandler(ignoreCancelled = true , priority = EventPriority .HIGHEST )
52- fun onEntityDamageByEntityEvent (event : EntityDamageByEntityEvent ) {
59+ fun onEntityDamageEvent (event : EntityDamageEvent ) {
5360 val target = event.entity as ? LivingEntity ? : return
54- val source = event.damager as ? LivingEntity
61+ when (event) {
62+ is EntityDamageByEntityEvent -> {
63+ val source = event.damager as ? LivingEntity
5564
56- // put source and target healthbars
57- healthbar(source, target, event.finalDamage)
58- source?.let { healthbar(target, it, 0.0 ) }
65+ // put source and target healthbars
66+ healthbar(source, target, event.finalDamage)
67+ source?.let { healthbar(target, it, 0.0 ) }
68+ }
69+ else -> {
70+ if (removeHealthbarTasks.contains(event.entity.uniqueId)) {
71+ healthbar(null , target, event.finalDamage)
72+ }
73+ }
74+ }
5975 }
6076
6177 @EventHandler(ignoreCancelled = true , priority = EventPriority .LOWEST )
0 commit comments