Skip to content

Commit c51a2c7

Browse files
committed
implement ICapabilityProvider on rsbridge
1 parent 2404887 commit c51a2c7

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
3030
import net.minecraft.core.Direction;
3131
import net.minecraft.world.item.ItemStack;
32+
import net.minecraftforge.common.capabilities.Capability;
33+
import net.minecraftforge.common.capabilities.ForgeCapabilities;
34+
import net.minecraftforge.common.capabilities.ICapabilityProvider;
35+
import net.minecraftforge.common.util.LazyOptional;
3236
import net.minecraftforge.fluids.FluidStack;
3337
import net.minecraftforge.fluids.capability.IFluidHandler;
3438
import net.minecraftforge.items.IItemHandler;
@@ -46,6 +50,7 @@ public class RsBridgePeripheral extends BasePeripheral<BlockEntityPeripheralOwne
4650
public static final String PERIPHERAL_TYPE = "rs_bridge";
4751

4852
private final RsBridgeEntity bridge;
53+
private final ICapabilityProvider capabilityWrapper = new CapabilityWrapper(this);
4954

5055
public RsBridgePeripheral(RsBridgeEntity tileEntity) {
5156
super(PERIPHERAL_TYPE, new BlockEntityPeripheralOwner<>(tileEntity));
@@ -68,11 +73,24 @@ private boolean isAvailable() {
6873
return getNetwork() != null;
6974
}
7075

76+
@Override
77+
public Object getTarget() {
78+
return capabilityWrapper;
79+
}
80+
7181
@Override
7282
public boolean isEnabled() {
7383
return APConfig.PERIPHERALS_CONFIG.enableRSBridge.get();
7484
}
7585

86+
protected RsItemHandler getItemHandler() {
87+
return new RsItemHandler(getNetwork());
88+
}
89+
90+
protected RsFluidHandler getFluidHandler() {
91+
return new RsFluidHandler(getNetwork());
92+
}
93+
7694
@Override
7795
@LuaFunction(mainThread = true)
7896
public final MethodResult isConnected() {
@@ -393,7 +411,7 @@ public final MethodResult getPatterns(IArguments arguments) throws LuaException
393411
}
394412

395413
protected MethodResult exportToChest(@NotNull IArguments arguments, @Nullable IItemHandler targetInventory) throws LuaException {
396-
RsItemHandler itemHandler = new RsItemHandler(getNetwork());
414+
RsItemHandler itemHandler = getItemHandler();
397415
if (targetInventory == null)
398416
return MethodResult.of(0, "INVALID_TARGET");
399417

@@ -405,7 +423,7 @@ protected MethodResult exportToChest(@NotNull IArguments arguments, @Nullable II
405423
}
406424

407425
protected MethodResult importToSystem(@NotNull IArguments arguments, @Nullable IItemHandler targetInventory) throws LuaException {
408-
RsItemHandler itemHandler = new RsItemHandler(getNetwork());
426+
RsItemHandler itemHandler = getItemHandler();
409427
if (targetInventory == null)
410428
return MethodResult.of(0, "INVALID_TARGET");
411429

@@ -417,7 +435,7 @@ protected MethodResult importToSystem(@NotNull IArguments arguments, @Nullable I
417435
}
418436

419437
protected MethodResult exportToTank(@NotNull IArguments arguments, @Nullable IFluidHandler targetInventory) throws LuaException {
420-
RsFluidHandler itemHandler = new RsFluidHandler(getNetwork());
438+
RsFluidHandler itemHandler = getFluidHandler();
421439
if (targetInventory == null)
422440
return MethodResult.of(0, "INVALID_TARGET");
423441

@@ -429,7 +447,7 @@ protected MethodResult exportToTank(@NotNull IArguments arguments, @Nullable IFl
429447
}
430448

431449
protected MethodResult importToSystem(@NotNull IArguments arguments, @Nullable IFluidHandler targetInventory) throws LuaException {
432-
RsFluidHandler itemHandler = new RsFluidHandler(getNetwork());
450+
RsFluidHandler itemHandler = getFluidHandler();
433451
if (targetInventory == null)
434452
return MethodResult.of(0, "INVALID_TARGET");
435453

@@ -714,4 +732,22 @@ public final MethodResult isCraftable(IArguments arguments) throws LuaException
714732

715733
return MethodResult.of(RsApi.findPatternFromFilters(getNetwork(), null, parsedFilter).getLeft() != null);
716734
}
735+
736+
private static final class CapabilityWrapper implements ICapabilityProvider {
737+
private final RsBridgePeripheral peripheral;
738+
739+
private CapabilityWrapper(RsBridgePeripheral peripheral) {
740+
this.peripheral = peripheral;
741+
}
742+
743+
@Override
744+
public <T> LazyOptional<T> getCapability(final Capability<T> cap, final Direction side) {
745+
if (cap == ForgeCapabilities.ITEM_HANDLER) {
746+
return LazyOptional.of(this.peripheral::getItemHandler).cast();
747+
} else if (cap == ForgeCapabilities.FLUID_HANDLER) {
748+
return LazyOptional.of(this.peripheral::getFluidHandler).cast();
749+
}
750+
return LazyOptional.empty();
751+
}
752+
}
717753
}

0 commit comments

Comments
 (0)