import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { Achievement } from "./achievement";
export class KnightAchievement extends Achievement {
    constructor() {
        super(...arguments);
        this.SkinIndex = 9;
        this.total = 6;
    }
    onSetup() {
        mc.world.afterEvents.entityDie.subscribe(this.killMonster.bind(this));
    }
    killMonster(event) {
        var _a;
        if (event.damageSource.damagingEntity instanceof mc.Player) {
            const player = new utils.SafetyCheckedPlayer(event.damageSource.damagingEntity);
            const progress = this.getProgress(player);
            const item = (_a = player.Equipment) === null || _a === void 0 ? void 0 : _a.getEquipment(mc.EquipmentSlot.Mainhand);
            if (item && !progress.swords.includes(item.typeId) && item.typeId.includes("sword")) {
                progress.swords.push(item.typeId);
                utils.mclog(`Used ${progress.swords.map(sword => sword.replace("minecraft:", "").replace("_sword", ""))} to kill monsters`);
                if (progress.swords.length >= this.total) {
                    this.setStatus(player, true);
                }
                this.setProgress(player, progress);
            }
        }
    }
    getProgress(player) {
        var _a;
        return (_a = utils.readDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievement.knight")) !== null && _a !== void 0 ? _a : { swords: [] };
    }
    setProgress(player, progress) {
        utils.writeDynamicProperty(player, utils.GlobalNamespace.Namespace + "achievement.knight", progress);
    }
    onReset(player) {
        super.onReset(player);
        this.setProgress(player, { swords: [] });
    }
}
new KnightAchievement();
