Skip to content

Commit

Permalink
feat: add fluid export
Browse files Browse the repository at this point in the history
  • Loading branch information
123FLO321 committed Nov 7, 2022
1 parent 3c9b415 commit 19471c7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<IAEFluidStack> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 19471c7

Please sign in to comment.