|
4 | 4 | import org.bukkit.block.Block;
|
5 | 5 | import org.bukkit.block.BrewingStand;
|
6 | 6 | import org.bukkit.block.Chest;
|
| 7 | +import org.bukkit.block.ChiseledBookshelf; |
| 8 | +import org.bukkit.block.Crafter; |
7 | 9 | import org.bukkit.block.Furnace;
|
8 | 10 | import org.bukkit.block.ShulkerBox;
|
9 | 11 | import org.bukkit.entity.Player;
|
|
19 | 21 | import java.util.Arrays;
|
20 | 22 | import java.util.Collections;
|
21 | 23 | import java.util.List;
|
| 24 | +import java.util.stream.IntStream; |
22 | 25 |
|
23 | 26 | /**
|
24 | 27 | * 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.
|
@@ -49,6 +52,10 @@ public static List<ItemStack> addItemsToInventory(InventoryHolder container, boo
|
49 | 52 | return addItemsToFurnace((Furnace) container, stacks);
|
50 | 53 | } else if(container instanceof BrewingStand) {
|
51 | 54 | return addItemsToBrewingStand((BrewingStand) container, stacks);
|
| 55 | + } else if(container instanceof Crafter) { |
| 56 | + return addItemsToCrafter((Crafter) container, stacks); |
| 57 | + } else if(container instanceof ChiseledBookshelf) { |
| 58 | + return addItemsToChiseledBookshelf((ChiseledBookshelf) container, stacks); |
52 | 59 | } else { //Basic inventories like chests, dispensers, storage carts, etc.
|
53 | 60 | List<ItemStack> leftovers = new ArrayList<>();
|
54 | 61 | if (container instanceof ShulkerBox) {
|
@@ -156,6 +163,58 @@ public static List<ItemStack> addItemsToBrewingStand(BrewingStand brewingStand,
|
156 | 163 | return leftovers;
|
157 | 164 | }
|
158 | 165 |
|
| 166 | + /** |
| 167 | + * Adds items to a Crafter, respecting disabled slots, returning the leftovers. |
| 168 | + * |
| 169 | + * @param crafter The Crafter to add the items to. |
| 170 | + * @param stacks The stacks to add to the inventory. |
| 171 | + * @return The stacks that could not be added. |
| 172 | + */ |
| 173 | + public static List<ItemStack> addItemsToCrafter(Crafter crafter, ItemStack ... stacks) { |
| 174 | + |
| 175 | + List<ItemStack> leftovers = new ArrayList<>(); |
| 176 | + int[] availableSlots = IntStream.rangeClosed(0, crafter.getInventory().getSize() - 1).filter(slot -> !crafter.isSlotDisabled(slot)).toArray(); |
| 177 | + |
| 178 | + for(ItemStack stack : stacks) { |
| 179 | + Inventory inv = crafter.getInventory(); |
| 180 | + |
| 181 | + for (int i : availableSlots) { |
| 182 | + if (stack == null) { |
| 183 | + break; |
| 184 | + } |
| 185 | + if (inv.getItem(i) == null) { |
| 186 | + inv.setItem(i, stack); |
| 187 | + stack = null; |
| 188 | + } else { |
| 189 | + stack = ItemUtil.addToStack(inv.getItem(i), stack); |
| 190 | + } |
| 191 | + } |
| 192 | + if (stack != null) { |
| 193 | + leftovers.add(stack); |
| 194 | + } |
| 195 | + } |
| 196 | + return leftovers; |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Adds items to a chiseled bookshelf. |
| 201 | + * |
| 202 | + * @param chiseledBookshelf The chiseled bookshelf to add the items to. |
| 203 | + * @param stacks The stacks to add to the inventory. |
| 204 | + * @return The stacks that could not be added. |
| 205 | + */ |
| 206 | + public static List<ItemStack> addItemsToChiseledBookshelf(ChiseledBookshelf chiseledBookshelf, ItemStack ... stacks) { |
| 207 | + |
| 208 | + List<ItemStack> leftovers = new ArrayList<>(); |
| 209 | + |
| 210 | + Arrays.stream(stacks).filter(item -> !ItemUtil.isAStorableBook(item)).forEach(leftovers::add); |
| 211 | + stacks = Arrays.stream(stacks).filter(item -> ItemUtil.isAStorableBook(item)).toArray(ItemStack[]::new); |
| 212 | + |
| 213 | + leftovers.addAll(chiseledBookshelf.getInventory().addItem(stacks).values()); |
| 214 | + |
| 215 | + return leftovers; |
| 216 | + } |
| 217 | + |
159 | 218 | /**
|
160 | 219 | * Checks whether the inventory contains all the given itemstacks.
|
161 | 220 | *
|
@@ -308,6 +367,8 @@ public static boolean doesBlockHaveInventory(Block block) {
|
308 | 367 | case SMOKER:
|
309 | 368 | case BARREL:
|
310 | 369 | case CHISELED_BOOKSHELF:
|
| 370 | + case DECORATED_POT: |
| 371 | + case CRAFTER: |
311 | 372 | return true;
|
312 | 373 | default:
|
313 | 374 | return false;
|
|
0 commit comments