import { system, world, EquipmentSlot } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";

world.beforeEvents.playerInteractWithBlock.subscribe((ass) => {

    system.run(() => {

        const block = ass.block;
        const location = block.center();
        const player = ass.player;
        const dimension = player.dimension;
        const blockType = block.typeId;
        const state = "stupidstuff:door";
        const value = block.permutation.getState(state);
        const permutation = !value;
        const hand = player.getComponent("minecraft:equippable");
        const empty = hand.getEquipmentSlot(EquipmentSlot.Mainhand);
        const noItem = empty.hasItem();
        if (blockType === "stupidstuff:microwave" && !noItem) {
            block.setPermutation(block.permutation.withState(state, permutation));
            dimension.playSound("microwave", location)
            system.runTimeout(() => {
                block.setPermutation(block.permutation.withState(state, !permutation));
            }, 80);
            system.runTimeout(() => {
                if (dimension.getBlock(location).typeId === "stupidstuff:microwave") {
                    dimension.createExplosion(location, 2, { causesFire: true });
                };
            }, 420);
        };
    });
});