/
InventoryUtil.java
311 lines (266 loc) · 11.7 KB
/
InventoryUtil.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
package com.sk89q.craftbook.util;
import org.bukkit.block.Block;
import org.bukkit.block.BrewingStand;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Furnace;
import org.bukkit.block.ShulkerBox;
import org.bukkit.entity.Player;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.DoubleChestInventory;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.IntFunction;
/**
* Class for utilities that include adding items to a furnace based on if it is a fuel or not, and adding items to a chest. Also will include methdos for checking contents and removing.
*/
public class InventoryUtil {
/**
* Adds items to an inventory, returning the leftovers.
*
* @param container The InventoryHolder to add the items to.
* @param stacks The stacks to add to the inventory.
* @return The stacks that could not be added.
*/
public static List<ItemStack> addItemsToInventory(InventoryHolder container, ItemStack ... stacks) {
return addItemsToInventory(container, true, stacks);
}
/**
* Adds items to an inventory, returning the leftovers.
*
* @param container The InventoryHolder to add the items to.
* @param stacks The stacks to add to the inventory.
* @return The stacks that could not be added.
*/
public static List<ItemStack> addItemsToInventory(InventoryHolder container, boolean update, ItemStack ... stacks) {
if(container instanceof Furnace) {
return addItemsToFurnace((Furnace) container, stacks);
} else if(container instanceof BrewingStand) {
return addItemsToBrewingStand((BrewingStand) container, stacks);
} else { //Basic inventories like chests, dispensers, storage carts, etc.
List<ItemStack> leftovers = new ArrayList<>();
if (container instanceof ShulkerBox) {
Arrays.stream(stacks).filter(item -> ItemUtil.isShulkerBox(item.getType())).forEach(leftovers::add);
stacks = Arrays.stream(stacks).filter(item -> !ItemUtil.isShulkerBox(item.getType())).toArray(ItemStack[]::new);
}
leftovers.addAll(container.getInventory().addItem(stacks).values());
if (container.getInventory() instanceof DoubleChestInventory) {
((Chest) ((DoubleChestInventory) container.getInventory()).getLeftSide().getHolder()).update(true);
((Chest) ((DoubleChestInventory) container.getInventory()).getRightSide().getHolder()).update(true);
}
//if(container instanceof BlockState && update)
// ((BlockState) container).update();
return leftovers;
}
}
/**
* Adds items to a furnace, returning the leftovers.
*
* @param furnace The Furnace to add the items to.
* @param stacks The stacks to add to the inventory.
* @return The stacks that could not be added.
*/
public static List<ItemStack> addItemsToFurnace(Furnace furnace, ItemStack ... stacks) {
List<ItemStack> leftovers = new ArrayList<>();
for(ItemStack stack : stacks) {
if(!ItemUtil.isStackValid(stack))
continue;
if (ItemUtil.isFurnacable(stack) && fitsInSlot(stack, furnace.getInventory().getSmelting())) {
if (furnace.getInventory().getSmelting() == null)
furnace.getInventory().setSmelting(stack);
else
leftovers.add(ItemUtil.addToStack(furnace.getInventory().getSmelting(), stack));
} else if (ItemUtil.isAFuel(stack) && fitsInSlot(stack, furnace.getInventory().getFuel())) {
if (furnace.getInventory().getFuel() == null)
furnace.getInventory().setFuel(stack);
else
leftovers.add(ItemUtil.addToStack(furnace.getInventory().getFuel(), stack));
} else {
leftovers.add(stack);
}
}
leftovers.removeAll(Collections.singleton(null));
//furnace.update();
return leftovers;
}
/**
* Adds items to a BrewingStand, returning the leftovers.
*
* @param brewingStand The BrewingStand to add the items to.
* @param stacks The stacks to add to the inventory.
* @return The stacks that could not be added.
*/
public static List<ItemStack> addItemsToBrewingStand(BrewingStand brewingStand, ItemStack ... stacks) {
List<ItemStack> leftovers = new ArrayList<>();
for(ItemStack stack : stacks) {
if (!ItemUtil.isAPotionIngredient(stack)) {
leftovers.add(stack);
continue;
}
BrewerInventory inv = brewingStand.getInventory();
if (InventoryUtil.fitsInSlot(stack, inv.getIngredient())) {
if (inv.getIngredient() == null) {
inv.setIngredient(stack);
} else {
leftovers.add(ItemUtil.addToStack(inv.getIngredient(), stack));
}
} else {
leftovers.add(stack);
}
}
//brewingStand.update();
return leftovers;
}
/**
* Checks whether the inventory contains all the given itemstacks.
*
* @param inv The inventory to check.
* @param exact Whether the stacks need to be the exact amount.
* @param stacks The stacks to check.
* @return whether the inventory contains all the items. If there are no items to check, it returns true.
*/
public static boolean doesInventoryContain(Inventory inv, boolean exact, ItemStack ... stacks) {
return doesInventoryContain(inv, !exact, false, false, false, stacks);
}
/**
* Checks whether the inventory contains all the given itemstacks.
*
* @param inv The inventory to check.
* @param ignoreStackSize Whether to ignore stack size count.
* @param ignoreDurability Whether to ignore durability if damageable.
* @param ignoreMeta Whether to ignore meta/nbt data.
* @param ignoreEnchants Whether to ignore enchantment data.
* @param stacks The stacks to check.
* @return whether the inventory contains all the items. If there are no items to check, it returns true.
*/
public static boolean doesInventoryContain(Inventory inv, boolean ignoreStackSize, boolean ignoreDurability, boolean ignoreMeta, boolean ignoreEnchants, ItemStack ... stacks) {
ArrayList<ItemStack> itemsToFind = new ArrayList<>(Arrays.asList(stacks));
if(itemsToFind.isEmpty())
return true;
List<ItemStack> items = new ArrayList<>(Arrays.asList(inv.getContents()));
if (inv instanceof PlayerInventory) {
items.addAll(Arrays.asList(((PlayerInventory) inv).getArmorContents()));
items.add(((PlayerInventory) inv).getItemInOffHand());
}
for (ItemStack item : items) {
if(!ItemUtil.isStackValid(item))
continue;
for(ItemStack base : stacks) {
if(!itemsToFind.contains(base))
continue;
if(!ItemUtil.isStackValid(base)) {
itemsToFind.remove(base);
continue;
}
if(ItemUtil.areItemsSimilar(base, item)) {
if(!ignoreStackSize && base.getAmount() != item.getAmount())
continue;
if(!ignoreDurability && (base.getType().getMaxDurability() > 0 || item.getType().getMaxDurability() > 0) && base.getDurability() != item.getDurability())
continue;
if(!ignoreMeta) {
if(base.hasItemMeta() != item.hasItemMeta()) {
if(!ignoreEnchants)
continue;
if(base.hasItemMeta() && ItemUtil.hasDisplayNameOrLore(base))
continue;
else if(item.hasItemMeta() && ItemUtil.hasDisplayNameOrLore(item))
continue;
} else if(base.hasItemMeta()) {
if(base.hasItemMeta() && !ItemUtil.areItemMetaIdentical(base.getItemMeta(), item.getItemMeta(), !ignoreEnchants))
continue;
}
}
itemsToFind.remove(base);
break;
}
}
}
return itemsToFind.isEmpty();
}
/**
* Removes items from an inventory.
*
* @param inv The inventory to remove it from.
* @param stacks The stacks to remove.
* @return Whether the stacks were removed.
*/
public static boolean removeItemsFromInventory(InventoryHolder inv, ItemStack ... stacks) {
List<ItemStack> leftovers = new ArrayList<>(inv.getInventory().removeItem(stacks).values());
if(!leftovers.isEmpty()) {
List<ItemStack> itemsToAdd = new ArrayList<>(Arrays.asList(stacks));
itemsToAdd.removeAll(leftovers);
inv.getInventory().addItem(itemsToAdd.toArray(new ItemStack[itemsToAdd.size()]));
}
//if(inv instanceof BlockState)
// ((BlockState) inv).update();
return leftovers.isEmpty();
}
/**
* Checks whether the itemstack can easily stack onto the other itemstack.
*
* @param stack The stack to add.
* @param slot The base stack.
* @return whether it can be added or not.
*/
public static boolean fitsInSlot(ItemStack stack, ItemStack slot) {
return slot == null || ItemUtil.areItemsIdentical(stack, slot) && stack.getAmount() + slot.getAmount() <= stack.getMaxStackSize();
}
/**
* Checks whether the block has an inventory.
*
* @param block The block.
* @return If it has an inventory.
*/
public static boolean doesBlockHaveInventory(Block block) {
switch(block.getType()) {
case CHEST:
case TRAPPED_CHEST:
case DROPPER:
case DISPENSER:
case FURNACE:
case BREWING_STAND:
case HOPPER:
case WHITE_SHULKER_BOX:
case ORANGE_SHULKER_BOX:
case MAGENTA_SHULKER_BOX:
case LIGHT_BLUE_SHULKER_BOX:
case YELLOW_SHULKER_BOX:
case GREEN_SHULKER_BOX:
case PINK_SHULKER_BOX:
case GRAY_SHULKER_BOX:
case LIGHT_GRAY_SHULKER_BOX:
case BLUE_SHULKER_BOX:
case PURPLE_SHULKER_BOX:
case CYAN_SHULKER_BOX:
case BROWN_SHULKER_BOX:
case LIME_SHULKER_BOX:
case BLACK_SHULKER_BOX:
case RED_SHULKER_BOX:
return true;
default:
return false;
}
}
public static ItemStack getItemInHand(Player player, EquipmentSlot slot) {
if (slot == EquipmentSlot.HAND) {
return player.getInventory().getItemInMainHand();
} else if (slot == EquipmentSlot.OFF_HAND) {
return player.getInventory().getItemInOffHand();
}
return null;
}
public static void setItemInHand(Player player, EquipmentSlot slot, ItemStack itemStack) {
if (slot == EquipmentSlot.HAND) {
player.getInventory().setItemInMainHand(itemStack);
} else if (slot == EquipmentSlot.OFF_HAND) {
player.getInventory().setItemInOffHand(itemStack);
}
}
}