Search in sources :

Example 1 with IMekanismFluidHandler

use of mekanism.api.fluid.IMekanismFluidHandler in project Mekanism by mekanism.

the class LookingAtUtils method displayFluid.

private static void displayFluid(LookingAtHelper info, IFluidHandler fluidHandler) {
    if (fluidHandler instanceof IMekanismFluidHandler) {
        IMekanismFluidHandler mekFluidHandler = (IMekanismFluidHandler) fluidHandler;
        for (IExtendedFluidTank fluidTank : mekFluidHandler.getFluidTanks(null)) {
            if (fluidTank instanceof FluidTankWrapper) {
                MergedTank mergedTank = ((FluidTankWrapper) fluidTank).getMergedTank();
                CurrentType currentType = mergedTank.getCurrentType();
                if (currentType != CurrentType.EMPTY && currentType != CurrentType.FLUID) {
                    // Skip if the tank is on a chemical
                    continue;
                }
            }
            addFluidInfo(info, fluidTank.getFluid(), fluidTank.getCapacity());
        }
    } else {
        // Fallback handling if it is not our fluid handler (probably never gets used)
        for (int tank = 0; tank < fluidHandler.getTanks(); tank++) {
            addFluidInfo(info, fluidHandler.getFluidInTank(tank), fluidHandler.getTankCapacity(tank));
        }
    }
}
Also used : IMekanismFluidHandler(mekanism.api.fluid.IMekanismFluidHandler) CurrentType(mekanism.common.capabilities.merged.MergedTank.CurrentType) MergedTank(mekanism.common.capabilities.merged.MergedTank) FluidTankWrapper(mekanism.common.capabilities.fluid.FluidTankWrapper) IExtendedFluidTank(mekanism.api.fluid.IExtendedFluidTank)

Example 2 with IMekanismFluidHandler

use of mekanism.api.fluid.IMekanismFluidHandler in project Mekanism by mekanism.

the class FluidRecipeData method applyToStack.

@Override
public boolean applyToStack(ItemStack stack) {
    if (fluidTanks.isEmpty()) {
        return true;
    }
    Item item = stack.getItem();
    Optional<IFluidHandlerItem> capability = FluidUtil.getFluidHandler(stack).resolve();
    List<IExtendedFluidTank> fluidTanks = new ArrayList<>();
    if (capability.isPresent()) {
        IFluidHandlerItem fluidHandler = capability.get();
        for (int i = 0; i < fluidHandler.getTanks(); i++) {
            int tank = i;
            fluidTanks.add(BasicFluidTank.create(fluidHandler.getTankCapacity(tank), fluid -> fluidHandler.isFluidValid(tank, fluid), null));
        }
    } else if (item instanceof BlockItem) {
        TileEntityMekanism tile = getTileFromBlock(((BlockItem) item).getBlock());
        if (tile == null || !tile.handles(SubstanceType.FLUID)) {
            // Something went wrong
            return false;
        }
        for (int i = 0; i < tile.getTanks(); i++) {
            int tank = i;
            fluidTanks.add(BasicFluidTank.create(tile.getTankCapacity(tank), fluid -> tile.isFluidValid(tank, fluid), null));
        }
    } else {
        return false;
    }
    if (fluidTanks.isEmpty()) {
        // We don't actually have any tanks in the output
        return true;
    }
    // TODO: Improve the logic used so that it tries to batch similar types of fluids together first
    // and maybe make it try multiple slot combinations
    IMekanismFluidHandler outputHandler = new IMekanismFluidHandler() {

        @Nonnull
        @Override
        public List<IExtendedFluidTank> getFluidTanks(@Nullable Direction side) {
            return fluidTanks;
        }

        @Override
        public void onContentsChanged() {
        }
    };
    boolean hasData = false;
    for (IExtendedFluidTank fluidTank : this.fluidTanks) {
        if (!fluidTank.isEmpty()) {
            if (!outputHandler.insertFluid(fluidTank.getFluid(), Action.EXECUTE).isEmpty()) {
                // If we have a remainder something failed so bail
                return false;
            }
            hasData = true;
        }
    }
    if (hasData) {
        // We managed to transfer it all into valid slots, so save it to the stack
        ItemDataUtils.setList(stack, NBTConstants.FLUID_TANKS, DataHandlerUtils.writeContainers(fluidTanks));
    }
    return true;
}
Also used : TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ListNBT(net.minecraft.nbt.ListNBT) SubstanceType(mekanism.common.tile.base.SubstanceType) FluidUtil(net.minecraftforge.fluids.FluidUtil) Item(net.minecraft.item.Item) DataHandlerUtils(mekanism.api.DataHandlerUtils) IExtendedFluidTank(mekanism.api.fluid.IExtendedFluidTank) Direction(net.minecraft.util.Direction) IMekanismFluidHandler(mekanism.api.fluid.IMekanismFluidHandler) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) ArrayList(java.util.ArrayList) ItemDataUtils(mekanism.common.util.ItemDataUtils) ItemStack(net.minecraft.item.ItemStack) List(java.util.List) FieldsAreNonnullByDefault(mekanism.api.annotations.FieldsAreNonnullByDefault) BlockItem(net.minecraft.item.BlockItem) NBTConstants(mekanism.api.NBTConstants) BasicFluidTank(mekanism.common.capabilities.fluid.BasicFluidTank) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) Optional(java.util.Optional) Nonnull(javax.annotation.Nonnull) Action(mekanism.api.Action) Nullable(javax.annotation.Nullable) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ArrayList(java.util.ArrayList) BlockItem(net.minecraft.item.BlockItem) Direction(net.minecraft.util.Direction) IMekanismFluidHandler(mekanism.api.fluid.IMekanismFluidHandler) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) IExtendedFluidTank(mekanism.api.fluid.IExtendedFluidTank) Nullable(javax.annotation.Nullable)

Aggregations

IExtendedFluidTank (mekanism.api.fluid.IExtendedFluidTank)2 IMekanismFluidHandler (mekanism.api.fluid.IMekanismFluidHandler)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)1 Action (mekanism.api.Action)1 DataHandlerUtils (mekanism.api.DataHandlerUtils)1 NBTConstants (mekanism.api.NBTConstants)1 FieldsAreNonnullByDefault (mekanism.api.annotations.FieldsAreNonnullByDefault)1 BasicFluidTank (mekanism.common.capabilities.fluid.BasicFluidTank)1 FluidTankWrapper (mekanism.common.capabilities.fluid.FluidTankWrapper)1 MergedTank (mekanism.common.capabilities.merged.MergedTank)1 CurrentType (mekanism.common.capabilities.merged.MergedTank.CurrentType)1 SubstanceType (mekanism.common.tile.base.SubstanceType)1 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)1 ItemDataUtils (mekanism.common.util.ItemDataUtils)1 BlockItem (net.minecraft.item.BlockItem)1