20
20
public class ItemShopScreen extends CScreen {
21
21
22
22
public ItemShopScreen () {
23
- super (3 , Component .text ("Item shop" , NamedTextColor .GOLD , TextDecoration .BOLD ), 3 , false );
23
+ super (3 , Component .text ("Item shop" , NamedTextColor .GOLD , TextDecoration .BOLD ), 4 , false );
24
24
}
25
25
26
26
@ Override
@@ -37,8 +37,29 @@ public void init(Player player, Inventory inventory) {
37
37
38
38
public void openCategory (CClickEvent event , int id , Inventory inventory ) {
39
39
final ShopCategory category = ShopCategory .values ()[id ];
40
- for (int i = 0 ; i < category .getItems ().size (); i ++) {
41
- setItemNotCached (event .getPlayer (), 19 + i , category .getItems ().get (i ), inventory );
40
+ for (int i = 0 ; i < 9 ; i ++) {
41
+ if (category .getItems ().size () <= i ) {
42
+ setItemNotCached (event .getPlayer (), 18 + i , ShopCategory .spacer (), inventory );
43
+ } else {
44
+ setItemNotCached (event .getPlayer (), 18 + i , category .getItems ().get (i ), inventory );
45
+ }
46
+ }
47
+ }
48
+
49
+ public static void removeAmountFromInventory (Player player , Material material , int amount ) {
50
+ int count = amount ;
51
+ for (ItemStack item : player .getInventory ()) {
52
+ if (item != null && item .getType () == material ) {
53
+ int sub = Math .min (item .getAmount (), count );
54
+ int newAmount = item .getAmount () - sub ;
55
+ if (newAmount == 0 ) {
56
+ item .setAmount (newAmount );
57
+ }
58
+ count -= sub ;
59
+ if (count <= 0 ) {
60
+ break ;
61
+ }
62
+ }
42
63
}
43
64
}
44
65
@@ -126,16 +147,14 @@ public List<CItem> getItems() {
126
147
private static ItemStack item = new ItemStackBuilder (Material .BLACK_STAINED_GLASS_PANE ).withName (Component .text ("§r" )).buildStack ();
127
148
128
149
public static CItem spacer () {
129
- return new CItem (item );
150
+ return new CItem (item ). notClickable () ;
130
151
}
131
152
132
153
public static CItem itemWithPrice (Material material , String name , NamedTextColor color , int price , int amount ) {
133
- final var stackBuilder = new ItemStackBuilder (material ).withName (Component .text (name , color ));
134
-
135
- return new CItem (stackBuilder
154
+ return new CItem (new ItemStackBuilder (material ).withName (Component .text (name , color ))
136
155
.withLore (Component .text ("Price: " , NamedTextColor .GRAY ).append (Component .text (price , NamedTextColor .GOLD )))
137
156
.buildStack ()
138
- ).onClick (event -> buyFunction (event , stackBuilder .withAmount (amount ).buildStack (), price ));
157
+ ).onClick (event -> buyFunction (event , new ItemStackBuilder ( material ). withName ( Component . text ( name , color )) .withAmount (amount ).buildStack (), price ));
139
158
}
140
159
141
160
public static CItem itemWithPriceCustom (Material material , String name , NamedTextColor color , int price , ItemStack sold ) {
@@ -146,21 +165,23 @@ public static CItem itemWithPriceCustom(Material material, String name, NamedTex
146
165
}
147
166
148
167
public static void buyFunction (CClickEvent event , ItemStack stack , int price ) {
149
- event .getPlayer ().closeInventory ();
150
-
151
168
// Get the amount of pumpkins in the inventory
152
169
int count = 0 ;
153
170
for (ItemStack item : event .getPlayer ().getInventory ()) {
154
- if (item .getType () == Material .CARVED_PUMPKIN ) {
171
+ if (item != null && item .getType () == Material .CARVED_PUMPKIN ) {
155
172
count += item .getAmount ();
156
173
}
157
174
}
158
175
159
176
if (count < price ) {
160
177
event .getPlayer ().sendMessage (Vampires .PREFIX .append (Component .text ("You don't have enough pumpkins to purchase this item." , NamedTextColor .RED )));
178
+ event .getPlayer ().closeInventory ();
161
179
return ;
162
180
}
163
181
182
+ // Remove the pumpkins from the players inventory
183
+ removeAmountFromInventory (event .getPlayer (), Material .CARVED_PUMPKIN , price );
184
+
164
185
event .getPlayer ().getInventory ().addItem (stack );
165
186
}
166
187
}
0 commit comments