Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ 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)
style: BAR # style of healthbar (ABSOLUTE, PERCENT, or 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:
Expand Down
30 changes: 23 additions & 7 deletions src/main/kotlin/org/simplemc/simplehealthbars2/DamageListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,8 +31,8 @@ class DamageListener(
// <editor-fold desc="Set always on healthbars on spawn/join">
@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)
}
Expand All @@ -48,14 +49,29 @@ class DamageListener(
}
// </editor-fold>

/**
* 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)
Expand Down