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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  const glowceliumSprouts = [
    "nicothekid:glowcelium_sprouts_tall_block"
  ]
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:glowcelium_sprouts_particles", {
    onRandomTick: eventData => {
      const block = eventData.block;
      if (glowceliumSprouts.includes(block.typeId)) {
        if (block.permutation.matches("nicothekid:glowcelium_sprouts_tall_block", { "nicothekid:sprouts_particles": false })) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:glowcelium_sprouts_tall_block", { "nicothekid:sprouts_particles": true }))
          return;
        }
      }
      else return;
    }
  });
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:glowcelium_sprouts_particles_tick", {
    onTick: eventData => {
      const block = eventData.block;
      const blockLocation = block.location;
      if (glowceliumSprouts.includes(block.typeId)) {
        if (block.permutation.matches("nicothekid:glowcelium_sprouts_tall_block", { "nicothekid:sprouts_particles": true })) {
          block.dimension.spawnParticle("nicothekid:glowcelium_sprouts_particle", { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 })
          return;
        }
      }
      else return;
    }
  });
});
