From 76268d650d02e205751b786d3c74cef3ccad6a0f Mon Sep 17 00:00:00 2001 From: OhPointFive Date: Fri, 10 Feb 2023 04:01:05 -0500 Subject: [PATCH] Add health display Signed-off-by: OhPointFive --- core/src/main/java/tc/oc/pgm/api/Modules.java | 3 +- .../pgm/scoreboard/ScoreboardDisplayItem.java | 6 ++ .../pgm/scoreboard/ScoreboardMatchModule.java | 33 +++++------ .../oc/pgm/scoreboard/ScoreboardModule.java | 57 +++++++++++++++++++ 4 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardDisplayItem.java create mode 100644 core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardModule.java diff --git a/core/src/main/java/tc/oc/pgm/api/Modules.java b/core/src/main/java/tc/oc/pgm/api/Modules.java index f2062d9b8c..6fb8c57eef 100644 --- a/core/src/main/java/tc/oc/pgm/api/Modules.java +++ b/core/src/main/java/tc/oc/pgm/api/Modules.java @@ -103,6 +103,7 @@ import tc.oc.pgm.score.ScoreMatchModule; import tc.oc.pgm.score.ScoreModule; import tc.oc.pgm.scoreboard.ScoreboardMatchModule; +import tc.oc.pgm.scoreboard.ScoreboardModule; import tc.oc.pgm.scoreboard.SidebarMatchModule; import tc.oc.pgm.shield.ShieldMatchModule; import tc.oc.pgm.shops.ShopMatchModule; @@ -223,13 +224,13 @@ void registerAll() { // MatchModules that require other dependencies register(GoalMatchModule.class, new GoalMatchModule.Factory()); - register(ScoreboardMatchModule.class, new ScoreboardMatchModule.Factory()); register(JoinMatchModule.class, new JoinMatchModule.Factory()); register(StartMatchModule.class, new StartMatchModule.Factory()); register(SidebarMatchModule.class, new SidebarMatchModule.Factory()); register(PickerMatchModule.class, new PickerMatchModule.Factory()); // MapModules that create a MatchModule + register(ScoreboardModule.class, ScoreboardMatchModule.class, new ScoreboardModule.Factory()); register(VariablesModule.class, VariablesMatchModule.class, new VariablesModule.Factory()); register(TeamModule.class, TeamMatchModule.class, new TeamModule.Factory()); register(FreeForAllModule.class, FreeForAllMatchModule.class, new FreeForAllModule.Factory()); diff --git a/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardDisplayItem.java b/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardDisplayItem.java new file mode 100644 index 0000000000..117c54c439 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardDisplayItem.java @@ -0,0 +1,6 @@ +package tc.oc.pgm.scoreboard; + +public enum ScoreboardDisplayItem { + NONE, + HEALTH +} diff --git a/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardMatchModule.java b/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardMatchModule.java index 42a095601f..00751af8f5 100644 --- a/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardMatchModule.java +++ b/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardMatchModule.java @@ -2,9 +2,7 @@ import static tc.oc.pgm.util.Assert.assertNotNull; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -12,6 +10,8 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.scoreboard.Criterias; +import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.NameTagVisibility; import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Scoreboard; @@ -21,8 +21,6 @@ import tc.oc.pgm.api.match.Match; import tc.oc.pgm.api.match.MatchModule; import tc.oc.pgm.api.match.MatchScope; -import tc.oc.pgm.api.match.factory.MatchModuleFactory; -import tc.oc.pgm.api.module.exception.ModuleLoadException; import tc.oc.pgm.api.party.Competitor; import tc.oc.pgm.api.party.Party; import tc.oc.pgm.api.party.event.PartyAddEvent; @@ -31,32 +29,20 @@ import tc.oc.pgm.api.player.MatchPlayer; import tc.oc.pgm.events.ListenerScope; import tc.oc.pgm.events.PlayerPartyChangeEvent; -import tc.oc.pgm.ffa.FreeForAllMatchModule; -import tc.oc.pgm.teams.TeamMatchModule; import tc.oc.pgm.util.StringUtils; import tc.oc.pgm.util.text.TextTranslations; @ListenerScope(MatchScope.LOADED) public class ScoreboardMatchModule implements MatchModule, Listener { - public static class Factory implements MatchModuleFactory { - @Override - public Collection> getWeakDependencies() { - return ImmutableList.of(TeamMatchModule.class, FreeForAllMatchModule.class); - } - - @Override - public ScoreboardMatchModule createMatchModule(Match match) throws ModuleLoadException { - return new ScoreboardMatchModule(match); - } - } - private final Match match; private final Map partyScoreboards = new HashMap<>(); private final Scoreboard hiddenScoreboard; + private final ScoreboardDisplayItem belowName; - private ScoreboardMatchModule(Match match) { + public ScoreboardMatchModule(Match match, ScoreboardDisplayItem belowName) { this.match = match; + this.belowName = belowName; this.hiddenScoreboard = PGM.get().getServer().getScoreboardManager().getNewScoreboard(); } @@ -126,6 +112,15 @@ protected void addPartyScoreboard(Party newParty) { createPartyScoreboardTeam( newParty, entry.getValue(), !(entry.getKey() instanceof Competitor)); } + + switch (belowName) { + case NONE: + break; + case HEALTH: + Objective objective = + newScoreboard.registerNewObjective(ChatColor.DARK_RED + "❤", Criterias.HEALTH); + objective.setDisplaySlot(DisplaySlot.BELOW_NAME); + } } protected void removePartyScoreboard(Party party) { diff --git a/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardModule.java b/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardModule.java new file mode 100644 index 0000000000..056c60bc16 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardModule.java @@ -0,0 +1,57 @@ +package tc.oc.pgm.scoreboard; + +import com.google.common.collect.ImmutableList; +import java.util.Collection; +import java.util.logging.Logger; +import org.jdom2.Document; +import org.jdom2.Element; +import org.jetbrains.annotations.Nullable; +import tc.oc.pgm.api.map.MapModule; +import tc.oc.pgm.api.map.factory.MapFactory; +import tc.oc.pgm.api.map.factory.MapModuleFactory; +import tc.oc.pgm.api.match.Match; +import tc.oc.pgm.api.module.exception.ModuleLoadException; +import tc.oc.pgm.ffa.FreeForAllModule; +import tc.oc.pgm.teams.TeamModule; +import tc.oc.pgm.util.xml.InvalidXMLException; +import tc.oc.pgm.util.xml.XMLUtils; + +public class ScoreboardModule implements MapModule { + + private final ScoreboardDisplayItem belowName; + + private ScoreboardModule(ScoreboardDisplayItem belowName) { + this.belowName = belowName; + } + + public static class Factory implements MapModuleFactory { + + @Override + public Collection>> getWeakDependencies() { + return ImmutableList.of(TeamModule.class, FreeForAllModule.class); + } + + @Override + public @Nullable ScoreboardModule parse(MapFactory factory, Logger logger, Document doc) + throws InvalidXMLException { + Element elScoreboard = doc.getRootElement().getChild("scoreboard"); + + ScoreboardDisplayItem belowName = ScoreboardDisplayItem.NONE; + + if (elScoreboard != null) { + belowName = + XMLUtils.parseEnum( + elScoreboard.getAttribute("below-name"), + ScoreboardDisplayItem.class, + "scoreboard display item"); + } + + return new ScoreboardModule(belowName); + } + } + + @Override + public @Nullable ScoreboardMatchModule createMatchModule(Match match) throws ModuleLoadException { + return new ScoreboardMatchModule(match, belowName); + } +}