Java Code Examples for org.bukkit.inventory.ItemStack#getMaxStackSize()

The following examples show how to use org.bukkit.inventory.ItemStack#getMaxStackSize() . 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: CraftingPipe.java    From Transport-Pipes with MIT License 6 votes vote down vote up
public int spaceForItem(ItemData data) {
    int space = 0;

    for (int i = 0; i < 9; i++) {
        if (i >= cachedItems.size()) {
            space += data.toItemStack().getMaxStackSize();
        } else {
            ItemStack item = cachedItems.get(i);
            if (item.isSimilar(data.toItemStack()) && item.getAmount() < item.getMaxStackSize()) {
                space += item.getMaxStackSize() - item.getAmount();
            }
        }
    }

    return space;
}
 
Example 2
Source File: CraftingPipe.java    From Transport-Pipes with MIT License 6 votes vote down vote up
public ItemStack addCachedItem(ItemStack item, TransportPipes transportPipes) {
    for (ItemStack cachedItem : cachedItems) {
        if (cachedItem.isSimilar(item)) {
            int cachedItemAmount = cachedItem.getAmount();
            cachedItem.setAmount(Math.min(cachedItem.getMaxStackSize(), cachedItemAmount + item.getAmount()));
            int overflow = cachedItemAmount + item.getAmount() - cachedItem.getMaxStackSize();
            if (overflow > 0) {
                item.setAmount(overflow);
            } else {
                item = null;
                break;
            }
        }
    }
    if (cachedItems.size() < 9 && item != null) {
        cachedItems.add(item);
        item = null;
    }

    transportPipes.runTaskSync(() -> {
        settingsInv.save(null);
        settingsInv.populate();
    });
    return item;
}
 
Example 3
Source File: SimpleInventoryContainer.java    From Transport-Pipes with MIT License 6 votes vote down vote up
@Override
public int spaceForItem(TPDirection insertDirection, ItemStack insertion) {
    if (isInvLocked(cachedInvHolder)) {
        return 0;
    }

    int space = 0;

    for (int i = 0; i < cachedInv.getSize(); i++) {
        ItemStack item = cachedInv.getItem(i);
        if (item == null || item.getType() == Material.AIR) {
            space += insertion.getMaxStackSize();
        } else if (item.isSimilar(insertion) && item.getAmount() < item.getMaxStackSize()) {
            space += item.getMaxStackSize() - item.getAmount();
        }
    }

    return space;
}
 
Example 4
Source File: BlockContainer.java    From Transport-Pipes with MIT License 6 votes vote down vote up
/**
 * puts "put" into "before" and returns the result. if "put" can't be accumulated with "before", "before" is returned.
 * if "put" does not fit completely in "before", "put"'s amount decreases by how much does fit and the returned item will have an amount of the max stack size
 */
protected ItemStack accumulateItems(ItemStack before, ItemStack put) {
    if (put == null) {
        return before;
    }
    if (before == null) {
        ItemStack putCopy = put.clone();
        put.setAmount(0);
        return putCopy;
    }
    if (!before.isSimilar(put)) {
        return before;
    }
    int beforeAmount = before.getAmount();
    int putAmount = put.getAmount();
    int maxStackSize = before.getMaxStackSize();
    ItemStack returnItem = before.clone();
    returnItem.setAmount(Math.min(beforeAmount + putAmount, maxStackSize));
    put.setAmount(Math.max(putAmount - (maxStackSize - beforeAmount), 0));
    return returnItem;
}
 
Example 5
Source File: InventoryImpl.java    From Civs with GNU General Public License v3.0 6 votes vote down vote up
private int firstPartial(ItemStack item) {
    ItemStack[] inventory = this.getStorageContents();
    ItemStack filteredItem = item.clone();
    if (item == null) {
        return -1;
    } else {
        for(int i = 0; i < inventory.length; ++i) {
            ItemStack cItem = inventory[i];
            if (cItem != null && cItem.getAmount() < cItem.getMaxStackSize() && cItem.isSimilar(filteredItem)) {
                return i;
            }
        }

        return -1;
    }
}
 
Example 6
Source File: Util.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
public static ItemStack[] getItems(List<List<CVItem>> addItems) {
    List<ItemStack> output = new ArrayList<>();
    outer: for (List<CVItem> tempItems : addItems) {
        double rand = Math.random();
        double prevChance = 0;
        for (CVItem item : tempItems) {
            if ((prevChance < rand) && (prevChance + item.getChance() > rand)) {
                ItemStack is = item.createItemStack();
                is.setAmount(1);
                int amount = item.getQty();
                int max = is.getMaxStackSize();
                for (;;) {
                    ItemStack isa;
                    if (amount > max) {
                        isa = item.createItemStack();
                        isa.setAmount(max);
                    } else {
                        isa = item.createItemStack();
                        isa.setAmount(amount);
                    }
                    output.add(isa);
                    if (amount > max) {
                        amount -= max;
                    } else {
                        continue outer;
                    }
                }
            }
            prevChance += item.getChance();
        }
    }
    return output.toArray(new ItemStack[0]);
}
 
Example 7
Source File: ItemUtil.java    From SonarPet with GNU General Public License v3.0 5 votes vote down vote up
public static boolean matches(ItemStack itemStack1, ItemStack itemStack2) {
    if (itemStack1 == null || itemStack2 == null) {
        return false;
    }

    if (!itemStack1.getType().equals(itemStack2.getType())) {
        return false;
    }

    if (itemStack1.getAmount() != itemStack2.getAmount()) {
        return false;
    }

    if (itemStack1.getDurability() != itemStack2.getDurability()) {
        return false;
    }

    if (itemStack1.getMaxStackSize() != itemStack2.getMaxStackSize()) {
        return false;
    }

    if (itemStack1.hasItemMeta() != itemStack2.hasItemMeta()) {
        return false;
    }

    if (itemStack1.hasItemMeta() && itemStack2.hasItemMeta() && !itemStack1.getItemMeta().equals(itemStack2.getItemMeta())) {
        return false;
    }

    return true;
}
 
Example 8
Source File: CraftInventory.java    From Thermos with GNU General Public License v3.0 5 votes vote down vote up
public int firstPartial(int materialId) {
    ItemStack[] inventory = getContents();
    for (int i = 0; i < inventory.length; i++) {
        ItemStack item = inventory[i];
        if (item != null && item.getTypeId() == materialId && item.getAmount() < item.getMaxStackSize()) {
            return i;
        }
    }
    return -1;
}
 
Example 9
Source File: CraftInventory.java    From Thermos with GNU General Public License v3.0 5 votes vote down vote up
private int firstPartial(ItemStack item) {
    if (item == null) {
        return -1;
    }
    ItemStack[] inventory = getContents();
    ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
    for (int i = 0; i < inventory.length; i++) {
        ItemStack cItem = inventory[i];
        if (cItem != null && cItem.getAmount() < cItem.getMaxStackSize() && cItem.isSimilar(filteredItem)) {
            return i;
        }
    }
    return -1;
}
 
Example 10
Source File: DirtyChestMenu.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
public ItemStack pushItem(ItemStack item, int... slots) {
    if (item == null || item.getType() == Material.AIR) {
        throw new IllegalArgumentException("Cannot push null or AIR");
    }

    int amount = item.getAmount();

    for (int slot : slots) {
        if (amount <= 0) {
            break;
        }

        ItemStack stack = getItemInSlot(slot);

        if (stack == null) {
            replaceExistingItem(slot, item);
            return null;
        }
        else if (stack.getAmount() < stack.getMaxStackSize() && ItemUtils.canStack(item, stack)) {
            amount -= (stack.getMaxStackSize() - stack.getAmount());
            stack.setAmount(Math.min(stack.getAmount() + item.getAmount(), stack.getMaxStackSize()));
        }
    }

    if (amount > 0) {
        return new CustomItem(item, amount);
    }
    else {
        return null;
    }
}
 
Example 11
Source File: PlayerInventoryImpl.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
public int firstPartial(Material material) {
    Validate.notNull(material, "Material cannot be null");
    ItemStack[] inventory = this.getStorageContents();

    for(int i = 0; i < inventory.length; ++i) {
        ItemStack item = inventory[i];
        if (item != null && item.getType() == material && item.getAmount() < item.getMaxStackSize()) {
            return i;
        }
    }

    return -1;
}
 
Example 12
Source File: ItemStackUtils.java    From NovaGuilds with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks if two items are the same and only amount may be different
 *
 * @param itemStack1 item 1
 * @param itemStack2 item 2
 * @return boolean
 */
public static boolean isSimilar(ItemStack itemStack1, ItemStack itemStack2) {
	if(itemStack1 == null && itemStack2 != null || itemStack2 == null) {
		return false;
	}

	if(itemStack1.getType() != itemStack2.getType()) {
		return false;
	}

	if((itemStack1.hasItemMeta() || itemStack2.hasItemMeta()) && !itemStack1.getItemMeta().equals(itemStack2.getItemMeta())) {
		return false;
	}

	if(itemStack1.getDurability() != itemStack2.getDurability()) {
		return false;
	}

	if(!itemStack1.getEnchantments().equals(itemStack2.getEnchantments())) {
		return false;
	}

	if(itemStack1.getMaxStackSize() != itemStack2.getMaxStackSize()) {
		return false;
	}

	return true;
}
 
Example 13
Source File: CraftInventory.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public int firstPartial(int materialId) {
    ItemStack[] inventory = getStorageContents();
    for (int i = 0; i < inventory.length; i++) {
        ItemStack item = inventory[i];
        if (item != null && item.getTypeId() == materialId && item.getAmount() < item.getMaxStackSize()) {
            return i;
        }
    }
    return -1;
}
 
Example 14
Source File: ResourceSpawner.java    From BedwarsRel with GNU General Public License v3.0 5 votes vote down vote up
public boolean canContainItem(Inventory inv, ItemStack item) {
  int space = 0;
  for (ItemStack stack : inv.getContents()) {
    if (stack == null) {
      space += item.getMaxStackSize();
    } else if (stack.getType() == item.getType()
        && stack.getDurability() == item.getDurability()) {
      space += item.getMaxStackSize() - stack.getAmount();
    }
  }
  return space >= item.getAmount();
}
 
Example 15
Source File: InventoryCompressor.java    From Minepacks with GNU General Public License v3.0 5 votes vote down vote up
private void tryToStack(ItemStack stack)
{
	if(stack.getAmount() >= stack.getMaxStackSize()) return;
	for(int i = 0; i < filled && stack.getAmount() > 0; i++)
	{
		if(stack.isSimilar(targetStacks[i]) && targetStacks[i].getAmount() < targetStacks[i].getMaxStackSize())
		{ // Same material and none full stack
			int move = targetStacks[i].getMaxStackSize() - targetStacks[i].getAmount();
			move = Math.min(stack.getAmount(), move);
			targetStacks[i].setAmount(targetStacks[i].getAmount() + move);
			stack.setAmount(stack.getAmount() - move);
		}
	}
}
 
Example 16
Source File: ItemUtil.java    From EchoPet with GNU General Public License v3.0 5 votes vote down vote up
public static boolean matches(ItemStack itemStack1, ItemStack itemStack2) {
    if (itemStack1 == null || itemStack2 == null) {
        return false;
    }

    if (!itemStack1.getType().equals(itemStack2.getType())) {
        return false;
    }

    if (itemStack1.getAmount() != itemStack2.getAmount()) {
        return false;
    }

    if (itemStack1.getDurability() != itemStack2.getDurability()) {
        return false;
    }

    if (itemStack1.getMaxStackSize() != itemStack2.getMaxStackSize()) {
        return false;
    }

    if (itemStack1.hasItemMeta() != itemStack2.hasItemMeta()) {
        return false;
    }

    if (itemStack1.hasItemMeta() && itemStack2.hasItemMeta() && !itemStack1.getItemMeta().equals(itemStack2.getItemMeta())) {
        return false;
    }

    return true;
}
 
Example 17
Source File: CraftInventory.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
private int firstPartial(ItemStack item) {
    ItemStack[] inventory = getStorageContents();
    ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
    if (item == null) {
        return -1;
    }
    for (int i = 0; i < inventory.length; i++) {
        ItemStack cItem = inventory[i];
        if (cItem != null && cItem.getAmount() < cItem.getMaxStackSize() && cItem.isSimilar(filteredItem)) {
            return i;
        }
    }
    return -1;
}
 
Example 18
Source File: CraftInventory.java    From Thermos with GNU General Public License v3.0 4 votes vote down vote up
public HashMap<Integer, ItemStack> addItem(ItemStack... items) {
    Validate.noNullElements(items, "Item cannot be null");
    HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();

    /* TODO: some optimization
     *  - Create a 'firstPartial' with a 'fromIndex'
     *  - Record the lastPartial per Material
     *  - Cache firstEmpty result
     */

    for (int i = 0; i < items.length; i++) {
        ItemStack item = items[i];
        while (true) {
            // Do we already have a stack of it?
            int firstPartial = firstPartial(item);

            // Drat! no partial stack
            if (firstPartial == -1) {
                // Find a free spot!
                int firstFree = firstEmpty();

                if (firstFree == -1) {
                    // No space at all!
                    leftover.put(i, item);
                    break;
                } else {
                    // More than a single stack!
                    if (item.getAmount() > getMaxItemStack()) {
                        CraftItemStack stack = CraftItemStack.asCraftCopy(item);
                        stack.setAmount(getMaxItemStack());
                        setItem(firstFree, stack);
                        item.setAmount(item.getAmount() - getMaxItemStack());
                    } else {
                        // Just store it
                        setItem(firstFree, item);
                        break;
                    }
                }
            } else {
                // So, apparently it might only partially fit, well lets do just that
                ItemStack partialItem = getItem(firstPartial);

                int amount = item.getAmount();
                int partialAmount = partialItem.getAmount();
                int maxAmount = partialItem.getMaxStackSize();

                // Check if it fully fits
                if (amount + partialAmount <= maxAmount) {
                    partialItem.setAmount(amount + partialAmount);
                    break;
                }

                // It fits partially
                partialItem.setAmount(maxAmount);
                item.setAmount(amount + partialAmount - maxAmount);
            }
        }
    }
    return leftover;
}
 
Example 19
Source File: InventoryImpl.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
@Override
public HashMap<Integer, ItemStack> addItem(ItemStack... items) {
    Validate.noNullElements(items, "Item cannot be null");
    HashMap<Integer, ItemStack> leftover = new HashMap();

    label35:
    for(int i = 0; i < items.length; ++i) {
        ItemStack item = items[i];

        while(true) {
            while(true) {
                int firstPartial = this.firstPartial(item);
                if (firstPartial == -1) {
                    int firstFree = this.firstEmpty();
                    if (firstFree == -1) {
                        leftover.put(i, item);
                        continue label35;
                    }

                    if (item.getAmount() <= 64) {
                        this.setItem(firstFree, item);
                        continue label35;
                    }

                    ItemStack stack = item.clone();
                    stack.setAmount(64);
                    this.setItem(firstFree, stack);
                    item.setAmount(item.getAmount() - 64);
                } else {
                    ItemStack partialItem = this.getItem(firstPartial);
                    int amount = item.getAmount();
                    int partialAmount = partialItem.getAmount();
                    int maxAmount = partialItem.getMaxStackSize();
                    if (amount + partialAmount <= maxAmount) {
                        partialItem.setAmount(amount + partialAmount);
                        this.setItem(firstPartial, partialItem);
                        continue label35;
                    }

                    partialItem.setAmount(maxAmount);
                    this.setItem(firstPartial, partialItem);
                    item.setAmount(amount + partialAmount - maxAmount);
                }
            }
        }
    }

    return leftover;
}
 
Example 20
Source File: InventoryGui.java    From InventoryGui with MIT License 4 votes vote down vote up
/**
 * Simulate the collecting to the cursor while respecting elements that can't be modified
 * @param click The click that startet it all
 */
void simulateCollectToCursor(GuiElement.Click click) {
    ItemStack newCursor = click.getEvent().getCursor().clone();

    boolean itemInGui = false;
    for (int i = 0; i < click.getEvent().getView().getTopInventory().getSize(); i++) {
        if (i != click.getEvent().getRawSlot()) {
            ItemStack viewItem = click.getEvent().getView().getTopInventory().getItem(i);
            if (newCursor.isSimilar(viewItem)) {
                itemInGui = true;
            }
            GuiElement element = getElement(i);
            if (element instanceof GuiStorageElement) {
                GuiStorageElement storageElement = (GuiStorageElement) element;
                ItemStack otherStorageItem = storageElement.getStorageItem(i);
                if (addToStack(newCursor, otherStorageItem)) {
                    if (otherStorageItem.getAmount() == 0) {
                        otherStorageItem = null;
                    }
                    storageElement.setStorageItem(i, otherStorageItem);
                    if (newCursor.getAmount() == newCursor.getMaxStackSize()) {
                        break;
                    }
                }
            }
        }
    }

    if (itemInGui) {
        click.getEvent().setCurrentItem(null);
        click.getEvent().setCancelled(true);
        if (click.getEvent().getWhoClicked() instanceof Player) {
            ((Player) click.getEvent().getWhoClicked()).updateInventory();
        }
    
        if (click.getElement() instanceof GuiStorageElement) {
            ((GuiStorageElement) click.getElement()).setStorageItem(click.getSlot(), null);
        }

        if (newCursor.getAmount() < newCursor.getMaxStackSize()) {
            Inventory bottomInventory = click.getEvent().getView().getBottomInventory();
            for (ItemStack bottomIem : bottomInventory) {
                if (addToStack(newCursor, bottomIem)) {
                    if (newCursor.getAmount() == newCursor.getMaxStackSize()) {
                        break;
                    }
                }
            }
        }
        click.getEvent().setCursor(newCursor);
        draw();
    }
}