package org.paolo565.drills.utils;

import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class InventoryUtils {
    public static int countItemsInInventory(Inventory inventory, Material material) {
        int count = 0;

        for(int i = 0; i < inventory.getSize(); i++) {
            ItemStack stack = inventory.getItem(i);
            if(stack.getType() == material) {
                count += stack.getAmount();
            }
        }

        return count;
    }

    public static void removeItemsFromInventory(Inventory inventory, Material material, int amount) {
        for(int i = 0; i < inventory.getSize() && amount > 0; i++) {
            ItemStack stack = inventory.getItem(i);
            if(stack.getType() == material) {
                int remove = stack.getAmount() > amount ? amount : stack.getAmount();
                amount -= remove;
                if(stack.getAmount() == remove) {
                    stack.setType(Material.AIR);
                } else {
                    stack.setAmount(stack.getAmount() - remove);
                }
            }
        }
    }
}

@@ -395,32 +395,49 @@
         if(stack
+ != null && stack
 .getType() == ma
@@ -783,24 +783,41 @@
     if(stack
+ != null && stack
 .getType() =

Fix NullPointerException when the inventory doesn't contain sugar canes