Search in sources :

Example 1 with IFluidHandlerItem

use of net.minecraftforge.fluids.capability.IFluidHandlerItem in project MinecraftForge by MinecraftForge.

the class UniversalBucket method getSubItems.

@SideOnly(Side.CLIENT)
@Override
public void getSubItems(@Nonnull Item itemIn, @Nullable CreativeTabs tab, @Nonnull NonNullList<ItemStack> subItems) {
    for (Fluid fluid : FluidRegistry.getRegisteredFluids().values()) {
        if (fluid != FluidRegistry.WATER && fluid != FluidRegistry.LAVA && !fluid.getName().equals("milk")) {
            // add all fluids that the bucket can be filled  with
            FluidStack fs = new FluidStack(fluid, getCapacity());
            ItemStack stack = new ItemStack(this);
            IFluidHandlerItem fluidHandler = new FluidBucketWrapper(stack);
            if (fluidHandler.fill(fs, true) == fs.amount) {
                ItemStack filled = fluidHandler.getContainer();
                subItems.add(filled);
            }
        }
    }
}
Also used : FluidBucketWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBucketWrapper) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) ItemStack(net.minecraft.item.ItemStack) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 2 with IFluidHandlerItem

use of net.minecraftforge.fluids.capability.IFluidHandlerItem in project MinecraftForge by MinecraftForge.

the class DispenseFluidContainer method dumpContainer.

/**
     * Drains a filled container and places the fluid in front of the Dispenser.
     */
@Nonnull
private ItemStack dumpContainer(IBlockSource source, @Nonnull ItemStack stack) {
    ItemStack singleStack = stack.copy();
    singleStack.setCount(1);
    IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(singleStack);
    if (fluidHandler == null) {
        return super.dispenseStack(source, stack);
    }
    FluidStack fluidStack = fluidHandler.drain(Fluid.BUCKET_VOLUME, false);
    EnumFacing dispenserFacing = source.getBlockState().getValue(BlockDispenser.FACING);
    BlockPos blockpos = source.getBlockPos().offset(dispenserFacing);
    FluidActionResult result = fluidStack != null ? FluidUtil.tryPlaceFluid(null, source.getWorld(), blockpos, stack, fluidStack) : FluidActionResult.FAILURE;
    if (result.isSuccess()) {
        ItemStack drainedStack = result.getResult();
        if (drainedStack.getCount() == 1) {
            return drainedStack;
        } else if (!drainedStack.isEmpty() && ((TileEntityDispenser) source.getBlockTileEntity()).addItemStack(drainedStack) < 0) {
            this.dispenseBehavior.dispense(source, drainedStack);
        }
        ItemStack stackCopy = drainedStack.copy();
        stackCopy.shrink(1);
        return stackCopy;
    } else {
        return this.dispenseBehavior.dispense(source, stack);
    }
}
Also used : IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 3 with IFluidHandlerItem

use of net.minecraftforge.fluids.capability.IFluidHandlerItem in project MinecraftForge by MinecraftForge.

the class FluidUtil method tryFillContainer.

/**
     * Fill a container from the given fluidSource.
     *
     * @param container   The container to be filled. Will not be modified.
     *                    Separate handling must be done to reduce the stack size, stow containers, etc, on success.
     *                    See {@link  #tryFillContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer)}.
     * @param fluidSource The fluid handler to be drained.
     * @param maxAmount   The largest amount of fluid that should be transferred.
     * @param player      The player to make the filling noise. Pass null for no noise.
     * @param doFill      true if the container should actually be filled, false if it should be simulated.
     * @return a {@link FluidActionResult} holding the filled container if successful.
     */
@Nonnull
public static FluidActionResult tryFillContainer(@Nonnull ItemStack container, IFluidHandler fluidSource, int maxAmount, @Nullable EntityPlayer player, boolean doFill) {
    // do not modify the input
    ItemStack containerCopy = container.copy();
    containerCopy.setCount(1);
    IFluidHandlerItem containerFluidHandler = getFluidHandler(containerCopy);
    if (containerFluidHandler != null) {
        FluidStack simulatedTransfer = tryFluidTransfer(containerFluidHandler, fluidSource, maxAmount, false);
        if (simulatedTransfer != null) {
            if (doFill) {
                tryFluidTransfer(containerFluidHandler, fluidSource, maxAmount, true);
                if (player != null) {
                    SoundEvent soundevent = simulatedTransfer.getFluid().getFillSound(simulatedTransfer);
                    player.playSound(soundevent, 1f, 1f);
                }
            } else {
                containerFluidHandler.fill(simulatedTransfer, true);
            }
            ItemStack resultContainer = containerFluidHandler.getContainer();
            return new FluidActionResult(resultContainer);
        }
    }
    return FluidActionResult.FAILURE;
}
Also used : SoundEvent(net.minecraft.util.SoundEvent) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 4 with IFluidHandlerItem

use of net.minecraftforge.fluids.capability.IFluidHandlerItem in project MinecraftForge by MinecraftForge.

the class FluidUtil method tryEmptyContainer.

/**
     * Takes a filled container and tries to empty it into the given tank.
     *
     * @param container        The filled container. Will not be modified.
     *                         Separate handling must be done to reduce the stack size, stow containers, etc, on success.
     *                         See {@link #tryEmptyContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer)}.
     * @param fluidDestination The fluid handler to be filled by the container.
     * @param maxAmount        The largest amount of fluid that should be transferred.
     * @param player           Player for making the bucket drained sound. Pass null for no noise.
     * @param doDrain          true if the container should actually be drained, false if it should be simulated.
     * @return a {@link FluidActionResult} holding the empty container if the fluid handler was filled.
     *         NOTE If the container is consumable, the empty container will be null on success.
     */
@Nonnull
public static FluidActionResult tryEmptyContainer(@Nonnull ItemStack container, IFluidHandler fluidDestination, int maxAmount, @Nullable EntityPlayer player, boolean doDrain) {
    // do not modify the input
    ItemStack containerCopy = container.copy();
    containerCopy.setCount(1);
    IFluidHandlerItem containerFluidHandler = getFluidHandler(containerCopy);
    if (containerFluidHandler != null) {
        FluidStack simulatedTransfer = tryFluidTransfer(fluidDestination, containerFluidHandler, maxAmount, false);
        if (simulatedTransfer != null) {
            if (doDrain) {
                tryFluidTransfer(fluidDestination, containerFluidHandler, maxAmount, true);
                if (player != null) {
                    SoundEvent soundevent = simulatedTransfer.getFluid().getEmptySound(simulatedTransfer);
                    player.playSound(soundevent, 1f, 1f);
                }
            } else {
                containerFluidHandler.drain(simulatedTransfer, true);
            }
            ItemStack resultContainer = containerFluidHandler.getContainer();
            return new FluidActionResult(resultContainer);
        }
    }
    return FluidActionResult.FAILURE;
}
Also used : SoundEvent(net.minecraft.util.SoundEvent) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 5 with IFluidHandlerItem

use of net.minecraftforge.fluids.capability.IFluidHandlerItem in project MinecraftForge by MinecraftForge.

the class DynBucketTest method onBucketFill.

@SubscribeEvent
public void onBucketFill(FillBucketEvent event) {
    RayTraceResult target = event.getTarget();
    if (target != null) {
        IBlockState state = event.getWorld().getBlockState(target.getBlockPos());
        if (state.getBlock() instanceof IFluidBlock) {
            Fluid fluid = ((IFluidBlock) state.getBlock()).getFluid();
            FluidStack fs = new FluidStack(fluid, Fluid.BUCKET_VOLUME);
            ItemStack bucket = event.getEmptyBucket();
            IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(bucket);
            if (fluidHandler != null) {
                int fillAmount = fluidHandler.fill(fs, true);
                if (fillAmount > 0) {
                    ItemStack filledBucket = fluidHandler.getContainer();
                    event.setFilledBucket(filledBucket);
                    event.setResult(Result.ALLOW);
                }
            }
        }
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) FluidStack(net.minecraftforge.fluids.FluidStack) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) IFluidBlock(net.minecraftforge.fluids.IFluidBlock) Fluid(net.minecraftforge.fluids.Fluid) TestFluid(net.minecraftforge.debug.ModelFluidDebug.TestFluid) RayTraceResult(net.minecraft.util.math.RayTraceResult) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

IFluidHandlerItem (net.minecraftforge.fluids.capability.IFluidHandlerItem)7 ItemStack (net.minecraft.item.ItemStack)6 Nonnull (javax.annotation.Nonnull)3 SoundEvent (net.minecraft.util.SoundEvent)2 Fluid (net.minecraftforge.fluids.Fluid)2 FluidStack (net.minecraftforge.fluids.FluidStack)2 Nullable (javax.annotation.Nullable)1 IBlockState (net.minecraft.block.state.IBlockState)1 EnumFacing (net.minecraft.util.EnumFacing)1 BlockPos (net.minecraft.util.math.BlockPos)1 RayTraceResult (net.minecraft.util.math.RayTraceResult)1 TestFluid (net.minecraftforge.debug.ModelFluidDebug.TestFluid)1 IFluidBlock (net.minecraftforge.fluids.IFluidBlock)1 FluidBucketWrapper (net.minecraftforge.fluids.capability.wrappers.FluidBucketWrapper)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)1