import {system, world} from "@minecraft/server"

export function getClosestEntity(sourceEntity, identifier) {
    return sourceEntity.dimension.getEntities({
        type: identifier,
        closest: 1,
        location: sourceEntity.location,
        maxDistance: 6
    })[0];
}

system.runInterval(() => {
    const overworld = world.getDimension('overworld');
    const seatEntities = overworld.getEntities({
        families: ["raft"]
    });

    for (const seatEntity of seatEntities) {
        const angle = seatEntity.getRotation();

        const tigerSharkRider = getClosestEntity(seatEntity, "cubc_ra_su:tiger_shark");
        if (tigerSharkRider !== undefined && tigerSharkRider.hasTag("cubc_ra_su:is_riding")) {
            tigerSharkRider.setRotation({x: 0, y: angle.y});
        }
    }
}, 1);
