Skip to content

Commit e394600

Browse files
authored
Pipe support for crafter, decorated_pot and chiseled_bookshelf (#1333)
* add pipe support for crafter, decorated_pot and chiseled_bookshelf added crafter, decorated_pot as pipe input and output added chiseled_bookshelf as pipe input fixed non book items getting deleted when using chiseled_bookshelf as pipe output * fix pipe event leftovers not respecting disabled slots of crafters --------- Co-authored-by: ANRAR4 <->
1 parent c1f57c9 commit e394600

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

src/main/java/com/sk89q/craftbook/mechanics/pipe/Pipes.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.bukkit.block.Dropper;
3030
import org.bukkit.block.Furnace;
3131
import org.bukkit.block.Jukebox;
32+
import org.bukkit.block.Crafter;
3233
import org.bukkit.block.data.BlockData;
3334
import org.bukkit.block.data.Directional;
3435
import org.bukkit.block.data.type.Piston;
@@ -357,6 +358,9 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {
357358
|| fac.getType() == Material.DISPENSER
358359
|| fac.getType() == Material.HOPPER
359360
|| fac.getType() == Material.BARREL
361+
|| fac.getType() == Material.CHISELED_BOOKSHELF
362+
|| fac.getType() == Material.CRAFTER
363+
|| fac.getType() == Material.DECORATED_POT
360364
|| Tag.SHULKER_BOXES.isTagged(fac.getType())) {
361365
for (ItemStack stack : ((InventoryHolder) fac.getState()).getInventory().getContents()) {
362366

@@ -382,9 +386,13 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {
382386
}
383387

384388
if (!items.isEmpty()) {
385-
for (ItemStack item : items) {
386-
if (item == null) continue;
387-
leftovers.addAll(((InventoryHolder) fac.getState()).getInventory().addItem(item).values());
389+
if (fac.getType() == Material.CRAFTER)
390+
leftovers.addAll(InventoryUtil.addItemsToCrafter((Crafter) fac.getState(), items.toArray(new ItemStack[items.size()])));
391+
else {
392+
for (ItemStack item : items) {
393+
if (item == null) continue;
394+
leftovers.addAll(((InventoryHolder) fac.getState()).getInventory().addItem(item).values());
395+
}
388396
}
389397
}
390398
} else if (fac.getType() == Material.FURNACE || fac.getType() == Material.BLAST_FURNACE || fac.getType() == Material.SMOKER) {

src/main/java/com/sk89q/craftbook/util/InventoryUtil.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.bukkit.block.Block;
55
import org.bukkit.block.BrewingStand;
66
import org.bukkit.block.Chest;
7+
import org.bukkit.block.ChiseledBookshelf;
8+
import org.bukkit.block.Crafter;
79
import org.bukkit.block.Furnace;
810
import org.bukkit.block.ShulkerBox;
911
import org.bukkit.entity.Player;
@@ -19,6 +21,7 @@
1921
import java.util.Arrays;
2022
import java.util.Collections;
2123
import java.util.List;
24+
import java.util.stream.IntStream;
2225

2326
/**
2427
* 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
4952
return addItemsToFurnace((Furnace) container, stacks);
5053
} else if(container instanceof BrewingStand) {
5154
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);
5259
} else { //Basic inventories like chests, dispensers, storage carts, etc.
5360
List<ItemStack> leftovers = new ArrayList<>();
5461
if (container instanceof ShulkerBox) {
@@ -156,6 +163,58 @@ public static List<ItemStack> addItemsToBrewingStand(BrewingStand brewingStand,
156163
return leftovers;
157164
}
158165

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+
159218
/**
160219
* Checks whether the inventory contains all the given itemstacks.
161220
*
@@ -308,6 +367,8 @@ public static boolean doesBlockHaveInventory(Block block) {
308367
case SMOKER:
309368
case BARREL:
310369
case CHISELED_BOOKSHELF:
370+
case DECORATED_POT:
371+
case CRAFTER:
311372
return true;
312373
default:
313374
return false;

src/main/java/com/sk89q/craftbook/util/ItemUtil.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,26 @@ public static boolean isAPotionIngredient(ItemStack item) {
797797
}
798798
}
799799

800+
/**
801+
* Checks whether an item can be put in a chiseled bookshelf.
802+
*
803+
* @param item The item to check.
804+
* @return If the item can be put in a chiseled bookshelf.
805+
*/
806+
public static boolean isAStorableBook(ItemStack item) {
807+
808+
switch(item.getType()) {
809+
case BOOK:
810+
case WRITABLE_BOOK:
811+
case WRITTEN_BOOK:
812+
case ENCHANTED_BOOK:
813+
case KNOWLEDGE_BOOK:
814+
return true;
815+
default:
816+
return false;
817+
}
818+
}
819+
800820
public static boolean containsRawFood(Inventory inv) {
801821

802822
return getRawFood(inv).size() > 0;

0 commit comments

Comments
 (0)