From 46bae7ce05d9bfca2e43010bb73fd42b3765400b Mon Sep 17 00:00:00 2001 From: Taylor Becker Date: Sat, 10 May 2025 17:36:21 -0400 Subject: [PATCH] Listen to all entity damage events to update healthbars --- CHANGELOG.md | 3 ++ README.md | 4 +-- .../simplehealthbars2/DamageListener.kt | 30 ++++++++++++++----- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3079b4..283b293 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ### Added - GH-22: Always on healthbars for NAME or SCOREBOARD via special-case `always` duration (@tajobe) +### Fixed +- GH-29: Update health for existing bars on all damage events (@tajobe) + ## [0.4.0] - 2025-02-17 ### Changed - MC 1.21, Kotlin 2.1, Gradle 8.12 (@tajobe) diff --git a/README.md b/README.md index c255e34..6c62612 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ player-bar: type: SCOREBOARD # healthbar type (AKA location, can be SCOREBOARD or ACTION) style: ABSOLUTE # style of healthbar (ABSOLUTE, PERCENT, or BAR) useMainScoreboard: false # use the main scoreboard (true) or a new scoreboard (false) - duration: 5 # duration (in seconds) of the healthbar (for NAME or SCOREBOARD type) + duration: 5 # duration (in seconds) of the healthbar, or `always` (for NAME or SCOREBOARD type) mob-bar: type: NAME # healthbar type (AKA location, can be NAME or ACTION) @@ -22,7 +22,7 @@ mob-bar: length: 20 # length of the bar (number of characters) char: 0x25ae # character to use for the bar showMobNames: true # if the mob's name should show alongside the healthbar (for NAME or ACTION type) - duration: 5 # duration (in seconds) of the healthbar (for NAME or SCOREBOARD type) + duration: 5 # duration (in seconds) of the healthbar, or `always` (for NAME or SCOREBOARD type) # per world configs/overrides worlds: diff --git a/src/main/kotlin/org/simplemc/simplehealthbars2/DamageListener.kt b/src/main/kotlin/org/simplemc/simplehealthbars2/DamageListener.kt index 2822bab..48c1ece 100644 --- a/src/main/kotlin/org/simplemc/simplehealthbars2/DamageListener.kt +++ b/src/main/kotlin/org/simplemc/simplehealthbars2/DamageListener.kt @@ -7,6 +7,7 @@ import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.Listener import org.bukkit.event.entity.EntityDamageByEntityEvent +import org.bukkit.event.entity.EntityDamageEvent import org.bukkit.event.entity.EntityDeathEvent import org.bukkit.event.entity.EntitySpawnEvent import org.bukkit.event.player.PlayerJoinEvent @@ -30,8 +31,8 @@ class DamageListener( // @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) fun onEntitySpawn(event: EntitySpawnEvent) { - val entity = event.entity as? LivingEntity - entity?.healthbar?.let { healthbar -> + val entity = event.entity as? LivingEntity ?: return + entity.healthbar?.let { healthbar -> if (healthbar.durationTicks == null) { healthbar(null, entity, 0.0) } @@ -48,14 +49,29 @@ class DamageListener( } // + /** + * Update healthbars as needed + * + * Damage from other entities may create a healthbar if configured. + * Other damage can only update existing healthbars. + */ @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) - fun onEntityDamageByEntityEvent(event: EntityDamageByEntityEvent) { + fun onEntityDamageEvent(event: EntityDamageEvent) { val target = event.entity as? LivingEntity ?: return - val source = event.damager as? LivingEntity + when (event) { + is EntityDamageByEntityEvent -> { + val source = event.damager as? LivingEntity - // put source and target healthbars - healthbar(source, target, event.finalDamage) - source?.let { healthbar(target, it, 0.0) } + // put source and target healthbars + healthbar(source, target, event.finalDamage) + source?.let { healthbar(target, it, 0.0) } + } + else -> { + if (removeHealthbarTasks.contains(event.entity.uniqueId)) { + healthbar(null, target, event.finalDamage) + } + } + } } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)