3333import de .srendi .advancedperipherals .common .util .inventory .ItemFilter ;
3434import de .srendi .advancedperipherals .lib .peripherals .BasePeripheral ;
3535import net .minecraft .core .Direction ;
36+ import net .minecraftforge .common .capabilities .Capability ;
37+ import net .minecraftforge .common .capabilities .ForgeCapabilities ;
38+ import net .minecraftforge .common .capabilities .ICapabilityProvider ;
39+ import net .minecraftforge .common .util .LazyOptional ;
3640import net .minecraftforge .fluids .capability .IFluidHandler ;
3741import net .minecraftforge .items .IItemHandler ;
3842import org .jetbrains .annotations .NotNull ;
@@ -50,6 +54,7 @@ public class MeBridgePeripheral extends BasePeripheral<BlockEntityPeripheralOwne
5054 public static final String PERIPHERAL_TYPE = "me_bridge" ;
5155
5256 private final MeBridgeEntity bridge ;
57+ private final ICapabilityProvider capabilityWrapper = new CapabilityWrapper (this );
5358 private IGridNode node ;
5459
5560 public MeBridgePeripheral (MeBridgeEntity tileEntity ) {
@@ -62,6 +67,11 @@ public void setNode(IManagedGridNode node) {
6267 this .node = node .getNode ();
6368 }
6469
70+ @ Override
71+ public Object getTarget () {
72+ return capabilityWrapper ;
73+ }
74+
6575 @ Override
6676 public boolean isEnabled () {
6777 return APConfig .PERIPHERALS_CONFIG .enableMEBridge .get ();
@@ -71,6 +81,14 @@ private ICraftingService getCraftingService() {
7181 return node .getGrid ().getCraftingService ();
7282 }
7383
84+ protected MeItemHandler getItemHandler () {
85+ return new MeItemHandler (AppEngApi .getMonitor (node ), bridge );
86+ }
87+
88+ protected MeFluidHandler getFluidHandler () {
89+ return new MeFluidHandler (AppEngApi .getMonitor (node ), bridge );
90+ }
91+
7492 /**
7593 * exports an item out of the system to a valid inventory
7694 *
@@ -79,8 +97,7 @@ private ICraftingService getCraftingService() {
7997 * @return the exportable amount or null with a string if something went wrong
8098 */
8199 protected MethodResult exportToChest (@ NotNull IArguments arguments , @ Nullable IItemHandler targetInventory ) throws LuaException {
82- MEStorage monitor = AppEngApi .getMonitor (node );
83- MeItemHandler itemHandler = new MeItemHandler (monitor , bridge );
100+ MeItemHandler itemHandler = getItemHandler ();
84101 Pair <ItemFilter , String > filter = ItemFilter .parse (arguments .getTable (0 ));
85102
86103 if (filter .rightPresent ())
@@ -100,8 +117,7 @@ protected MethodResult exportToChest(@NotNull IArguments arguments, @Nullable II
100117 * @return the exportable amount or null with a string if something went wrong
101118 */
102119 protected MethodResult exportToTank (@ NotNull IArguments arguments , @ Nullable IFluidHandler targetTank ) throws LuaException {
103- MEStorage monitor = AppEngApi .getMonitor (node );
104- MeFluidHandler fluidHandler = new MeFluidHandler (monitor , bridge );
120+ MeFluidHandler fluidHandler = getFluidHandler ();
105121 Pair <FluidFilter , String > filter = FluidFilter .parse (arguments .getTable (0 ));
106122
107123 if (filter .rightPresent ())
@@ -121,8 +137,7 @@ protected MethodResult exportToTank(@NotNull IArguments arguments, @Nullable IFl
121137 * @return the imported amount or null with a string if something went wrong
122138 */
123139 protected MethodResult importToME (@ NotNull IArguments arguments , @ Nullable IItemHandler targetInventory ) throws LuaException {
124- MEStorage monitor = AppEngApi .getMonitor (node );
125- MeItemHandler itemHandler = new MeItemHandler (monitor , bridge );
140+ MeItemHandler itemHandler = getItemHandler ();
126141 Pair <ItemFilter , String > filter = ItemFilter .parse (arguments .getTable (0 ));
127142
128143 if (filter .rightPresent ())
@@ -142,8 +157,7 @@ protected MethodResult importToME(@NotNull IArguments arguments, @Nullable IItem
142157 * @return the imported amount or null with a string if something went wrong
143158 */
144159 protected MethodResult importToME (@ NotNull IArguments arguments , @ Nullable IFluidHandler targetTank ) throws LuaException {
145- MEStorage monitor = AppEngApi .getMonitor (node );
146- MeFluidHandler fluidHandler = new MeFluidHandler (monitor , bridge );
160+ MeFluidHandler fluidHandler = getFluidHandler ();
147161 Pair <FluidFilter , String > filter = FluidFilter .parse (arguments .getTable (0 ));
148162
149163 if (filter .rightPresent ())
@@ -788,4 +802,22 @@ public final MethodResult getCraftingCPUs() throws LuaException {
788802 }
789803 return MethodResult .of (map );
790804 }
805+
806+ private static final class CapabilityWrapper implements ICapabilityProvider {
807+ private final MeBridgePeripheral peripheral ;
808+
809+ private CapabilityWrapper (MeBridgePeripheral peripheral ) {
810+ this .peripheral = peripheral ;
811+ }
812+
813+ @ Override
814+ public <T > LazyOptional <T > getCapability (final Capability <T > cap , final Direction side ) {
815+ if (cap == ForgeCapabilities .ITEM_HANDLER ) {
816+ return LazyOptional .of (this .peripheral ::getItemHandler ).cast ();
817+ } else if (cap == ForgeCapabilities .FLUID_HANDLER ) {
818+ return LazyOptional .of (this .peripheral ::getFluidHandler ).cast ();
819+ }
820+ return LazyOptional .empty ();
821+ }
822+ }
791823}
0 commit comments