Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to use the main scoreboard for the scoreboard healthbar #20

Merged
merged 1 commit into from
May 12, 2020
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
@@ -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

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

/**
Expand Down Expand Up @@ -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")))
)
)
Expand All @@ -63,6 +66,7 @@ class SimpleHealthbars2 : JavaPlugin() {

override fun onDisable() {
listener.close()
Bukkit.getScoreboardManager()?.mainScoreboard?.getObjective(OBJECTIVE_NAME)?.unregister()
logger.info("${description.name} disabled.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -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)
Expand Down