package eu.mhsl.minenet.minigames.util;

import net.minestom.server.entity.Player;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;

public class InventoryUtil {
    public static void removeItemFromPlayer(Player player, Material material, int amount) {
        ItemStack[] items = player.getInventory().getItemStacks();
        for (int i = 0; i < items.length; i++) {
            ItemStack item = items[i];
            if (item.material() != material) continue;

            int stackSize = item.amount();
            if (stackSize < amount) {
                amount -= stackSize;
                player.getInventory().setItemStack(i, ItemStack.AIR);
            } else {
                player.getInventory().setItemStack(i, item.withAmount(stackSize - amount));
                break;
            }
        }
    }
}
