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

function replaceRandomNearby(player, map, chance = 0.05, r = 5) {
  if (Math.random() > chance) return;

  const d = player.dimension, p = player.location, list = [];

  for (let x = -r; x <= r; x++)
    for (let y = -r; y <= r; y++)
      for (let z = -r; z <= r; z++) {

        const bx = Math.floor(p.x) + x;
        const by = Math.floor(p.y) + y;
        const bz = Math.floor(p.z) + z;

        const block = d.getBlock({ x: bx, y: by, z: bz });

        if (block && map[block.typeId]) list.push(block);
      }

  if (!list.length) return;

  const block = list[Math.random() * list.length | 0];
  const newId = map[block.typeId];

  const loc = block.location;

  d.runCommand(`setblock ${loc.x} ${loc.y} ${loc.z} ${newId}`);
}

system.runInterval(() => {
  for (const player of world.getPlayers()) {
    replaceRandomNearby(player, {
      "minecraft:oak_log": "kick:scratched_oak",
      "minecraft:spruce_log": "kick:scratched_spruce",
      "minecraft:dark_oak_log": "kick:scratched_dark_oak",
      "minecraft:birch_log": "kick:scratched_birch",
      "minecraft:spruce_wood": "kick:scratched_spruce",
      "minecraft:pale_oak_log": "kick:scratched_pale"
    }, 0.6);
  }
}, 8000)