import { EntityComponentTypes, EquipmentSlot, ItemStack } from '@minecraft/server'

export function removeItemFromHand(player, amount = 1) {
    let equipmentComponent = player.getComponent(EntityComponentTypes.Equippable)

    if (equipmentComponent) {
        let equipment = equipmentComponent.getEquipment(EquipmentSlot.Mainhand)

        if (equipment) {
            if (equipment.amount <= amount) {
                equipment = new ItemStack('minecraft:air')
            } else {
                equipment.amount = equipment.amount - amount
            }

            equipmentComponent.setEquipment(EquipmentSlot.Mainhand, equipment)
        }
    }
}

export function getAmountFromHand(player) {
    let equipmentComponent = player.getComponent(EntityComponentTypes.Equippable)

    if (equipmentComponent) {
        let equipment = equipmentComponent.getEquipment(EquipmentSlot.Mainhand)

        if (equipment) {
            return equipment.amount
        }
    }

    return undefined
}