Skip to content

Commit 1b10109

Browse files
authored
Listen to all entity damage events to update healthbars (#32)
1 parent 8affaa8 commit 1b10109

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
### Added
66
- GH-22: Always on healthbars for NAME or SCOREBOARD via special-case `always` duration (@tajobe)
77

8+
### Fixed
9+
- GH-29: Update health for existing bars on all damage events (@tajobe)
10+
811
## [0.4.0] - 2025-02-17
912
### Changed
1013
- MC 1.21, Kotlin 2.1, Gradle 8.12 (@tajobe)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ player-bar:
1414
type: SCOREBOARD # healthbar type (AKA location, can be SCOREBOARD or ACTION)
1515
style: ABSOLUTE # style of healthbar (ABSOLUTE, PERCENT, or BAR)
1616
useMainScoreboard: false # use the main scoreboard (true) or a new scoreboard (false)
17-
duration: 5 # duration (in seconds) of the healthbar (for NAME or SCOREBOARD type)
17+
duration: 5 # duration (in seconds) of the healthbar, or `always` (for NAME or SCOREBOARD type)
1818

1919
mob-bar:
2020
type: NAME # healthbar type (AKA location, can be NAME or ACTION)
2121
style: BAR # style of healthbar (ABSOLUTE, PERCENT, or BAR)
2222
length: 20 # length of the bar (number of characters)
2323
char: 0x25ae # character to use for the bar
2424
showMobNames: true # if the mob's name should show alongside the healthbar (for NAME or ACTION type)
25-
duration: 5 # duration (in seconds) of the healthbar (for NAME or SCOREBOARD type)
25+
duration: 5 # duration (in seconds) of the healthbar, or `always` (for NAME or SCOREBOARD type)
2626

2727
# per world configs/overrides
2828
worlds:

src/main/kotlin/org/simplemc/simplehealthbars2/DamageListener.kt

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.bukkit.event.EventHandler
77
import org.bukkit.event.EventPriority
88
import org.bukkit.event.Listener
99
import org.bukkit.event.entity.EntityDamageByEntityEvent
10+
import org.bukkit.event.entity.EntityDamageEvent
1011
import org.bukkit.event.entity.EntityDeathEvent
1112
import org.bukkit.event.entity.EntitySpawnEvent
1213
import 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

Comments
 (0)