Search in sources :

Example 1 with CreativeFluidTankTileEntity

use of com.simibubi.create.content.contraptions.fluids.tank.CreativeFluidTankTileEntity in project Create by Creators-of-Create.

the class FluidHelper method tryFillItemFromTE.

public static boolean tryFillItemFromTE(Level world, Player player, InteractionHand handIn, ItemStack heldItem, SmartTileEntity te) {
    if (!GenericItemFilling.canItemBeFilled(world, heldItem))
        return false;
    LazyOptional<IFluidHandler> capability = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY);
    IFluidHandler tank = capability.orElse(null);
    if (tank == null)
        return false;
    for (int i = 0; i < tank.getTanks(); i++) {
        FluidStack fluid = tank.getFluidInTank(i);
        if (fluid.isEmpty())
            continue;
        int requiredAmountForItem = GenericItemFilling.getRequiredAmountForItem(world, heldItem, fluid.copy());
        if (requiredAmountForItem == -1)
            continue;
        if (requiredAmountForItem > fluid.getAmount())
            continue;
        if (world.isClientSide)
            return true;
        if (player.isCreative() || te instanceof CreativeFluidTankTileEntity)
            heldItem = heldItem.copy();
        ItemStack out = GenericItemFilling.fillItem(world, requiredAmountForItem, heldItem, fluid.copy());
        FluidStack copy = fluid.copy();
        copy.setAmount(requiredAmountForItem);
        tank.drain(copy, FluidAction.EXECUTE);
        if (!player.isCreative())
            player.getInventory().placeItemBackInInventory(out);
        te.notifyUpdate();
        return true;
    }
    return false;
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.world.item.ItemStack) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) CreativeFluidTankTileEntity(com.simibubi.create.content.contraptions.fluids.tank.CreativeFluidTankTileEntity)

Example 2 with CreativeFluidTankTileEntity

use of com.simibubi.create.content.contraptions.fluids.tank.CreativeFluidTankTileEntity in project Create by Creators-of-Create.

the class FluidHelper method tryEmptyItemIntoTE.

public static boolean tryEmptyItemIntoTE(Level worldIn, Player player, InteractionHand handIn, ItemStack heldItem, SmartTileEntity te) {
    if (!EmptyingByBasin.canItemBeEmptied(worldIn, heldItem))
        return false;
    Pair<FluidStack, ItemStack> emptyingResult = EmptyingByBasin.emptyItem(worldIn, heldItem, true);
    LazyOptional<IFluidHandler> capability = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY);
    IFluidHandler tank = capability.orElse(null);
    FluidStack fluidStack = emptyingResult.getFirst();
    if (tank == null || fluidStack.getAmount() != tank.fill(fluidStack, FluidAction.SIMULATE))
        return false;
    if (worldIn.isClientSide)
        return true;
    ItemStack copyOfHeld = heldItem.copy();
    emptyingResult = EmptyingByBasin.emptyItem(worldIn, copyOfHeld, false);
    tank.fill(fluidStack, FluidAction.EXECUTE);
    if (!player.isCreative() && !(te instanceof CreativeFluidTankTileEntity)) {
        if (copyOfHeld.isEmpty())
            player.setItemInHand(handIn, emptyingResult.getSecond());
        else {
            player.setItemInHand(handIn, copyOfHeld);
            player.getInventory().placeItemBackInInventory(emptyingResult.getSecond());
        }
    }
    return true;
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.world.item.ItemStack) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) CreativeFluidTankTileEntity(com.simibubi.create.content.contraptions.fluids.tank.CreativeFluidTankTileEntity)

Aggregations

CreativeFluidTankTileEntity (com.simibubi.create.content.contraptions.fluids.tank.CreativeFluidTankTileEntity)2 ItemStack (net.minecraft.world.item.ItemStack)2 FluidStack (net.minecraftforge.fluids.FluidStack)2 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)2