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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:mosshroom_light_on_step_on", {
    onStepOn: eventData => {
      const block = eventData.block;
      const blockLocation = block.location;
      if (block.permutation.matches("nicothekid:mosshroom_light", { "nicothekid:mosshroom_light_lighten": false })) {
        block.dimension.playSound("dig.shroomlight", blockLocation, { pitch: 1.1, volume: 1.0 });
        block.dimension.spawnParticle("nicothekid:mosshroom_light_particle", { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
        block.setPermutation(BlockPermutation.resolve("nicothekid:mosshroom_light", { "nicothekid:mosshroom_light_lighten": true }));
        return;
      }
      else return;
    }
  });
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:mosshroom_light_on_step_off", {
    onStepOff: eventData => {
      const block = eventData.block;
      if (block.permutation.matches("nicothekid:mosshroom_light", { "nicothekid:mosshroom_light_lighten": true })) {
        block.setPermutation(BlockPermutation.resolve("nicothekid:mosshroom_light", { "nicothekid:mosshroom_light_lighten": false }));
        return;
      }
      else return;
    }
  });
});
