SHOW:
|
|
- or go back to the newest paste.
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 | private static Map<Player, Sidebar> playerSidebars = new HashMap<>(); | |
40 | ||
41 | @SuppressWarnings("deprecation") | |
42 | public Sidebar(String title, String... lines) { | |
43 | scoreboard = manager.getNewScoreboard(); // Create a new Scoreboard | |
44 | objective = this.scoreboard.registerNewObjective("test", "dummy"); // Register the Scoreboard objective | |
45 | objective.setDisplaySlot(DisplaySlot.SIDEBAR); // Show the Scoreboard on the side of screen | |
46 | objective.setDisplayName(title); // Set title of Scoreboard | |
47 | ||
48 | taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(TalvorGames.getInstance(), new Runnable() { | |
49 | ||
50 | public void run() { | |
51 | ||
52 | for (Player player : toUpdate) { | |
53 | Team team = scoreboard.getTeam(player.getName() + "team"); | |
54 | if (team == null) { | |
55 | team = scoreboard.registerNewTeam(player.getName() + "team"); | |
56 | } | |
57 | team.getEntries().forEach(Team::removeEntry); | |
58 | - | // Register the team associated to this line |
58 | + | |
59 | - | // We use teams for performance reasons and anti-flickering |
59 | + | |
60 | - | Team team = scoreboard.getTeam(player.getName() + "team" + (i - 1)); |
60 | + | |
61 | // Set the content of the team to the current line | |
62 | ||
63 | - | team = scoreboard.registerNewTeam(player.getName() + "team" + (i - 1)); |
63 | + | |
64 | line = Placeholders.setPlaceholders(line, player); | |
65 | } catch (Exception placeholderApiNotFound) { | |
66 | // PlaceholderAPI has not been found on this server | |
67 | // We don't use PluginManager#isPluginEnabled because of this reason: | |
68 | // - A plugin can have the same name as PlaceholderAPI but not be the | |
69 | // PlaceholderAPI that we want to use here | |
70 | } | |
71 | ||
72 | team.addEntry(line); | |
73 | ||
74 | // Get an objective of current line | |
75 | Score score = objective.getScore(line); | |
76 | ||
77 | // Set the number of the objective which is used to sort the Scoreboard | |
78 | score.setScore((lines.length - i) + 1); | |
79 | } | |
80 | ||
81 | } | |
82 | ||
83 | } | |
84 | ||
85 | }, 10L, 10L); | |
86 | ||
87 | } | |
88 | ||
89 | public void showTo(Player player) { | |
90 | ||
91 | if (!player.getScoreboard().equals(scoreboard)) { | |
92 | player.setScoreboard(scoreboard); | |
93 | } | |
94 | ||
95 | toUpdate.add(player); | |
96 | playerSidebars.put(player, this); | |
97 | } | |
98 | ||
99 | public void remove() { | |
100 | ||
101 | for (Player player : toUpdate) { | |
102 | player.setScoreboard(manager.getNewScoreboard()); | |
103 | } | |
104 | ||
105 | toUpdate.clear(); | |
106 | Bukkit.getScheduler().cancelTask(taskId); | |
107 | } | |
108 | ||
109 | public void remove(Player player) { | |
110 | player.setScoreboard(manager.getNewScoreboard()); | |
111 | toUpdate.remove(player); | |
112 | } | |
113 | ||
114 | public static Sidebar getPlayerSidebar(Player player) { | |
115 | return playerSidebars.get(player); | |
116 | } | |
117 | ||
118 | } |