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

The following examples show how to use net.minecraftforge.items.IItemHandler#getSlots() . 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: DamageXpHandler.java    From TinkersToolLeveling with MIT License 7 votes vote down vote up
private void distributeXpToPlayerForTool(EntityPlayer player, ItemStack tool, float damage) {
  if(!tool.isEmpty() && player.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
    IItemHandler itemHandler = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

    // check for identity. should work in most cases because the entity was killed without loading/unloading
    for(int i = 0; i < itemHandler.getSlots(); i++) {
      if(itemHandler.getStackInSlot(i) == tool) {
        TinkerToolLeveling.modToolLeveling.addXp(tool, Math.round(damage), player);
        return;
      }
    }

    // check for equal stack in case instance equality didn't find it
    for(int i = 0; i < itemHandler.getSlots(); i++) {
      if(ToolCore.isEqualTinkersItem(itemHandler.getStackInSlot(i), tool)) {
        TinkerToolLeveling.modToolLeveling.addXp(itemHandler.getStackInSlot(i), Math.round(damage), player);
        return;
      }
    }
  }
}
 
Example 2
Source File: BlockMixin.java    From customstuff4 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
    TileEntity tile = worldIn.getTileEntity(pos);

    if (tile != null)
    {
        IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        if (itemHandler != null)
        {
            for (int i = 0; i < itemHandler.getSlots(); i++)
            {
                ItemStack stack = itemHandler.getStackInSlot(i);
                if (!stack.isEmpty())
                {
                    InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack);
                }
            }
        }
    }

    super.breakBlock(worldIn, pos, state);
}
 
Example 3
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static ItemStack insertItem(IItemHandler handler, ItemStack insert, boolean simulate) {
    insert = insert.copy();
    for (int pass = 0; pass < 2; pass++) {
        for (int slot = 0; slot < handler.getSlots(); slot++) {
            ItemStack stack = handler.getStackInSlot(slot);
            if (pass == 0 && stack.isEmpty()) {
                continue;
            }
            if (insert.isEmpty()) {
                return ItemStack.EMPTY;
            }
            insert = handler.insertItem(slot, insert, simulate);
        }
    }

    return insert;
}
 
Example 4
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 5
Source File: GuiPearlSwap.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void updateText(ItemStack heldItem, ItemStack pearlStorageStack, IPearlStorageHolder pearlStorage) {
	int count = pearlStorage.getPearlCount(pearlStorageStack);
	if (count > 0) {

		IItemHandler itemHandler = pearlStorage.getPearls(pearlStorageStack);
		if (itemHandler != null) {
			if (heldItem.getItem() instanceof IPearlStorageHolder) {
				if (count >= itemHandler.getSlots()) {
					centerText = LibrarianLib.PROXY.translate("wizardry.pearlui.belt_full") + "\n\n" + LibrarianLib.PROXY.translate("wizardry.pearlui.pop_pearls");
				} else {
					centerText = LibrarianLib.PROXY.translate("wizardry.pearlui.pop_pearls") + "\n\n" + LibrarianLib.PROXY.translate("wizardry.pearlui.attach_pearls");
				}
			} else if (heldItem.getItem() instanceof IPearlSwappable) {
				centerText = LibrarianLib.PROXY.translate("wizardry.pearlui.pop_pearls");
			}
		}
	} else if (heldItem.getItem() instanceof IPearlStorageHolder) {
		centerText = LibrarianLib.PROXY.translate("wizardry.pearlui.attach_pearls");
	}
}
 
Example 6
Source File: IPearlStorageHolder.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
default void sortInv(IItemHandler handler) {
	if (handler == null) return;

	Deque<ItemStack> stacks = new ArrayDeque<>();

	final int slots = handler.getSlots();
	for (int i = 0; i < slots; i++) {
		ItemStack stack = handler.extractItem(i, 1, false);
		if (stack.isEmpty()) continue;
		stacks.add(stack);
	}

	for (int i = 0; i < slots; i++) {
		if (stacks.isEmpty()) break;
		handler.insertItem(i, stacks.pop(), false);
	}
}
 
Example 7
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 8
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 9
Source File: SimpleItemFilter.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static int itemFilterMatch(IItemHandler filterSlots, boolean ignoreDamage, boolean ignoreNBTData, ItemStack itemStack) {
    for (int i = 0; i < filterSlots.getSlots(); i++) {
        ItemStack filterStack = filterSlots.getStackInSlot(i);
        if (!filterStack.isEmpty() && areItemsEqual(ignoreDamage, ignoreNBTData, filterStack, itemStack)) {
            return i;
        }
    }
    return -1;
}
 
Example 10
Source File: LootTableHelper.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static List<Integer> getEmptySlotsRandomized(IItemHandler inventory, Random rand) {
    List<Integer> list = Lists.newArrayList();
    for (int i = 0; i < inventory.getSlots(); ++i) {
        if (inventory.getStackInSlot(i).isEmpty()) {
            list.add(i);
        }
    }
    Collections.shuffle(list, rand);
    return list;
}
 
Example 11
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 12
Source File: IPearlStorageHolder.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
default int getPearlCount(ItemStack holder) {
	IItemHandler handler = getPearls(holder);
	if (handler == null) return 0;

	int total = 0;
	for (int i = 0; i < handler.getSlots(); i++) {
		ItemStack pearl = handler.getStackInSlot(i);
		if (pearl.isEmpty()) continue;

		total++;
	}

	return total;
}
 
Example 13
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Checks if the given inventory <b>inv</b> has at least <b>amount</b> number of items
 * matching the item in <b>stackTemplate</b>.
 * If useOreDict is true, then any matches via OreDictionary are also accepted.
 */
public static boolean checkInventoryHasItems(IItemHandler inv, @Nonnull ItemStack stackTemplate, int amount, 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();
            }
        }

        if (found >= amount)
        {
            return true;
        }
    }

    return false;
}
 
Example 14
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Drops all the ItemStacks from the given inventory into the world as EntityItems
 * @param world
 * @param pos
 * @param inv
 */
public static void dropInventoryContentsInWorld(World world, BlockPos pos, IItemHandler inv)
{
    final int invSize = inv.getSlots();

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

        if (stack.isEmpty() == false)
        {
            EntityUtils.dropItemStacksInWorld(world, pos, stack, -1, true);
        }
    }
}
 
Example 15
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static int calcRedstoneFromInventory(IItemHandler inv)
{
    final int slots = inv.getSlots();
    int items = 0;
    int capacity = 0;

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

        if ((inv instanceof IItemHandlerSize) && stack.isEmpty() == false)
        {
            capacity += ((IItemHandlerSize)inv).getItemStackLimit(slot, stack);
        }
        else
        {
            capacity += inv.getSlotLimit(slot);
        }

        if (stack.isEmpty() == false)
        {
            items += stack.getCount();
        }
    }

    if (capacity > 0)
    {
        int strength = (14 * items) / capacity;

        // Emit a signal strength of 1 as soon as there is one item in the inventory
        if (items > 0)
        {
            strength += 1;
        }

        return strength;
    }

    return 0;
}
 
Example 16
Source File: CartHopperBehaviourItems.java    From Signals with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isCartFull(IItemHandler capability){
    for(int i = 0; i < capability.getSlots(); i++) {
        if(capability.getStackInSlot(i).isEmpty()) return false;
    }
    return true;
}
 
Example 17
Source File: Misc.java    From Logistics-Pipes-2 with MIT License 5 votes vote down vote up
public static void spawnInventoryInWorld(World world, double x, double y, double z, IItemHandler inventory){
	if (inventory != null && !world.isRemote){
		for (int i = 0; i < inventory.getSlots(); i ++){
			if (inventory.getStackInSlot(i) != ItemStack.EMPTY){
				world.spawnEntity(new EntityItem(world,x,y,z,inventory.getStackInSlot(i)));
			}
		}
	}
}
 
Example 18
Source File: DestinationProviderItems.java    From Signals with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isCartApplicable(TileEntity te, EntityMinecart cart, Pattern destinationRegex){
    IItemHandler cap = cart.getCapability(ITEM_HANDLER, null);
    if(cap != null && destinationRegex.matcher("ITEM").matches()) {
        IInventory inv = (IInventory)te;
        for(int cartSlot = 0; cartSlot < cap.getSlots(); cartSlot++) {
            ItemStack cartStack = cap.getStackInSlot(cartSlot);
            if(!cartStack.isEmpty()) {
                if(isStackApplicable(cartStack, inv)) return true;
            }
        }
    }
    return false;
}
 
Example 19
Source File: PlainInventory.java    From HoloInventory with MIT License 4 votes vote down vote up
private void scan(IItemHandler ii)
{
    int size = ii.getSlots();
    stacks = new ArrayList<>(size);
    for (int i = 0; i < size; i++) add(ii.getStackInSlot(i));
}
 
Example 20
Source File: TileEntityQuickStackerAdvanced.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Tries to move all items from enabled slots in the player's inventory to the given external inventory
 */
public static Result quickStackItems(IItemHandler playerInv, IItemHandler externalInv, long slotMask, boolean matchingOnly, FilterSettings filter)
{
    Result ret = Result.MOVED_NONE;
    boolean movedAll = true;
    long bit = 0x1;

    for (int slotPlayer = 0; slotPlayer < playerInv.getSlots(); slotPlayer++)
    {
        ItemStack stack = playerInv.getStackInSlot(slotPlayer);

        // Only take from slots that have been enabled
        if ((slotMask & bit) != 0 && stack.isEmpty() == false && (filter == null || filter.itemAllowedByFilter(stack)))
        {
            stack = playerInv.extractItem(slotPlayer, 64, false);

            if (stack.isEmpty())
            {
                continue;
            }

            if (matchingOnly == false || InventoryUtils.getSlotOfLastMatchingItemStack(externalInv, stack) != -1)
            {
                int sizeOrig = stack.getCount();
                stack = InventoryUtils.tryInsertItemStackToInventory(externalInv, stack);

                if (ret == Result.MOVED_NONE && (stack.isEmpty() || stack.getCount() != sizeOrig))
                {
                    ret = Result.MOVED_SOME;
                }
            }

            // Return the items that were left over
            if (stack.isEmpty() == false)
            {
                playerInv.insertItem(slotPlayer, stack, false);
                movedAll = false;
            }
        }

        bit <<= 1;
    }

    if (movedAll && ret == Result.MOVED_SOME)
    {
        ret = Result.MOVED_ALL;
    }

    return ret;
}