import { system, world, Player } from '@minecraft/server';


export const entity_list = {
    "ph_furn:vehicles.car": {
    },
    "ph_furn:vehicles.old_truck": {
    },
    "ph_furn:vehicles.sports_coupe": {
    },
};

world.afterEvents.dataDrivenEntityTrigger.subscribe((xm) => {
    const event = xm.eventId;
    const entity = xm.entity;
    const entity_id = entity_list[entity?.typeId];
    if (!entity_id) return;


    let elapsed = 0;
    const entity_dimension = entity.dimension;
    const owner = entity;

    if (event == "add:moving") {
        const interval = system.runInterval(() => {
            const moving = entity.getComponent("is_saddled");
            if (!moving) {
                system.clearRun(interval);
                return;
            }

            try {
                const distance = entity_dimension.getEntities({ location: entity.location, maxDistance: 2 });
                for (const entity of distance) {
                    if (entity != owner) {
                        entity.applyKnockback(Math.floor(Math.random() * 7) - 3, Math.floor(Math.random() * 7) - 3, 3, 0.5);
                    }
                }
            } catch { }

        }, 2);
    }
    if (event == "remove:moving") {
        elapsed = 1;
    }
});



const overworld = world.getDimension("overworld");
const nether = world.getDimension("nether");
const the_end = world.getDimension("the_end");

system.runInterval(() => {
    const entities = [...overworld.getEntities(), ...nether.getEntities(), ...the_end.getEntities()];
    entities.forEach((entity) => {
        const getrider = entity.getComponent("rideable")?.getRiders().find((e) => (e instanceof Player));
        if (getrider instanceof Player) {
            if (getrider.isJumping) {
                try {
                    entity.triggerEvent("add:event");
                }
                catch{
                }
            }
        }
    });
});