Advertisement
Guest User

SimpleScoreboard Source

a guest
Apr 30th, 2014
11,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import java.util.AbstractMap;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.OfflinePlayer;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.scoreboard.DisplaySlot;
  10. import org.bukkit.scoreboard.Objective;
  11. import org.bukkit.scoreboard.Scoreboard;
  12. import org.bukkit.scoreboard.Team;
  13.  
  14. import com.google.common.base.Preconditions;
  15. import com.google.common.base.Splitter;
  16. import com.google.common.collect.Lists;
  17. import com.google.common.collect.Maps;
  18.  
  19. public class SimpleScoreboard {
  20.  
  21. private Scoreboard scoreboard;
  22.  
  23. private String title;
  24. private Map<String, Integer> scores;
  25. private List<Team> teams;
  26.  
  27. public SimpleScoreboard(String title) {
  28. this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
  29. this.title = title;
  30. this.scores = Maps.newLinkedHashMap();
  31. this.teams = Lists.newArrayList();
  32. }
  33.  
  34. public void blankLine() {
  35. add(" ");
  36. }
  37.  
  38. public void add(String text) {
  39. add(text, null);
  40. }
  41.  
  42. public void add(String text, Integer score) {
  43. Preconditions.checkArgument(text.length() < 48, "text cannot be over 48 characters in length");
  44. text = fixDuplicates(text);
  45. scores.put(text, score);
  46. }
  47.  
  48. private String fixDuplicates(String text) {
  49. while (scores.containsKey(text))
  50. text += "ยงr";
  51. if (text.length() > 48)
  52. text = text.substring(0, 47);
  53. return text;
  54. }
  55.  
  56. private Map.Entry<Team, String> createTeam(String text) {
  57. String result = "";
  58. if (text.length() <= 16)
  59. return new AbstractMap.SimpleEntry<>(null, text);
  60. Team team = scoreboard.registerNewTeam("text-" + scoreboard.getTeams().size());
  61. Iterator<String> iterator = Splitter.fixedLength(16).split(text).iterator();
  62. team.setPrefix(iterator.next());
  63. result = iterator.next();
  64. if (text.length() > 32)
  65. team.setSuffix(iterator.next());
  66. teams.add(team);
  67. return new AbstractMap.SimpleEntry<>(team, result);
  68. }
  69.  
  70. public void build() {
  71. Objective obj = scoreboard.registerNewObjective((title.length() > 16 ? title.substring(0, 15) : title), "dummy");
  72. obj.setDisplayName(title);
  73. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
  74.  
  75. int index = scores.size();
  76.  
  77. for (Map.Entry<String, Integer> text : scores.entrySet()) {
  78. Map.Entry<Team, String> team = createTeam(text.getKey());
  79. Integer score = text.getValue() != null ? text.getValue() : index;
  80. OfflinePlayer player = Bukkit.getOfflinePlayer(team.getValue());
  81. if (team.getKey() != null)
  82. team.getKey().addPlayer(player);
  83. obj.getScore(player).setScore(score);
  84. index -= 1;
  85. }
  86. }
  87.  
  88. public void reset() {
  89. title = null;
  90. scores.clear();
  91. for (Team t : teams)
  92. t.unregister();
  93. teams.clear();
  94. }
  95.  
  96. public Scoreboard getScoreboard() {
  97. return scoreboard;
  98. }
  99.  
  100. public void send(Player... players) {
  101. for (Player p : players)
  102. p.setScoreboard(scoreboard);
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement