Advertisement
Guest User

Scoreboard API

a guest
Jun 30th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1.  
  2. import org.bukkit.Bukkit;
  3. import org.bukkit.ChatColor;
  4. import org.bukkit.scoreboard.DisplaySlot;
  5. import org.bukkit.scoreboard.Objective;
  6. import org.bukkit.scoreboard.Scoreboard;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11.  
  12. /**
  13. * Class created by JohnnyKPL for Spigot Community.
  14. */
  15. public class ScoreboardWrapper {
  16.  
  17.  
  18. public static void aaaa() {
  19.  
  20. }
  21.  
  22. public static final int MAX_LINES = 16;
  23.  
  24. private final Scoreboard scoreboard;
  25. private final Objective objective;
  26.  
  27. private final List<String> modifies = new ArrayList<>(MAX_LINES);
  28.  
  29. /**
  30. * Instantiate a new ScoreboardWrapper with a default title.
  31. */
  32. public ScoreboardWrapper(String title) {
  33. scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
  34. objective = scoreboard.registerNewObjective("dummy", title);
  35. objective.setDisplayName(title);
  36. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
  37. }
  38.  
  39. /**
  40. * Set the scoreboard title.
  41. */
  42. public void setTitle(String title) {
  43. objective.setDisplayName(title);
  44. }
  45.  
  46. private String getLineCoded(String line) {
  47. String result = line;
  48. while (modifies.contains(result)) {
  49. result += ChatColor.RESET;
  50. }
  51. return result.substring(0, Math.min(40, result.length()));
  52. }
  53.  
  54. /**
  55. * Add a new line to the scoreboard. Throw an error if the lines count are higher than 16.
  56. */
  57. public void addLine(String line) {
  58. if (modifies.size() > MAX_LINES)
  59. throw new IndexOutOfBoundsException("You cannot add more than 16 lines.");
  60. String modified = getLineCoded(line);
  61. modifies.add(modified);
  62. objective.getScore(modified).setScore(-(modifies.size()));
  63. }
  64.  
  65. /**
  66. * Add a blank space to the scoreboard.
  67. */
  68. public void addBlankSpace() {
  69. addLine(" ");
  70. }
  71.  
  72. /**
  73. * Set a scoreboard line to an exact index (between 0 and 15).
  74. */
  75. public void setLine(int index, String line) {
  76. if (index < 0 || index >= MAX_LINES)
  77. throw new IndexOutOfBoundsException("The index cannot be negative or higher than 15.");
  78. String oldModified = modifies.get(index);
  79. scoreboard.resetScores(oldModified);
  80. String modified = getLineCoded(line);
  81. modifies.set(index, modified);
  82. objective.getScore(modified).setScore(-(index + 1));
  83. }
  84.  
  85. /**
  86. * Get the Bukkit Scoreboard.
  87. */
  88. public Scoreboard getScoreboard() {
  89. return scoreboard;
  90. }
  91.  
  92. /**
  93. * Just for debug.
  94. */
  95. @Override
  96. public String toString() {
  97. String out = "";
  98. int i = 0;
  99. for (String string : modifies)
  100. out += -(i + 1) + ") -> " + string + ";\n";
  101. return out;
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement