import { world, GameMode } from "@minecraft/server";

/** @type {import("@minecraft/server").BlockCustomComponent} */

const SubwayStationBlock = {
  onTick(event) {
    //暂时取消这个特殊的方块刷怪，因为刷结构的机制改变了
    const { x, y, z } = event.block.location;
    const mobType = "pm_m8:spider";
    const entityList2 = event.dimension.getEntities({
      location: { x, y, z },
      minDistance: 0,
      maxDistance: 64,
      type: mobType,
    });
    // console.warn(`subway_station spawn at ${x}, ${y}, ${z}`);
    if (entityList2.length > 0) {
    //   console.warn(`${mobType} already exists at ${x}, ${y}, ${z}`);
    } else {
    //     event.dimension.runCommand(
    //         "structure load pm_m8:subway_station " + x +
    //           " " +
    //           y +
    //           " " +
    //           z +
    //           " 0_degrees none true false "
    //       );
    //   event.dimension.runCommand("setblock " + x + " " + y + " " + z + " air");
    //   console.warn(`Summoned ${mobType} at ${x}, ${y}, ${z}`);
    }
    event.dimension.runCommand(`setblock ${x} ${y} ${z} air`);
  },
};

const ResearchLaboratoryBlock = {
    onTick(event) {
        // event.dimension.runCommand("say 1")
        const {x, y, z} = event.block.location;
        // event.dimension.runCommand("structure load pm_m8:research_laboratory " + x + " " + (y) + " " + z + " 0_degrees none true false ");
        event.dimension.runCommand("setblock " + x + " " + y + " " + z + " air");
        // console.warn(`research_laboratory spawn at ${x}, ${y}, ${z}`);
    }
};

const OutpostBlock = {
    onTick(event) {
        // event.dimension.runCommand("say 1")
        const {x, y, z} = event.block.location;
        // event.dimension.runCommand("structure load pm_m8:outpost " + x + " " + (y) + " " + z + " 0_degrees none true false ");
        event.dimension.runCommand("setblock " + x + " " + y + " " + z + " air");
        // console.warn(`outpost spawn at ${x}, ${y}, ${z}`);
    }
};

const MonsterNestRuinsBlock = {
    onTick(event) {
        // event.dimension.runCommand("say 1")
        const {x, y, z} = event.block.location;
        // event.dimension.runCommand("structure load pm_m8:monster_nest_ruins " + x + " " + (y) + " " + z + " 0_degrees none true false ");
        event.dimension.runCommand("setblock " + x + " " + y + " " + z + " air");
        // console.warn(`monster_nest_ruins spawn at ${x}, ${y}, ${z}`);
    }
};

const MonsterCaveBlock = {
    onTick(event) {
        // event.dimension.runCommand("say 1")
        const {x, y, z} = event.block.location;
        // event.dimension.runCommand("structure load pm_m8:monster_cave " + x + " " + (y) + " " + z + " 0_degrees none true false ");
        event.dimension.runCommand("setblock " + x + " " + y + " " + z + " air");
        // console.warn(`monster_cave spawn at ${x}, ${y}, ${z}`);
    }
};

const AbandonedFactoryBlock = {
    onTick(event) {
        // event.dimension.runCommand("say 1")
        const {x, y, z} = event.block.location;
        // event.dimension.runCommand("structure load pm_m8:abandoned_factory " + x + " " + (y) + " " + z + " 0_degrees none true false ");
        event.dimension.runCommand("setblock " + x + " " + y + " " + z + " air");
        // console.warn(`abandoned_factory spawn at ${x}, ${y}, ${z}`);
    }
};

const MedicalCenterBlock = {
    onTick(event) {
        // event.dimension.runCommand("say 1")
        const {x, y, z} = event.block.location;
        // event.dimension.runCommand("structure load pm_m8:medical_center " + x + " " + (y) + " " + z + " 0_degrees none true false ");
        event.dimension.runCommand("setblock " + x + " " + y + " " + z + " air");
        // console.warn(`medical_center spawn at ${x}, ${y}, ${z}`);
    }
}

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    blockComponentRegistry.registerCustomComponent( "pm_m8:subway_station_block", SubwayStationBlock );
    blockComponentRegistry.registerCustomComponent( "pm_m8:research_laboratory_block", ResearchLaboratoryBlock );
    blockComponentRegistry.registerCustomComponent( "pm_m8:outpost_block", OutpostBlock );
    blockComponentRegistry.registerCustomComponent( "pm_m8:monster_nest_ruins_block", MonsterNestRuinsBlock );
    blockComponentRegistry.registerCustomComponent( "pm_m8:monster_cave_block", MonsterCaveBlock );
    blockComponentRegistry.registerCustomComponent( "pm_m8:abandoned_factory_block", AbandonedFactoryBlock );
    blockComponentRegistry.registerCustomComponent( "pm_m8:medical_center_block", MedicalCenterBlock );
});