Example usage for net.minecraftforge.items ItemHandlerHelper canItemStacksStack

List of usage examples for net.minecraftforge.items ItemHandlerHelper canItemStacksStack

Introduction

In this page you can find the example usage for net.minecraftforge.items ItemHandlerHelper canItemStacksStack.

Prototype

public static boolean canItemStacksStack(@Nonnull ItemStack a, @Nonnull ItemStack b) 

Source Link

Usage

From source file:blusunrize.immersiveengineering.common.blocks.metal.TileEntityArcFurnace.java

@Override
public boolean additionalCanProcessCheck(MultiblockProcess<ArcFurnaceRecipe> process) {
    if (!hasElectrodes())
        return false;
    if (process.recipe != null && !process.recipe.slag.isEmpty()) {
        if (this.inventory.get(22).isEmpty())
            return true;
        return ItemHandlerHelper.canItemStacksStack(this.inventory.get(22), process.recipe.slag)
                && inventory.get(22).getCount() + process.recipe.slag.getCount() <= getSlotLimit(22);
    }//from w  w w. j  av  a  2  s  . c  om
    return true;
}

From source file:blusunrize.immersiveengineering.common.blocks.metal.TileEntityArcFurnace.java

@Override
public void onProcessFinish(MultiblockProcess<ArcFurnaceRecipe> process) {
    if (!process.recipe.slag.isEmpty()) {
        if (this.inventory.get(22).isEmpty())
            this.inventory.set(22, process.recipe.slag.copy());
        else if (ItemHandlerHelper.canItemStacksStack(this.inventory.get(22), process.recipe.slag)
                || inventory.get(22).getCount() + process.recipe.slag.getCount() > getSlotLimit(22))
            this.inventory.get(22).grow(process.recipe.slag.getCount());
    }//from  w  w w.  jav  a 2s  .co m
}

From source file:blusunrize.immersiveengineering.common.blocks.metal.TileEntityBelljar.java

@Override
public void update() {
    ApiUtils.checkForNeedlessTicking(this);
    if (dummy != 0 || world.getRedstonePowerFromNeighbors(getPos()) > 0)
        return;//w  w  w  .j a v  a 2s  .  c  o  m
    ItemStack soil = inventory.get(SLOT_SOIL);
    ItemStack seed = inventory.get(SLOT_SEED);
    if (getWorld().isRemote) {
        if (energyStorage.getEnergyStored() > IEConfig.Machines.belljar_consumption && fertilizerAmount > 0
                && renderActive) {
            IPlantHandler handler = getCurrentPlantHandler();
            if (handler != null && handler.isCorrectSoil(seed, soil) && fertilizerAmount > 0) {
                if (renderGrowth < 1) {
                    renderGrowth += belljar_growth_mod
                            * handler.getGrowthStep(seed, soil, renderGrowth, this, fertilizerMod, true);
                    fertilizerAmount--;
                } else
                    renderGrowth = handler.resetGrowth(seed, soil, renderGrowth, this, true);
                if (Utils.RAND.nextInt(8) == 0) {
                    double partX = getPos().getX() + .5;
                    double partY = getPos().getY() + 2.6875;
                    double partZ = getPos().getZ() + .5;
                    ImmersiveEngineering.proxy.spawnRedstoneFX(getWorld(), partX, partY, partZ, .25, .25, .25,
                            1f, .55f, .1f, .1f);
                }
            }
        }
    } else {
        if (!seed.isEmpty()) {
            IPlantHandler handler = getCurrentPlantHandler();
            if (handler != null && handler.isCorrectSoil(seed, soil) && fertilizerAmount > 0
                    && energyStorage.extractEnergy(IEConfig.Machines.belljar_consumption,
                            true) == IEConfig.Machines.belljar_consumption) {
                boolean consume = false;
                if (growth >= 1) {
                    ItemStack[] outputs = handler.getOutput(seed, soil, this);
                    int canFit = 0;
                    boolean[] emptySlotsUsed = new boolean[4];
                    for (ItemStack output : outputs)
                        if (!output.isEmpty())
                            for (int j = 3; j < 7; j++) {
                                ItemStack existing = inventory.get(j);
                                if ((existing.isEmpty() && !emptySlotsUsed[j - 3])
                                        || (ItemHandlerHelper.canItemStacksStack(existing, output)
                                                && existing.getCount() + output.getCount() <= existing
                                                        .getMaxStackSize())) {
                                    canFit++;
                                    if (existing.isEmpty())
                                        emptySlotsUsed[j - 3] = true;
                                    break;
                                }
                            }
                    if (canFit >= outputs.length) {
                        for (ItemStack output : outputs)
                            for (int j = 3; j < 7; j++) {
                                ItemStack existing = inventory.get(j);
                                if (existing.isEmpty()) {
                                    inventory.set(j, output.copy());
                                    break;
                                } else if (ItemHandlerHelper.canItemStacksStack(existing, output)
                                        && existing.getCount() + output.getCount() <= existing
                                                .getMaxStackSize()) {
                                    existing.grow(output.getCount());
                                    break;
                                }
                            }
                        growth = handler.resetGrowth(seed, soil, growth, this, false);
                        consume = true;
                    }
                } else if (growth < 1) {
                    growth += belljar_growth_mod
                            * handler.getGrowthStep(seed, soil, growth, this, fertilizerMod, false);
                    consume = true;
                    if (world.getTotalWorldTime() % 32 == ((getPos().getX() ^ getPos().getZ()) & 31))
                        sendSyncPacket(0);
                }
                if (consume) {
                    energyStorage.extractEnergy(IEConfig.Machines.belljar_consumption, false);
                    fertilizerAmount--;
                    if (!renderActive) {
                        renderActive = true;
                        sendSyncPacket(0);
                    }
                } else if (renderActive) {
                    renderActive = false;
                    sendSyncPacket(0);
                }
            } else
                growth = 0;

            if (fertilizerAmount <= 0 && tank.getFluidAmount() >= IEConfig.Machines.belljar_fluid) {
                FluidFertilizerHandler fluidFert = BelljarHandler.getFluidFertilizerHandler(tank.getFluid());
                if (fluidFert != null) {
                    fertilizerMod = fluidFert.getGrowthMultiplier(tank.getFluid(), seed, soil, this);
                    tank.drain(IEConfig.Machines.belljar_fluid, true);
                    ItemStack fertilizer = inventory.get(SLOT_FERTILIZER);
                    if (!fertilizer.isEmpty()) {
                        ItemFertilizerHandler itemFert = BelljarHandler.getItemFertilizerHandler(fertilizer);
                        if (itemFert != null)
                            fertilizerMod *= itemFert.getGrowthMultiplier(fertilizer, seed, soil, this);
                        fertilizer.shrink(1);
                        if (fertilizer.getCount() <= 0)
                            inventory.set(2, ItemStack.EMPTY);
                    }
                    fertilizerAmount = IEConfig.Machines.belljar_fertilizer;
                    sendSyncPacket(1);
                }
            }
        } else
            growth = 0;

        if (world.getTotalWorldTime() % 8 == 0) {
            BlockPos outputPos = getPos().up().offset(facing.getOpposite());
            TileEntity outputTile = Utils.getExistingTileEntity(world, outputPos);
            if (outputTile != null)
                for (int j = 3; j < 7; j++) {
                    ItemStack outStack = inventory.get(j);
                    if (!outStack.isEmpty()) {
                        int outCount = Math.min(outStack.getCount(), 16);
                        ItemStack stack = Utils.copyStackWithAmount(outStack, outCount);
                        stack = Utils.insertStackIntoInventory(outputTile, stack, facing);
                        if (!stack.isEmpty())
                            outCount -= stack.getCount();
                        outStack.shrink(outCount);
                        if (outStack.getCount() <= 0)
                            this.inventory.set(j, ItemStack.EMPTY);
                    }
                }
        }
    }
}

From source file:blusunrize.immersiveengineering.common.util.inventory.IEInventoryHandler.java

@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
    if (!canInsert[slot] || stack.isEmpty())
        return stack;
    stack = stack.copy();/*from  w w w.j ava 2 s.c om*/

    if (!inv.isStackValid(this.slotOffset + slot, stack))
        return stack;

    int offsetSlot = this.slotOffset + slot;
    ItemStack currentStack = inv.getInventory().get(offsetSlot);

    if (currentStack.isEmpty()) {
        int accepted = Math.min(stack.getMaxStackSize(), inv.getSlotLimit(offsetSlot));
        if (accepted < stack.getCount()) {
            if (!simulate) {
                inv.getInventory().set(offsetSlot, stack.splitStack(accepted));
                inv.doGraphicalUpdates(offsetSlot);
                return stack;
            } else {
                stack.shrink(accepted);
                return stack;
            }
        } else {
            if (!simulate) {
                inv.getInventory().set(offsetSlot, stack);
                inv.doGraphicalUpdates(offsetSlot);
            }
            return ItemStack.EMPTY;
        }
    } else {
        if (!ItemHandlerHelper.canItemStacksStack(stack, currentStack))
            return stack;

        int accepted = Math.min(stack.getMaxStackSize(), inv.getSlotLimit(offsetSlot))
                - currentStack.getCount();
        if (accepted < stack.getCount()) {
            if (!simulate) {
                ItemStack newStack = stack.splitStack(accepted);
                newStack.grow(currentStack.getCount());
                inv.getInventory().set(offsetSlot, newStack);
                inv.doGraphicalUpdates(offsetSlot);
                return stack;
            } else {
                stack.shrink(accepted);
                return stack;
            }
        } else {
            if (!simulate) {
                ItemStack newStack = stack.copy();
                newStack.grow(currentStack.getCount());
                inv.getInventory().set(offsetSlot, newStack);
                inv.doGraphicalUpdates(offsetSlot);
            }
            return ItemStack.EMPTY;
        }
    }
}

From source file:com.buuz135.industrial.utils.ItemStackUtils.java

License:Open Source License

public static void fillItemFromTank(ItemStackHandler fluidItems, IFluidTank tank) {
    if (tank.getFluid() == null)
        return;//from   ww w  . j  a  va  2 s.  c om
    ItemStack stack = fluidItems.getStackInSlot(0).copy();
    if (!stack.isEmpty()) {
        if (stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
            FluidActionResult result = FluidUtil.tryFillContainer(stack, (IFluidHandler) tank,
                    tank.getCapacity(), null, false);
            if (result.isSuccess() && (fluidItems.getStackInSlot(1).isEmpty()
                    || (ItemHandlerHelper.canItemStacksStack(result.getResult(), fluidItems.getStackInSlot(1))
                            && result.getResult().getCount() + fluidItems.getStackInSlot(1).getCount() <= result
                                    .getResult().getMaxStackSize()))) {
                result = FluidUtil.tryFillContainer(stack, (IFluidHandler) tank, tank.getCapacity(), null,
                        true);
                if (fluidItems.getStackInSlot(1).isEmpty()) {
                    fluidItems.setStackInSlot(1, result.getResult());
                } else {
                    fluidItems.getStackInSlot(1).grow(1);
                }
                fluidItems.getStackInSlot(0).shrink(1);
            }
        }
    }
}

From source file:com.buuz135.industrial.utils.ItemStackUtils.java

License:Open Source License

public static void fillTankFromItem(ItemStackHandler fluidItems, IFluidTank tank) {
    ItemStack stack = fluidItems.getStackInSlot(0).copy();
    if (!stack.isEmpty()) {
        if (stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
            IFluidHandlerItem cap = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY,
                    null);//  w  w w  .j  av a 2s .c o  m
            FluidStack fluidStack = cap.drain(1000, false);
            if (fluidStack != null && tank.fill(fluidStack, false) == 0)
                return;
            FluidActionResult result = FluidUtil.tryEmptyContainer(stack, (IFluidHandler) tank, 1000, null,
                    false);
            if (result.isSuccess() && (fluidItems.getStackInSlot(1).isEmpty()
                    || (ItemHandlerHelper.canItemStacksStack(result.getResult(), fluidItems.getStackInSlot(1))
                            && result.getResult().getCount() + fluidItems.getStackInSlot(1).getCount() <= result
                                    .getResult().getMaxStackSize()))) {
                result = FluidUtil.tryEmptyContainer(stack, (IFluidHandler) tank, tank.getCapacity(), null,
                        true);
                if (fluidItems.getStackInSlot(1).isEmpty()) {
                    fluidItems.setStackInSlot(1, result.getResult());
                } else {
                    fluidItems.getStackInSlot(1).grow(1);
                }
                fluidItems.getStackInSlot(0).shrink(1);
            }
        }
    }
}

From source file:com.teambr.bookshelf.common.items.InventoryHandlerItem.java

License:Creative Commons License

/**
 * Inserts an ItemStack into the given slot and return the remainder.
 * The ItemStack should not be modified in this function!
 * Note: This behaviour is subtly different from IFluidHandlers.fill()
 *
 * @param slot     Slot to insert into./*from w  w  w . j  av  a  2 s.  com*/
 * @param stack    ItemStack to insert.
 * @param simulate If true, the insertion is only simulated
 * @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null).
 * May be the same as the input ItemStack if unchanged, otherwise a new ItemStack.
 **/
@Nonnull
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
    checkStackTag();
    if (isItemValidForSlot(slot, stack) || stack.isEmpty() || !isValidSlot(slot))
        return stack;

    ItemStack existing = this.inventoryContents.get(slot);

    int limit = getSlotLimit(slot);

    if (!existing.isEmpty()) {
        if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
            return stack;

        limit -= existing.getCount();
    }

    if (limit <= 0)
        return stack;

    boolean reachedLimit = stack.getCount() > limit;

    if (!simulate) {
        if (existing.isEmpty()) {
            this.inventoryContents.set(slot,
                    reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.setCount(existing.getCount() + (reachedLimit ? limit : stack.getCount()));
        }
        onInventoryChanged(slot);
    }

    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit)
            : ItemStack.EMPTY;
}

From source file:com.teambr.bookshelf.common.tiles.InventoryHandler.java

License:Creative Commons License

/**
 * Inserts an ItemStack into the given slot and return the remainder.
 * The ItemStack should not be modified in this function!
 * Note: This behaviour is subtly different from IFluidHandlers.fill()
 *
 * @param slot     Slot to insert into.//from   w ww .  j  ava2  s . c  om
 * @param stack    ItemStack to insert.
 * @param simulate If true, the insertion is only simulated
 * @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null).
 *         May be the same as the input ItemStack if unchanged, otherwise a new ItemStack.
 **/
@Nonnull
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
    if (!isItemValidForSlot(slot, stack))
        return stack;

    if (stack.isEmpty() || !isValidSlot(slot))
        return stack;

    ItemStack existing = this.inventoryContents.get(slot);

    int limit = getSlotLimit(slot);

    if (!existing.isEmpty()) {
        if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
            return stack;

        limit -= existing.getCount();
    }

    if (limit <= 0)
        return stack;

    boolean reachedLimit = stack.getCount() > limit;

    if (!simulate) {
        if (existing.isEmpty()) {
            this.inventoryContents.set(slot,
                    reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.setCount(existing.getCount() + (reachedLimit ? limit : stack.getCount()));
        }
        onInventoryChanged(slot);
    }

    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit)
            : ItemStack.EMPTY;
}

From source file:com.teambrmodding.neotech.common.tiles.AbstractMachine.java

License:Creative Commons License

/**
 * Inserts an ItemStack into the given slot and return the remainder.
 * The ItemStack should not be modified in this function!
 * Note: This behaviour is subtly different from IFluidHandlers.fill()
 *
 * @param slot     Slot to insert into./*from   w  ww .ja v a  2s.c om*/
 * @param stack    ItemStack to insert.
 * @param simulate If true, the insertion is only simulated
 * @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null).
 *         May be the same as the input ItemStack if unchanged, otherwise a new ItemStack.
 **/
@Nonnull
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
    if (stack.isEmpty() || !isItemValidForSlot(slot, stack))
        return ItemStack.EMPTY;

    if (!isValidSlot(slot))
        return ItemStack.EMPTY;

    ItemStack existing = this.inventoryContents.get(slot);

    int limit = getSlotLimit(slot);

    if (!existing.isEmpty()) {
        if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
            return stack;

        limit -= existing.getCount();
    }

    if (limit <= 0)
        return stack;

    boolean reachedLimit = stack.getCount() > limit;

    if (!simulate) {
        if (existing.isEmpty()) {
            this.inventoryContents.set(slot,
                    reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.grow(reachedLimit ? limit : stack.getCount());
        }
        onInventoryChanged(slot);
    }
    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit)
            : ItemStack.EMPTY;
}

From source file:com.teambrmodding.neotech.common.tiles.storage.TileEnergyStorage.java

License:Creative Commons License

/**
 * Inserts an ItemStack into the given slot and return the remainder.
 * The ItemStack should not be modified in this function!
 * Note: This behaviour is subtly different from IFluidHandlers.fill()
 *
 * @param slot     Slot to insert into.// w  ww . j av  a2  s  . co m
 * @param stack    ItemStack to insert.
 * @param simulate If true, the insertion is only simulated
 * @return The remaining ItemStack that was not inserted (if the entire stack is accepted, then return null).
 *         May be the same as the input ItemStack if unchanged, otherwise a new ItemStack.
 **/
@Nonnull
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
    if (stack == null || stack.getCount() == 0 || !isItemValidForSlot(slot, stack))
        return ItemStack.EMPTY;

    if (!isValidSlot(slot))
        return ItemStack.EMPTY;

    ItemStack existing = this.inventoryContents.get(slot);

    int limit = getSlotLimit(slot);

    if (!existing.isEmpty()) {
        if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
            return stack;

        limit -= existing.getCount();
    }

    if (limit <= 0)
        return stack;

    boolean reachedLimit = stack.getCount() > limit;

    if (!simulate) {
        if (existing.isEmpty()) {
            this.inventoryContents.set(slot,
                    reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
        } else {
            existing.setCount(existing.getCount() + (reachedLimit ? limit : stack.getCount()));
        }
        onInventoryChanged(slot);
    }

    return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit)
            : ItemStack.EMPTY;
}