import { world } from '@minecraft/server'
import { removeItemFromHand } from 'ds/mch/player/player.js'

world.beforeEvents.worldInitialize.subscribe(event => {
    event.itemComponentRegistry.registerCustomComponent('ds_mc:throw_egg', {
        onUse(arg) {
            let { itemStack, source } = arg

            const location = {
                x: source.location.x + (source.getViewDirection().x * 1),
                y: source.location.y + 1.5 + (source.getViewDirection().y * 1),
                z: source.location.z + (source.getViewDirection().z * 1)
            }
            const projectile = source.dimension.spawnEntity(itemStack.typeId, location)
            const impulseForce = 1
            const direction = {
                x: source.getViewDirection().x * impulseForce,
                y: source.getViewDirection().y * impulseForce,
                z: source.getViewDirection().z * impulseForce
            }

            projectile.applyImpulse(direction)

            removeItemFromHand(source, 1)
        }
    })
})