-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSpiGUITest.java
461 lines (365 loc) · 19.5 KB
/
SpiGUITest.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
package com.samjakob.spiguitest;
import com.samjakob.spigui.menu.SGMenu;
import com.samjakob.spigui.SpiGUI;
import com.samjakob.spigui.buttons.SGButton;
import com.samjakob.spigui.item.ItemBuilder;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* SpiGUITest
* <p>
* Simple test plugin to showcase some of the functionality of SpiGUI.
* You can build this from the main repository with the 'testJar' Gradle task.
*
* @author SamJakob
* @version 1.3.0
*/
public class SpiGUITest extends JavaPlugin {
/*
Please feel free to use code from here. Though, do note that it is a very rough proof of concept intended to
showcase and test some of the functionality of SpiGUI.
*/
private static SpiGUI spiGUI;
// Start: variables for demonstration purposes.
private final Map<Player, Integer> gems = new HashMap<>();
// End: variables for demonstration purposes.
@Override
public void onEnable() {
spiGUI = new SpiGUI(this);
}
@Override
public void onDisable() {
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getLabel().equalsIgnoreCase("spigui")) {
if (!(sender instanceof Player)) {
sender.sendMessage("[SpiGUI] [ERROR] You must be a player to run this command.");
return true;
}
Player player = (Player) sender;
// START DEFAULT INVENTORY
// This is a menu intended to showcase general functionality.
if (args.length == 0) {
// Open a test SpiGUI menu.
SGMenu myAwesomeMenu = SpiGUITest.getSpiGUI().create("&c&lSpiGUI &c(Page {currentPage}/{maxPage})", 3);
myAwesomeMenu.setToolbarBuilder((slot, page, defaultType, menu) -> {
if (slot == 8) {
return new SGButton(
new ItemBuilder(Material.EMERALD)
.name(String.format("&a&l%d gems", gems.getOrDefault(player, 5)))
.lore(
"&aUse gems to buy cosmetics",
"&aand other items in the store!",
"",
"&7&o(Click to add more)"
)
.build()
).withListener((event) -> {
gems.put(player, gems.getOrDefault(player, 5) + 5);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a&l&oSUCCESS! &aYou have been given &25 &agems!"));
menu.refreshInventory(event.getWhoClicked());
});
}
// Fallback to rendering the default button for a slot.
return spiGUI.getDefaultToolbarBuilder().buildToolbarButton(slot, page, defaultType, menu);
// Or, alternatively, to render a button when NEITHER a custom per-inventory button OR a fallback
// button has been defined:
// (Comment above line and uncomment below to enable this)
/*
// Ensure fallbackButton is not null before rendering. If it is, render an alternative button
// instead.
SGButton fallbackButton = spiGUI.getDefaultToolbarBuilder().buildToolbarButton(slot, page, defaultType, menu);
if (fallbackButton != null) return fallbackButton;
return new SGButton(new ItemBuilder(Material.BARRIER).name(" ").build());
// You could check if defaultType is UNASSIGNED, however this won't deal with the cases when the
// previous or next button is not shown (there will be an empty space).
*/
});
myAwesomeMenu.setButton(0, 10, new SGButton(
new ItemBuilder(Material.SKULL_ITEM)
.skullOwner(player.getName())
.name("&e&l" + player.getDisplayName())
.lore(
"&eGame Mode: &6" + player.getGameMode().toString(),
"&eLocation: &6" + String.format(
"%.0f, %.0f, %.0f",
player.getLocation().getX(),
player.getLocation().getY(),
player.getLocation().getZ()
),
"&eExperience: &6" + player.getTotalExperience()
)
.build()
));
myAwesomeMenu.setButton(1, 0, new SGButton(
new ItemBuilder(Material.GOLD_ORE)
.name("&6Get rich quick!")
.build()
).withListener(event -> {
Inventory playerInventory = event.getWhoClicked().getInventory();
IntStream.range(0, 9).forEach(hotBarSlot -> playerInventory.setItem(
hotBarSlot, new ItemBuilder(
event.getCurrentItem().getType() == Material.GOLD_ORE
? Material.GOLD_BLOCK
: event.getCurrentItem().getType()
).amount(64).build()
));
event.getWhoClicked().sendMessage(
ChatColor.translateAlternateColorCodes('&',
event.getCurrentItem().getType() == Material.GOLD_ORE
? "&e&lYou are now rich!"
: "&7&lYou are now poor."
)
);
Material newMaterial = event.getCurrentItem().getType() == Material.GOLD_ORE
? Material.DIRT
: Material.GOLD_ORE;
myAwesomeMenu.getButton(1, 0).setIcon(
new ItemBuilder(newMaterial).name(
newMaterial == Material.GOLD_ORE ? "&6Get rich quick!" : "&7Get poor quick!"
).amount(1).build()
);
myAwesomeMenu.refreshInventory(event.getWhoClicked());
((Player) event.getWhoClicked()).updateInventory();
}));
AtomicReference<BukkitTask> borderRunnable = new AtomicReference<>();
myAwesomeMenu.setOnPageChange(inventory -> {
if (inventory.getCurrentPage() != 0) {
if (borderRunnable.get() != null) borderRunnable.get().cancel();
} else borderRunnable.set(
inventory.getCurrentPage() != 0
? null
: new BukkitRunnable(){
private final int[] TILES_TO_UPDATE = {
0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26
};
private short currentColor = 1;
@Override
public void run() {
IntStream.range(0, TILES_TO_UPDATE.length).map(i -> TILES_TO_UPDATE.length - i + -1).forEach(
index -> myAwesomeMenu.setButton(TILES_TO_UPDATE[index], nextColorButton())
);
currentColor++;
if (currentColor >= 15) currentColor = 0;
myAwesomeMenu.refreshInventory(player);
}
private SGButton nextColorButton() {
return new SGButton(
new ItemBuilder(Material.STAINED_GLASS_PANE)
.name("&" + Integer.toHexString(currentColor) + "&lSpiGUI!!!")
.data(currentColor)
.build()
);
}
}.runTaskTimer(this, 0L, 20L)
);
});
myAwesomeMenu.setOnClose(inventory -> {
if (borderRunnable.get() != null) borderRunnable.get().cancel();
});
myAwesomeMenu.getOnPageChange().accept(myAwesomeMenu);
player.openInventory(myAwesomeMenu.getInventory());
return true;
}
// END DEFAULT INVENTORY
// The following are additional menus intended to test specific functionality:
switch (args[0]) {
case "inventorySizeTest": {
int size;
if (args.length == 1) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&l&oERROR &cYou must specify an item count as an integer."));
return true;
}
try {
size = Integer.parseInt(args[1]);
} catch (NumberFormatException ex) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&l&oERROR &cThe item count must be a valid integer."));
return true;
}
// Create a menu with one row, so that pagination values are easy to calculate (each page is a
// multiple of 9, then the remainder can just be added to ensure the number of items match up).
SGMenu inventorySizeTest = SpiGUITest.getSpiGUI().create("Test Menu", 1);
IntStream.range(0, size).forEach(i -> inventorySizeTest.addButton(new SGButton(
new ItemBuilder(Material.GOLD_ORE).name(String.format("&6Item %d", i + 1))
.build()
)));
player.openInventory(inventorySizeTest.getInventory());
return true;
}
case "refreshTest": {
SGMenu refreshTestMenu = SpiGUITest.getSpiGUI().create("&bMatches", 1);
// Generate 3 to 8 random matches.
List<Match> matches = IntStream.range(0, ThreadLocalRandom.current().nextInt(5) + 3)
.mapToObj((i) -> Match.generateFakeMatch(true))
.collect(Collectors.toList());
for (int i = 0; i < matches.size(); i++) {
Match match = matches.get(i);
refreshTestMenu.setButton(i, new SGButton(new ItemBuilder(match.getKit().getIcon())
.name(match.getKit().getName())
.lore(
String.format("&a%s &evs. &a%s", match.getPlayerNames()[0], match.getPlayerNames()[1]),
String.format("&fTime: &b%s", match.getTime()),
"",
String.format("&fKit: &b%s", match.getKit().getName()),
String.format("&fArena: &b%s &7(%s)", match.getArena(), match.getKit().getName())
)
.build()));
}
// Start a refresh task for the menu.
AtomicReference<BukkitTask> refreshMatchesTask = new AtomicReference<>(new BukkitRunnable(){
@Override
public void run() {
for (int i = 0; i < matches.size(); i++) {
Match match = matches.get(i);
refreshTestMenu.setButton(i, new SGButton(new ItemBuilder(match.getKit().getIcon())
.flag(ItemFlag.HIDE_ATTRIBUTES)
.flag(ItemFlag.HIDE_DESTROYS)
.flag(ItemFlag.HIDE_PLACED_ON)
.flag(ItemFlag.HIDE_POTION_EFFECTS)
.name(match.getKit().getName())
.lore(
String.format("&a%s &evs. &a%s", match.getPlayerNames()[0], match.getPlayerNames()[1]),
String.format("&fTime: &b%s", match.getTime()),
"",
String.format("&fKit: &b%s", match.getKit().getName()),
String.format("&fArena: &b%s &7(%s)", match.getArena(), match.getKit().getName())
)
.build()));
}
refreshTestMenu.refreshInventory(player);
}
}.runTaskTimer(this, 0L, 20L));
// Cancel the refresh task when the inventory is closed.
refreshTestMenu.setOnClose(menu -> {
if (refreshMatchesTask.get() != null) refreshMatchesTask.get().cancel();
});
player.openInventory(refreshTestMenu.getInventory());
return true;
}
}
player.sendMessage("Unrecognized command.");
}
return false;
}
public static SpiGUI getSpiGUI() {
return spiGUI;
}
// The following is mock classes/data for the above test GUIs.
private static class Kit {
private final String name;
private final ItemStack icon;
public Kit(String name, ItemStack icon) {
this.name = name;
this.icon = icon;
}
public String getName() {
return this.name;
}
public ItemStack getIcon() {
return this.icon;
}
}
private static class Match {
private enum MatchState {
/** Waiting to start. */
WAITING,
/** Currently ongoing. */
ONGOING,
/** Ended. */
ENDED
}
// Begin mock data.
private static final String[] fakePlayerNames = {"MoreHaro", "Pixelle", "SpyPlenty", "Winlink", "Herobrine", "Notch", "Dinnerbone", "CinnamonTown", "TreeMushrooms"};
private static final Kit[] fakeKits = {
new Kit("Classic Battle", new ItemBuilder(Material.STONE_SWORD).name("&7Classic Battle").build()),
new Kit("OP Battle", new ItemBuilder(Material.DIAMOND_SWORD).name("&bOP Battle").build()),
new Kit("Classic UHC", new ItemBuilder(Material.GOLDEN_APPLE).name("&eClassic UHC").build()),
new Kit("OP UHC", new ItemBuilder(Material.GOLDEN_APPLE).data((short) 1).name("&6OP UHC").build()),
};
private static final String[] fakeArenas = {"King's Road", "Ilios", "Fort Starr", "The Hopper"};
/** Generates a Match with fake data. */
public static Match generateFakeMatch() { return generateFakeMatch(false); }
public static Match generateFakeMatch(boolean alreadyStarted) {
// Ensure unique values are generated for player1 and player2.
int player1 = ThreadLocalRandom.current().nextInt(fakePlayerNames.length);
int player2;
do {
player2 = ThreadLocalRandom.current().nextInt(fakePlayerNames.length);
} while (player2 == player1);
Match fakeMatch = new Match(
new String[]{fakePlayerNames[player1], fakePlayerNames[player2]},
fakeKits[ThreadLocalRandom.current().nextInt(fakeKits.length)],
fakeArenas[ThreadLocalRandom.current().nextInt(fakeArenas.length)]
);
if (alreadyStarted) {
// If alreadyStarted specified to true, then generate a match with current time minus up to 5 minutes.
fakeMatch.matchStartTime = System.currentTimeMillis()
- ThreadLocalRandom.current().nextLong(5 * 60000);
}
return fakeMatch;
}
// End mock data.
/** List of players in match. Two players implies a duel. */
private final String[] playerNames;
public String[] getPlayerNames() { return playerNames; }
/** Match start time in UNIX milliseconds. */
private Long matchStartTime;
/** Match end time in UNIX milliseconds. */
private Long matchEndTime;
/** Name of the kit used for the duel. */
private final Kit kit;
public Kit getKit() { return kit; }
/** Name of the arena used for the duel. */
private final String arena;
public String getArena() { return arena; }
public String getTime() {
switch (getState()) {
case WAITING: return "Waiting...";
case ONGOING:
case ENDED: {
long duration = (matchEndTime != null ? matchEndTime : System.currentTimeMillis()) - matchStartTime;
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(minutes);
return String.format("%02d:%02d", minutes, seconds);
}
}
return "ERROR";
}
public Match(String[] playerNames, Kit kit, String arena) {
this.playerNames = playerNames;
this.kit = kit;
this.arena = arena;
}
public void start() {
if (this.matchStartTime != null) throw new IllegalStateException("Match already started!");
this.matchStartTime = System.currentTimeMillis();
}
public void stop() {
if (this.matchEndTime != null) throw new IllegalStateException("Match already finished!");
this.matchEndTime = System.currentTimeMillis();
}
public MatchState getState() {
if (this.matchStartTime == null) return MatchState.WAITING;
else if (this.matchEndTime == null) return MatchState.ONGOING;
return MatchState.ENDED;
}
}
}