Skip to content

Commit bc8db13

Browse files
committed
add InventoryManagerCuriosPlugin and allow pocket inventory upgrade
1 parent 46f8694 commit bc8db13

20 files changed

Lines changed: 329 additions & 128 deletions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "advancedperipherals:inventory_manager_pocket",
3+
"item": "advancedperipherals:inventory_manager"
4+
}

src/main/java/de/srendi/advancedperipherals/common/addons/APAddon.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public enum APAddon {
1010
AE2THINGS("ae2things"),
1111
APP_MEKANISTICS("appmek"),
1212
CURIOS("curios"),
13+
DIMSTORAGE("dimstorage"),
1314
MEKANISM("mekanism"),
1415
MINECOLONIES("minecolonies"),
1516
PATCHOULI("patchouli"),

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/integrations/IntegrationPeripheralProvider.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33
import dan200.computercraft.api.ComputerCraftAPI;
44
import dan200.computercraft.api.peripheral.PeripheralCapability;
55
import de.srendi.advancedperipherals.AdvancedPeripherals;
6+
import de.srendi.advancedperipherals.common.addons.APAddon;
67
import de.srendi.advancedperipherals.common.util.Platform;
78
import net.minecraft.world.level.block.Blocks;
89
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
910

10-
import java.util.Optional;
11-
1211
public class IntegrationPeripheralProvider {
1312

14-
private static final String[] SUPPORTED_MODS = new String[]{"mekanism", "powah"};
15-
1613
public static void load() {
1714
ComputerCraftAPI.registerGenericSource(new BeaconIntegration());
1815

19-
for (String mod : SUPPORTED_MODS) {
20-
Optional<Object> integration = Platform.maybeLoadIntegration(mod, mod + ".Integration");
21-
if (integration.isEmpty()) {
22-
AdvancedPeripherals.LOGGER.warn("Failed to load integration for {}", mod);
16+
for (APAddon addon : APAddon.values()) {
17+
if (!addon.isLoaded()) {
18+
continue;
19+
}
20+
String modid = addon.getModId();
21+
Runnable integration = Platform.maybeLoadIntegration(modid + ".Integration");
22+
if (integration == null) {
23+
AdvancedPeripherals.debug("Integration does not exist for {}", modid);
2324
continue;
2425
}
25-
Runnable runnable = (Runnable) integration.get();
26-
AdvancedPeripherals.LOGGER.info("Successfully loaded integration for {}", mod);
27-
runnable.run();
26+
AdvancedPeripherals.LOGGER.info("Loading integration for {}", modid);
27+
integration.run();
2828
}
2929
}
3030

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/InventoryManagerPeripheral.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import dan200.computercraft.api.lua.MethodResult;
66
import dan200.computercraft.api.peripheral.IComputerAccess;
77
import dan200.computercraft.api.peripheral.IPeripheral;
8+
import dan200.computercraft.api.pocket.IPocketAccess;
9+
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
810
import de.srendi.advancedperipherals.common.addons.computercraft.owner.InventoryManagerOwner;
11+
import de.srendi.advancedperipherals.common.addons.computercraft.owner.PocketPeripheralOwner;
912
import de.srendi.advancedperipherals.common.blocks.blockentities.InventoryManagerEntity;
1013
import de.srendi.advancedperipherals.common.configuration.APConfig;
1114
import de.srendi.advancedperipherals.common.util.EmptyLuaTable;
@@ -16,27 +19,46 @@
1619
import de.srendi.advancedperipherals.common.util.inventory.InventoryUtil;
1720
import de.srendi.advancedperipherals.common.util.inventory.ItemFilter;
1821
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
22+
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralPlugin;
1923
import net.minecraft.server.level.ServerPlayer;
2024
import net.minecraft.world.entity.player.Player;
2125
import net.minecraft.world.item.ItemStack;
2226
import net.neoforged.neoforge.items.IItemHandler;
2327
import net.neoforged.neoforge.items.wrapper.PlayerInvWrapper;
2428
import org.jetbrains.annotations.NotNull;
2529

30+
import java.util.ArrayList;
2631
import java.util.List;
2732
import java.util.Map;
2833
import java.util.Optional;
2934
import java.util.Set;
3035
import java.util.concurrent.ConcurrentHashMap;
36+
import java.util.function.Function;
3137

32-
public class InventoryManagerPeripheral extends BasePeripheral<InventoryManagerOwner> {
38+
public class InventoryManagerPeripheral extends BasePeripheral<IPeripheralOwner> {
3339
public static final String PLAYER_INV_MAGIC_NAME = "@";
3440
public static final String PERIPHERAL_TYPE = "inventory_manager";
41+
private static final List<Function<InventoryManagerPeripheral, IPeripheralPlugin>> PERIPHERAL_PLUGINS = new ArrayList<>();
3542

3643
private final Set<IComputerAccess> computerAccesses = ConcurrentHashMap.newKeySet();
3744

45+
protected InventoryManagerPeripheral(IPeripheralOwner owner) {
46+
super(PERIPHERAL_TYPE, owner);
47+
for (Function<InventoryManagerPeripheral, IPeripheralPlugin> plugin : PERIPHERAL_PLUGINS) {
48+
this.addPlugin(plugin.apply(this));
49+
}
50+
}
51+
3852
public InventoryManagerPeripheral(InventoryManagerEntity tileEntity) {
39-
super(PERIPHERAL_TYPE, new InventoryManagerOwner(tileEntity));
53+
this(new InventoryManagerOwner(tileEntity));
54+
}
55+
56+
public InventoryManagerPeripheral(IPocketAccess pocket) {
57+
this(PocketPeripheralOwner.of(pocket));
58+
}
59+
60+
public static void addIntegrationPlugin(Function<InventoryManagerPeripheral, IPeripheralPlugin> plugin) {
61+
PERIPHERAL_PLUGINS.add(plugin);
4062
}
4163

4264
@Override
@@ -68,24 +90,26 @@ protected Map<String, Object> getPeripheralConfiguration() {
6890
}
6991

7092
public Player getOwnerPlayer() {
71-
return owner.getOwner();
93+
return owner instanceof InventoryManagerOwner imOwner
94+
? imOwner.getOwner()
95+
: owner.getHoldingEntity() instanceof Player player ? player : null;
7296
}
7397

74-
private ServerPlayer getOwnerPlayerOrError() throws LuaException {
75-
Player player = owner.getOwner();
98+
public ServerPlayer getOwnerPlayerOrError() throws LuaException {
99+
Player player = this.getOwnerPlayer();
76100
if (player == null) {
77101
throw new LuaException("The Inventory Manager doesn't have a memory card or it isn't bound to a player.");
78102
}
79103
return (ServerPlayer) player;
80104
}
81105

82-
private IItemHandler getPlayerInventory() throws LuaException {
106+
public IItemHandler getPlayerInventory() throws LuaException {
83107
return new PlayerInvWrapper(this.getOwnerPlayerOrError().getInventory());
84108
}
85109

86110
@LuaFunction
87111
public final MethodResult getOwner() throws LuaException {
88-
Player player = owner.getOwner();
112+
Player player = this.getOwnerPlayer();
89113
if (player == null) {
90114
return MethodResult.of();
91115
}
@@ -98,7 +122,7 @@ public final int size() throws LuaException {
98122
}
99123

100124
@LuaFunction(mainThread = true)
101-
public final Map<Integer, Object> list() throws LuaException {
125+
public final Map<Integer, ?> list() throws LuaException {
102126
return InventoryUtil.list(this.getPlayerInventory());
103127
}
104128

@@ -111,8 +135,8 @@ public final MethodResult pushItems(IComputerAccess computer, String toName, Opt
111135
return MethodResult.of(null, filter.right());
112136
}
113137

114-
IItemHandler inventoryTo = this.getInventoryHandler(computer, toName);
115138
IItemHandler inventoryFrom = this.getPlayerInventory();
139+
IItemHandler inventoryTo = this.getItemHandler(computer, toName);
116140
return MethodResult.of(ItemUtil.moveItem(inventoryFrom, inventoryTo, filter.left()));
117141
}
118142

@@ -125,8 +149,8 @@ public final MethodResult pullItems(IComputerAccess computer, String fromName, O
125149
return MethodResult.of(null, filter.right());
126150
}
127151

128-
IItemHandler inventoryFrom = this.getInventoryHandler(computer, fromName);
129152
IItemHandler inventoryTo = this.getPlayerInventory();
153+
IItemHandler inventoryFrom = this.getItemHandler(computer, fromName);
130154
return MethodResult.of(ItemUtil.moveItem(inventoryFrom, inventoryTo, filter.left()));
131155
}
132156

@@ -174,25 +198,18 @@ public final Map<String, Object> getItemInOffHand() throws LuaException {
174198
return LuaConverter.itemStackToLua(this.getOwnerPlayerOrError().getOffhandItem());
175199
}
176200

177-
private void assertAllowItemTransfers() throws LuaException {
201+
public void assertAllowItemTransfers() throws LuaException {
178202
if (!APConfig.PERIPHERALS_CONFIG.enableItemsTransfer.get()) {
179203
throw new LuaException("This function is disabled in the config [Inventory_Manager.enableItemsTransfer]. Activate it or ask admins if they can activate it.");
180204
}
181205
}
182206

207+
@Override
183208
@NotNull
184-
private IItemHandler getInventoryHandler(IComputerAccess computer, String name) throws LuaException {
209+
public IItemHandler getItemHandler(IComputerAccess computer, String name) throws LuaException {
185210
if (name.equals(PLAYER_INV_MAGIC_NAME)) {
186211
return this.getPlayerInventory();
187212
}
188-
IPeripheral toPeripheral = computer.getAvailablePeripheral(name);
189-
if (toPeripheral == null) {
190-
throw new LuaException("Target '" + name + "' does not exist");
191-
}
192-
IItemHandler inventory = ItemUtil.extractHandler(toPeripheral);
193-
if (inventory == null) {
194-
throw new LuaException("Target '" + name + "' is not an inventory");
195-
}
196-
return inventory;
213+
return super.getItemHandler(computer, name);
197214
}
198215
}

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/MEBridgePeripheral.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import de.srendi.advancedperipherals.common.util.inventory.ItemFilter;
3838
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
3939
import me.ramidzkh.mekae2.ae2.MekanismKey;
40+
import net.minecraft.core.Direction;
4041
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
4142
import net.neoforged.neoforge.items.IItemHandler;
4243
import org.jetbrains.annotations.NotNull;
@@ -340,14 +341,20 @@ public final MethodResult importItem(IComputerAccess computer, IArguments argume
340341
return notConnected(0);
341342

342343
String side = arguments.getString(1);
343-
IItemHandler inventory = ItemUtil.getHandlerFromDirection(side, owner);
344-
345-
if (inventory == null) {
346-
inventory = ItemUtil.getHandlerFromName(computer, side);
344+
IItemHandler inventory;
345+
if (side.length() >= 1 && side.charAt(0) == '@') {
346+
Direction dir = this.mapDirection(side.substring(1));
347+
if (dir == null) {
348+
throw new LuaException("Target '" + side + "' is an invalid direction");
349+
}
350+
inventory = ItemUtil.getHandlerFromDirection(owner, dir);
351+
} else {
352+
inventory = ItemUtil.extractHandler(computer.getAvailablePeripheral(side));
347353
}
348354

349-
if (inventory == null)
355+
if (inventory == null) {
350356
return MethodResult.of(0, StatusConstants.INVENTORY_NOT_FOUND.name());
357+
}
351358

352359
return importToME(arguments, inventory);
353360
}
@@ -359,14 +366,20 @@ public final MethodResult exportItem(IComputerAccess computer, @NotNull IArgumen
359366
return notConnected(0);
360367

361368
String side = arguments.getString(1);
362-
IItemHandler inventory = ItemUtil.getHandlerFromDirection(side, owner);
363-
364-
if (inventory == null) {
365-
inventory = ItemUtil.getHandlerFromName(computer, side);
369+
IItemHandler inventory;
370+
if (side.length() >= 1 && side.charAt(0) == '@') {
371+
Direction dir = this.mapDirection(side.substring(1));
372+
if (dir == null) {
373+
throw new LuaException("Target '" + side + "' is an invalid direction");
374+
}
375+
inventory = ItemUtil.getHandlerFromDirection(owner, dir);
376+
} else {
377+
inventory = ItemUtil.extractHandler(computer.getAvailablePeripheral(side));
366378
}
367379

368-
if (inventory == null)
380+
if (inventory == null) {
369381
return MethodResult.of(0, StatusConstants.INVENTORY_NOT_FOUND.name());
382+
}
370383

371384
return exportToChest(arguments, inventory);
372385
}

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/RSBridgePeripheral.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import de.srendi.advancedperipherals.common.util.inventory.ItemUtil;
4040
import de.srendi.advancedperipherals.common.util.inventory.ItemFilter;
4141
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
42+
import net.minecraft.core.Direction;
4243
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
4344
import net.neoforged.neoforge.items.IItemHandler;
4445
import org.jetbrains.annotations.NotNull;
@@ -376,13 +377,20 @@ public final MethodResult importItem(IComputerAccess computer, IArguments argume
376377
if (!isAvailable())
377378
return notConnected(0);
378379

379-
IItemHandler inventory = ItemUtil.getHandlerFromDirection(arguments.getString(1), owner);
380+
String side = arguments.getString(1);
381+
IItemHandler inventory;
382+
if (side.length() >= 1 && side.charAt(0) == '@') {
383+
Direction dir = this.mapDirection(side.substring(1));
384+
if (dir == null) {
385+
throw new LuaException("Target '" + side + "' is an invalid direction");
386+
}
387+
inventory = ItemUtil.getHandlerFromDirection(owner, dir);
388+
} else {
389+
inventory = ItemUtil.extractHandler(computer.getAvailablePeripheral(side));
390+
}
380391

381392
if (inventory == null) {
382-
inventory = ItemUtil.getHandlerFromName(computer, arguments.getString(1));
383-
if (inventory == null) {
384-
return MethodResult.of(0, StatusConstants.INVENTORY_NOT_FOUND.name());
385-
}
393+
return MethodResult.of(0, StatusConstants.INVENTORY_NOT_FOUND.name());
386394
}
387395

388396
return importToRS(arguments, inventory);
@@ -394,13 +402,20 @@ public final MethodResult exportItem(IComputerAccess computer, IArguments argume
394402
if (!isAvailable())
395403
return notConnected(0);
396404

397-
IItemHandler inventory = ItemUtil.getHandlerFromDirection(arguments.getString(1), owner);
405+
String side = arguments.getString(1);
406+
IItemHandler inventory;
407+
if (side.length() >= 1 && side.charAt(0) == '@') {
408+
Direction dir = this.mapDirection(side.substring(1));
409+
if (dir == null) {
410+
throw new LuaException("Target '" + side + "' is an invalid direction");
411+
}
412+
inventory = ItemUtil.getHandlerFromDirection(owner, dir);
413+
} else {
414+
inventory = ItemUtil.extractHandler(computer.getAvailablePeripheral(side));
415+
}
398416

399417
if (inventory == null) {
400-
inventory = ItemUtil.getHandlerFromName(computer, arguments.getString(1));
401-
if (inventory == null) {
402-
return MethodResult.of(0, StatusConstants.INVENTORY_NOT_FOUND.name());
403-
}
418+
return MethodResult.of(0, StatusConstants.INVENTORY_NOT_FOUND.name());
404419
}
405420

406421
return exportToChest(arguments, inventory);

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataBlockHandPlugin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ public final MethodResult placeBlock(@NotNull IArguments arguments) throws LuaEx
184184
String anchor = options.optString("anchor").orElse(null);
185185
String front = options.optString("front").orElse(null);
186186
String top = options.optString("top").orElse(null);
187-
Direction anchorDir = anchor != null ? automataCore.validateSide(anchor) : null;
188-
Direction frontDir = front != null ? automataCore.validateSide(front) : null;
189-
Direction topDir = top != null ? automataCore.validateSide(top) : null;
187+
Direction anchorDir = anchor != null ? automataCore.mapDirection(anchor) : null;
188+
Direction frontDir = front != null ? automataCore.mapDirection(front) : null;
189+
Direction topDir = top != null ? automataCore.mapDirection(top) : null;
190190

191191
int distance =
192192
Math.max(0, Math.abs(x) - freeDist) +
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.srendi.advancedperipherals.common.addons.computercraft.pocket;
2+
3+
import dan200.computercraft.api.pocket.IPocketAccess;
4+
import dan200.computercraft.api.pocket.IPocketUpgrade;
5+
import dan200.computercraft.api.upgrades.UpgradeType;
6+
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.InventoryManagerPeripheral;
7+
import de.srendi.advancedperipherals.common.setup.CCRegistration;
8+
import de.srendi.advancedperipherals.lib.pocket.BasePocketUpgrade;
9+
import net.minecraft.world.item.ItemStack;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
public class PocketInventoryManagerUpgrade extends BasePocketUpgrade<InventoryManagerPeripheral> {
13+
14+
public PocketInventoryManagerUpgrade(ItemStack stack) {
15+
super(CCRegistration.ID.INVENTORY_MANAGER_POCKET, stack);
16+
}
17+
18+
@Override
19+
@NotNull
20+
protected InventoryManagerPeripheral buildPeripheral(@NotNull IPocketAccess pocketAccess) {
21+
return new InventoryManagerPeripheral(pocketAccess);
22+
}
23+
24+
@Override
25+
public UpgradeType<? extends IPocketUpgrade> getType() {
26+
return CCRegistration.INVENTORY_MANAGER_POCKET.get();
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package de.srendi.advancedperipherals.common.addons.curios;
2+
3+
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.InventoryManagerPeripheral;
4+
5+
public final class Integration implements Runnable {
6+
@Override
7+
public void run() {
8+
InventoryManagerPeripheral.addIntegrationPlugin(InventoryManagerCuriosPlugin::new);
9+
}
10+
}

0 commit comments

Comments
 (0)