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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:heated_netherrack_active_random_inactive", {
    onRandomTick: eventData => {
      const block = eventData.block;
      if (block.permutation.matches("nicothekid:heated_netherrack_active")) {
        block.setPermutation(BlockPermutation.resolve("nicothekid:heated_netherrack"))
        return;
      }
      else return;
    }
  });
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:heated_netherrack_active_on_step_on", {
    onStepOn: eventData => {
      const entity = eventData.entity;
      const block = eventData.block;
      const blockLocation = block.location;
      if (entity === undefined || entity.isSneaking || entity.typeId === "minecraft:item") return;
      if (entity.matches({ excludeFamilies:[ "blaze", "magmacube", "strider", "zoglin", "zombie_pigman", "fire_bird", "soul_fire_bird", "magma_golem", "nether_wisp", "nether_soul_wisp" ] })) {
        if (block.permutation.matches("nicothekid:heated_netherrack_active", { "nicothekid:heated_netherrack_active_lighten": false })) {
          block.dimension.playSound("random.fizz", blockLocation, { pitch: 1.1, volume: 1.0 });
          block.dimension.spawnParticle("nicothekid:heated_netherrack_active_particle", { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
          entity.applyDamage(1, { cause: EntityDamageCause.magma });
          block.setPermutation(BlockPermutation.resolve("nicothekid:heated_netherrack_active", { "nicothekid:heated_netherrack_active_lighten": true }));
          return;
        }
      }
      else return;
    }
  });
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:heated_netherrack_active_exploding", {
    onTick: eventData => {
      const block = eventData.block;
      const blockLocation = block.location;
      if (block.permutation.matches("nicothekid:heated_netherrack_active", { "nicothekid:heated_netherrack_active_lighten": true })) {
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
        block.dimension.spawnEntity("nicothekid:projectile_heated_netherrack_active_explosion", { x: blockLocation.x + 0.5, y: blockLocation.y, z: blockLocation.z + 0.5 });
        return;
      }
      else return;
    }
  });
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:heated_netherrack_active_on_step_off", {
    onStepOff: eventData => {
      const block = eventData.block;
      if (block.permutation.matches("nicothekid:heated_netherrack_active", { "nicothekid:heated_netherrack_active_lighten": true })) {
        block.setPermutation(BlockPermutation.resolve("nicothekid:heated_netherrack_active", { "nicothekid:heated_netherrack_active_lighten": false }));
        return;
      }
      else return;
    }
  });
})
