import { world } from '@minecraft/server';

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('pa:brote', {
    onRandomTick: e => {
      const x = e.block.x;
      const y = e.block.y;
      const z = e.block.z;
      e.dimension.runCommand(`structure load squareDarkOak ${x} ${y} ${z}`);
    },
    onPlayerInteract: e => {
      if (!e.player) return;
      const equippable = e.player.getComponent("minecraft:equippable");
      if (!equippable) return;
      const mainhand = equippable.getEquipmentSlot("Mainhand");
      if (!mainhand.hasItem() || mainhand.typeId !== "minecraft:bone_meal") return;
      mainhand.amount > 1 ? mainhand.amount-- : mainhand.setItem(undefined);
      const x = e.block.x;
      const y = e.block.y;
      const z = e.block.z;
      e.dimension.runCommand(`structure load squareDarkOak ${x} ${y} ${z}`);
      const effectLocation = e.block.center();
      e.dimension.playSound("item.bone_meal.use", effectLocation);
      e.dimension.spawnParticle("minecraft:crop_growth_emitter", effectLocation);
    },
  });
});
