/
UIRenderer.java
527 lines (475 loc) · 17.1 KB
/
UIRenderer.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
package net.ME1312.SubServers.Client.Bukkit.Graphic;
import net.ME1312.Galaxi.Library.Container.ContainedPair;
import net.ME1312.Galaxi.Library.Container.Container;
import net.ME1312.Galaxi.Library.Container.Pair;
import net.ME1312.Galaxi.Library.Container.Value;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Library.Version.Version;
import net.ME1312.SubServers.Client.Bukkit.SubPlugin;
import net.ME1312.SubServers.Client.Common.Network.API.Host;
import net.ME1312.SubServers.Client.Common.Network.API.SubServer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* GUI Renderer Layout Class
*/
public abstract class UIRenderer {
static HashMap<String, PluginRenderer<Host>> hostPlugins = new HashMap<String, PluginRenderer<Host>>();
static HashMap<String, PluginRenderer<SubServer>> subserverPlugins = new HashMap<String, PluginRenderer<SubServer>>();
private Pair<String, Integer> tdownload = null;
private int download = -1;
private final UUID player;
private SubPlugin plugin;
/**
* Creates a new UIRenderer
*
* @param plugin SubPlugin
* @param player Player
*/
public UIRenderer(SubPlugin plugin, UUID player) {
if (Util.isNull(plugin, player)) throw new NullPointerException();
this.plugin = plugin;
this.player = player;
}
/**
* Opens a new window
*/
public abstract void newUI();
/**
* Clears the Window History
*/
public abstract void clearHistory();
/**
* If there is any Window History
*
* @return Window History Status
*/
public abstract boolean hasHistory();
/**
* Reopens the current window
*/
public abstract void reopen();
/**
* Reopens the previous window
*/
public abstract void back();
/**
* Attempt to send a Title Message
*
* @param str Message
* @return Success Status
*/
public boolean sendTitle(String str) {
return sendTitle(str, -1);
}
/**
* Attempt to send a Title Message
*
* @param str Message
* @param stay How long the message should stay
* @return Success Status
*/
public boolean sendTitle(String str, int stay) {
return sendTitle(str, -1, stay, -1);
}
/**
* Attempt to send a Title Message
*
* @param str Message
* @param fadein FadeIn Transition length (in ticks)
* @param stay How long the message should stay (in ticks)
* @param fadeout FadeOut Transition length (in ticks)
* @return Success Status
*/
public boolean sendTitle(String str, int fadein, int stay, int fadeout) {
if (Util.isNull(str, fadein, stay, fadeout)) throw new NullPointerException();
if (plugin.config.get().getMap("Settings").getBoolean("Use-Title-Messages", true)) {
String line1, line2;
if (!str.startsWith("\n") && str.contains("\n")) {
line1 = str.split("\\n")[0];
line2 = str.split("\\n")[1];
} else {
line1 = str.replace("\n", "");
line2 = ChatColor.RESET.toString();
}
try {
Player player = Bukkit.getPlayer(this.player);
if (plugin.api.getGameVersion().compareTo(new Version("1.11")) >= 0) {
if (ChatColor.stripColor(line1).length() == 0 && ChatColor.stripColor(line2).length() == 0) {
player.resetTitle();
} else {
player.sendTitle(line1, line2, (fadein >= 0)?fadein:10, (stay >= 0)?stay:70, (fadeout >= 0)?fadeout:20);
}
return true;
} else if (Bukkit.getPluginManager().getPlugin("TitleManager") != null) {
if (Util.isException(() -> Util.reflect(Class.forName("io.puharesource.mc.titlemanager.api.v2.TitleManagerAPI").getMethod("sendTitles", Player.class, String.class, String.class, int.class, int.class, int.class),
Bukkit.getPluginManager().getPlugin("TitleManager"), player, line1, line2, (fadein >= 0)?fadein:10, (stay >= 0)?stay:70, (fadeout >= 0)?fadeout:20))) { // Attempt TitleAPI v2
// Fallback to TitleAPI v1
io.puharesource.mc.titlemanager.api.TitleObject obj = io.puharesource.mc.titlemanager.api.TitleObject.class.getConstructor(String.class, String.class).newInstance(line1, line2);
if (fadein >= 0) obj.setFadeIn(fadein);
if (stay >= 0) obj.setStay(stay);
if (fadeout >= 0) obj.setFadeOut(fadeout);
obj.send(player);
}
return true;
} else return false;
} catch (Throwable e) {
return false;
}
} else return false;
}
/**
* Shows/Hides the Downloading Title Message
*
* @param subtitle Subtitle to display (or null to hide)
*/
public void setDownloading(String subtitle) {
if (subtitle != null && !(plugin.config.get().getMap("Settings").getBoolean("Use-Title-Messages", true) && (plugin.api.getGameVersion().compareTo(new Version("1.11")) >= 0 || Bukkit.getPluginManager().getPlugin("TitleManager") != null))) {
if (download != -1) Bukkit.getScheduler().cancelTask(download);
download = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
if (tdownload != null) Bukkit.getPlayer(player).sendMessage(plugin.api.getLang("SubServers", "Interface.Generic.Downloading").replace("$str$", subtitle));
download = -1;
}, 50L);
} if (subtitle != null && tdownload == null) {
tdownload = new ContainedPair<String, Integer>(subtitle, 0);
final Value<Integer> delay = new Container<Integer>(0);
Bukkit.getScheduler().runTask(plugin, new Runnable() {
@Override
public void run() {
if (tdownload != null) {
String word = ChatColor.stripColor(plugin.api.getLang("SubServers", "Interface.Generic.Downloading.Title"));
int i = 0;
int start = (tdownload.value() - 3 < 0)?0: tdownload.value()-3;
int end = (tdownload.value() >= word.length())?word.length(): tdownload.value();
String str = plugin.api.getLang("SubServers", (delay.value() > 7 && start == 0)?"Interface.Generic.Downloading.Title-Color-Alt":"Interface.Generic.Downloading.Title-Color");
delay.value(delay.value() + 1);
if (delay.value() > 7) tdownload.value(tdownload.value() + 1);
if (tdownload.value() >= word.length() + 3) {
tdownload.value(0);
delay.value(0);
}
for (char c : word.toCharArray()) {
i++;
if (i == start) str += plugin.api.getLang("SubServers", "Interface.Generic.Downloading.Title-Color-Alt");
str += c;
if (i == end) str += plugin.api.getLang("SubServers", "Interface.Generic.Downloading.Title-Color");
}
str += '\n' + plugin.api.getLang("SubServers", "Interface.Generic.Downloading.Title-Color-Alt") + tdownload.key();
sendTitle(str, 0, 10, 5);
Bukkit.getScheduler().runTaskLater(plugin, this, 1);
} else {
sendTitle(ChatColor.RESET.toString(), 0, 1, 0);
}
}
});
} else if (subtitle != null) {
tdownload.key(subtitle);
} else {
if (tdownload != null) {
tdownload = null;
}
if (download != -1) {
Bukkit.getScheduler().cancelTask(download);
download = -1;
}
}
}
/**
* Parse an ItemStack from a String
*
* @param str String to parse
* @return ItemStack
*/
public ItemStack parseItem(String str) {
return parseItem(str, new ItemStack(Material.AIR));
}
/**
* Parse an ItemStack from a String
*
* @param str String to parse
* @param def Default to return if unable to parse
* @return ItemStack
*/
public ItemStack parseItem(String str, ItemStack def) {
final Value<String> item = new Container<String>(str);
if (plugin.api.getGameVersion().compareTo(new Version("1.13")) < 0) {
try {
// int
Matcher matcher = Pattern.compile("(?i)^(\\d+)$").matcher(item.value());
if (matcher.find()) {
return ItemStack.class.getConstructor(int.class, int.class).newInstance(Integer.parseInt(matcher.group(1)), 1);
}
// int:int
matcher.reset();
matcher = Pattern.compile("(?i)^(\\d+):(\\d+)$").matcher(item.value());
if (matcher.find()) {
return ItemStack.class.getConstructor(int.class, int.class, short.class).newInstance(Integer.parseInt(matcher.group(1)), 1, Short.parseShort(matcher.group(2)));
}
} catch (Exception e) {
return def;
}
}
// minecraft:name
if (item.value().toLowerCase().startsWith("minecraft:")) {
item.value(item.value().substring(10));
} else
// bukkit:name
if (item.value().toLowerCase().startsWith("bukkit:")) {
item.value(item.value().substring(7));
if (!Util.isException(() -> Material.valueOf(item.value().toUpperCase()))) {
return new ItemStack(Material.valueOf(item.value().toUpperCase()), 1);
}
}
// material name
if (plugin.api.getGameVersion().compareTo(new Version("1.13")) < 0) {
if (!Util.isException(() -> Material.valueOf(item.value().toUpperCase()))) {
return new ItemStack(Material.valueOf(item.value().toUpperCase()), 1);
}
} else try {
if (Material.class.getMethod("getMaterial", String.class, boolean.class).invoke(null, item.value().toUpperCase(), false) != null) {
return new ItemStack((Material) Material.class.getMethod("getMaterial", String.class, boolean.class).invoke(null, item.value().toUpperCase(), false), 1);
}
} catch (Exception e) {}
return def;
}
/**
* Add Host Plugin
*
* @param handle Handle to bind
* @param renderer Renderer
*/
public static void addHostPlugin(String handle, PluginRenderer<Host> renderer) {
if (Util.isNull(handle, renderer)) throw new NullPointerException();
hostPlugins.put(handle, renderer);
}
/**
* Get Host Plugins
*
* @return Host Plugins
*/
public static Map<String, PluginRenderer> getHostPlugins() {
return new HashMap<String, PluginRenderer>(hostPlugins);
}
/**
* Remove Host Plugin
*
* @param handle Handle
*/
public static void removeHostPlugin(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
hostPlugins.remove(handle);
}
/**
* Add SubServer Plugin
*
* @param handle Handle to bind
* @param renderer Renderer
*/
public static void addSubServerPlugin(String handle, PluginRenderer<SubServer> renderer) {
if (Util.isNull(handle, renderer)) throw new NullPointerException();
subserverPlugins.put(handle, renderer);
}
/**
* Get SubServer Plugins
*
* @return SubServer Plugins
*/
public static Map<String, PluginRenderer> getSubServerPlugins() {
return new HashMap<String, PluginRenderer>(subserverPlugins);
}
/**
* Remove SubServer Plugin
*
* @param handle Handle
*/
public static void removeSubServerPlugin(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
subserverPlugins.remove(handle);
}
/**
* Opens the Host Menu
*
* @param page Page Number (starting from page 1)
*/
public abstract void hostMenu(int page);
/**
* Opens Host/<name>
*
* @param host Host Name
*/
public abstract void hostAdmin(String host);
/**
* Opens Host/<name>/Create
*
* @param options Creator Options
*/
public abstract void hostCreator(CreatorOptions options);
/**
* Opens Host/<name>/Plugins
*
* @param host Host Name
*/
public abstract void hostPlugin(int page, String host);
/**
* Opens the Group Menu
*
* @param page Page Number (starting from page 1)
*/
public abstract void groupMenu(int page);
/**
* Opens the SubServer Menu
*
* @param page Page Number (starting from page 1)
* @param host Host Name (or null to scan all hosts)
*/
public abstract void serverMenu(int page, String host, String group);
/**
* Opens SubServer/<name>
*
* @param server SubServer Name
*/
public abstract void subserverAdmin(String server);
/**
* Opens SubServer/<name>/Plugins
*
* @param server SubServer Name
*/
public abstract void subserverPlugin(int page, String server);
/**
* Options Layout Class
*/
public abstract static class Options {
List<Runnable> history = new LinkedList<Runnable>();
private boolean init = false;
public boolean init() {
if (!init) {
init = true;
return false;
} else {
return true;
}
}
/**
* If there is any undo history
*
* @return Undo History Status
*/
public boolean hasHistory() {
return !history.isEmpty();
}
/**
* Reverts the last change
*/
public void undo() {
Runnable lastWindow = history.get(history.size() - 1);
history.remove(history.size() - 1);
lastWindow.run();
}
}
/**
* SubCreator Options Class
*/
public static class CreatorOptions extends Options {
private String host;
private String name = null;
private String template = null;
private Version version = null;
private Integer port = null;
/**
* Grabs a raw CreatorOptions instance
*
* @param host Host Name
*/
public CreatorOptions(String host) {
this.host = host;
}
/**
* Gets the Host Name
*
* @return Host Name
*/
public String getHost() {
return this.host;
}
/**
* Gets the Server Name
*
* @return Server Name
*/
public String getName() {
return name;
}
/**
* Sets the Server Name
*
* @param value Value
*/
public void setName(String value) {
if (Util.isNull(value)) throw new NullPointerException();
final String name = this.name;
history.add(() -> this.name = name);
this.name = value;
}
/**
* Gets the Template
*
* @return Template
*/
public String getTemplate() {
return template;
}
/**
* Sets the Template
*
* @param value Value
*/
public void setTemplate(String value) {
if (Util.isNull(value)) throw new NullPointerException();
final String template = this.template;
history.add(() -> this.template = template);
this.template = value;
}
/**
* Gets the Server Version
*
* @return Server Version
*/
public Version getVersion() {
return version;
}
/**
* Sets the Server Version
*
* @param value Value
*/
public void setVersion(Version value) {
final Version version = this.version;
history.add(() -> this.version = version);
this.version = value;
}
/**
* Gets the Port Number for the Server
*
* @return Server Port Number (null for auto-select)
*/
public Integer getPort() {
return port;
}
/**
* Sets the Port Number for the Server
*
* @param value Value (null for auto-select)
*/
public void setPort(Integer value) {
final Integer port = this.port;
history.add(() -> this.port = port);
this.port = value;
}
}
}