Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. public class SimpleScoreboard {
  2.  
  3. private static final Map<Scoreboard, SimpleScoreboard> scoreboardCache = new WeakHashMap<>();
  4. private static final BiMap<Integer, OfflinePlayer> playerHolder = HashBiMap.create(15);
  5. private final Scoreboard scoreboard;
  6. private final Objective objective;
  7. private final Map<Integer, Team> teams = new HashMap<>();
  8. private int counter;
  9.  
  10. static {
  11. for (int i = 15; i >= 0; i--) {
  12. String name = ChatColor.values()[i].toString() + ChatColor.RESET.toString();
  13. playerHolder.put(i, Bukkit.getOfflinePlayer(name));
  14. }
  15. }
  16.  
  17. public SimpleScoreboard(Scoreboard scoreboard) {
  18. this.scoreboard = scoreboard;
  19.  
  20. int teamCounter = 0;
  21. for (int i = 15; i >= 0; i--) {
  22. Team team = scoreboard.registerNewTeam("V_SS_TEAM_" + teamCounter++);
  23. team.addPlayer(getPlayerForPosition(i));
  24. this.teams.put(i, team);
  25. }
  26. this.objective = scoreboard.registerNewObjective("V_SS_OBJECTIVE", "dummy");
  27. this.objective.setDisplayName("SIMPLE_SCOREBOARD");
  28. this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
  29. }
  30.  
  31. public void clearText(int index) {
  32. setText(index, null);
  33. }
  34.  
  35. public void setText(int index, String text) {
  36. int position = 15 - index;
  37. OfflinePlayer player = getPlayerForPosition(position);
  38. Team team = this.teams.get(position);
  39. if ((text == null) || (text.isEmpty())) {
  40. this.counter -= 1;
  41. team.setPrefix("");
  42. team.setSuffix("");
  43. this.scoreboard.resetScores(player);
  44. if (this.counter == 0) {
  45. this.scoreboard.clearSlot(DisplaySlot.SIDEBAR);
  46. }
  47. return;
  48. }
  49. this.counter += 1;
  50. if (this.scoreboard.getObjective(DisplaySlot.SIDEBAR) != this.objective) {
  51. this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
  52. }
  53. applyTeamName(text, team);
  54.  
  55. this.objective.getScore(player).setScore(position);
  56. }
  57.  
  58. public void setTitle(String title) {
  59. this.objective.setDisplayName(title);
  60. }
  61.  
  62. public Scoreboard getScoreboard() {
  63. return this.scoreboard;
  64. }
  65.  
  66. public static SimpleScoreboard of(Scoreboard scoreboard) {
  67. SimpleScoreboard simpleScoreboard = scoreboardCache.get(scoreboard);
  68. if (simpleScoreboard != null) {
  69. return simpleScoreboard;
  70. }
  71. simpleScoreboard = new SimpleScoreboard(scoreboard);
  72. scoreboardCache.put(scoreboard, simpleScoreboard);
  73. return simpleScoreboard;
  74. }
  75.  
  76. public static OfflinePlayer getPlayerForPosition(Integer position) {
  77. return playerHolder.get(position);
  78. }
  79.  
  80. public static Integer getPositionForPlayer(OfflinePlayer player) {
  81. return playerHolder.inverse().get(player);
  82. }
  83.  
  84. private static void applyTeamName(String string, Team team) {
  85. if (string.length() <= 16) {
  86. team.setPrefix(string);
  87. team.setSuffix("");
  88. } else {
  89. String firstPart = string.substring(0, 16);
  90. String secondPart = ChatColor.getLastColors(firstPart) + string.substring(16, Math.min(32, string.length()));
  91. team.setPrefix(firstPart);
  92. team.setSuffix(secondPart.length() <= 16 ? secondPart : secondPart.substring(0, 16));
  93. }
  94. }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement