|
1 | 1 | package de.srendi.advancedperipherals.common.util.inventory; |
2 | 2 |
|
3 | | -import dan200.computercraft.api.lua.LuaException; |
4 | | -import dan200.computercraft.api.peripheral.IComputerAccess; |
5 | | -import dan200.computercraft.api.peripheral.IPeripheral; |
6 | | -import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner; |
7 | | -import de.srendi.advancedperipherals.common.util.CoordUtil; |
8 | | -import net.minecraft.core.BlockPos; |
9 | | -import net.minecraft.core.Direction; |
10 | | -import net.minecraft.world.Container; |
11 | | -import net.minecraft.world.item.ItemStack; |
12 | | -import net.minecraft.world.level.Level; |
13 | | -import net.minecraft.world.level.block.entity.BlockEntity; |
14 | | -import net.neoforged.neoforge.capabilities.Capabilities; |
15 | | -import net.neoforged.neoforge.items.IItemHandler; |
16 | | -import net.neoforged.neoforge.items.ItemHandlerHelper; |
17 | | -import net.neoforged.neoforge.items.wrapper.InvWrapper; |
18 | | -import org.jetbrains.annotations.NotNull; |
19 | | -import org.jetbrains.annotations.Nullable; |
20 | | - |
21 | | -import java.util.Objects; |
22 | | -import java.util.stream.IntStream; |
23 | | - |
24 | 3 | public class InventoryUtil { |
25 | 4 |
|
26 | 5 | private InventoryUtil() { |
27 | 6 | } |
28 | 7 |
|
29 | | - public static IItemHandler extractHandler(@Nullable Object object, @Nullable Level level, @Nullable BlockPos pos, @Nullable Direction direction) { |
30 | | - if (object instanceof IItemHandler itemHandler) { |
31 | | - return itemHandler; |
32 | | - } |
33 | | - if (object instanceof Container container) { |
34 | | - return new InvWrapper(container); |
35 | | - } |
36 | | - if (object instanceof BlockEntity blockEntity && level == null && pos == null) { |
37 | | - pos = blockEntity.getBlockPos(); |
38 | | - level = blockEntity.getLevel(); |
39 | | - } |
40 | | - if (level != null && pos != null) { |
41 | | - return level.getCapability(Capabilities.ItemHandler.BLOCK, pos, direction != null ? direction : Direction.NORTH); |
42 | | - } |
43 | | - return null; |
44 | | - } |
45 | | - |
46 | | - public static int moveItem(IItemHandler inventoryFrom, IItemHandler inventoryTo, ItemFilter filter) { |
47 | | - if (inventoryFrom == null) { |
48 | | - return 0; |
49 | | - } |
50 | | - |
51 | | - int fromSlot = filter.getFromSlot(); |
52 | | - int toSlot = filter.getToSlot(); |
53 | | - |
54 | | - if (!(inventoryFrom instanceof IStorageSystemItemHandler) && fromSlot >= inventoryFrom.getSlots()) { |
55 | | - return 0; |
56 | | - } |
57 | | - if (!(inventoryTo instanceof IStorageSystemItemHandler) && toSlot >= inventoryTo.getSlots()) { |
58 | | - return 0; |
59 | | - } |
60 | | - |
61 | | - int needs = filter.getCount(); |
62 | | - if (needs <= 0) { |
63 | | - return 0; |
64 | | - } |
65 | | - |
66 | | - ItemInserter inserter = inventoryTo instanceof IStorageSystemItemHandler storageTo |
67 | | - ? (stack) -> storageTo.insertItem(stack, false) |
68 | | - : toSlot < 0 |
69 | | - ? (stack) -> ItemHandlerHelper.insertItem(inventoryTo, stack, false) |
70 | | - : (stack) -> inventoryTo.insertItem(toSlot, stack, false); |
71 | | - |
72 | | - // The logic changes with storage systems since these systems do not have slots |
73 | | - if (inventoryFrom instanceof IStorageSystemItemHandler storageFrom) { |
74 | | - return storageFrom.extractItems(filter, (extracted) -> extracted.getCount() - inserter.insertItem(extracted).getCount(), false); |
75 | | - } |
76 | | - |
77 | | - int[] fromSlots = ( |
78 | | - fromSlot >= 0 |
79 | | - ? IntStream.of(fromSlot) |
80 | | - : IntStream.range(0, inventoryFrom.getSlots()) |
81 | | - ) |
82 | | - .filter((i) -> filter.test(inventoryFrom.getStackInSlot(i))) |
83 | | - .toArray(); |
84 | | - if (fromSlots.length == 0) { |
85 | | - return 0; |
86 | | - } |
87 | | - |
88 | | - for (int i : fromSlots) { |
89 | | - ItemStack extracted = inventoryFrom.extractItem(i, needs, true); |
90 | | - if (extracted.isEmpty()) { |
91 | | - continue; |
92 | | - } |
93 | | - ItemStack remaining = inserter.insertItem(extracted); |
94 | | - int inserted = extracted.getCount() - remaining.getCount(); |
95 | | - if (inserted == 0) { |
96 | | - continue; |
97 | | - } |
98 | | - needs -= inserted; |
99 | | - inventoryFrom.extractItem(i, inserted, false); |
100 | | - if (needs <= 0) { |
101 | | - break; |
102 | | - } |
103 | | - } |
104 | | - return filter.getCount() - needs; |
105 | | - } |
106 | | - |
107 | | - @Nullable |
108 | | - public static IItemHandler getHandlerFromName(@NotNull IComputerAccess access, String name) throws LuaException { |
109 | | - IPeripheral location = access.getAvailablePeripheral(name); |
110 | | - if (location == null) { |
111 | | - return null; |
112 | | - } |
113 | | - return extractHandler(location.getTarget(), null, null, null); |
114 | | - } |
115 | | - |
116 | | - @Nullable |
117 | | - public static IItemHandler getHandlerFromDirection(@NotNull String direction, @NotNull IPeripheralOwner owner) throws LuaException { |
118 | | - Level level = Objects.requireNonNull(owner.getLevel()); |
119 | | - Direction relativeDirection = CoordUtil.getDirection(owner.getFrontAndTop(), direction); |
120 | | - if (relativeDirection == null) { |
121 | | - return null; |
122 | | - } |
123 | | - BlockEntity target = level.getBlockEntity(owner.getPos().relative(relativeDirection)); |
124 | | - if (target == null) { |
125 | | - return null; |
126 | | - } |
127 | | - return extractHandler(target, level, target.getBlockPos(), relativeDirection.getOpposite()); |
128 | | - } |
129 | | - |
130 | | - @FunctionalInterface |
131 | | - private interface ItemInserter { |
132 | | - ItemStack insertItem(ItemStack stack); |
133 | | - } |
134 | 8 | } |
0 commit comments