From bf2a2ce41fd416fba79cef8bf6d4391f3e6c7bed Mon Sep 17 00:00:00 2001 From: Taylor Becker Date: Sun, 10 May 2020 17:17:12 -0400 Subject: [PATCH] Added option to use the main scoreboard for the scoreboard healthbar --- CHANGELOG.md | 3 ++ README.md | 1 + .../simplehealthbars2/SimpleHealthbars2.kt | 4 +++ .../healthbar/ScoreboardHealthbar.kt | 28 +++++++++++++++---- src/main/resources/config.yml | 1 + 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abf101b..10bd39a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog ## [Unreleased] +### Added +- `useMainScoreboard` config option on `SCOREBOARD` healthbars to configure if it should use the main scoreboard (`true`) or a new one (`false`) (default: `false`) + ### Changed - Updated README for clarity around types and styles valid for a specific configuration diff --git a/README.md b/README.md index 693748a..b96b275 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Simple, easy-to-use healthbar plugin with optional player and mob healthbars 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) mob-bar: type: NAME # healthbar type (AKA location, can be SCOREBOARD, NAME, or ACTION) diff --git a/src/main/kotlin/org/simplemc/simplehealthbars2/SimpleHealthbars2.kt b/src/main/kotlin/org/simplemc/simplehealthbars2/SimpleHealthbars2.kt index bff5f32..bb23166 100644 --- a/src/main/kotlin/org/simplemc/simplehealthbars2/SimpleHealthbars2.kt +++ b/src/main/kotlin/org/simplemc/simplehealthbars2/SimpleHealthbars2.kt @@ -1,5 +1,6 @@ package org.simplemc.simplehealthbars2 +import org.bukkit.Bukkit import org.bukkit.configuration.ConfigurationSection import org.bukkit.plugin.java.JavaPlugin import org.simplemc.simplehealthbars2.healthbar.ActionHealthbar @@ -8,6 +9,7 @@ import org.simplemc.simplehealthbars2.healthbar.MobHealthbar import org.simplemc.simplehealthbars2.healthbar.NameHealthbar import org.simplemc.simplehealthbars2.healthbar.PlayerHealthbar import org.simplemc.simplehealthbars2.healthbar.ScoreboardHealthbar +import org.simplemc.simplehealthbars2.healthbar.ScoreboardHealthbar.Companion.OBJECTIVE_NAME import org.simplemc.simplehealthbars2.healthbar.StringHealthbar /** @@ -48,6 +50,7 @@ class SimpleHealthbars2 : JavaPlugin() { Healthbar.Type.ACTION -> ActionHealthbar(loadStringBar(config)) Healthbar.Type.SCOREBOARD -> ScoreboardHealthbar( ScoreboardHealthbar.Config( + useMainScoreboard = config.getBoolean("useMainScoreboard", false), style = Healthbar.Style.valueOf(checkNotNull(config.getString("style", "ABSOLUTE"))) ) ) @@ -63,6 +66,7 @@ class SimpleHealthbars2 : JavaPlugin() { override fun onDisable() { listener.close() + Bukkit.getScoreboardManager()?.mainScoreboard?.getObjective(OBJECTIVE_NAME)?.unregister() logger.info("${description.name} disabled.") } } diff --git a/src/main/kotlin/org/simplemc/simplehealthbars2/healthbar/ScoreboardHealthbar.kt b/src/main/kotlin/org/simplemc/simplehealthbars2/healthbar/ScoreboardHealthbar.kt index 6838c2f..af0fdec 100644 --- a/src/main/kotlin/org/simplemc/simplehealthbars2/healthbar/ScoreboardHealthbar.kt +++ b/src/main/kotlin/org/simplemc/simplehealthbars2/healthbar/ScoreboardHealthbar.kt @@ -11,21 +11,39 @@ import org.simplemc.simplehealthbars2.getDamagedHealthRatio import kotlin.math.ceil class ScoreboardHealthbar(private val config: Config) : PlayerHealthbar { - data class Config(val style: Healthbar.Style = Healthbar.Style.ABSOLUTE) + + companion object { + const val OBJECTIVE_NAME = "simplehealthbar" + } + + data class Config( + val useMainScoreboard: Boolean = false, + val style: Healthbar.Style = Healthbar.Style.ABSOLUTE + ) private val objective: Objective init { val scoreboardManager = checkNotNull(Bukkit.getScoreboardManager()) + + // clean up previously added objective if it exists + scoreboardManager.mainScoreboard.getObjective(OBJECTIVE_NAME)?.unregister() + + val scoreboard = if (config.useMainScoreboard) { + scoreboardManager.mainScoreboard + } else { + scoreboardManager.newScoreboard + } + objective = when (config.style) { - Healthbar.Style.ABSOLUTE -> scoreboardManager.newScoreboard.registerNewObjective( - "healthbar", + Healthbar.Style.ABSOLUTE -> scoreboard.registerNewObjective( + OBJECTIVE_NAME, "health", "${ChatColor.RED}${0x2764.toChar()}", RenderType.HEARTS ) - Healthbar.Style.PERCENT -> scoreboardManager.newScoreboard.registerNewObjective( - "healthbar", + Healthbar.Style.PERCENT -> scoreboard.registerNewObjective( + OBJECTIVE_NAME, "dummy", "${ChatColor.RED}${0x2764.toChar()}", RenderType.INTEGER diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 8a788f7..db878b4 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,6 +1,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) mob-bar: type: NAME # healthbar type (AKA location, can be NAME or ACTION)