Advertisement
Guest User

Untitled

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