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

world.beforeEvents.itemUse.subscribe((data) => {
  let player = data.source;
  if (data.itemStack.typeId == "txd_pirate1:craft_book01")
    system.run(() => craft_book01(player));

  function craft_book01() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    player.runCommand("replaceitem entity @s slot.weapon.mainhand 0 txd_pirate1:craft_book02");
  }
});

world.beforeEvents.itemUse.subscribe((data) => {
  let player = data.source;
  if (data.itemStack.typeId == "txd_pirate1:craft_book02")
    system.run(() => craft_book02(player));

  function craft_book02() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    player.runCommand("replaceitem entity @s slot.weapon.mainhand 0 txd_pirate1:craft_book03");
  }
});

world.beforeEvents.itemUse.subscribe((data) => {
  let player = data.source;
  if (data.itemStack.typeId == "txd_pirate1:craft_book03")
    system.run(() => craft_book03(player));

  function craft_book03() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    player.runCommand("replaceitem entity @s slot.weapon.mainhand 0 txd_pirate1:craft_book04");
  }
});

world.beforeEvents.itemUse.subscribe((data) => {
  let player = data.source;
  if (data.itemStack.typeId == "txd_pirate1:craft_book04")
    system.run(() => craft_book04(player));

  function craft_book04() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    player.runCommand("replaceitem entity @s slot.weapon.mainhand 0 txd_pirate1:craft_book05");
  }
});

world.beforeEvents.itemUse.subscribe((data) => {
  let player = data.source;
  if (data.itemStack.typeId == "txd_pirate1:craft_book05")
    system.run(() => craft_book05(player));

  function craft_book05() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    player.runCommand("replaceitem entity @s slot.weapon.mainhand 0 txd_pirate1:craft_book06");
  }
});

world.beforeEvents.itemUse.subscribe((data) => {
  let player = data.source;
  if (data.itemStack.typeId == "txd_pirate1:craft_book06")
    system.run(() => craft_book06(player));

  function craft_book06() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    player.runCommand("replaceitem entity @s slot.weapon.mainhand 0 txd_pirate1:craft_book01");
  }
});
