Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2020
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 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.  * ScoreboardWrapper is a class that wraps Bukkit Scoreboard API
  13.  * and makes your life easier.
  14.  */
  15. public class ScoreboardWrapper {
  16.  
  17.     public static final int MAX_LINES = 16;
  18.  
  19.     private final Scoreboard scoreboard;
  20.     private final Objective objective;
  21.  
  22.     private final List<String> modifies = new ArrayList<>(MAX_LINES);
  23.  
  24.     /**
  25.      * Instantiates a new ScoreboardWrapper with a default title.
  26.      */
  27.     public ScoreboardWrapper(String title) {
  28.         scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
  29.         objective = scoreboard.registerNewObjective(title, "dummy");
  30.         objective.setDisplayName(title);
  31.         objective.setDisplaySlot(DisplaySlot.SIDEBAR);
  32.     }
  33.  
  34.     /**
  35.      * Sets the scoreboard title.
  36.      */
  37.     public void setTitle(String title) {
  38.         objective.setDisplayName(title);
  39.     }
  40.  
  41.     /**
  42.      * Modifies the line with §r strings in the way to add
  43.      * a line equal to another.
  44.      */
  45.     private String getLineCoded(String line) {
  46.         String result = line;
  47.         while (modifies.contains(result))
  48.             result += ChatColor.RESET;
  49.         return result.substring(0, Math.min(40, result.length()));
  50.     }
  51.  
  52.     /**
  53.      * Adds a new line to the scoreboard. Throw an error if the lines count are higher than 16.
  54.      */
  55.     public void addLine(String line) {
  56.         if (modifies.size() > MAX_LINES)
  57.             throw new IndexOutOfBoundsException("You cannot add more than 16 lines.");
  58.         String modified = getLineCoded(line);
  59.         modifies.add(modified);
  60.         objective.getScore(modified).setScore(-(modifies.size()));
  61.     }
  62.  
  63.     /**
  64.      * Adds a blank space to the scoreboard.
  65.      */
  66.     public void addBlankSpace() {
  67.         addLine(" ");
  68.     }
  69.  
  70.     /**
  71.      * Sets a scoreboard line to an exact index (between 0 and 15).
  72.      */
  73.     public void setLine(int index, String line) {
  74.         if (index < 0 || index >= MAX_LINES)
  75.             throw new IndexOutOfBoundsException("The index cannot be negative or higher than 15.");
  76.         String oldModified = modifies.get(index);
  77.         scoreboard.resetScores(oldModified);
  78.         String modified = getLineCoded(line);
  79.         modifies.set(index, modified);
  80.         objective.getScore(modified).setScore(-(index + 1));
  81.     }
  82.  
  83.     /**
  84.      * Gets the Bukkit Scoreboard.
  85.      */
  86.     public Scoreboard getScoreboard() {
  87.         return scoreboard;
  88.     }
  89.  
  90.     /**
  91.      * Just for debug.
  92.      */
  93.     @Override
  94.     public String toString() {
  95.         String out = "";
  96.         int i = 0;
  97.         for (String string : modifies)
  98.             out += -(i + 1) + ")-> " + string + ";\n";
  99.         return out;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement