Example usage for net.minecraftforge.fluids FluidUtil getFluidHandler

List of usage examples for net.minecraftforge.fluids FluidUtil getFluidHandler

Introduction

In this page you can find the example usage for net.minecraftforge.fluids FluidUtil getFluidHandler.

Prototype

public static LazyOptional<IFluidHandlerItem> getFluidHandler(@Nonnull ItemStack itemStack) 

Source Link

Document

Helper method to get an IFluidHandlerItem for an itemStack.

Usage

From source file:blusunrize.immersiveengineering.common.crafting.RecipeJerrycan.java

@Override
public boolean matches(@Nonnull InventoryCrafting inv, World world) {

    ItemStack jerrycan = ItemStack.EMPTY;
    ItemStack container = ItemStack.EMPTY;
    int[] slots = getRelevantSlots(inv);
    if (slots[0] >= 0)
        jerrycan = inv.getStackInSlot(slots[0]);
    if (slots[1] >= 0)
        container = inv.getStackInSlot(slots[1]);
    if (!jerrycan.isEmpty() && !container.isEmpty()) {
        IFluidHandler handler = FluidUtil.getFluidHandler(container);
        FluidStack fs = handler.drain(Integer.MAX_VALUE, false);
        return fs == null || (fs.amount < handler.getTankProperties()[0].getCapacity()
                && fs.isFluidEqual(FluidUtil.getFluidContained(jerrycan)));
    }/*  ww w  . jav a2  s .c om*/
    return false;
}

From source file:blusunrize.immersiveengineering.common.crafting.RecipeJerrycan.java

@Nonnull
@Override//from   ww w  . jav a 2s.  c o m
public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) {
    ItemStack jerrycan = ItemStack.EMPTY;
    ItemStack container = ItemStack.EMPTY;
    FluidStack fs = null;
    int[] slots = getRelevantSlots(inv);
    if (slots[0] >= 0) {
        jerrycan = inv.getStackInSlot(slots[0]);
        fs = FluidUtil.getFluidContained(jerrycan);
    }
    if (slots[1] >= 0)
        container = inv.getStackInSlot(slots[1]);
    if (fs != null && !container.isEmpty()) {
        ItemStack newContainer = Utils.copyStackWithAmount(container, 1);
        IFluidHandlerItem handler = FluidUtil.getFluidHandler(newContainer);
        int accepted = handler.fill(fs, false);
        if (accepted > 0) {
            handler.fill(fs, true);
            newContainer = handler.getContainer();// Because buckets are silly
            //            FluidUtil.getFluidHandler(jerrycan).drain(accepted,true);
            ItemNBTHelper.setInt(jerrycan, "jerrycanDrain", accepted);
        }
        return newContainer;
    }
    return ItemStack.EMPTY;
}

From source file:blusunrize.immersiveengineering.common.crafting.RecipeJerrycan.java

private int[] getRelevantSlots(InventoryCrafting inv) {
    int[] ret = { -1, -1 };
    for (int i = 0; i < inv.getSizeInventory(); i++) {
        ItemStack stackInSlot = inv.getStackInSlot(i);
        if (!stackInSlot.isEmpty())
            if (ret[0] < 0 && IEContent.itemJerrycan.equals(stackInSlot.getItem())
                    && FluidUtil.getFluidContained(stackInSlot) != null)
                ret[0] = i;//from w  ww.  ja v a  2 s  .  c om
            else if (ret[1] < 0 && FluidUtil.getFluidHandler(stackInSlot) != null)
                ret[1] = i;
            else {
                ret[0] = ret[1] = -1;
                return ret;
            }
    }
    return ret;
}

From source file:blusunrize.immersiveengineering.common.crafting.RecipeShapedIngredient.java

@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) //getRecipeLeftovers
{
    NonNullList<ItemStack> remains = NonNullList.withSize(inv.getSizeInventory(), ItemStack.EMPTY);
    for (int yy = 0; yy < this.height; yy++)
        for (int xx = 0; xx < this.width; xx++) {
            int i = this.width * yy + xx;
            int transposedI = inv.getWidth() * (yy + lastStartY) + (xx + lastStartX);
            ItemStack s = inv.getStackInSlot(transposedI);
            NonNullList<Ingredient> matchedIngr = lastMatch == 1 ? ingredientsQuarterTurn
                    : lastMatch == 2 ? ingredientsEighthTurn : this.input;
            if (matchedIngr.get(i) instanceof IngredientFluidStack) {
                IFluidHandlerItem handler = FluidUtil
                        .getFluidHandler(s.getCount() > 1 ? Utils.copyStackWithAmount(s, 1) : s);
                if (handler != null) {
                    FluidStack fluid = ((IngredientFluidStack) matchedIngr.get(i)).getFluid();
                    handler.drain(fluid.amount, true);
                    remains.set(transposedI, handler.getContainer().copy());
                } else
                    remains.set(transposedI, ForgeHooks.getContainerItem(s));
            }// w  w  w  .  j av  a  2 s.  co  m
        }
    return remains;
}

From source file:blusunrize.immersiveengineering.common.items.ItemDrill.java

@Override
public boolean onBlockDestroyed(ItemStack stack, World world, IBlockState state, BlockPos pos,
        EntityLivingBase living) {//from  ww  w .jav  a 2s .c  o  m
    if ((double) state.getBlockHardness(world, pos) != 0.0D) {
        int dmg = ForgeHooks.isToolEffective(world, pos, stack) ? 1 : 3;
        ItemStack head = getHead(stack);
        if (!head.isEmpty()) {
            if (living instanceof EntityPlayer) {
                if (((EntityPlayer) living).capabilities.isCreativeMode)
                    return true;
                ((IDrillHead) head.getItem()).afterBlockbreak(stack, head, (EntityPlayer) living);
            }
            if (!getUpgrades(stack).getBoolean("oiled") || Utils.RAND.nextInt(4) == 0)
                ((IDrillHead) head.getItem()).damageHead(head, dmg);
            this.setHead(stack, head);
            IFluidHandler handler = FluidUtil.getFluidHandler(stack);
            handler.drain(1, true);

            Triple<ItemStack, ShaderRegistryEntry, ShaderCase> shader = ShaderRegistry
                    .getStoredShaderAndCase(stack);
            if (shader != null)
                shader.getMiddle().getEffectFunction().execute(world, shader.getLeft(), stack,
                        shader.getRight().getShaderType(),
                        new Vec3d(pos.getX() + .5, pos.getY() + .5, pos.getZ() + .5), null, .375f);
        }
    }

    return true;
}

From source file:blusunrize.immersiveengineering.common.items.ItemJerrycan.java

@Override
public ItemStack getContainerItem(ItemStack stack) {
    ItemStack compare = stack;/*w w  w. j av a2s  .  c o m*/
    if (stack.isEmpty()) {
        stack.grow(1);
        compare = stack.copy();
        stack.shrink(1);
    }
    if (ItemNBTHelper.hasKey(compare, "jerrycanDrain")) {
        ItemStack ret = compare.copy();
        IFluidHandler handler = FluidUtil.getFluidHandler(ret);
        handler.drain(ItemNBTHelper.getInt(ret, "jerrycanDrain"), true);
        ItemNBTHelper.remove(ret, "jerrycanDrain");
        return ret;
    } else if (FluidUtil.getFluidContained(compare) != null) {
        ItemStack ret = compare.copy();
        IFluidHandler handler = FluidUtil.getFluidHandler(ret);
        handler.drain(1000, true);
        return ret;
    }
    return stack;
}

From source file:buildcraft.lib.fluids.Tank.java

License:Mozilla Public License

public void onGuiClicked(ContainerBC_Neptune container) {
    EntityPlayer player = container.player;
    ItemStack held = player.inventory.getItemStack();
    if (held.isEmpty()) {
        return;/*from w  w w .j  a v a  2s .com*/
    }

    // first try to fill this tank from the item

    boolean hasFilled = false;

    ItemStack copy = held.copy();
    copy.setCount(1);
    int space = capacity - getFluidAmount();

    boolean isCreative = player.capabilities.isCreativeMode;
    boolean isSurvival = !isCreative;

    FluidGetResult result = map(copy, space);
    if (result != null && result.fluidStack != null && result.fluidStack.amount > 0) {
        if (isCreative) {
            held = copy;// so we don't change the stack held by the player.
        }
        int potential = held.getCount();
        // Insert a single item until a fluid was not accepted.
        for (int p = 0; p < potential; p++) {
            int accepted = fill(result.fluidStack, false);
            if (isCreative ? (accepted > 0) : (accepted == result.fluidStack.amount)) {
                hasFilled = true;
                int reallyAccepted = fill(result.fluidStack, true);
                if (reallyAccepted != accepted) {
                    throw new IllegalStateException("We seem to be buggy! (accepted = " + accepted
                            + ", reallyAccepted = " + reallyAccepted + ")");
                }
                held.shrink(1);
                if (isSurvival) {
                    if (held.isEmpty()) {
                        held = result.itemStack;
                        break;
                    } else if (!result.itemStack.isEmpty()) {
                        player.inventory.addItemStackToInventory(result.itemStack);
                        player.inventoryContainer.detectAndSendChanges();
                    } else {
                        continue;
                    }
                } else if (held.isEmpty()) {
                    break;
                }
            } else {
                break;
            }
        }
        if (isSurvival) {
            player.inventory.setItemStack(held.isEmpty() ? StackUtil.EMPTY : held);
            ((EntityPlayerMP) player).updateHeldItem();
        }
        if (hasFilled) {
            FluidStack fl = getFluid();
            if (fl != null) {
                SoundEvent sound = fl.getFluid().getEmptySound(container.player.world,
                        container.player.getPosition());
                container.player.world.playSound(null, player.getPosition(), sound, SoundCategory.BLOCKS, 1, 1);
            }
            return;
        }
    }
    // Now try to drain the fluid into the item
    IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(held.copy());
    if (fluidHandler == null)
        return;
    FluidStack drained = drain(capacity, false);
    if (drained == null || drained.amount <= 0)
        return;
    int filled = fluidHandler.fill(drained, true);
    if (filled > 0) {
        FluidStack reallyDrained = drain(filled, true);
        if ((reallyDrained == null || reallyDrained.amount != filled)) {
            throw new IllegalStateException("Somehow drained differently than expected! ( drained = "//
                    + drained + ", filled = " + filled + ", reallyDrained = " + reallyDrained + " )");
        }
        if (isSurvival) {
            ItemStack filledContainer = fluidHandler.getContainer();
            player.inventory.setItemStack(filledContainer);
            ((EntityPlayerMP) player).updateHeldItem();
        }
        SoundEvent sound = reallyDrained.getFluid().getFillSound(container.player.world,
                container.player.getPosition());
        container.player.world.playSound(null, player.getPosition(), sound, SoundCategory.BLOCKS, 1, 1);
    }
}

From source file:buildcraft.lib.fluids.Tank.java

License:Mozilla Public License

/** Maps the given stack to a fluid result.
 * /*from   w  ww  .  j  ava2  s. c  om*/
 * @param stack The stack to map. This will ALWAYS have an {@link ItemStack#getCount()} of 1.
 * @param space The maximum amount of fluid that can be accepted by this tank. */
protected FluidGetResult map(ItemStack stack, int space) {
    IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(stack.copy());
    if (fluidHandler == null)
        return null;
    FluidStack drained = fluidHandler.drain(space, true);
    if (drained == null || drained.amount <= 0)
        return null;
    ItemStack leftOverStack = fluidHandler.getContainer();
    if (leftOverStack.isEmpty())
        leftOverStack = StackUtil.EMPTY;
    return new FluidGetResult(leftOverStack, drained);
}

From source file:com.lothrazar.cyclicmagic.block.anvilmagma.BlockAnvilMagma.java

License:Open Source License

@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player,
        EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    // check the TE
    TileEntityAnvilMagma te = (TileEntityAnvilMagma) world.getTileEntity(pos);
    boolean success = FluidUtil.interactWithFluidHandler(player, hand, world, pos, side);
    if (te != null) {
        if (!world.isRemote) {
            int currentFluid = te.getCurrentFluidStackAmount();
            UtilChat.sendStatusMessage(player, UtilChat.lang("cyclic.fluid.amount") + currentFluid);
        }//w  w  w  . j  av  a 2s.  co  m
    }
    // otherwise return true if it is a fluid handler to prevent in world placement
    return success || FluidUtil.getFluidHandler(player.getHeldItem(hand)) != null
            || super.onBlockActivated(world, pos, state, player, hand, side, hitX, hitY, hitZ);
}

From source file:com.lothrazar.cyclicmagic.block.enchanter.BlockEnchanter.java

License:Open Source License

@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player,
        EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    // check the TE
    TileEntityEnchanter te = (TileEntityEnchanter) world.getTileEntity(pos);
    boolean success = FluidUtil.interactWithFluidHandler(player, hand, world, pos, side);
    if (te != null) {
        if (!world.isRemote) {
            int currentFluid = te.getCurrentFluidStackAmount();
            UtilChat.sendStatusMessage(player, UtilChat.lang("cyclic.fluid.amount") + currentFluid);
        }//  ww w .j a v  a  2 s. co  m
    }
    // otherwise return true if it is a fluid handler to prevent in world placement
    return success || FluidUtil.getFluidHandler(player.getHeldItem(hand)) != null
            || super.onBlockActivated(world, pos, state, player, hand, side, hitX, hitY, hitZ);
}