forked from koca2000/NoteBlockAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteBlockAPI.java
More file actions
259 lines (220 loc) · 7.2 KB
/
NoteBlockAPI.java
File metadata and controls
259 lines (220 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.xxmicloxx.NoteBlockAPI;
import com.xxmicloxx.NoteBlockAPI.songplayer.SongPlayer;
import com.xxmicloxx.NoteBlockAPI.utils.MathUtils;
import com.xxmicloxx.NoteBlockAPI.utils.Updater;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.DrilldownPie;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.scheduler.BukkitWorker;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* Main class; contains methods for playing and adjusting songs for players
*/
public class NoteBlockAPI extends JavaPlugin {
private static NoteBlockAPI plugin;
private Map<UUID, ArrayList<SongPlayer>> playingSongs = new ConcurrentHashMap<UUID, ArrayList<SongPlayer>>();
private Map<UUID, Byte> playerVolume = new ConcurrentHashMap<UUID, Byte>();
private boolean disabling = false;
private HashMap<Plugin, Boolean> dependentPlugins = new HashMap<>();
/**
* Returns true if a Player is currently receiving a song
* @param player
* @return is receiving a song
*/
public static boolean isReceivingSong(Player player) {
return isReceivingSong(player.getUniqueId());
}
/**
* Returns true if a Player with specified UUID is currently receiving a song
* @param uuid
* @return is receiving a song
*/
public static boolean isReceivingSong(UUID uuid) {
ArrayList<SongPlayer> songs = plugin.playingSongs.get(uuid);
return (songs != null && !songs.isEmpty());
}
/**
* Stops the song for a Player
* @param player
*/
public static void stopPlaying(Player player) {
stopPlaying(player.getUniqueId());
}
/**
* Stops the song for a Player
* @param uuid
*/
public static void stopPlaying(UUID uuid) {
ArrayList<SongPlayer> songs = plugin.playingSongs.get(uuid);
if (songs == null) {
return;
}
for (SongPlayer songPlayer : songs) {
songPlayer.removePlayer(uuid);
}
}
/**
* Sets the volume for a given Player
* @param player
* @param volume
*/
public static void setPlayerVolume(Player player, byte volume) {
setPlayerVolume(player.getUniqueId(), volume);
}
/**
* Sets the volume for a given Player
* @param uuid
* @param volume
*/
public static void setPlayerVolume(UUID uuid, byte volume) {
plugin.playerVolume.put(uuid, volume);
}
/**
* Gets the volume for a given Player
* @param player
* @return volume (byte)
*/
public static byte getPlayerVolume(Player player) {
return getPlayerVolume(player.getUniqueId());
}
/**
* Gets the volume for a given Player
* @param uuid
* @return volume (byte)
*/
public static byte getPlayerVolume(UUID uuid) {
Byte byteObj = plugin.playerVolume.get(uuid);
if (byteObj == null) {
byteObj = 100;
plugin.playerVolume.put(uuid, byteObj);
}
return byteObj;
}
public static ArrayList<SongPlayer> getSongPlayersByPlayer(Player player){
return getSongPlayersByPlayer(player.getUniqueId());
}
public static ArrayList<SongPlayer> getSongPlayersByPlayer(UUID player){
return plugin.playingSongs.get(player);
}
public static void setSongPlayersByPlayer(Player player, ArrayList<SongPlayer> songs){
setSongPlayersByPlayer(player.getUniqueId(), songs);
}
public static void setSongPlayersByPlayer(UUID player, ArrayList<SongPlayer> songs){
plugin.playingSongs.put(player, songs);
}
@Override
public void onEnable() {
plugin = this;
for (Plugin pl : getServer().getPluginManager().getPlugins()){
if (pl.getDescription().getDepend().contains("NoteBlockAPI") || pl.getDescription().getSoftDepend().contains("NoteBlockAPI")){
dependentPlugins.put(pl, false);
}
}
Metrics metrics = new Metrics(this, 1083);
new NoteBlockPlayerMain().onEnable();
getServer().getScheduler().runTaskLater(this, new Runnable() {
@Override
public void run() {
for (RegisteredListener rl : PlayerRangeStateChangeEvent.getHandlerList().getRegisteredListeners()) {
dependentPlugins.put(rl.getPlugin(), true);
}
for (RegisteredListener rl : SongDestroyingEvent.getHandlerList().getRegisteredListeners()) {
dependentPlugins.put(rl.getPlugin(), true);
}
for (RegisteredListener rl : SongEndEvent.getHandlerList().getRegisteredListeners()) {
dependentPlugins.put(rl.getPlugin(), true);
}
for (RegisteredListener rl : SongStoppedEvent.getHandlerList().getRegisteredListeners()) {
dependentPlugins.put(rl.getPlugin(), true);
}
metrics.addCustomChart(new DrilldownPie("deprecated", () -> {
Map<String, Map<String, Integer>> map = new HashMap<>();
for (Plugin pl : dependentPlugins.keySet()){
String deprecated = dependentPlugins.get(pl) ? "yes" : "no";
Map<String, Integer> entry = new HashMap<>();
entry.put(pl.getDescription().getFullName(), 1);
map.put(deprecated, entry);
}
return map;
}));
}
}, 1);
getServer().getScheduler().runTaskTimerAsynchronously(this, new Runnable() {
@Override
public void run() {
try {
if (Updater.checkUpdate("19287", getDescription().getVersion())){
Bukkit.getLogger().info(String.format("[%s] New update available!", plugin.getDescription().getName()));
}
} catch (IOException e) {
Bukkit.getLogger().info(String.format("[%s] Cannot receive update from Spigot resource page!", plugin.getDescription().getName()));
}
}
}, 20*10, 20 * 60 * 60 * 24);
}
@Override
public void onDisable() {
disabling = true;
Bukkit.getScheduler().cancelTasks(this);
List<BukkitWorker> workers = Bukkit.getScheduler().getActiveWorkers();
for (BukkitWorker worker : workers){
if (!worker.getOwner().equals(this))
continue;
worker.getThread().interrupt();
}
NoteBlockPlayerMain.plugin.onDisable();
}
public void doSync(Runnable runnable) {
getServer().getScheduler().runTask(this, runnable);
}
public void doAsync(Runnable runnable) {
getServer().getScheduler().runTaskAsynchronously(this, runnable);
}
public boolean isDisabling() {
return disabling;
}
public static NoteBlockAPI getAPI(){
return plugin;
}
protected void handleDeprecated(StackTraceElement[] ste){
int pom = 1;
String clazz = ste[pom].getClassName();
while (clazz.startsWith("com.xxmicloxx.NoteBlockAPI")){
pom++;
clazz = ste[pom].getClassName();
}
String[] packageParts = clazz.split("\\.");
ArrayList<Plugin> plugins = new ArrayList<Plugin>();
plugins.addAll(dependentPlugins.keySet());
ArrayList<Plugin> notResult = new ArrayList<Plugin>();
parts:
for (int i = 0; i < packageParts.length - 1; i++){
for (Plugin pl : plugins){
if (notResult.contains(pl)){ continue;}
if (plugins.size() - notResult.size() == 1){
break parts;
}
String[] plParts = pl.getDescription().getMain().split("\\.");
if (!packageParts[i].equalsIgnoreCase(plParts[i])){
notResult.add(pl);
continue;
}
}
plugins.removeAll(notResult);
notResult.clear();
}
plugins.removeAll(notResult);
notResult.clear();
if (plugins.size() == 1){
dependentPlugins.put(plugins.get(0), true);
}
}
}