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

/** @type {import("@minecraft/server").BlockCustomComponent} */
export const FakeWaterBlockComponent = {
  onPlace({ block }) {
    const overworld = block.dimension
    const loc = block.location;
    overworld.spawnEntity(`sbs_tra:fake_water`, {x:loc.x+0.5, y:loc.y, z:loc.z+0.5});
    overworld.runCommandAsync(`setblock ${block.x} ${block.y} ${block.z} barrier`);
    overworld.runCommand(`setblock ${block.x} ${block.y} ${block.z} water`);
  }
}

export const QuicksandBlockComponent = {
  onTick(event) {
    const block = event.block
    event.dimension.runCommand(`execute positioned ${block.x} ${block.y} ${block.z} run effect @e[r=0.6,family=!trap] slowness 1 3 true`);
    event.dimension.runCommand(`execute positioned ${block.x} ${block.y} ${block.z} run effect @e[r=0.6,family=!trap] slow_falling 1 1 true`)
  },
  onPlayerDestroy(event) {
    const block = event.block
    event.dimension.runCommand(`execute positioned ${block.x} ${block.y} ${block.z} run playsound sbs_tra:smoke @a[r=16] ~ ~ ~ 1 1 1`);
    event.dimension.runCommand(`execute positioned ${block.x} ${block.y} ${block.z} run playsound dig.sand @a[r=16] ~ ~ ~ 1 1 1`);
    event.dimension.runCommand(`execute positioned ${block.x} ${block.y} ${block.z} run loot spawn ~ ~1 ~ loot "sbs/trap/quicksand"`)
    event.dimension.runCommand(`execute positioned ${block.x} ${block.y} ${block.z} run particle sbs_tra:despawn ~ ~ ~`)
  }
}

export const FakeOreBlockComponent = {
  onPlayerDestroy(event) {
    const block = event.block
    const player = event.player
    player.runCommand(`execute positioned ${block.x} ${block.y} ${block.z} run playsound sbs_tra:smoke @a[r=16] ~ ~ ~ 1 1 1`);
    player.runCommand(`execute positioned ${block.x} ${block.y} ${block.z} run playsound sbs_tra:fake_ore @a[r=16] ~ ~ ~ 1 1 1`);
    try {
      const equipment = player.getComponent('equippable');
      let item = equipment.getEquipment(EquipmentSlot.Mainhand);
      item.getComponent("durability").damage += 5;
      equipment.setEquipment(EquipmentSlot.Mainhand, item);
    } catch { }
  }
}