Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. package com.talvorgames.minkizz.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.scoreboard.DisplaySlot;
  11. import org.bukkit.scoreboard.Objective;
  12. import org.bukkit.scoreboard.Score;
  13. import org.bukkit.scoreboard.Scoreboard;
  14. import org.bukkit.scoreboard.ScoreboardManager;
  15. import org.bukkit.scoreboard.Team;
  16.  
  17. import com.talvorgames.minkizz.TalvorGames;
  18.  
  19. import me.minkizz.aimp.utilities.Placeholders;
  20.  
  21. public class Sidebar {
  22.  
  23. // The ScoreboardManager allows us to create Scoreboards
  24. private static ScoreboardManager manager = Bukkit.getScoreboardManager();
  25.  
  26. // The Scoreboard from Bukkit API
  27. private Scoreboard scoreboard;
  28.  
  29. // Each Scoreboard has an "objective", so we can set the Scoreboard title,
  30. // position on screen...
  31. private Objective objective;
  32.  
  33. // List of players that need an update from Scoreboard
  34. private List<Player> toUpdate = new ArrayList<>();
  35.  
  36. // The update task stored in a variable so may cancel it later
  37. private int taskId;
  38.  
  39. // The original lines of the Sidebar without placeholders set
  40. private String[] originalLines;
  41.  
  42. private static Map<Player, Sidebar> playerSidebars = new HashMap<>();
  43.  
  44. @SuppressWarnings("deprecation")
  45. public Sidebar(String title, String... lines) {
  46. scoreboard = manager.getNewScoreboard(); // Create a new Scoreboard
  47. objective = this.scoreboard.registerNewObjective("test", "dummy"); // Register the Scoreboard objective
  48. objective.setDisplaySlot(DisplaySlot.SIDEBAR); // Show the Scoreboard on the side of screen
  49. objective.setDisplayName(title); // Set title of Scoreboard
  50. originalLines = lines; // Keep lines with placeholders in a variable
  51.  
  52. for (int i = lines.length; i > 0; i--) {
  53. // Current line
  54. String line = lines[(i - 1)];
  55.  
  56. // Register the team associated to this line
  57. // We use teams for performance reasons and anti-flickering
  58. Team team = scoreboard.registerNewTeam("team" + (i - 1));
  59.  
  60. // Set the content of the team to the current line
  61. team.addPlayer(Bukkit.getOfflinePlayer(line));
  62.  
  63. // Get an objective of current line
  64. Score score = objective.getScore(line);
  65.  
  66. // Set the number of the objective which is used to sort the Scoreboard
  67. score.setScore((lines.length - i) + 1);
  68. }
  69.  
  70. // Put the Bukkit task in a variable so we can cancel it later
  71. taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(TalvorGames.getInstance(), new Runnable() {
  72.  
  73. public void run() {
  74.  
  75. // Iterate through all players that needs a Scoreboard update
  76. for (Player player : toUpdate) {
  77.  
  78. // Iterate through original lines with placeholders kept
  79. for (int i = originalLines.length; i > 0; i--) {
  80. // Get team associated with this line
  81. Team team = scoreboard.getTeam("team" + (i - 1));
  82.  
  83. // Get a copy of current line
  84. String line = new String(originalLines[i - 1]);
  85.  
  86. try {
  87. line = Placeholders.setPlaceholders(line, player);
  88. } catch (Exception placeholderApiNotFound) {
  89. // PlaceholderAPI has not been found on this server
  90. // We don't use PluginManager#isPluginEnabled because of this reason:
  91. // - A plugin can have the same name as PlaceholderAPI but not be the
  92. // PlaceholderAPI that we want to use here
  93. }
  94.  
  95. team.setPrefix("§r "); // I don't leave a blank space " " or "", because it throw an exception
  96. team.setSuffix("§r "); // I don't leave a blank space " " or "", because it throw an exception
  97. }
  98.  
  99. }
  100.  
  101. }
  102.  
  103. }, 10L, 10L);
  104.  
  105. }
  106.  
  107. public void showTo(Player player) {
  108.  
  109. if (!player.getScoreboard().equals(scoreboard)) {
  110. player.setScoreboard(scoreboard);
  111. }
  112.  
  113. toUpdate.add(player);
  114. playerSidebars.put(player, this);
  115. }
  116.  
  117. public void remove() {
  118.  
  119. for (Player player : toUpdate) {
  120. player.setScoreboard(manager.getNewScoreboard());
  121. }
  122.  
  123. toUpdate.clear();
  124. Bukkit.getScheduler().cancelTask(taskId);
  125. }
  126.  
  127. public void remove(Player player) {
  128. player.setScoreboard(manager.getNewScoreboard());
  129. toUpdate.remove(player);
  130. }
  131.  
  132. public static Sidebar getPlayerSidebar(Player player) {
  133. return playerSidebars.get(player);
  134. }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement