import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
import { WornHat } from "../worn_hat";
class PirateHat extends WornHat {
    constructor() {
        super(...arguments);
        this.hatTypeId = "ldz_crcomp:hat_pirate";
        this.onBreakEvent = this.onBreak.bind(this);
        this.onSpawnEvent = this.onSpawn.bind(this);
        this.validOres = [
            "minecraft:coal",
            "minecraft:raw_copper",
            "minecraft:raw_gold",
            "minecraft:raw_iron",
            "minecraft:lapis_lazuli",
            "minecraft:redstone",
            "minecraft:emerald",
            "minecraft:diamond",
        ];
    }
    async onEquip() {
        mc.world.afterEvents.playerBreakBlock.subscribe(this.onBreakEvent);
    }
    async onUnequip() {
        mc.world.afterEvents.playerBreakBlock.unsubscribe(this.onBreakEvent);
    }
    onBreak(event) {
        var _a;
        if (String((_a = event.brokenBlockPermutation.getItemStack()) === null || _a === void 0 ? void 0 : _a.typeId).includes("ore") && event.player.id === this.hatWearer.Entity.id) {
            mc.world.afterEvents.entitySpawn.subscribe(this.onSpawnEvent);
            utils.waitForEvent(2, () => mc.world.afterEvents.entitySpawn.unsubscribe(this.onSpawnEvent));
        }
    }
    onSpawn(event) {
        if (event.entity.typeId === "minecraft:item") {
            const item = new utils.SafetyCheckedEntity(event.entity).getComponent("item");
            if (item && this.validOres.includes(item.itemStack.typeId)) {
                utils.randomChanceEvent(3, () => {
                    event.entity.dimension.spawnItem(item.itemStack, event.entity.location);
                });
                mc.world.afterEvents.entitySpawn.unsubscribe(this.onSpawnEvent);
            }
        }
    }
}
WornHat.HatTable["ldz_crcomp:hat_pirate"] = PirateHat;
