import EventWrappers from '../../utils/AfterEvents.js';
import { Server } from "../../../libs/Core/index.js";
import { EntityProjectileComponent } from '@minecraft/server';

EventWrappers.SpawnAfterEvent.registerEvent('plank_waao:void_beast_projectile', (e) => {
    let projectile = e.entity;
    const shooter = projectile.dimension.getEntities({
        closest: true,
        type: 'plank_waao:void_beast',
        location: projectile.location
    })[0];

    if (!shooter || !shooter.isValid()) {
        projectile.remove();
        return;
    }

    const targetBlock = Server.getRelativePosition(shooter, { x: 32, y: 1, z: 0 }) // max targeting distance is 32

    /** @type {EntityProjectileComponent} */
    let projectileComp = projectile.getComponent('minecraft:projectile')
    projectileComp.owner = shooter;

    Server.shootProjectile(projectile, shooter, 1, { gravity: 0, facingOffset: { x: 4, y: -1, z: 2.5 }, targetLocation: targetBlock });
});