Advertisement
Guest User

SimpleScoreboard

a guest
Jun 15th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. package me.filipenock.supersw.Until;
  2.  
  3. import java.util.HashMap;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.scoreboard.DisplaySlot;
  11. import org.bukkit.scoreboard.Objective;
  12. import org.bukkit.scoreboard.Scoreboard;
  13. import org.bukkit.scoreboard.Team;
  14.  
  15. @SuppressWarnings("all")
  16. public abstract class ScoreManager {
  17.  
  18. private Scoreboard scoreboard;
  19. private Objective objective;
  20. private Player player;
  21.  
  22. protected int currentLine = 16;
  23. protected Map<Integer, String> last = new HashMap<>();
  24. protected Map<Integer, String> cache = new LinkedHashMap<>();
  25.  
  26. public ScoreManager(Player p, String title) {
  27. this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
  28. this.objective = scoreboard.registerNewObjective("sbapi", "dummy");
  29. this.objective.setDisplayName(title.replace("&", "§"));
  30. this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
  31. this.player = p;
  32. p.setScoreboard(this.scoreboard);
  33. }
  34.  
  35. public boolean add(String text) {
  36. if (currentLine <= 0)
  37. return false;
  38. cache.put(currentLine--, text.replace("&", "§"));
  39. return true;
  40. }
  41.  
  42. public boolean blank() {
  43. return add("");
  44. }
  45.  
  46. public void updateScoreboard() {
  47. currentLine = 16;
  48. for (String text : cache.values())
  49. update(text);
  50. }
  51.  
  52. protected boolean update(String text) {
  53. text = placeHolders(text);
  54. if (text.length() > 32) {
  55. if (last.containsKey(currentLine))
  56. if (!last.get(currentLine).equals(text.substring(16, 32)))
  57. scoreboard.resetScores(last.get(currentLine));
  58. }
  59. String prefix = text.length() > 32 ? text.substring(0, 16) : "";
  60. String entry = text.length() > 32 ? text.substring(16, 32) : ChatColor.values()[currentLine - 1] + "§r";
  61. String suffix = text.length() > 32 ? text.substring(32, text.length() > 48 ? 48 : text.length()) : "";
  62. if (!text.isEmpty() && prefix == "")
  63. prefix = text.substring(0, text.length() > 16 ? 16 : text.length());
  64. if (text.length() > 16 && suffix == "") {
  65. if (prefix.substring(prefix.length() - 1).equals("§")) {
  66. prefix = prefix.substring(0, prefix.length() - 1);
  67. text = text.replace(prefix, "");
  68. suffix = text.substring(0, text.length() > 16 ? 16 : text.length());
  69. } else {
  70. text = ChatColor.getLastColors(prefix) + text.replace(prefix, "");
  71. suffix = text.substring(0, text.length() > 16 ? 16 : text.length());
  72. }
  73. }
  74. Team t = scoreboard.getTeam("[Team:" + currentLine + "]");
  75. if (t == null)
  76. t = scoreboard.registerNewTeam("[Team:" + currentLine + "]");
  77. t.setPrefix(prefix);
  78. t.setSuffix(suffix);
  79. if (!t.hasEntry(entry))
  80. t.addEntry(entry);
  81. objective.getScore(entry).setScore(currentLine);
  82. last.put(currentLine--, entry);
  83. return true;
  84. }
  85.  
  86. public abstract String placeHolders(String str);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement