From 55578561442e280549474c452ccf3eb5150b94fa Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Sun, 13 Apr 2014 10:10:32 -0500 Subject: [PATCH] Add methods to use arbitrary entries in scoreboards. Adds BUKKIT-3977 --- .../scoreboard/CraftObjective.java | 7 ++++++ .../craftbukkit/scoreboard/CraftScore.java | 19 +++++++++------ .../scoreboard/CraftScoreboard.java | 24 +++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftObjective.java b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftObjective.java index 431807a80f..9eaec710b7 100644 --- a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftObjective.java +++ b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftObjective.java @@ -93,6 +93,13 @@ final class CraftObjective extends CraftScoreboardComponent implements Objective return new CraftScore(this, player.getName()); } + public Score getScore(String entry) throws IllegalArgumentException, IllegalStateException { + Validate.notNull(entry, "Entry cannot be null"); + CraftScoreboard scoreboard = checkState(); + + return new CraftScore(this, entry); + } + @Override public void unregister() throws IllegalStateException { CraftScoreboard scoreboard = checkState(); diff --git a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScore.java b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScore.java index 0ffbe9b08d..7095f6d83a 100644 --- a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScore.java +++ b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScore.java @@ -17,16 +17,20 @@ import org.bukkit.scoreboard.Score; * Also, as an added perk, a CraftScore will (intentionally) stay a valid reference so long as objective is valid. */ final class CraftScore implements Score { - private final String playerName; + private final String entry; private final CraftObjective objective; - CraftScore(CraftObjective objective, String playerName) { + CraftScore(CraftObjective objective, String entry) { this.objective = objective; - this.playerName = playerName; + this.entry = entry; } public OfflinePlayer getPlayer() { - return Bukkit.getOfflinePlayer(playerName); + return Bukkit.getOfflinePlayer(entry); + } + + public String getEntry() { + return entry; } public Objective getObjective() { @@ -36,18 +40,19 @@ final class CraftScore implements Score { public int getScore() throws IllegalStateException { Scoreboard board = objective.checkState().board; - if (board.getPlayers().contains(playerName)) { // Lazy - Map scores = board.getPlayerObjectives(playerName); + if (board.getPlayers().contains(entry)) { // Lazy + Map scores = board.getPlayerObjectives(entry); ScoreboardScore score = scores.get(objective.getHandle()); if (score != null) { // Lazy return score.getScore(); } } + return 0; // Lazy } public void setScore(int score) throws IllegalStateException { - objective.checkState().board.getPlayerScoreForObjective(playerName, objective.getHandle()).setScore(score); + objective.checkState().board.getPlayerScoreForObjective(entry, objective.getHandle()).setScore(score); } public CraftScoreboard getScoreboard() { diff --git a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboard.java b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboard.java index 72b02b07ce..ad65b3f859 100644 --- a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboard.java +++ b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboard.java @@ -84,12 +84,28 @@ public final class CraftScoreboard implements org.bukkit.scoreboard.Scoreboard { return scores.build(); } + public ImmutableSet getScores(String entry) throws IllegalArgumentException { + Validate.notNull(entry, "Entry cannot be null"); + + ImmutableSet.Builder scores = ImmutableSet.builder(); + for (CraftObjective objective : objectives.values()) { + scores.add(objective.getScore(entry)); + } + return scores.build(); + } + public void resetScores(OfflinePlayer player) throws IllegalArgumentException { Validate.notNull(player, "OfflinePlayer cannot be null"); board.resetPlayerScores(player.getName()); } + public void resetScores(String entry) throws IllegalArgumentException { + Validate.notNull(entry, "Entry cannot be null"); + + board.resetPlayerScores(entry); + } + public Team getPlayerTeam(OfflinePlayer player) throws IllegalArgumentException { Validate.notNull(player, "OfflinePlayer cannot be null"); @@ -123,6 +139,14 @@ public final class CraftScoreboard implements org.bukkit.scoreboard.Scoreboard { return players.build(); } + public ImmutableSet getEntries() { + ImmutableSet.Builder entries = ImmutableSet.builder(); + for (Object entry : board.getPlayers()) { + entries.add(entry.toString()); + } + return entries.build(); + } + public void clearSlot(DisplaySlot slot) throws IllegalArgumentException { Validate.notNull(slot, "Slot cannot be null"); board.setDisplaySlot(CraftScoreboardTranslations.fromBukkitSlot(slot), null);