Skip to content

Commit d4d7762

Browse files
committed
add wrapCuriosStorageItem
1 parent ab89fe2 commit d4d7762

6 files changed

Lines changed: 97 additions & 30 deletions

File tree

src/main/java/de/srendi/advancedperipherals/common/addons/curios/InventoryManagerCuriosPlugin.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import de.srendi.advancedperipherals.common.util.inventory.InventoryUtil;
1111
import de.srendi.advancedperipherals.common.util.inventory.ItemFilter;
1212
import de.srendi.advancedperipherals.common.util.inventory.ItemUtil;
13+
import de.srendi.advancedperipherals.common.util.inventory.PlayerStorageItemWrapper;
1314
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralPlugin;
1415
import net.minecraft.core.NonNullList;
16+
import net.minecraft.world.item.ItemStack;
1517
import net.neoforged.neoforge.items.IItemHandler;
1618
import org.jetbrains.annotations.NotNull;
1719
import top.theillusivec4.curios.api.CuriosApi;
@@ -63,13 +65,13 @@ public final Map<String, Integer> getCuriosSizes() throws LuaException {
6365
}
6466

6567
@NotNull
66-
private IDynamicStackHandler getCuriosHandler(String id) throws LuaException {
68+
private IDynamicStackHandler getCuriosHandler(String curiosId) throws LuaException {
6769
final ICuriosItemHandler curiosInv = CuriosApi.getCuriosInventory(this.peripheral.getOwnerPlayerOrError()).orElse(null);
6870
if (curiosInv == null) {
69-
throw new LuaException("Curios slot '" + id + "' does not exist");
71+
throw new LuaException("Curios slot '" + curiosId + "' does not exist");
7072
}
71-
return curiosInv.getStacksHandler(id)
72-
.orElseThrow(() -> new LuaException("Curios slot '" + id + "' does not exist"))
73+
return curiosInv.getStacksHandler(curiosId)
74+
.orElseThrow(() -> new LuaException("Curios slot '" + curiosId + "' does not exist"))
7375
.getStacks();
7476
}
7577

@@ -100,4 +102,22 @@ public final MethodResult pullCuriosItems(IComputerAccess computer, String toCur
100102
IItemHandler inventoryFrom = this.peripheral.getItemHandler(computer, fromName);
101103
return MethodResult.of(ItemUtil.moveItem(inventoryFrom, inventoryTo, filter.left()));
102104
}
105+
106+
@LuaFunction(mainThread = true)
107+
public final PlayerStorageItemWrapper wrapCuriosStorageItem(IComputerAccess computer, String curiosId, int slot) throws LuaException {
108+
return PlayerStorageItemWrapper.create(computer, this.peripheral, this.peripheral.getOwnerPlayerOrError(), (player) -> {
109+
final ICuriosItemHandler curiosInv = CuriosApi.getCuriosInventory(player).orElse(null);
110+
if (curiosInv == null) {
111+
return ItemStack.EMPTY;
112+
}
113+
final ICurioStacksHandler handler = curiosInv.getStacksHandler(curiosId).orElse(null);
114+
if (handler == null) {
115+
return ItemStack.EMPTY;
116+
}
117+
if (slot <= 0 || slot > handler.getSlots()) {
118+
return ItemStack.EMPTY;
119+
}
120+
return handler.getStacks().getStackInSlot(slot - 1);
121+
});
122+
}
103123
}

src/main/java/de/srendi/advancedperipherals/common/util/inventory/ChemicalUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dan200.computercraft.api.peripheral.IPeripheral;
44
import dan200.computercraft.shared.peripheral.generic.GenericPeripheral;
5+
import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner;
56
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
67
import de.srendi.advancedperipherals.common.util.FingerprintUtil;
78
import mekanism.api.Action;
@@ -119,7 +120,7 @@ public static IChemicalHandler extractHandler(@Nullable Object object, @Nullable
119120
}
120121

121122
@Nullable
122-
public static IChemicalHandler getHandlerFromDirection(@NotNull IPeripheralOwner owner, @NotNull Direction direction) {
123+
public static IChemicalHandler getHandlerFromDirection(@NotNull BlockEntityPeripheralOwner<?> owner, @NotNull Direction direction) {
123124
Level level = Objects.requireNonNull(owner.getLevel());
124125
BlockEntity target = level.getBlockEntity(owner.getPos().relative(direction));
125126
if (target == null) {

src/main/java/de/srendi/advancedperipherals/common/util/inventory/FluidUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dan200.computercraft.api.peripheral.IPeripheral;
44
import dan200.computercraft.shared.peripheral.generic.GenericPeripheral;
5+
import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner;
56
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
67
import de.srendi.advancedperipherals.common.util.FingerprintUtil;
78
import net.minecraft.core.BlockPos;
@@ -95,7 +96,7 @@ public static IFluidHandler extractHandler(@Nullable Object object, @Nullable Le
9596
}
9697

9798
@Nullable
98-
public static IFluidHandler getHandlerFromDirection(@NotNull IPeripheralOwner owner, @NotNull Direction direction) {
99+
public static IFluidHandler getHandlerFromDirection(@NotNull BlockEntityPeripheralOwner<?> owner, @NotNull Direction direction) {
99100
Level level = Objects.requireNonNull(owner.getLevel());
100101
BlockEntity target = level.getBlockEntity(owner.getPos().relative(direction));
101102
if (target == null) {

src/main/java/de/srendi/advancedperipherals/common/util/inventory/ItemUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dan200.computercraft.api.peripheral.IPeripheral;
44
import dan200.computercraft.shared.peripheral.generic.GenericPeripheral;
5+
import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner;
56
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
67
import de.srendi.advancedperipherals.common.util.FingerprintUtil;
78
import net.minecraft.core.BlockPos;
@@ -129,7 +130,7 @@ public static int moveItem(IItemHandler inventoryFrom, IItemHandler inventoryTo,
129130
}
130131

131132
@Nullable
132-
public static IItemHandler getHandlerFromDirection(@NotNull IPeripheralOwner owner, @NotNull Direction direction) {
133+
public static IItemHandler getHandlerFromDirection(@NotNull BlockEntityPeripheralOwner<?> owner, @NotNull Direction direction) {
133134
Level level = Objects.requireNonNull(owner.getLevel());
134135
BlockEntity target = level.getBlockEntity(owner.getPos().relative(direction));
135136
if (target == null) {

src/main/java/de/srendi/advancedperipherals/common/util/inventory/PlayerStorageItemWrapper.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import de.srendi.advancedperipherals.common.util.EmptyLuaTable;
1010
import de.srendi.advancedperipherals.common.util.Pair;
1111
import net.minecraft.server.level.ServerPlayer;
12+
import net.minecraft.world.entity.player.Inventory;
1213
import net.minecraft.world.item.ItemStack;
1314
import net.neoforged.neoforge.capabilities.Capabilities;
1415
import net.neoforged.neoforge.items.IItemHandler;
@@ -19,44 +20,63 @@
1920
import java.lang.ref.WeakReference;
2021
import java.util.Map;
2122
import java.util.Optional;
23+
import java.util.function.Function;
2224

2325
// TODO: fluid variant?
2426
public class PlayerStorageItemWrapper {
2527
private final IComputerAccess computer;
2628
private final WeakReference<InventoryManagerPeripheral> peripheral;
2729
private final WeakReference<ServerPlayer> player;
28-
private final int slot;
30+
private final Function<ServerPlayer, ItemStack> itemProvider;
2931
private final ItemStack stack;
30-
private final IItemHandler handler;
31-
32-
protected PlayerStorageItemWrapper(IComputerAccess computer, InventoryManagerPeripheral peripheral, ServerPlayer player, int slot, ItemStack stack, IItemHandler handler) {
32+
private final IItemHandler itemHandler;
33+
34+
protected PlayerStorageItemWrapper(
35+
IComputerAccess computer,
36+
InventoryManagerPeripheral peripheral,
37+
ServerPlayer player,
38+
Function<ServerPlayer, ItemStack> itemProvider,
39+
ItemStack stack,
40+
IItemHandler itemHandler
41+
) {
3342
this.computer = computer;
3443
this.peripheral = new WeakReference<>(peripheral);
3544
this.player = new WeakReference<>(player);
36-
this.slot = slot;
45+
this.itemProvider = itemProvider;
3746
this.stack = stack;
38-
this.handler = handler;
47+
this.itemHandler = itemHandler;
3948
}
4049

4150
@Nullable
4251
public static PlayerStorageItemWrapper create(IComputerAccess computer, InventoryManagerPeripheral peripheral, @NotNull ServerPlayer player, int slot) {
43-
ItemStack stack = player.getInventory().getItem(slot);
52+
return create(computer, peripheral, player, (p) -> {
53+
Inventory inventory = p.getInventory();
54+
if (slot < 0 || slot >= inventory.getContainerSize()) {
55+
return ItemStack.EMPTY;
56+
}
57+
return inventory.getItem(slot);
58+
});
59+
}
60+
61+
@Nullable
62+
public static PlayerStorageItemWrapper create(IComputerAccess computer, InventoryManagerPeripheral peripheral, @NotNull ServerPlayer player, Function<ServerPlayer, ItemStack> itemProvider) {
63+
ItemStack stack = itemProvider.apply(player);
4464
if (stack.isEmpty()) {
4565
return null;
4666
}
47-
IItemHandler handler = stack.getCapability(Capabilities.ItemHandler.ITEM);
48-
if (handler == null) {
67+
IItemHandler itemHandler = stack.getCapability(Capabilities.ItemHandler.ITEM);
68+
if (itemHandler == null) {
4969
return null;
5070
}
51-
return new PlayerStorageItemWrapper(computer, peripheral, player, slot, stack, handler);
71+
return new PlayerStorageItemWrapper(computer, peripheral, player, itemProvider, stack, itemHandler);
5272
}
5373

5474
public boolean isValid() {
5575
ServerPlayer player = this.player.get();
5676
if (player == null || player.isRemoved()) {
5777
return false;
5878
}
59-
if (player.getInventory().getItem(this.slot) != this.stack) {
79+
if (this.itemProvider.apply(player) != this.stack) {
6080
return false;
6181
}
6282
InventoryManagerPeripheral peripheral = this.peripheral.get();
@@ -80,13 +100,13 @@ public final boolean isValidLua() {
80100
@LuaFunction(mainThread = true)
81101
public final int size() throws LuaException {
82102
this.assertValid();
83-
return this.handler.getSlots();
103+
return this.itemHandler.getSlots();
84104
}
85105

86106
@LuaFunction(mainThread = true)
87107
public final Map<Integer, ?> list() throws LuaException {
88108
this.assertValid();
89-
return InventoryUtil.list(this.handler);
109+
return InventoryUtil.list(this.itemHandler);
90110
}
91111

92112
@LuaFunction(mainThread = true)
@@ -100,7 +120,7 @@ public final MethodResult pushItems(String toName, Optional<Map<?, ?>> filterTab
100120

101121
IItemHandler inventoryTo = this.getItemHandler(toName);
102122

103-
return MethodResult.of(ItemUtil.moveItem(this.handler, inventoryTo, filter.left()));
123+
return MethodResult.of(ItemUtil.moveItem(this.itemHandler, inventoryTo, filter.left()));
104124
}
105125

106126
@LuaFunction(mainThread = true)
@@ -114,7 +134,7 @@ public final MethodResult pullItems(String fromName, Optional<Map<?, ?>> filterT
114134

115135
IItemHandler inventoryFrom = this.getItemHandler(fromName);
116136

117-
return MethodResult.of(ItemUtil.moveItem(inventoryFrom, this.handler, filter.left()));
137+
return MethodResult.of(ItemUtil.moveItem(inventoryFrom, this.itemHandler, filter.left()));
118138
}
119139

120140
@NotNull

src/main/java/de/srendi/advancedperipherals/lib/peripherals/BasePeripheral.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import dan200.computercraft.api.peripheral.IComputerAccess;
1010
import dan200.computercraft.api.peripheral.IDynamicPeripheral;
1111
import dan200.computercraft.api.peripheral.IPeripheral;
12+
import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner;
1213
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
1314
import de.srendi.advancedperipherals.common.addons.computercraft.owner.OperationAbility;
1415
import de.srendi.advancedperipherals.common.addons.computercraft.owner.PeripheralOwnerAbility;
@@ -196,24 +197,24 @@ protected <T> MethodResult withOperation(IPeripheralOperation<T> operation, T co
196197

197198
@Nullable
198199
protected IItemHandler getItemHandlerOrNull(IComputerAccess computer, String name) {
199-
if (name.length() >= 1 && name.charAt(0) == '@') {
200+
if (name.length() >= 1 && name.charAt(0) == '@' && this.owner instanceof BlockEntityPeripheralOwner<?> beOwner) {
200201
Direction dir = this.mapDirection(name.substring(1));
201202
if (dir == null) {
202203
return null;
203204
}
204-
return ItemUtil.getHandlerFromDirection(owner, dir);
205+
return ItemUtil.getHandlerFromDirection(beOwner, dir);
205206
}
206207
return ItemUtil.extractHandler(computer.getAvailablePeripheral(name));
207208
}
208209

209210
@NotNull
210211
protected IItemHandler getItemHandler(IComputerAccess computer, String name) throws LuaException {
211-
if (name.length() >= 1 && name.charAt(0) == '@') {
212+
if (name.length() >= 1 && name.charAt(0) == '@' && this.owner instanceof BlockEntityPeripheralOwner<?> beOwner) {
212213
Direction dir = this.mapDirection(name.substring(1));
213214
if (dir == null) {
214215
throw new LuaException("Target '" + name + "' is an invalid direction");
215216
}
216-
IItemHandler inventory = ItemUtil.getHandlerFromDirection(owner, dir);
217+
IItemHandler inventory = ItemUtil.getHandlerFromDirection(beOwner, dir);
217218
if (inventory == null) {
218219
throw new LuaException("Target '" + name + "' is not an inventory");
219220
}
@@ -232,25 +233,48 @@ protected IItemHandler getItemHandler(IComputerAccess computer, String name) thr
232233

233234
@Nullable
234235
protected IFluidHandler getFluidHandlerOrNull(IComputerAccess computer, String name) {
235-
if (name.length() >= 1 && name.charAt(0) == '@') {
236+
if (name.length() >= 1 && name.charAt(0) == '@' && this.owner instanceof BlockEntityPeripheralOwner<?> beOwner) {
236237
Direction dir = this.mapDirection(name.substring(1));
237238
if (dir == null) {
238239
return null;
239240
}
240-
return FluidUtil.getHandlerFromDirection(owner, dir);
241+
return FluidUtil.getHandlerFromDirection(beOwner, dir);
241242
}
242243
return FluidUtil.extractHandler(computer.getAvailablePeripheral(name));
243244
}
244245

246+
@NotNull
247+
protected IFluidHandler getFluidHandler(IComputerAccess computer, String name) throws LuaException {
248+
if (name.length() >= 1 && name.charAt(0) == '@' && this.owner instanceof BlockEntityPeripheralOwner<?> beOwner) {
249+
Direction dir = this.mapDirection(name.substring(1));
250+
if (dir == null) {
251+
throw new LuaException("Target '" + name + "' is an invalid direction");
252+
}
253+
IFluidHandler tank = FluidUtil.getHandlerFromDirection(beOwner, dir);
254+
if (tank == null) {
255+
throw new LuaException("Target '" + name + "' is not a tank");
256+
}
257+
return tank;
258+
}
259+
IPeripheral toPeripheral = computer.getAvailablePeripheral(name);
260+
if (toPeripheral == null) {
261+
throw new LuaException("Target '" + name + "' does not exist");
262+
}
263+
IFluidHandler tank = FluidUtil.extractHandler(toPeripheral);
264+
if (tank == null) {
265+
throw new LuaException("Target '" + name + "' is not a tank");
266+
}
267+
return tank;
268+
}
245269

246270
@Nullable
247271
protected Object /*IChemicalHandler*/ getChemicalHandlerOrNull(IComputerAccess computer, String name) {
248-
if (name.length() >= 1 && name.charAt(0) == '@') {
272+
if (name.length() >= 1 && name.charAt(0) == '@' && this.owner instanceof BlockEntityPeripheralOwner<?> beOwner) {
249273
Direction dir = this.mapDirection(name.substring(1));
250274
if (dir == null) {
251275
return null;
252276
}
253-
return ChemicalUtil.getHandlerFromDirection(owner, dir);
277+
return ChemicalUtil.getHandlerFromDirection(beOwner, dir);
254278
}
255279
return ChemicalUtil.extractHandler(computer.getAvailablePeripheral(name));
256280
}

0 commit comments

Comments
 (0)