So I have a player, and a shop plugin.
The shop tries to give the player 16 WOOL.
If the player has a free 1 to 63 wool (Ofc having another inv space free) my method should return true. However, if the player has only 63 wool and his other spaces full of other materials, method should return false. My problem here is when the player has 1, and 1, and 1 wool item stacks in inv.getContents(), my method will not calculate it correctly.
Here is my code:
public static boolean canStackItemToInventory(Player player, ItemStack item) {
Material type = item.getType();
int amount = item.getAmount();
if (type.equals(Material.BOW)) return false;
PlayerInventory inventory = player.getInventory();
int itemsNumber = 0;
for (ItemStack itemStack : inventory.getContents()) {
if (itemStack.getType()!=type) continue;
if (itemStack.getAmount()==itemStack.getMaxStackSize()) continue;
itemsNumber+=itemStack.getAmount();
}
itemsNumber+=amount;
return itemsNumber <= type.getMaxStackSize();
}