-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShopModule.java
More file actions
784 lines (722 loc) · 37 KB
/
Copy pathShopModule.java
File metadata and controls
784 lines (722 loc) · 37 KB
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
package dev.vupe.core.module.impl;
import dev.vupe.core.VupeCore;
import dev.vupe.core.data.PlayerData;
import dev.vupe.core.module.VupeModule;
import dev.vupe.core.util.Items;
import dev.vupe.core.util.Text;
import org.bukkit.*;
import org.bukkit.block.BlockState;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.*;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.*;
import java.util.*;
public final class ShopModule extends VupeModule {
private final Map<UUID, String> openMenus = new HashMap<>();
public ShopModule(VupeCore plugin) {
super(plugin, "shop");
}
@Override
protected void onEnable() {
plugin.commands().register("sell", this::sellCommand);
plugin.commands().register("autosell", this::autosellCommand);
plugin.commands().register("boosters", this::boostersCommand);
plugin.commands().register("box", this::boxCommand);
plugin.commands().register("sellwandgive", this::sellwandCompatibility);
}
public double effectiveSellMultiplier(Player player) {
return effectiveSellMultiplier(player.getUniqueId());
}
public double effectiveSellMultiplier(UUID uuid) {
PlayerData data = plugin.data().player(uuid);
double booster = data.sellBoosterUntil > System.currentTimeMillis()
? Math.max(1.0, data.sellBoosterMultiplier) : 1.0;
if (data.sellBoosterUntil <= System.currentTimeMillis() && data.sellBoosterMultiplier != 1.0) {
data.sellBoosterMultiplier = 1.0;
data.sellBoosterUntil = 0;
plugin.data().markDirty(uuid);
}
double global = plugin.data().server().globalSellBoosterUntil > System.currentTimeMillis()
? Math.max(1.0, plugin.data().server().globalSellBoosterMultiplier) : 1.0;
double base = plugin.configs().get("economy").getDouble("sell.multiplier-base", 1.0);
return Math.max(0, base + data.sellMultiplierBonus) * booster * global;
}
public double sellInventory(Player player, Inventory inventory) {
double base = 0;
for (int slot = 0; slot < inventory.getSize(); slot++) {
ItemStack item = inventory.getItem(slot);
if (item == null || item.getType().isAir()) continue;
double each = sellValue(item);
if (each <= 0) continue;
base += each * item.getAmount();
inventory.setItem(slot, null);
}
PlayerData data = plugin.data().player(player.getUniqueId());
for (Map.Entry<String, Integer> entry : new HashMap<>(data.virtualGeneratorStorage).entrySet()) {
GeneratorModule.GeneratorType type = plugin.modules().generators().type(entry.getKey());
if (type == null || entry.getValue() <= 0) continue;
base += type.sell() * entry.getValue();
data.virtualGeneratorStorage.remove(entry.getKey());
}
if (base <= 0) return 0;
double finalValue = Math.round(base * effectiveSellMultiplier(player) * 100.0) / 100.0;
plugin.modules().economy().addMoney(player.getUniqueId(), finalValue);
plugin.modules().events().progress(player, "sell", finalValue);
plugin.data().markDirty(player.getUniqueId());
return finalValue;
}
public double sellValue(ItemStack item) {
String compressed = Items.tag(item, "compressed_generator_drop");
if (compressed != null) {
String[] parts = compressed.split(":", 2);
if (parts.length == 2) {
GeneratorModule.GeneratorType type = plugin.modules().generators().type(parts[0]);
try {
int count = Integer.parseInt(parts[1]);
if (type != null && count > 0) return type.sell() * count;
} catch (NumberFormatException ignored) {}
}
}
String genDrop = Items.tag(item, "generator_drop_type");
if (genDrop != null) {
GeneratorModule.GeneratorType type = plugin.modules().generators().type(genDrop);
if (type != null) return type.sell();
}
return plugin.configs().get("economy").getDouble("sell.inventory." + item.getType().name(), 0);
}
private boolean sellwandCompatibility(CommandSender sender, String label, String[] args) {
if (!sender.hasPermission("vupe.admin")) {
Text.send(sender, "<red>No permission.");
return true;
}
if (args.length < 2) {
Text.send(sender, "<red>Usage: /sellwandgive <player> <multiplier>");
return true;
}
Player target = Bukkit.getPlayerExact(args[0]);
if (target == null) {
Text.send(sender, "<red>Player not found.");
return true;
}
double multiplier;
try { multiplier = Math.max(1, Double.parseDouble(args[1])); }
catch (NumberFormatException ex) { Text.send(sender, "<red>Invalid multiplier."); return true; }
target.getInventory().addItem(Items.tagged(Material.BLAZE_ROD,
"<gold><bold>" + Text.format(multiplier) + "x Sellwand</bold>",
List.of("<gray>Right-click a container to sell its contents."),
"sellwand", Double.toString(multiplier)));
Text.send(sender, "<green>Sellwand given.");
return true;
}
private boolean autosellCommand(CommandSender sender, String label, String[] args) {
if (!(sender instanceof Player player)) {
Text.send(sender, "<red>Player-only.");
return true;
}
if (!plugin.configs().modules().getBoolean("modules.autosell", true)
|| !plugin.configs().get("shops").getBoolean("autosell.enabled", true)) {
Text.send(player, "<red>Autosell is disabled.");
return true;
}
String permission = plugin.configs().get("shops").getString("autosell.permission", "");
if (permission != null && !permission.isBlank()
&& !player.hasPermission(permission) && !player.hasPermission("vupe.admin")) {
Text.send(player, "<red>You do not have access to autosell.");
return true;
}
PlayerData data = plugin.data().player(player.getUniqueId());
data.autosellEnabled = !data.autosellEnabled;
plugin.data().markDirty(player.getUniqueId());
Text.send(player, "<gray>Generator autosell: " + (data.autosellEnabled ? "<green>enabled" : "<red>disabled"));
return true;
}
private boolean boostersCommand(CommandSender sender, String label, String[] args) {
if (!(sender instanceof Player player)) {
Text.send(sender, "<red>Player-only.");
return true;
}
PlayerData data = plugin.data().player(player.getUniqueId());
long now = System.currentTimeMillis();
Text.raw(player, "<#8B5CF6><bold>VUPE BOOSTERS</bold>");
Text.raw(player, "<gray>Sell booster: <white>" +
(data.sellBoosterUntil > now ? Text.format(data.sellBoosterMultiplier) + "x (" +
dev.vupe.core.util.TimeUtil.pretty(data.sellBoosterUntil - now) + ")" : "None"));
Text.raw(player, "<gray>Crystal booster: <white>" +
(data.crystalBoosterUntil > now ? Text.format(data.crystalBoosterMultiplier) + "x (" +
dev.vupe.core.util.TimeUtil.pretty(data.crystalBoosterUntil - now) + ")" : "None"));
return true;
}
private boolean sellCommand(CommandSender sender, String label, String[] args) {
if (!(sender instanceof Player player)) {
Text.send(sender, "<red>Player-only.");
return true;
}
double value = sellInventory(player, player.getInventory());
if (value <= 0) Text.send(player, "<red>You have nothing sellable.");
else Text.send(player, "<gray>Sold items for <green>$" + Text.format(value) + "<gray>.");
return true;
}
private boolean shopCommand(CommandSender sender, String label, String[] args) {
if (!(sender instanceof Player player)) {
Text.send(sender, "<red>Player-only.");
return true;
}
if (args.length >= 1 && args[0].equalsIgnoreCase("admin")) {
return handleAdmin(player, Arrays.copyOfRange(args, 1, args.length));
}
openGeneratorShop(player);
return true;
}
private void openGeneratorShop(Player player) {
int size = 54;
Inventory inventory = Bukkit.createInventory(null, size, Text.component(
plugin.configs().get("shops").getString("shop.title", "<#8B5CF6><bold>VUPE SHOP</bold>")
));
int slot = 0;
for (GeneratorModule.GeneratorType type : plugin.modules().generators().types()) {
if (slot >= 45) break;
double price = Math.max(1, type.upgrade() > 0 ? type.upgrade() : type.sell() * 100);
ItemStack button = Items.tagged(
type.block(),
"<" + type.color() + "><bold>" + type.display() + " Core</bold>",
List.of(
"<gray>Buy price: <green>$" + Text.format(price),
"<gray>Drop value: <green>$" + Text.format(type.sell()),
"",
"<yellow>Click to buy 1."
),
"shop_generator",
type.id()
);
inventory.setItem(slot++, button);
}
inventory.setItem(49, Items.item(Material.EMERALD, "<green><bold>SELL INVENTORY</bold>", List.of(
"<gray>Current multiplier: <white>" + Text.format(effectiveSellMultiplier(player)) + "x",
"",
"<yellow>Click to sell all."
)));
inventory.setItem(53, Items.item(Material.NETHER_STAR, "<#8B5CF6><bold>GOLD STORE</bold>", List.of("<yellow>Click to browse.")));
openMenus.put(player.getUniqueId(), "shop");
player.openInventory(inventory);
}
private boolean storeCommand(CommandSender sender, String label, String[] args) {
if (!plugin.configs().modules().getBoolean("modules.gold-store", true)) {
Text.send(sender, "<red>Gold store is disabled."); return true;
}
if (!(sender instanceof Player player)) {
Text.send(sender, "<red>Player-only.");
return true;
}
openStore(player);
return true;
}
private void openStore(Player player) {
Inventory inv = Bukkit.createInventory(null, 27, Text.component("<gradient:#8B5CF6:#F472B6><bold>VUPE STORE</bold></gradient>"));
PlayerData data = plugin.data().player(player.getUniqueId());
int slot = 9;
ConfigurationSection ranks = plugin.configs().get("shops").getConfigurationSection("gold-store.ranks");
if (ranks != null) {
for (String id : ranks.getKeys(false)) {
if (slot > 13) break;
long price = ranks.getLong(id);
inv.setItem(slot++, Items.tagged(Material.NETHER_STAR,
plugin.modules().progression().donorDisplay(id),
List.of("<gray>Price: <gold>" + price + " Gold", "", "<yellow>Click to buy."),
"store_rank", id));
}
}
inv.setItem(16, Items.item(Material.GOLD_INGOT, "<gold><bold>YOUR GOLD</bold>", List.of(
"<gray>Balance: <gold>" + data.gold,
"<gray>Store URL: <white>" + plugin.configs().get("branding").getString("brand.website", "CHANGE_ME_STORE_URL")
)));
inv.setItem(22, Items.item(Material.TRIPWIRE_HOOK, "<#22D3EE><bold>CRATE KEYS</bold>", List.of("<gray>Use <white>/crates<gray> to view your keys.")));
openMenus.put(player.getUniqueId(), "store");
player.openInventory(inv);
}
@EventHandler(ignoreCancelled = true)
public void onMenuClick(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player player)) return;
String menu = openMenus.get(player.getUniqueId());
if (menu == null) return;
event.setCancelled(true);
if (event.getClickedInventory() == null || event.getRawSlot() < 0) return;
ItemStack clicked = event.getCurrentItem();
if (clicked == null) return;
if (menu.equals("shop")) {
String typeId = Items.tag(clicked, "shop_generator");
if (typeId != null) {
GeneratorModule.GeneratorType type = plugin.modules().generators().type(typeId);
if (type == null) return;
double price = Math.max(1, type.upgrade() > 0 ? type.upgrade() : type.sell() * 100);
if (!plugin.modules().economy().takeMoney(player.getUniqueId(), price)) {
Text.send(player, "<red>You need <green>$" + Text.format(price) + "<red>.");
return;
}
plugin.modules().generators().give(player, type.id(), 1);
Text.send(player, "<gray>Bought one <" + type.color() + ">" + type.display() + " Core<gray>.");
return;
}
if (event.getRawSlot() == 49) {
double sold = sellInventory(player, player.getInventory());
Text.send(player, sold > 0 ? "<gray>Sold for <green>$" + Text.format(sold) : "<red>Nothing sellable.");
return;
}
if (event.getRawSlot() == 53) {
openStore(player);
}
} else if (menu.equals("crystalshop")) {
String offer = Items.tag(clicked, "crystal_offer");
if (offer != null) {
buyCrystalOffer(player, offer);
crystalShopCommand(player, "crystalshop", new String[0]);
}
} else if (menu.equals("store")) {
String rank = Items.tag(clicked, "store_rank");
if (rank != null) {
long price = plugin.configs().get("shops").getLong("gold-store.ranks." + rank, -1);
if (price <= 0) return;
if (!plugin.modules().economy().takeGold(player.getUniqueId(), price)) {
Text.send(player, "<red>You need <gold>" + price + " Gold<red>.");
return;
}
plugin.modules().progression().setDonorRank(player, rank);
Text.send(player, "<gray>You unlocked " + plugin.modules().progression().donorDisplay(rank) + "<gray>.");
openStore(player);
}
}
}
@EventHandler
public void onMenuClose(InventoryCloseEvent event) {
openMenus.remove(event.getPlayer().getUniqueId());
}
@EventHandler(ignoreCancelled = true)
public void onSpecialItemUse(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
ItemStack item = event.getItem();
if (item == null) return;
String simpleBox = Items.tag(item, "simple_box");
if (simpleBox != null && plugin.configs().modules().getBoolean("modules.boxes", true)) {
event.setCancelled(true);
consumeOne(item);
openSimpleBox(event.getPlayer(), simpleBox);
return;
}
String lootbox = Items.tag(item, "lootbox");
if (lootbox != null && plugin.configs().modules().getBoolean("modules.boxes", true)) {
event.setCancelled(true);
consumeOne(item);
openLootbox(event.getPlayer(), lootbox);
return;
}
String voucher = Items.tag(item, "voucher");
if (voucher != null && plugin.configs().modules().getBoolean("modules.vouchers", true)) {
event.setCancelled(true);
redeemVoucher(event.getPlayer(), item, voucher);
return;
}
String box = Items.tag(item, "box_crate");
if (box != null) {
event.setCancelled(true);
consumeOne(item);
plugin.modules().crates().openFree(event.getPlayer(), box);
return;
}
String wand = Items.tag(item, "sellwand");
if (wand != null && plugin.configs().modules().getBoolean("modules.sellwands", true) && event.getClickedBlock() != null) {
BlockState state = event.getClickedBlock().getState();
if (!(state instanceof InventoryHolder holder)) return;
event.setCancelled(true);
double multiplier;
try { multiplier = Double.parseDouble(wand); }
catch (NumberFormatException ex) { multiplier = 1; }
double base = 0;
Inventory inventory = holder.getInventory();
for (int i = 0; i < inventory.getSize(); i++) {
ItemStack stack = inventory.getItem(i);
if (stack == null) continue;
double each = sellValue(stack);
if (each <= 0) continue;
base += each * stack.getAmount();
inventory.setItem(i, null);
}
if (base <= 0) {
Text.send(event.getPlayer(), "<red>No sellable items in that container.");
return;
}
double total = base * multiplier * effectiveSellMultiplier(event.getPlayer());
plugin.modules().economy().addMoney(event.getPlayer().getUniqueId(), total);
plugin.modules().events().progress(event.getPlayer(), "sell", total);
Text.send(event.getPlayer(), "<gray>Sellwand sold the container for <green>$" + Text.format(total) + "<gray>.");
}
}
private boolean handleAdmin(Player sender, String[] args) {
if (!sender.hasPermission("vupe.admin")) {
Text.send(sender, "<red>No permission.");
return true;
}
if (args.length < 1) {
Text.send(sender, "<red>/shop admin <voucher|box|booster|sellwand> ...");
return true;
}
if (args[0].equalsIgnoreCase("voucher") && args.length >= 5) {
Player target = Bukkit.getPlayerExact(args[1]);
if (target == null) { Text.send(sender, "<red>Player not found."); return true; }
String type = args[2].toUpperCase(Locale.ROOT);
String value = args[3];
int amount;
try { amount = Math.max(1, Integer.parseInt(args[4])); } catch (NumberFormatException ex) { amount = 1; }
ItemStack voucher = Items.tagged(Material.PAPER,
"<#F472B6><bold>Vupe Voucher</bold>",
List.of("<gray>Type: <white>" + type, "<gray>Value: <white>" + value, "", "<yellow>Right-click to redeem."),
"voucher", type + ":" + value);
voucher.setAmount(Math.min(amount, 64));
target.getInventory().addItem(voucher);
return true;
}
if (args[0].equalsIgnoreCase("box") && args.length >= 3) {
Player target = Bukkit.getPlayerExact(args[1]);
if (target == null || plugin.modules().crates().crate(args[2]) == null) {
Text.send(sender, "<red>Invalid player or crate reward table.");
return true;
}
target.getInventory().addItem(Items.tagged(Material.ENDER_CHEST,
"<#8B5CF6><bold>Vupe Box</bold>",
List.of("<gray>Reward table: <white>" + args[2], "", "<yellow>Right-click to open."),
"box_crate", args[2].toLowerCase(Locale.ROOT)));
return true;
}
if (args[0].equalsIgnoreCase("globalbooster") && args.length >= 4) {
double multiplier;
long minutes;
try {
multiplier = Math.max(1, Double.parseDouble(args[2]));
minutes = Math.max(1, Long.parseLong(args[3]));
} catch (NumberFormatException ex) {
Text.send(sender, "<red>Usage: /shop admin globalbooster <sell|crystal> <multiplier> <minutes>");
return true;
}
long until = System.currentTimeMillis() + minutes * 60_000L;
if (args[1].equalsIgnoreCase("sell")) {
plugin.data().server().globalSellBoosterMultiplier = multiplier;
plugin.data().server().globalSellBoosterUntil = until;
} else if (args[1].equalsIgnoreCase("crystal")) {
plugin.data().server().globalCrystalBoosterMultiplier = multiplier;
plugin.data().server().globalCrystalBoosterUntil = until;
} else {
Text.send(sender, "<red>Global booster type must be sell or crystal.");
return true;
}
plugin.data().markServerDirty();
Bukkit.broadcast(Text.component(Text.prefix() + "<#F472B6><bold>GLOBAL BOOSTER</bold> <gray>"
+ Text.format(multiplier) + "x " + args[1] + " for " + minutes + " minutes."));
return true;
}
if (args[0].equalsIgnoreCase("booster") && args.length >= 5) {
Player target = Bukkit.getPlayerExact(args[1]);
if (target == null) { Text.send(sender, "<red>Player not found."); return true; }
double multiplier;
long minutes;
try {
multiplier = Math.max(1, Double.parseDouble(args[3]));
minutes = Math.max(1, Long.parseLong(args[4]));
} catch (NumberFormatException ex) {
Text.send(sender, "<red>Invalid multiplier/minutes.");
return true;
}
PlayerData data = plugin.data().player(target.getUniqueId());
long until = System.currentTimeMillis() + minutes * 60_000L;
if (args[2].equalsIgnoreCase("sell")) {
data.sellBoosterMultiplier = multiplier;
data.sellBoosterUntil = until;
} else if (args[2].equalsIgnoreCase("crystal")) {
data.crystalBoosterMultiplier = multiplier;
data.crystalBoosterUntil = until;
} else {
Text.send(sender, "<red>Booster type must be sell or crystal.");
return true;
}
plugin.data().markDirty(target.getUniqueId());
Text.send(sender, "<green>Booster applied.");
return true;
}
if (args[0].equalsIgnoreCase("sellwand") && args.length >= 3) {
Player target = Bukkit.getPlayerExact(args[1]);
if (target == null) return true;
double mult;
try { mult = Double.parseDouble(args[2]); } catch (NumberFormatException ex) { mult = 2; }
target.getInventory().addItem(Items.tagged(Material.BLAZE_ROD,
"<gold><bold>" + Text.format(mult) + "x Sellwand</bold>",
List.of("<gray>Right-click a container to sell its contents."),
"sellwand", Double.toString(mult)));
return true;
}
Text.send(sender, "<red>Invalid admin shop command.");
return true;
}
private boolean crystalShopCommand(CommandSender sender, String label, String[] args) {
if (!plugin.configs().modules().getBoolean("modules.crystal-shop", true)) {
Text.send(sender, "<red>Crystal shop is disabled."); return true;
}
if (!(sender instanceof Player player)) {
Text.send(sender, "<red>Player-only.");
return true;
}
Inventory inv = Bukkit.createInventory(null, 27, Text.component(
plugin.configs().get("crystalshop").getString("crystal-shop.title", "<#8B5CF6><bold>CRYSTAL SHOP</bold>")
));
ConfigurationSection offers = plugin.configs().get("crystalshop").getConfigurationSection("crystal-shop.offers");
if (offers != null) {
for (String id : offers.getKeys(false)) {
int slot = offers.getInt(id + ".slot", 0);
Material material = Material.matchMaterial(offers.getString(id + ".material", "PAPER"));
if (material == null || slot < 0 || slot >= inv.getSize()) continue;
long price = offers.getLong(id + ".price", 1);
inv.setItem(slot, Items.tagged(material,
offers.getString(id + ".display", id),
List.of("<gray>Price: <#8B5CF6>" + price + " Crystals", "", "<yellow>Click to buy."),
"crystal_offer", id));
}
}
openMenus.put(player.getUniqueId(), "crystalshop");
player.openInventory(inv);
return true;
}
private boolean boxCommand(CommandSender sender, String label, String[] args) {
if (!plugin.configs().modules().getBoolean("modules.boxes", true)) {
Text.send(sender, "<red>Boxes are disabled."); return true;
}
if (args.length == 0) {
Text.send(sender, "<gray>Boxes are configurable in <white>boxes.yml<gray>.");
return true;
}
if (!sender.hasPermission("vupe.admin")) {
Text.send(sender, "<red>No permission.");
return true;
}
if (args.length < 4 || !args[0].equalsIgnoreCase("give")) {
Text.send(sender, "<red>/box give <player> <money|crystals|lootbox> <rarity|id> [amount]");
return true;
}
Player target = Bukkit.getPlayerExact(args[1]);
if (target == null) {
Text.send(sender, "<red>Player not found.");
return true;
}
int amount = 1;
if (args.length >= 5) {
try { amount = Math.max(1, Integer.parseInt(args[4])); } catch (NumberFormatException ignored) {}
}
String type = args[2].toLowerCase(Locale.ROOT);
String value = args[3].toLowerCase(Locale.ROOT);
ItemStack box;
if (type.equals("lootbox")) {
String display = plugin.configs().get("boxes").getString("lootboxes.definitions." + value + ".display", value);
box = Items.tagged(Material.SHULKER_BOX, display,
List.of("<gray>Contains multiple configurable Vupe rewards.", "", "<yellow>Right-click to open."),
"lootbox", value);
} else {
String path = "boxes.types." + type + "." + value;
if (!plugin.configs().get("boxes").contains(path)) {
Text.send(sender, "<red>Unknown box type/rarity.");
return true;
}
box = Items.tagged(Material.ENDER_CHEST,
"<gradient:#8B5CF6:#22D3EE><bold>" + prettify(value) + " " + prettify(type) + " Box</bold></gradient>",
List.of("<gray>Right-click to reveal your reward."),
"simple_box", type + ":" + value);
}
box.setAmount(Math.min(amount, box.getMaxStackSize()));
target.getInventory().addItem(box);
return true;
}
private void buyCrystalOffer(Player player, String id) {
String path = "crystal-shop.offers." + id;
long price = plugin.configs().get("crystalshop").getLong(path + ".price", -1);
if (price <= 0 || !plugin.modules().economy().takeCrystals(player.getUniqueId(), price)) {
Text.send(player, "<red>You do not have enough crystals.");
return;
}
String type = plugin.configs().get("crystalshop").getString(path + ".type", "MONEY").toUpperCase(Locale.ROOT);
String value = plugin.configs().get("crystalshop").getString(path + ".value", "");
double amount = plugin.configs().get("crystalshop").getDouble(path + ".amount", 1);
switch (type) {
case "MONEY" -> plugin.modules().economy().addMoney(player.getUniqueId(), amount);
case "GOLD" -> plugin.modules().economy().addGold(player.getUniqueId(), Math.round(amount));
case "GEN_SLOTS" -> {
PlayerData data = plugin.data().player(player.getUniqueId());
data.generatorSlots += Math.max(1, (int) Math.round(amount));
plugin.data().markDirty(player.getUniqueId());
}
case "SELL_MULTIPLIER" -> {
PlayerData data = plugin.data().player(player.getUniqueId());
data.sellMultiplierBonus += amount;
plugin.data().markDirty(player.getUniqueId());
}
case "GENERATOR" -> plugin.modules().generators().give(player, value, Math.max(1, (int) Math.round(amount)));
case "CRATE_KEY" -> plugin.modules().crates().addKeys(player.getUniqueId(), value, Math.max(1, (int) Math.round(amount)));
case "SELLWAND" -> player.getInventory().addItem(Items.tagged(Material.BLAZE_ROD,
"<gold><bold>" + Text.format(amount) + "x Sellwand</bold>",
List.of("<gray>Right-click a container to sell its contents."), "sellwand", Double.toString(amount)));
case "BOX" -> {
String[] parts = value.split(":", 2);
if (parts.length == 2) giveSimpleBox(player, parts[0], parts[1], 1);
}
case "LOOTBOX" -> giveLootbox(player, value, 1);
default -> {
plugin.modules().economy().addCrystals(player.getUniqueId(), price);
Text.send(player, "<red>That crystal-shop offer is misconfigured; your crystals were refunded.");
return;
}
}
Text.send(player, "<green>Crystal-shop purchase complete.");
}
private void giveSimpleBox(Player player, String type, String rarity, int amount) {
ItemStack box = Items.tagged(Material.ENDER_CHEST,
"<gradient:#8B5CF6:#22D3EE><bold>" + prettify(rarity) + " " + prettify(type) + " Box</bold></gradient>",
List.of("<gray>Right-click to reveal your reward."), "simple_box", type + ":" + rarity);
box.setAmount(Math.min(amount, box.getMaxStackSize()));
player.getInventory().addItem(box);
}
private void giveLootbox(Player player, String id, int amount) {
String display = plugin.configs().get("boxes").getString("lootboxes.definitions." + id + ".display", id);
ItemStack box = Items.tagged(Material.SHULKER_BOX, display,
List.of("<gray>Contains multiple configurable Vupe rewards.", "", "<yellow>Right-click to open."),
"lootbox", id);
box.setAmount(Math.min(amount, box.getMaxStackSize()));
player.getInventory().addItem(box);
}
private void openSimpleBox(Player player, String encoded) {
String[] parts = encoded.split(":", 2);
if (parts.length != 2) return;
String path = "boxes.types." + parts[0] + "." + parts[1];
if (!plugin.configs().get("boxes").contains(path)) {
Text.send(player, "<red>This box reward is no longer configured.");
return;
}
long min = plugin.configs().get("boxes").getLong(path + ".min", 1);
long max = Math.max(min, plugin.configs().get("boxes").getLong(path + ".max", min));
long amount = java.util.concurrent.ThreadLocalRandom.current().nextLong(min, max + 1);
if (parts[0].equals("money")) {
plugin.modules().economy().addMoney(player.getUniqueId(), amount);
Text.send(player, "<gray>Your box contained <green>$" + Text.format(amount) + "<gray>.");
} else {
plugin.modules().economy().addCrystals(player.getUniqueId(), amount);
Text.send(player, "<gray>Your box contained <#8B5CF6>" + amount + " Crystals<gray>.");
}
}
private void openLootbox(Player player, String id) {
String path = "lootboxes.definitions." + id;
if (!plugin.configs().get("boxes").contains(path)) {
Text.send(player, "<red>That lootbox is no longer configured.");
return;
}
int rolls = Math.max(1, plugin.configs().get("boxes").getInt(path + ".rolls", 3));
List<Map<?, ?>> rewards = plugin.configs().get("boxes").getMapList(path + ".rewards");
if (rewards.isEmpty()) return;
for (int i = 0; i < rolls; i++) {
Map<?, ?> reward = chooseWeighted(rewards);
if (reward == null) continue;
grantLooseReward(player, reward);
}
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1f, 1.25f);
}
private Map<?, ?> chooseWeighted(List<Map<?, ?>> rewards) {
double total = rewards.stream().mapToDouble(r -> mapNumber(r, "weight", 1)).sum();
if (total <= 0) return null;
double roll = Math.random() * total, current = 0;
for (Map<?, ?> reward : rewards) {
current += Math.max(0, mapNumber(reward, "weight", 1));
if (roll <= current) return reward;
}
return rewards.getLast();
}
private void grantLooseReward(Player player, Map<?, ?> reward) {
String type = mapString(reward, "type", "MONEY").toUpperCase(Locale.ROOT);
double amount = mapNumber(reward, "amount", 1);
if (reward.containsKey("min") || reward.containsKey("max")) {
long min = Math.round(mapNumber(reward, "min", amount));
long max = Math.max(min, Math.round(mapNumber(reward, "max", min)));
amount = java.util.concurrent.ThreadLocalRandom.current().nextLong(min, max + 1);
}
String value = mapString(reward, "value", "");
switch (type) {
case "MONEY" -> plugin.modules().economy().addMoney(player.getUniqueId(), amount);
case "CRYSTALS" -> plugin.modules().economy().addCrystals(player.getUniqueId(), Math.round(amount));
case "GOLD" -> plugin.modules().economy().addGold(player.getUniqueId(), Math.round(amount));
case "CRATE_KEY" -> plugin.modules().crates().addKeys(player.getUniqueId(), value, Math.max(1, (int) Math.round(amount)));
case "GENERATOR" -> plugin.modules().generators().give(player, value, Math.max(1, (int) Math.round(amount)));
case "GEN_SLOTS" -> {
PlayerData data = plugin.data().player(player.getUniqueId());
data.generatorSlots += Math.max(1, (int) Math.round(amount));
plugin.data().markDirty(player.getUniqueId());
}
case "SELL_MULTIPLIER" -> {
PlayerData data = plugin.data().player(player.getUniqueId());
data.sellMultiplierBonus += amount;
plugin.data().markDirty(player.getUniqueId());
}
case "SELL_BOOSTER" -> {
PlayerData data = plugin.data().player(player.getUniqueId());
data.sellBoosterMultiplier = Math.max(1, amount);
long minutes = Math.max(1, Math.round(mapNumber(reward, "duration-minutes", 30)));
data.sellBoosterUntil = System.currentTimeMillis() + minutes * 60_000L;
plugin.data().markDirty(player.getUniqueId());
}
}
}
private static String mapString(Map<?, ?> map, String key, String fallback) {
Object v = map.get(key);
return v == null ? fallback : String.valueOf(v);
}
private static double mapNumber(Map<?, ?> map, String key, double fallback) {
Object v = map.get(key);
if (v instanceof Number n) return n.doubleValue();
try { return Double.parseDouble(String.valueOf(v)); } catch (Exception ex) { return fallback; }
}
private static String prettify(String id) {
StringBuilder out = new StringBuilder();
for (String part : id.replace('_', ' ').split(" ")) {
if (part.isBlank()) continue;
if (!out.isEmpty()) out.append(' ');
out.append(Character.toUpperCase(part.charAt(0))).append(part.substring(1).toLowerCase(Locale.ROOT));
}
return out.toString();
}
private void redeemVoucher(Player player, ItemStack item, String encoded) {
String[] parts = encoded.split(":", 2);
if (parts.length != 2) return;
String type = parts[0];
String value = parts[1];
try {
switch (type) {
case "MONEY" -> plugin.modules().economy().addMoney(player.getUniqueId(), Double.parseDouble(value));
case "CRYSTALS" -> plugin.modules().economy().addCrystals(player.getUniqueId(), Long.parseLong(value));
case "GOLD" -> plugin.modules().economy().addGold(player.getUniqueId(), Long.parseLong(value));
case "GEN_SLOTS" -> {
PlayerData data = plugin.data().player(player.getUniqueId());
data.generatorSlots += Integer.parseInt(value);
plugin.data().markDirty(player.getUniqueId());
}
case "SELL_MULTIPLIER" -> {
PlayerData data = plugin.data().player(player.getUniqueId());
data.sellMultiplierBonus += Double.parseDouble(value);
plugin.data().markDirty(player.getUniqueId());
}
case "RANK" -> plugin.modules().progression().setDonorRank(player, value);
case "KIT" -> plugin.modules().progression().giveKit(player, value);
default -> { Text.send(player, "<red>Unknown voucher type."); return; }
}
consumeOne(item);
Text.send(player, "<green>Voucher redeemed.");
} catch (NumberFormatException ex) {
Text.send(player, "<red>This voucher is malformed.");
}
}
private void consumeOne(ItemStack item) {
item.setAmount(item.getAmount() - 1);
}
}