/
SubCreator.java
540 lines (483 loc) · 15.3 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package net.ME1312.SubServers.Bungee.Host;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Library.Version.Version;
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidTemplateException;
import net.ME1312.SubServers.Bungee.SubAPI;
import com.google.common.collect.Range;
import java.io.File;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.function.Consumer;
/**
* SubCreator Layout Class
*/
public abstract class SubCreator {
public static class ServerTemplate {
private final boolean dynamic;
private String name;
private String nick = null;
private boolean enabled;
private boolean internal;
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 enabled Template Enabled Status
* @param icon Template Item Icon 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) {
this(name, enabled, false, icon, directory, build, options, true);
}
private ServerTemplate(String name, boolean enabled, boolean internal, String icon, File directory, ObjectMap<String> build, ObjectMap<String> options, boolean dynamic) {
Util.nullpo(name, enabled, directory, build, options);
if (name.contains(" ")) throw new InvalidTemplateException("Template names cannot have spaces: " + name);
this.name = name;
this.enabled = enabled;
this.internal = internal;
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;
this.dynamic = dynamic;
}
/**
* 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 if this Template is for Internal use only
*
* @return Internal Status
*/
public boolean isInternal() {
return internal;
}
/**
* 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 whether this Template requires the Version argument
*
* @return Version Requirement
*/
public boolean requiresVersion() {
return getBuildOptions().getBoolean("Require-Version", false);
}
/**
* Get whether this Template can be used to update it's servers
*
* @return Updatable Status
*/
public boolean canUpdate() {
return getBuildOptions().getBoolean("Can-Update", false);
}
/**
* Get whether this Template was generated by a SubCreator instance
*
* @return Dynamic Status
*/
public boolean isDynamic() {
return dynamic;
}
/**
* 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());
tinfo.set("version-req", requiresVersion());
tinfo.set("can-update", canUpdate());
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)
* @param callback Callback
* @return Success Status
*/
public abstract boolean create(UUID player, String name, ServerTemplate template, Version version, Integer port, Consumer<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)
* @param callback Callback
* @return Success Status
*/
public boolean create(String name, ServerTemplate template, Version version, Integer port, Consumer<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);
}
/**
* Update a SubServer
*
* @param player Player Updating
* @param server Server to Update
* @param template Server Template
* @param version Server Version (may be null)
* @param callback Callback
* @return Success Status
*/
public abstract boolean update(UUID player, SubServer server, ServerTemplate template, Version version, Consumer<Boolean> callback);
/**
* Update a SubServer
*
* @param player Player Updating
* @param server Server to Update
* @param template Server Template
* @param version Server Version (may be null)
* @return Success Status
*/
public boolean update(UUID player, SubServer server, ServerTemplate template, Version version) {
return update(player, server, template, version, null);
}
/**
* Update a SubServer
*
* @param server Server to Update
* @param template Server Template
* @param version Server Version (may be null)
* @param callback Callback
* @return Success Status
*/
public boolean update(SubServer server, ServerTemplate template, Version version, Consumer<Boolean> callback) {
return update(null, server, template, version, callback);
}
/**
* Update a SubServer
*
* @param server Server to Update
* @param template Server Template
* @param version Server Version (may be null)
* @return Success Status
*/
public boolean update(SubServer server, ServerTemplate template, Version version) {
return update(null, server, template, version);
}
/**
* Update a SubServer
*
* @param player Player Updating
* @param server Server to Update
* @param version Server Version (may be null)
* @return Success Status
*/
public boolean update(UUID player, SubServer server, Version version) {
return update(player, server, null, version);
}
/**
* Update a SubServer
*
* @param server Server to Update
* @param version Server Version (may be null)
* @return Success Status
*/
public boolean update(SubServer server, Version version) {
return update(null, server, version);
}
/**
* 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);
/**
* Create a SubCreator Template
*
* @param name Template Name
* @param enabled Template Enabled Status
* @param internal Template Internal Status
* @param icon Template Item Icon Name
* @param directory Template Directory
* @param build Build Options
* @param options Configuration Options
*/
protected ServerTemplate loadTemplate(String name, boolean enabled, boolean internal, String icon, File directory, ObjectMap<String> build, ObjectMap<String> options) {
return new ServerTemplate(name, enabled, internal, icon, directory, build, options, false);
}
/**
* Reload SubCreator
*/
public abstract void reload();
}