forked from Unbreathable/Vampires
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemShopScreen.java
167 lines (146 loc) · 7.92 KB
/
ItemShopScreen.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
package com.liphium.vampires.screens;
import com.liphium.core.inventory.CClickEvent;
import com.liphium.core.inventory.CItem;
import com.liphium.core.inventory.CScreen;
import com.liphium.core.util.ItemStackBuilder;
import com.liphium.vampires.Vampires;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.Map;
public class ItemShopScreen extends CScreen {
public ItemShopScreen() {
super(3, Component.text("Item shop", NamedTextColor.GOLD, TextDecoration.BOLD), 3, false);
}
@Override
public void init(Player player, Inventory inventory) {
background(player);
// Add all the categories
for (int i = 0; i < ShopCategory.values().length; i++) {
int finalI = i;
setItemNotCached(player, 10 + i, new CItem(ShopCategory.values()[i].getStack())
.onClick(event -> openCategory(event, finalI, inventory)));
}
}
public void openCategory(CClickEvent event, int id, Inventory inventory) {
final ShopCategory category = ShopCategory.values()[id];
for (int i = 0; i < category.getItems().size(); i++) {
setItemNotCached(event.getPlayer(), 19 + i, category.getItems().get(i), inventory);
}
}
enum ShopCategory {
VAMPIRES(
new ItemStackBuilder(Material.REDSTONE)
.withName(Component.text("Vampires", NamedTextColor.RED, TextDecoration.BOLD))
.withLore(Component.text("Stuff to catch those pesky creatures.", NamedTextColor.GRAY))
.buildStack(),
List.of(
itemWithPrice(Material.FEATHER, "Dash", NamedTextColor.RED, 5, 1),
itemWithPrice(Material.BLAZE_ROD, "Instant catcher", NamedTextColor.RED, 10, 1),
spacer(),
itemWithPrice(Material.LEATHER, "Leather", NamedTextColor.RED, 1, 1),
itemWithPrice(Material.IRON_INGOT, "Iron ingot", NamedTextColor.RED, 3, 1),
itemWithPrice(Material.DIAMOND, "Diamond", NamedTextColor.RED, 7, 1),
spacer(),
itemWithPrice(Material.GOLDEN_APPLE, "Golden apple dropper", NamedTextColor.RED, 20, 1),
itemWithPrice(Material.FIREWORK_ROCKET, "Rocket dropper", NamedTextColor.RED, 30, 1)
)
),
HUMANS(
new ItemStackBuilder(Material.GRASS_BLOCK)
.withName(Component.text("Humans", NamedTextColor.GREEN, TextDecoration.BOLD))
.withLore(Component.text("The stuff you need to survive.", NamedTextColor.GRAY))
.buildStack(),
List.of(
itemWithPrice(Material.TORCH, "Torch", NamedTextColor.GREEN, 4, 1),
spacer(),
itemWithPrice(Material.IRON_INGOT, "Iron ingot", NamedTextColor.GREEN, 5, 1),
itemWithPrice(Material.DIAMOND, "Diamond", NamedTextColor.GREEN, 10, 1),
itemWithPrice(Material.MACE, "Mace", NamedTextColor.WHITE, 25, 1),
spacer(),
itemWithPrice(Material.BEETROOT, "Blood garlic dropper", NamedTextColor.GREEN, 20, 1),
itemWithPrice(Material.TORCH, "Torch dropper", NamedTextColor.GREEN, 30, 1)
)
),
TOOLS(
new ItemStackBuilder(Material.IRON_PICKAXE)
.withName(Component.text("Tools & weapons", NamedTextColor.WHITE, TextDecoration.BOLD))
.withLore(Component.text("To aid you in the fight.", NamedTextColor.GRAY))
.buildStack(),
List.of(
itemWithPrice(Material.IRON_SHOVEL, "Iron shovel", NamedTextColor.WHITE, 4, 1),
itemWithPriceCustom(Material.GOLDEN_SHOVEL, "Golden shovel", NamedTextColor.WHITE, 10,
new ItemStackBuilder(Material.GOLDEN_SHOVEL)
.withName(Component.text("Golden shovel", NamedTextColor.WHITE))
.withEnchantments(Map.of(Enchantment.EFFICIENCY, 2))
.buildStack()
),
itemWithPrice(Material.WIND_CHARGE, "Wind charge", NamedTextColor.WHITE, 20, 5)
)
),
BUILDING(
new ItemStackBuilder(Material.REDSTONE)
.withName(Component.text("Building", NamedTextColor.GOLD, TextDecoration.BOLD))
.withLore(Component.text("Upgrade your base.", NamedTextColor.GRAY))
.buildStack(),
List.of(
itemWithPrice(Material.CARVED_PUMPKIN, "Pumpkin dropper", NamedTextColor.GOLD, 5, 1),
itemWithPrice(Material.BREWING_STAND, "Brewer", NamedTextColor.GOLD, 50, 1),
itemWithPrice(Material.ARMOR_STAND, "Pop-up item shop", NamedTextColor.GOLD, 50, 1),
spacer(),
itemWithPrice(Material.REDSTONE_TORCH, "Alarm trap", NamedTextColor.GOLD, 5, 1),
itemWithPrice(Material.GLOWSTONE_DUST, "Glow trap", NamedTextColor.GOLD, 10, 1)
)
);
final ItemStack stack;
final List<CItem> items;
ShopCategory(ItemStack stack, List<CItem> items) {
this.stack = stack;
this.items = items;
}
public ItemStack getStack() {
return stack;
}
public List<CItem> getItems() {
return items;
}
private static ItemStack item = new ItemStackBuilder(Material.BLACK_STAINED_GLASS_PANE).withName(Component.text("§r")).buildStack();
public static CItem spacer() {
return new CItem(item);
}
public static CItem itemWithPrice(Material material, String name, NamedTextColor color, int price, int amount) {
final var stackBuilder = new ItemStackBuilder(material).withName(Component.text(name, color));
return new CItem(stackBuilder
.withLore(Component.text("Price: ", NamedTextColor.GRAY).append(Component.text(price, NamedTextColor.GOLD)))
.buildStack()
).onClick(event -> buyFunction(event, stackBuilder.withAmount(amount).buildStack(), price));
}
public static CItem itemWithPriceCustom(Material material, String name, NamedTextColor color, int price, ItemStack sold) {
return new CItem(new ItemStackBuilder(material).withName(Component.text(name, color))
.withLore(Component.text("Price: ", NamedTextColor.GRAY).append(Component.text(price, NamedTextColor.GOLD)))
.buildStack()
).onClick(event -> buyFunction(event, sold, price));
}
public static void buyFunction(CClickEvent event, ItemStack stack, int price) {
event.getPlayer().closeInventory();
// Get the amount of pumpkins in the inventory
int count = 0;
for (ItemStack item : event.getPlayer().getInventory()) {
if (item.getType() == Material.CARVED_PUMPKIN) {
count += item.getAmount();
}
}
if (count < price) {
event.getPlayer().sendMessage(Vampires.PREFIX.append(Component.text("You don't have enough pumpkins to purchase this item.", NamedTextColor.RED)));
return;
}
event.getPlayer().getInventory().addItem(stack);
}
}
}