/
SubAPI.java
200 lines (179 loc) · 5.25 KB
/
SubAPI.java
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
package net.ME1312.SubServers.Client.Bukkit;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Library.Version.Version;
import net.ME1312.SubData.Client.DataClient;
import net.ME1312.SubData.Client.DataProtocol;
import net.ME1312.SubData.Client.SubDataClient;
import net.ME1312.SubServers.Client.Bukkit.Graphic.UIHandler;
import net.ME1312.SubServers.Client.Bukkit.Library.AccessMode;
import net.ME1312.SubServers.Client.Common.ClientAPI;
import org.bukkit.Bukkit;
import java.util.*;
/**
* SubAPI Class
*/
public final class SubAPI extends ClientAPI {
LinkedList<Runnable> reloadListeners = new LinkedList<Runnable>();
private final SubPlugin plugin;
private static SubAPI api;
AccessMode access;
String name;
SubAPI(SubPlugin plugin) {
this.plugin = plugin;
GAME_VERSION = getGameVersion();
access = AccessMode.DEFAULT;
api = this;
}
/**
* Gets the SubAPI Methods
*
* @return SubAPI
*/
public static SubAPI getInstance() {
return api;
}
/**
* Gets the SubServers Internals
*
* @deprecated Use SubAPI Methods when available
* @return SubPlugin Internals
*/
@Deprecated
public SubPlugin getInternals() {
return plugin;
}
/**
* Adds a SubAPI Reload Listener
*
* @param reload An Event that will be called after SubAPI is soft-reloaded
*/
public void addListener(Runnable reload) {
if (reload != null) reloadListeners.add(reload);
}
/**
* Get the Server Name
*
* @return Server Name
*/
public String getName() {
return name;
}
/**
* Gets the SubData Network Connections
*
* @return SubData Network Connections
*/
public DataClient[] getSubDataNetwork() {
Integer[] keys = plugin.subdata.keySet().toArray(new Integer[0]);
DataClient[] channels = new DataClient[keys.length];
Arrays.sort(keys);
for (int i = 0; i < keys.length; ++i) channels[i] = plugin.subdata.get(keys[i]);
return channels;
}
/**
* Gets the SubData Network Protocol
*
* @return SubData Network Protocol
*/
public DataProtocol getSubDataProtocol() {
return plugin.subprotocol;
}
/**
* Gets the current SubServers Lang Channels
*
* @return SubServers Lang Channel list
*/
public Collection<String> getLangChannels() {
return plugin.lang.value().keySet();
}
/**
* Gets values from the SubServers Lang
*
* @param channel Lang Channel
* @return Lang Value
*/
public Map<String, String> getLang(String channel) {
if (Util.isNull(channel)) throw new NullPointerException();
return new LinkedHashMap<>(plugin.lang.value().get(channel.toLowerCase()));
}
/**
* Get the plugin's access mode
*
* @return Access Mode
*/
public AccessMode getAccessMode() {
return access;
}
/**
* Set the plugin's access mode
*
* @param mode Access Mode
*/
public void setAccessMode(AccessMode mode) {
if (mode.value <= access.value && !plugin.isEnabled()) {
access = mode;
}
}
/**
* Gets the Graphics Handler
*
* @return Graphics Handler
*/
public UIHandler getGraphicHandler() {
return plugin.gui;
}
/**
* Sets the Graphics Handler for SubServers to use
*
* @param graphics Graphics Handler
*/
public void setGraphicHandler(UIHandler graphics) {
if (plugin.gui != null) plugin.gui.disable();
plugin.gui = graphics;
}
/**
* Gets the SubServers Version
*
* @return SubServers Version
*/
public Version getPluginVersion() {
return plugin.version;
}
/**
* Gets the SubServers Build Version
*
* @return SubServers Build Version (or null if unsigned)
*/
public Version getPluginBuild() {
return (SubPlugin.class.getPackage().getSpecificationTitle() != null)?new Version(SubPlugin.class.getPackage().getSpecificationTitle()):null;
}
/**
* Gets the Server Version
*
* @return Server Version
*/
public Version getServerVersion() {
return new Version(Bukkit.getServer().getVersion());
}
/**
* Gets the Minecraft Version
*
* @return Minecraft Version
*/
public Version getGameVersion() {
if (GAME_VERSION == null) {
if (System.getProperty("subservers.minecraft.version", "").length() > 0) {
return new Version(System.getProperty("subservers.minecraft.version"));
} else {
try {
return new Version(Bukkit.getBukkitVersion().split("-")[0]);
} catch (ArrayIndexOutOfBoundsException e) {
plugin.getLogger().warning("Could not determine this server's game version; Now using 1.x.x as a placeholder.");
plugin.getLogger().warning("Use this launch argument to specify what version this server serves: -Dsubservers.minecraft.version=1.x.x");
return new Version("1.x.x");
}
}
} else return GAME_VERSION;
}
private final Version GAME_VERSION;
}