Java Code Examples for net.minecraftforge.items.IItemHandler#getStackInSlot()

The following examples show how to use net.minecraftforge.items.IItemHandler#getStackInSlot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItemPickupManager.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns the ItemStacks of enabled Pickup Managers in the player's inventory
 */
public static List<ItemStack> getEnabledItems(EntityPlayer player)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    List<Integer> slots = InventoryUtils.getSlotNumbersOfMatchingItems(playerInv, EnderUtilitiesItems.PICKUP_MANAGER);
    List<ItemStack> enabledItems = new ArrayList<ItemStack>();

    for (int slot : slots)
    {
        ItemStack stack = playerInv.getStackInSlot(slot);

        if (isEnabled(stack))
        {
            enabledItems.add(stack);
        }
    }

    return enabledItems;
}
 
Example 2
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Get the slot number of the first slot containing a matching ItemStack (including NBT, ignoring stackSize) within the given slot range.
 * Note: stackIn can be empty.
 * @return The slot number of the first slot with a matching ItemStack, or -1 if there were no matches.
 */
public static int getSlotOfFirstMatchingItemStackWithinSlotRange(IItemHandler inv, @Nonnull ItemStack stackIn, SlotRange slotRange)
{
    final int lastSlot = Math.min(inv.getSlots() - 1, slotRange.lastInc);

    for (int slot = slotRange.first; slot <= lastSlot; slot++)
    {
        ItemStack stack = inv.getStackInSlot(slot);

        if (areItemStacksEqual(stack, stackIn))
        {
            return slot;
        }
    }

    return -1;
}
 
Example 3
Source File: TileGenericPipe.java    From Logistics-Pipes-2 with MIT License 6 votes vote down vote up
public ArrayList<Item> getItemTypesInInventory(EnumFacing face){
	ArrayList<Item> result = new ArrayList<Item>();
	
	if (!hasInventoryOnSide(face.getIndex())) {
		return result;
	}
	TileEntity te = world.getTileEntity(getPos().offset(face));
	if (!te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite())) {
		return result;
	}
	IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite());
	for(int i=0; i < itemHandler.getSlots(); i++) {
		ItemStack slotStack = itemHandler.getStackInSlot(i);
		Item item = slotStack.getItem();
		if(!slotStack.isEmpty() && !result.contains(item)) {
			result.add(item);
		}
	}
	return result;
}
 
Example 4
Source File: TileGenericPipe.java    From Logistics-Pipes-2 with MIT License 6 votes vote down vote up
public ArrayList<ItemStack> getItemsInInventory(EnumFacing face){
	ArrayList<ItemStack> result = new ArrayList<ItemStack>();
	
	if (!hasInventoryOnSide(face.getIndex())) {
		return result;
	}
	TileEntity te = world.getTileEntity(getPos().offset(face));
	if (!te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite())) {
		return result;
	}
	IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite());
	for(int i=0; i < itemHandler.getSlots(); i++) {
		ItemStack slotStack = itemHandler.getStackInSlot(i);
		if(!slotStack.isEmpty()) {
			result.add(slotStack);
		}
	}
	return result;
}
 
Example 5
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a copy of the non-empty stacks in the inventory and returns it in a new NonNullList.
 * @param inv
 * @return a NonNullList containing copies of the non-empty stacks
 */
public static NonNullList<ItemStack> createInventorySnapshotOfNonEmptySlots(IItemHandler inv)
{
    final int invSize = inv.getSlots();
    NonNullList<ItemStack> items = NonNullList.create();

    for (int i = 0; i < invSize; i++)
    {
        ItemStack stack = inv.getStackInSlot(i);

        if (stack.isEmpty() == false)
        {
            items.add(stack.copy());
        }
    }

    return items;
}
 
Example 6
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Counts the number of items in the inventory <b>inv</b> that are identical to <b>stackTemplate</b>.
 * If <b>useOreDict</b> is true, then Ore Dictionary matches are also accepted.
 */
public static int getNumberOfMatchingItemsInInventory(IItemHandler inv, @Nonnull ItemStack stackTemplate, boolean useOreDict)
{
    int found = 0;
    final int invSize = inv.getSlots();

    for (int slot = 0; slot < invSize; slot++)
    {
        ItemStack stackTmp = inv.getStackInSlot(slot);

        if (stackTmp.isEmpty() == false)
        {
            if (areItemStacksEqual(stackTmp, stackTemplate) ||
                (useOreDict && areItemStacksOreDictMatch(stackTmp, stackTemplate)))
            {
                found += stackTmp.getCount();
            }
        }
    }

    return found;
}
 
Example 7
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Get all the slot numbers that have matching items in the given inventory.
 * If <b>damage</b> is OreDictionary.WILDCARD_VALUE, then the item damage is ignored.
 * @param inv
 * @param item
 * @return an ArrayList containing the slot numbers of the slots with matching items
 */
public static List<Integer> getSlotNumbersOfMatchingItems(IItemHandler inv, Item item, int meta)
{
    List<Integer> slots = new ArrayList<Integer>();
    final int invSize = inv.getSlots();

    for (int slot = 0; slot < invSize; ++slot)
    {
        ItemStack stack = inv.getStackInSlot(slot);

        if (stack.isEmpty() == false && stack.getItem() == item &&
            (stack.getMetadata() == meta || meta == OreDictionary.WILDCARD_VALUE))
        {
            slots.add(Integer.valueOf(slot));
        }
    }

    return slots;
}
 
Example 8
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Get the slot number of the first slot containing a matching item and damage value.
 * If <b>damage</b> is OreDictionary.WILDCARD_VALUE, then the item damage is ignored.
 * @return The slot number of the first slot with a matching item and damage value, or -1 if there are no such items in the inventory.
 */
public static int getSlotOfFirstMatchingItem(IItemHandler inv, Item item, int meta)
{
    int invSize = inv.getSlots();

    for (int slot = 0; slot < invSize; slot++)
    {
        ItemStack stack = inv.getStackInSlot(slot);

        if (stack.isEmpty() == false && stack.getItem() == item &&
            (stack.getMetadata() == meta || meta == OreDictionary.WILDCARD_VALUE))
        {
            return slot;
        }
    }

    return -1;
}
 
Example 9
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Get the slot number of the last slot containing a matching item and damage value.
 * If <b>damage</b> is OreDictionary.WILDCARD_VALUE, then the item damage is ignored.
 * @param inv
 * @param item
 * @return The slot number of the last slot with a matching item and damage value, or -1 if there are no such items in the inventory.
 */
public static int getSlotOfLastMatchingItem(IItemHandler inv, Item item, int meta)
{
    for (int slot = inv.getSlots() - 1; slot >= 0; slot--)
    {
        ItemStack stack = inv.getStackInSlot(slot);

        if (stack.isEmpty() == false && stack.getItem() == item &&
            (stack.getMetadata() == meta || meta == OreDictionary.WILDCARD_VALUE))
        {
            return slot;
        }
    }

    return -1;
}
 
Example 10
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Get the ItemStack that has the given UUID stored in its NBT. If <b>containerTagName</b>
 * is not null, then the UUID is read from a compound tag by that name.
 */
public static ItemStack getItemStackByUUID(IItemHandler inv, UUID uuid, @Nullable String containerTagName)
{
    final int invSize = inv.getSlots();

    for (int slot = 0; slot < invSize; slot++)
    {
        ItemStack stack = inv.getStackInSlot(slot);

        if (stack.isEmpty() == false && uuid.equals(NBTUtils.getUUIDFromItemStack(stack, containerTagName, false)))
        {
            return stack;
        }
    }

    return ItemStack.EMPTY;
}
 
Example 11
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns a map of how many slots contain the same item, for each item found in the inventory.
 */
public static Map<ItemType, Integer> getSlotCountPerItem(IItemHandler inv)
{
    Map<ItemType, Integer> slots = new HashMap<ItemType, Integer>();
    final int invSize = inv.getSlots();

    for (int slot = 0; slot < invSize; slot++)
    {
        ItemStack stackTmp = inv.getStackInSlot(slot);

        if (stackTmp.isEmpty() == false)
        {
            ItemType item = new ItemType(stackTmp);
            Integer count = slots.get(item);
            count = (count != null) ? count + 1 : 1;
            slots.put(item, Integer.valueOf(count));
        }
    }

    return slots;
}
 
Example 12
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns the first matching item from the player's inventory, or an empty stack.
 */
public static ItemStack getFirstItemOfType(EntityPlayer player, Item item)
{
    IItemHandler inv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    final int numSlots = inv.getSlots();

    for (int slot = 0; slot < numSlots; slot++)
    {
        ItemStack stack = inv.getStackInSlot(slot);

        if (stack.isEmpty() == false && stack.getItem() == item)
        {
            return stack;
        }
    }

    return ItemStack.EMPTY;
}
 
Example 13
Source File: DryingRackBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void dropInventoryItems(World worldIn, BlockPos pos, IItemHandler inventory)
{
    for (int i = 0; i < inventory.getSlots(); ++i)
    {
        ItemStack itemstack = inventory.getStackInSlot(i);

        if (itemstack.getCount() > 0)
        {
            InventoryHelper.spawnItemStack(worldIn, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), itemstack);
        }
    }
}
 
Example 14
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static boolean canExtractStack(IItemHandler handler, int slot) {
    ItemStack stack = handler.getStackInSlot(slot);
    if (!stack.isEmpty()) {
        return !handler.extractItem(slot, stack.getMaxStackSize(), true).isEmpty();
    }
    return false;
}
 
Example 15
Source File: BlockPhysicsInfuser.java    From Valkyrien-Skies with Apache License 2.0 5 votes vote down vote up
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
    TileEntityPhysicsInfuser tileEntity = (TileEntityPhysicsInfuser) worldIn.getTileEntity(pos);
    // If there's a valid TileEntity, try dropping the contents of it's inventory.
    if (tileEntity != null && !tileEntity.isInvalid()) {
        IItemHandler handler = tileEntity
            .getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        // Safety in case the capabilities system breaks. If we can't find the handler then
        // there isn't anything to drop anyways.
        if (handler != null) {
            // Drop all the items
            for (int slot = 0; slot < handler.getSlots(); slot++) {
                ItemStack stack = handler.getStackInSlot(slot);
                InventoryHelper
                    .spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack);
            }
        }
    }
    // Remove the dummy block of this physics infuser, if there is one.
    BlockPos dummyBlockPos = getDummyStatePos(state, pos);
    super.breakBlock(worldIn, pos, state);
    if (worldIn.getBlockState(dummyBlockPos)
        .getBlock() == ValkyrienSkiesMod.INSTANCE.physicsInfuserDummy) {
        worldIn.setBlockToAir(dummyBlockPos);
    }
    // Finally, delete the tile entity.
    worldIn.removeTileEntity(pos);
}
 
Example 16
Source File: ItemHandlerList.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Nonnull
@Override
public ItemStack getStackInSlot(int slot) {
    IItemHandler itemHandler = handlerBySlotIndex.get(slot);
    int realSlot = slot - baseIndexOffset.get(itemHandler);
    return itemHandler.getStackInSlot(slot - baseIndexOffset.get(itemHandler));
}
 
Example 17
Source File: ChoppingBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void dropInventoryItems(World worldIn, BlockPos pos, IItemHandler inventory)
{
    for (int i = 0; i < inventory.getSlots(); ++i)
    {
        ItemStack itemstack = inventory.getStackInSlot(i);

        if (itemstack.getCount() > 0)
        {
            InventoryHelper.spawnItemStack(worldIn, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), itemstack);
        }
    }
}
 
Example 18
Source File: MachineBase.java    From EmergingTechnology with MIT License 5 votes vote down vote up
@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
    TileEntity te = world.getTileEntity(pos);

    IItemHandler cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

    for (int i = 0; i < cap.getSlots(); ++i) {
        ItemStack itemstack = cap.getStackInSlot(i);

        if (!itemstack.isEmpty()) {
            InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), itemstack);
        }
    }
}
 
Example 19
Source File: TileEntityPhysicsInfuserRenderer.java    From Valkyrien-Skies with Apache License 2.0 4 votes vote down vote up
@Override
public void render(TileEntityPhysicsInfuser tileentity, double x, double y, double z,
    float partialTick,
    int destroyStage, float alpha) {

    this.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
    GlStateManager.pushMatrix();
    GlStateManager.disableLighting();

    GlStateManager.translate(x, y, z);
    GlStateManager.disableAlpha();
    GlStateManager.disableBlend();
    GlStateManager.pushMatrix();

    int brightness = tileentity.getWorld()
        .getCombinedLight(tileentity.getPos(), 0);

    GlStateManager.translate(.5, 0, .5);
    double keyframe = 1;

    IBlockState physicsInfuserState = ValkyrienSkiesMod.INSTANCE.physicsInfuser
        .getStateFromMeta(tileentity.getBlockMetadata());
    EnumFacing enumfacing = physicsInfuserState.getValue(BlockPhysicsInfuser.FACING);
    int coreBrightness =
        physicsInfuserState.getValue(BlockPhysicsInfuser.INFUSER_LIGHT_ON) ? 15728864
            : brightness;
    float physicsInfuserRotation = -enumfacing.getHorizontalAngle() + 180;
    GlStateManager.rotate(physicsInfuserRotation, 0, 1, 0);

    GlStateManager.translate(-.5, 0, -.5);

    // First translate the model one block to the right
    GlStateManager.translate(-1, 0, 0);
    GlStateManager.scale(2, 2, 2);
    GibsAnimationRegistry.getAnimation("physics_infuser_empty")
        .renderAnimation(keyframe, brightness);

    IAtomAnimation cores_animation = GibsAnimationRegistry
        .getAnimation("physics_infuser_cores");
    // Render only the cores that exist within the physics infuser's inventory.
    IItemHandler handler = tileentity
        .getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

    for (TileEntityPhysicsInfuser.EnumInfuserCore infuserCore : TileEntityPhysicsInfuser.EnumInfuserCore
        .values()) {
        if (!handler.getStackInSlot(infuserCore.coreSlotIndex).isEmpty) {
            GlStateManager.pushMatrix();
            GlStateManager
                .translate(0, tileentity.getCoreVerticalOffset(infuserCore, partialTick), 0);
            cores_animation
                .renderAnimationNode(infuserCore.coreModelName, keyframe, coreBrightness);
            GlStateManager.popMatrix();
        }
    }

    GlStateManager.popMatrix();
    GlStateManager.popMatrix();
    GlStateManager.enableLighting();
    GlStateManager.resetColor();
}
 
Example 20
Source File: NBTUtils.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Stores a cached snapshot of the current inventory in a compound tag <b>InvCache</b>.
 * It is meant for tooltip use in the ItemBlocks.
 * @param nbt
 * @return
 */
public static NBTTagCompound storeCachedInventory(NBTTagCompound nbt, IItemHandler inv, int maxEntries)
{
    NBTTagList list = new NBTTagList();
    int stacks = 0;
    long items = 0;
    final int size = inv.getSlots();

    for (int slot = 0; slot < size; slot++)
    {
        ItemStack stack = inv.getStackInSlot(slot);

        if (stack.isEmpty() == false)
        {
            if (stacks < maxEntries)
            {
                NBTTagCompound tag = new NBTTagCompound();
                tag.setString("dn", stack.getDisplayName());
                tag.setInteger("c", stack.getCount());
                list.appendTag(tag);
            }

            stacks++;
            items += stack.getCount();
        }
    }

    if (stacks > 0)
    {
        NBTTagCompound wrapper = new NBTTagCompound();
        wrapper.setTag("il", list);
        wrapper.setInteger("ts", stacks);
        wrapper.setLong("ti", items);
        nbt.setTag("InvCache", wrapper);
    }
    else
    {
        nbt.removeTag("InvCache");
    }

    return nbt;
}