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

Add health display #1135

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
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: 2 additions & 1 deletion core/src/main/java/tc/oc/pgm/api/Modules.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tc.oc.pgm.scoreboard;

public enum ScoreboardDisplayItem {
NONE,
HEALTH
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

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;
import org.bukkit.ChatColor;
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;
Expand All @@ -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;
Expand All @@ -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<ScoreboardMatchModule> {
@Override
public Collection<Class<? extends MatchModule>> 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<Party, Scoreboard> 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();
}

Expand Down Expand Up @@ -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) {
Expand Down
57 changes: 57 additions & 0 deletions core/src/main/java/tc/oc/pgm/scoreboard/ScoreboardModule.java
Original file line number Diff line number Diff line change
@@ -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<ScoreboardMatchModule> {

private final ScoreboardDisplayItem belowName;

private ScoreboardModule(ScoreboardDisplayItem belowName) {
this.belowName = belowName;
}

public static class Factory implements MapModuleFactory<ScoreboardModule> {

@Override
public Collection<Class<? extends MapModule<?>>> 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);
}
}