From 19471c70c2ec315185a492474c67c80f7f7f979c Mon Sep 17 00:00:00 2001 From: Florian Kostenzer Date: Mon, 7 Nov 2022 13:42:10 +0100 Subject: [PATCH] feat: add fluid export (closes SquidDev-CC/plethora#247) --- .../appliedenergistics/MethodExportFluid.java | 74 +++++++++++++++++++ .../appliedenergistics/MethodExportItem.java | 3 + .../method/MethodsInventoryTransfer.java | 16 ++++ 3 files changed, 93 insertions(+) create mode 100644 src/main/java/org/squiddev/plethora/integration/appliedenergistics/MethodExportFluid.java diff --git a/src/main/java/org/squiddev/plethora/integration/appliedenergistics/MethodExportFluid.java b/src/main/java/org/squiddev/plethora/integration/appliedenergistics/MethodExportFluid.java new file mode 100644 index 00000000..476f19ec --- /dev/null +++ b/src/main/java/org/squiddev/plethora/integration/appliedenergistics/MethodExportFluid.java @@ -0,0 +1,74 @@ +package org.squiddev.plethora.integration.appliedenergistics; + +import appeng.api.AEApi; +import appeng.api.config.Actionable; +import appeng.api.networking.IGrid; +import appeng.api.networking.security.IActionHost; +import appeng.api.networking.storage.IStorageGrid; +import appeng.api.storage.channels.IFluidStorageChannel; +import appeng.api.storage.data.IAEFluidStack; +import appeng.core.AppEng; +import appeng.me.helpers.MachineSource; +import dan200.computercraft.api.lua.LuaException; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.capability.IFluidHandler; +import org.squiddev.plethora.api.method.IContext; +import org.squiddev.plethora.api.method.ITransferMethod; +import org.squiddev.plethora.api.method.MarkerInterfaces; +import org.squiddev.plethora.api.method.wrapper.FromContext; +import org.squiddev.plethora.api.method.wrapper.Optional; +import org.squiddev.plethora.api.method.wrapper.PlethoraMethod; + +import static org.squiddev.plethora.integration.vanilla.method.MethodsInventoryTransfer.extractFluidHandler; + +public final class MethodExportFluid { + private MethodExportFluid() { + } + + @PlethoraMethod( + modId = AppEng.MOD_ID, + doc = "-- Export this fluid from the AE network to a tank. Returns the amount transferred." + ) + @MarkerInterfaces(ITransferMethod.class) + public static long export( + IContext baked, @FromContext IGrid grid, @FromContext IActionHost host, + String toName, @Optional(defInt = Integer.MAX_VALUE) int limit + ) throws LuaException { + // Find location to transfer to + Object location = baked.getTransferLocation(toName); + if (location == null) throw new LuaException("Target '" + toName + "' does not exist"); + + // Validate our location is valid + IFluidHandler to = extractFluidHandler(location); + if (to == null) throw new LuaException("Target '" + toName + "' is not a tank"); + + if (limit <= 0) throw new LuaException("Limit must be > 0"); + + // Find the stack to extract + IFluidStorageChannel channel = AEApi.instance().storage().getStorageChannel(IFluidStorageChannel.class); + IStorageGrid storageGrid = grid.getCache(IStorageGrid.class); + MachineSource source = new MachineSource(host); + + // Extract said fluid + IAEFluidStack toExtract = baked.getTarget().copy(); + toExtract.setStackSize(Math.min(limit, toExtract.getStackSize())); + toExtract = storageGrid.getInventory(channel).extractItems(toExtract, Actionable.MODULATE, source); + if (toExtract == null) { + return 0; + } + + // Attempt to insert into the appropriate tank + FluidStack toInsert = toExtract.getFluidStack(); + int extracted = toInsert.amount; + int transferred = to.fill(toInsert, true); + + // If not everything could be inserted, replace back in the tank + if (transferred < extracted) { + FluidStack remainder = toInsert.copy(); + remainder.amount = extracted - transferred; + storageGrid.getInventory(channel).injectItems(channel.createStack(remainder), Actionable.MODULATE, source); + } + + return transferred; + } +} diff --git a/src/main/java/org/squiddev/plethora/integration/appliedenergistics/MethodExportItem.java b/src/main/java/org/squiddev/plethora/integration/appliedenergistics/MethodExportItem.java index b6cfddb8..f7e70f6e 100644 --- a/src/main/java/org/squiddev/plethora/integration/appliedenergistics/MethodExportItem.java +++ b/src/main/java/org/squiddev/plethora/integration/appliedenergistics/MethodExportItem.java @@ -56,6 +56,9 @@ public static long export( IAEItemStack toExtract = baked.getTarget().copy(); toExtract.setStackSize(Math.min(limit, toExtract.getDefinition().getMaxStackSize())); toExtract = storageGrid.getInventory(channel).extractItems(toExtract, Actionable.MODULATE, source); + if (toExtract == null) { + return 0; + } // Attempt to insert into the appropriate inventory ItemStack toInsert = toExtract.createItemStack(); diff --git a/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsInventoryTransfer.java b/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsInventoryTransfer.java index 1a26d46d..855d234a 100644 --- a/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsInventoryTransfer.java +++ b/src/main/java/org/squiddev/plethora/integration/vanilla/method/MethodsInventoryTransfer.java @@ -3,6 +3,8 @@ import dan200.computercraft.api.lua.LuaException; import net.minecraft.item.ItemStack; import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.fluids.capability.CapabilityFluidHandler; +import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemHandlerHelper; @@ -85,6 +87,20 @@ public static IItemHandler extractHandler(@Nonnull Object object) { return null; } + @Nullable + public static IFluidHandler extractFluidHandler(@Nonnull Object object) { + for (Object child : PlethoraAPI.instance().converterRegistry().convertAll(object)) { + if (child instanceof IFluidHandler) return (IFluidHandler) child; + + if (object instanceof ICapabilityProvider) { + IFluidHandler handler = ((ICapabilityProvider) object).getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null); + if (handler != null) return handler; + } + } + + return null; + } + /** * Move an item from one handler to another *