import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { Achievement } from "./achievement";
import { NPCBase } from "../npc/index";
export class PirateAchievement extends Achievement {
    constructor() {
        super(...arguments);
        this.SkinIndex = 13;
    }
    onSetup() {
        mc.world.afterEvents.dataDrivenEntityTrigger.subscribe(this.onRiding.bind(this), { entityTypes: ["ldz_crcomp:craftee"], eventTypes: ["set_is_riding_true"] });
    }
    async onRiding(event) {
        const craftee = NPCBase.getNPC(event.entity.id);
        if (craftee) {
            let rideable;
            const boat = utils.getEntitiesSafe(craftee.Entity.dimension, { location: craftee.Entity.location, closest: 5 }).find(entity => {
                rideable = entity.getComponent("minecraft:rideable");
                const riders = rideable === null || rideable === void 0 ? void 0 : rideable.getRiders().map(rider => rider.id);
                return riders && riders.includes(craftee.ID);
            });
            if (boat)
                utils.mclog(`Craftee in boat, awaiting owner`);
            while ((boat === null || boat === void 0 ? void 0 : boat.isValid()) && rideable && (rideable === null || rideable === void 0 ? void 0 : rideable.getRiders().map(rider => rider.id).includes(craftee.ID))) {
                if (rideable.getRiders().map(rider => rider.id).includes(craftee.Owner.id)) {
                    this.setStatus(craftee.Owner, true);
                    return;
                }
                await utils.waitForTicks(10);
            }
        }
    }
}
new PirateAchievement();
