Skip to content

Commit 22a6ed4

Browse files
committed
add AbstractStorageSystemPeripheral and migrate MEBridgePeripheral
1 parent 81c86db commit 22a6ed4

8 files changed

Lines changed: 1018 additions & 805 deletions

File tree

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

Lines changed: 796 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 135 additions & 615 deletions
Large diffs are not rendered by default.

src/main/java/de/srendi/advancedperipherals/common/util/StatusConstants.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package de.srendi.advancedperipherals.common.util;
22

3-
import de.srendi.advancedperipherals.common.addons.APAddon;
4-
53
/**
64
* A collection of constants used as return types for several peripherals
75
*/
@@ -41,12 +39,4 @@ public enum StatusConstants {
4139
NOT_FOUND, // Generic not found state
4240
ADDON_NOT_LOADED,
4341
UNKNOWN_ERROR;
44-
45-
public String withInfo(String extraInfo) {
46-
return this + "_" + extraInfo;
47-
}
48-
49-
public String withInfo(APAddon addon) {
50-
return this + "_" + addon.getModId();
51-
}
5242
}

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

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dan200.computercraft.api.lua.LuaException;
44
import dan200.computercraft.api.peripheral.IComputerAccess;
55
import dan200.computercraft.api.peripheral.IPeripheral;
6+
import dan200.computercraft.shared.peripheral.generic.GenericPeripheral;
67
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
78
import de.srendi.advancedperipherals.common.util.CoordUtil;
89
import de.srendi.advancedperipherals.common.util.FingerprintUtil;
@@ -90,9 +91,26 @@ public static long moveChemical(IChemicalHandler inventoryFrom, IChemicalHandler
9091
return filter.getAmount() - needs;
9192
}
9293

94+
@Nullable
95+
public static IChemicalHandler extractHandler(@Nullable IPeripheral peripheral) {
96+
if (peripheral == null) {
97+
return null;
98+
}
99+
Object target = peripheral.getTarget();
100+
if (target instanceof IChemicalHandler handler) {
101+
return handler;
102+
}
103+
if (target instanceof BlockEntity be) {
104+
Direction side = peripheral instanceof GenericPeripheral sided ? sided.side() : null;
105+
return be.getLevel().getCapability(Capabilities.CHEMICAL.block(), be.getBlockPos(), side);
106+
}
107+
return null;
108+
}
109+
93110
public static IChemicalHandler extractHandler(@Nullable Object object, @Nullable Level level, @Nullable BlockPos pos, @Nullable Direction direction) {
94-
if (object instanceof IChemicalHandler itemHandler)
95-
return itemHandler;
111+
if (object instanceof IChemicalHandler handler) {
112+
return handler;
113+
}
96114
if (object instanceof BlockEntity blockEntity && level == null && pos == null) {
97115
pos = blockEntity.getBlockPos();
98116
level = blockEntity.getLevel();
@@ -104,33 +122,13 @@ public static IChemicalHandler extractHandler(@Nullable Object object, @Nullable
104122
}
105123

106124
@Nullable
107-
public static IChemicalHandler getHandlerFromDirection(@NotNull String direction, @NotNull IPeripheralOwner owner) throws LuaException {
108-
Level level = owner.getLevel();
109-
Objects.requireNonNull(level);
110-
Direction relativeDirection = CoordUtil.getDirection(owner.getFrontAndTop(), direction);
111-
if (relativeDirection == null) {
112-
return null;
113-
}
114-
BlockEntity target = level.getBlockEntity(owner.getPos().relative(relativeDirection));
125+
public static IChemicalHandler getHandlerFromDirection(@NotNull IPeripheralOwner owner, @NotNull Direction direction) {
126+
Level level = Objects.requireNonNull(owner.getLevel());
127+
BlockEntity target = level.getBlockEntity(owner.getPos().relative(direction));
115128
if (target == null) {
116129
return null;
117130
}
118-
return extractHandler(target, level, owner.getPos().relative(relativeDirection), relativeDirection);
119-
}
120-
121-
@Nullable
122-
public static IChemicalHandler getHandlerFromName(@NotNull IComputerAccess access, String name) throws LuaException {
123-
IPeripheral location = access.getAvailablePeripheral(name);
124-
125-
// Tanks/Block Entities can't be accessed if the bridge is not exposed to the same network as the target tank/block entity
126-
// This can occur when the bridge was wrapped via a side and not via modems
127-
if (location == null)
128-
return null;
129-
130-
IChemicalHandler handler = extractHandler(location.getTarget(), null, null, null);
131-
if (handler == null)
132-
throw new LuaException("Target '" + name + "' is not a chemical handler");
133-
return handler;
131+
return extractHandler(target, level, target.getBlockPos(), direction.getOpposite());
134132
}
135133

136134
public static ChemicalStack toChemicalStack(Chemical chemical, long amount) {

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dan200.computercraft.api.lua.LuaException;
44
import dan200.computercraft.api.peripheral.IComputerAccess;
55
import dan200.computercraft.api.peripheral.IPeripheral;
6+
import dan200.computercraft.shared.peripheral.generic.GenericPeripheral;
67
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
78
import de.srendi.advancedperipherals.common.util.CoordUtil;
89
import de.srendi.advancedperipherals.common.util.FingerprintUtil;
@@ -66,9 +67,26 @@ public static int moveFluid(IFluidHandler inventoryFrom, IFluidHandler inventory
6667
return filter.getAmount() - needs;
6768
}
6869

70+
@Nullable
71+
public static IFluidHandler extractHandler(@Nullable IPeripheral peripheral) {
72+
if (peripheral == null) {
73+
return null;
74+
}
75+
Object target = peripheral.getTarget();
76+
if (target instanceof IFluidHandler handler) {
77+
return handler;
78+
}
79+
if (target instanceof BlockEntity be) {
80+
Direction side = peripheral instanceof GenericPeripheral sided ? sided.side() : null;
81+
return be.getLevel().getCapability(Capabilities.FluidHandler.BLOCK, be.getBlockPos(), side);
82+
}
83+
return null;
84+
}
85+
6986
public static IFluidHandler extractHandler(@Nullable Object object, @Nullable Level level, @Nullable BlockPos pos, @Nullable Direction direction) {
70-
if (object instanceof IFluidHandler itemHandler)
71-
return itemHandler;
87+
if (object instanceof IFluidHandler handler) {
88+
return handler;
89+
}
7290
if (object instanceof BlockEntity blockEntity && level == null && pos == null) {
7391
pos = blockEntity.getBlockPos();
7492
level = blockEntity.getLevel();
@@ -80,32 +98,13 @@ public static IFluidHandler extractHandler(@Nullable Object object, @Nullable Le
8098
}
8199

82100
@Nullable
83-
public static IFluidHandler getHandlerFromDirection(@NotNull String direction, @NotNull IPeripheralOwner owner) throws LuaException {
101+
public static IFluidHandler getHandlerFromDirection(@NotNull IPeripheralOwner owner, @NotNull Direction direction) {
84102
Level level = Objects.requireNonNull(owner.getLevel());
85-
Direction relativeDirection = CoordUtil.getDirection(owner.getFrontAndTop(), direction);
86-
if (relativeDirection == null) {
87-
return null;
88-
}
89-
BlockEntity target = level.getBlockEntity(owner.getPos().relative(relativeDirection));
103+
BlockEntity target = level.getBlockEntity(owner.getPos().relative(direction));
90104
if (target == null) {
91105
return null;
92106
}
93-
return extractHandler(target, level, owner.getPos().relative(relativeDirection), relativeDirection);
94-
}
95-
96-
@Nullable
97-
public static IFluidHandler getHandlerFromName(@NotNull IComputerAccess access, String name) throws LuaException {
98-
IPeripheral location = access.getAvailablePeripheral(name);
99-
100-
// Tanks/Block Entities can't be accessed if the bridge is not exposed to the same network as the target tank/block entity
101-
// This can occur when the bridge was wrapped via a side and not via modems
102-
if (location == null)
103-
return null;
104-
105-
IFluidHandler handler = extractHandler(location.getTarget(), null, null, null);
106-
if (handler == null)
107-
throw new LuaException("Target '" + name + "' is not a fluid handler");
108-
return handler;
107+
return extractHandler(target, level, target.getBlockPos(), direction.getOpposite());
109108
}
110109

111110
@NotNull

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

Lines changed: 0 additions & 125 deletions
This file was deleted.

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ public static int moveItem(IItemHandler inventoryFrom, IItemHandler inventoryTo,
129129
return filter.getCount() - needs;
130130
}
131131

132-
@Nullable
133-
public static IItemHandler getHandlerFromName(@NotNull IComputerAccess access, String name) {
134-
return extractHandler(access.getAvailablePeripheral(name));
135-
}
136-
137132
@Nullable
138133
public static IItemHandler getHandlerFromDirection(@NotNull IPeripheralOwner owner, @NotNull Direction direction) {
139134
Level level = Objects.requireNonNull(owner.getLevel());

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
import de.srendi.advancedperipherals.common.addons.computercraft.owner.OperationAbility;
1414
import de.srendi.advancedperipherals.common.addons.computercraft.owner.PeripheralOwnerAbility;
1515
import de.srendi.advancedperipherals.common.util.CoordUtil;
16+
import de.srendi.advancedperipherals.common.util.inventory.ChemicalUtil;
17+
import de.srendi.advancedperipherals.common.util.inventory.FluidUtil;
1618
import de.srendi.advancedperipherals.common.util.inventory.ItemUtil;
1719
import net.minecraft.core.BlockPos;
1820
import net.minecraft.core.Direction;
1921
import net.minecraft.server.level.ServerLevel;
2022
import net.minecraft.world.phys.Vec3;
23+
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
2124
import net.neoforged.neoforge.items.IItemHandler;
2225
import org.jetbrains.annotations.NotNull;
2326
import org.jetbrains.annotations.Nullable;
@@ -191,6 +194,18 @@ protected <T> MethodResult withOperation(IPeripheralOperation<T> operation, T co
191194
return operationAbility.performOperation(operation, context, check, method, successCallback, failCallback);
192195
}
193196

197+
@Nullable
198+
protected IItemHandler getItemHandlerOrNull(IComputerAccess computer, String name) {
199+
if (name.length() >= 1 && name.charAt(0) == '@') {
200+
Direction dir = this.mapDirection(name.substring(1));
201+
if (dir == null) {
202+
return null;
203+
}
204+
return ItemUtil.getHandlerFromDirection(owner, dir);
205+
}
206+
return ItemUtil.extractHandler(computer.getAvailablePeripheral(name));
207+
}
208+
194209
@NotNull
195210
protected IItemHandler getItemHandler(IComputerAccess computer, String name) throws LuaException {
196211
if (name.length() >= 1 && name.charAt(0) == '@') {
@@ -214,4 +229,29 @@ protected IItemHandler getItemHandler(IComputerAccess computer, String name) thr
214229
}
215230
return inventory;
216231
}
232+
233+
@Nullable
234+
protected IFluidHandler getFluidHandlerOrNull(IComputerAccess computer, String name) {
235+
if (name.length() >= 1 && name.charAt(0) == '@') {
236+
Direction dir = this.mapDirection(name.substring(1));
237+
if (dir == null) {
238+
return null;
239+
}
240+
return FluidUtil.getHandlerFromDirection(owner, dir);
241+
}
242+
return FluidUtil.extractHandler(computer.getAvailablePeripheral(name));
243+
}
244+
245+
246+
@Nullable
247+
protected Object /*IChemicalHandler*/ getChemicalHandlerOrNull(IComputerAccess computer, String name) {
248+
if (name.length() >= 1 && name.charAt(0) == '@') {
249+
Direction dir = this.mapDirection(name.substring(1));
250+
if (dir == null) {
251+
return null;
252+
}
253+
return ChemicalUtil.getHandlerFromDirection(owner, dir);
254+
}
255+
return ChemicalUtil.extractHandler(computer.getAvailablePeripheral(name));
256+
}
217257
}

0 commit comments

Comments
 (0)