-
Notifications
You must be signed in to change notification settings - Fork 23
/
SubCreator.java
401 lines (357 loc) · 11 KB
/
SubCreator.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package net.ME1312.SubServers.Bungee.Host;
import com.google.common.collect.Range;
import net.ME1312.Galaxi.Library.Callback.Callback;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidTemplateException;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Library.Version.Version;
import net.ME1312.SubServers.Bungee.SubAPI;
import java.io.File;
import java.net.InetSocketAddress;
import java.util.*;
/**
* SubCreator Layout Class
*/
public abstract class SubCreator {
public static class ServerTemplate {
private String name;
private String nick = null;
private boolean enabled;
private String icon;
private File directory;
private ServerType type;
private ObjectMap<String> build;
private ObjectMap<String> options;
/**
* Create a SubCreator Template
*
* @param name Template Name
* @param directory Template Directory
* @param build Build Options
* @param options Configuration Options
*/
public ServerTemplate(String name, boolean enabled, String icon, File directory, ObjectMap<String> build, ObjectMap<String> options) {
if (Util.isNull(name, enabled, directory, build, options)) throw new NullPointerException();
if (name.contains(" ")) throw new InvalidTemplateException("Template names cannot have spaces: " + name);
this.name = name;
this.enabled = enabled;
this.icon = icon;
this.directory = directory;
this.type = (build.contains("Server-Type"))?ServerType.valueOf(build.getRawString("Server-Type").toUpperCase()):ServerType.CUSTOM;
this.build = build;
this.options = options;
}
/**
* Get the Name of this Template
*
* @return Template Name
*/
public String getName() {
return name;
}
/**
* Get the Display Name of this Template
*
* @return Display Name
*/
public String getDisplayName() {
return (nick == null)?getName():nick;
}
/**
* Sets the Display Name for this Template
*
* @param value Value (or null to reset)
*/
public void setDisplayName(String value) {
if (value == null || value.length() == 0 || getName().equals(value)) {
this.nick = null;
} else {
this.nick = value;
}
}
/**
* Get the Enabled Status of this Template
*
* @return Enabled Status
*/
public boolean isEnabled() {
return enabled;
}
/**
* Set the Enabled Status of this Template
*
* @param value Value
*/
public void setEnabled(boolean value) {
enabled = value;
}
/**
* Get the Item Icon for this Template
*
* @return Item Icon Name/ID
*/
public String getIcon() {
return icon;
}
/**
* Set the Item Icon for this Template
*
* @param value Value
*/
public void setIcon(String value) {
icon = value;
}
/**
* Get the Directory for this Template
*
* @return Directory
*/
public File getDirectory() {
return directory;
}
/**
* Get the Type of this Template
*
* @return Template Type
*/
public ServerType getType() {
return type;
}
/**
* Get the Build Options for this Template
*
* @return Build Options
*/
public ObjectMap<String> getBuildOptions() {
return build;
}
/**
* Get the Configuration Options for this Template
*
* @return Configuration Options
*/
public ObjectMap<String> getConfigOptions() {
return options;
}
public ObjectMap<String> forSubData() {
ObjectMap<String> tinfo = new ObjectMap<String>();
tinfo.set("enabled", isEnabled());
tinfo.set("name", getName());
tinfo.set("display", getDisplayName());
tinfo.set("icon", getIcon());
tinfo.set("type", getType().toString());
return tinfo;
}
}
public enum ServerType {
SPIGOT,
VANILLA,
FORGE,
SPONGE,
CUSTOM;
@Override
public String toString() {
return super.toString().substring(0, 1).toUpperCase()+super.toString().substring(1).toLowerCase();
}
}
/**
* Create a SubServer
*
* @param player Player Creating
* @param name Server Name
* @param template Server Template
* @param version Server Version (may be null)
* @param port Server Port Number (null to auto-select)
* @return Success Status
*/
public abstract boolean create(UUID player, String name, ServerTemplate template, Version version, Integer port, Callback<SubServer> callback);
/**
* Create a SubServer
*
* @param player Player Creating
* @param name Server Name
* @param template Server Template
* @param version Server Version (may be null)
* @param port Server Port Number (null to auto-select)
* @return Success Status
*/
public boolean create(UUID player, String name, ServerTemplate template, Version version, Integer port) {
return create(player, name, template, version, port, null);
}
/**
* Create a SubServer
*
* @param name Server Name
* @param template Server Template
* @param version Server Version (may be null)
* @param port Server Port Number (null to auto-select)
* @return Success Status
*/
public boolean create(String name, ServerTemplate template, Version version, Integer port, Callback<SubServer> callback) {
return create(null, name, template, version, port, callback);
}
/**
* Create a SubServer
*
* @param name Server Name
* @param template Server Template
* @param version Server Version (may be null)
* @param port Server Port Number (null to auto-select)
* @return Success Status
*/
public boolean create(String name, ServerTemplate template, Version version, Integer port) {
return create(null, name, template, version, port);
}
/**
* Terminate All SubCreator Instances on this host
*/
public abstract void terminate();
/**
* Terminate a SubCreator Instance
*
* @param name Name of current creating server
*/
public abstract void terminate(String name);
/**
* Wait for All SubCreator Instances to Finish
*
* @throws InterruptedException
*/
public abstract void waitFor() throws InterruptedException;
/**
* Wait for SubCreator to Finish
*
* @param name Name of current creating server
* @throws InterruptedException
*/
public abstract void waitFor(String name) throws InterruptedException;
/**
* Gets the host this creator belongs to
*
* @return Host
*/
public abstract Host getHost();
/**
* Get the range of available port numbers
*
* @return Port Range
*/
public abstract Range getPortRange();
/**
* Get the range of available port numbers
*
* @param value Value
*/
public abstract void setPortRange(Range<Integer> value);
/**
* Gets the Git Bash install directory
*
* @return Git Bash Directory
*/
public abstract String getBashDirectory();
/**
* Gets all loggers for All SubCreator Instances
*
* @return SubCreator Loggers
*/
public abstract List<SubLogger> getLoggers();
/**
* Gets the Logger for a SubCreator Instance
*
* @param thread Thread ID
* @return SubCreator Logger
*/
public abstract SubLogger getLogger(String thread);
/**
* If the Creator is Logging to console
*
* @return Logging Status
*/
public abstract boolean isLogging();
/**
* Set if the Creator is Logging
*
* @param value Value
*/
public abstract void setLogging(boolean value);
/**
* Get a list of currently reserved Server names
*
* @return Reserved Names
*/
public abstract List<String> getReservedNames();
/**
* Get a list of currently reserved Server ports
*
* @return Reserved Ports
*/
public abstract List<Integer> getReservedPorts();
/**
* Check if a name has been reserved
*
* @param name Name to check
* @return Reserved Status
*/
public static boolean isReserved(String name) {
boolean reserved = false;
for (List<String> list : getAllReservedNames().values()) for (String reserve : list) {
if (reserve.equalsIgnoreCase(name)) reserved = true;
}
return reserved;
}
/**
* Check if an address has been reserved
*
* @param address Address to check
* @return Reserved Status
*/
public static boolean isReserved(InetSocketAddress address) {
boolean reserved = false;
for (InetSocketAddress list : getAllReservedAddresses()) {
if (list.equals(address)) reserved = true;
}
return reserved;
}
/**
* Get a list of all currently reserved Server names across all hosts
*
* @return All Reserved Names
*/
public static Map<Host, List<String>> getAllReservedNames() {
HashMap<Host, List<String>> names = new HashMap<Host, List<String>>();
for (Host host : SubAPI.getInstance().getHosts().values()) names.put(host, host.getCreator().getReservedNames());
return names;
}
/**
* Get a list of all currently reserved Server names across all hosts
*
* @return All Reserved Names
*/
public static List<InetSocketAddress> getAllReservedAddresses() {
List<InetSocketAddress> addresses = new ArrayList<InetSocketAddress>();
for (Server server : SubAPI.getInstance().getSubServers().values()) {
addresses.add(server.getAddress());
}
for (Host host : SubAPI.getInstance().getHosts().values())
for (int port : host.getCreator().getReservedPorts())
addresses.add(new InetSocketAddress(host.getAddress(), port));
return addresses;
}
/**
* Gets the Templates that can be used in this SubCreator instance
*
* @return Template Map
*/
public abstract Map<String, ServerTemplate> getTemplates();
/**
* Gets a SubCreator Template by name
*
* @param name Template Name
* @return Template
*/
public abstract ServerTemplate getTemplate(String name);
/**
* Reload SubCreator
*/
public abstract void reload();
}