import { world, EquipmentSlot } from "@minecraft/server";
/* 
Checks which item is in the mainhand
Adds effect if a specific item like a sword/spear is in the mainhand
*/

export function guideBook(player) {
  const typeId = player.typeId;
  if (typeId !== "minecraft:player") return;

  const mainHand = player.getComponent("equippable")?.getEquipment(EquipmentSlot.Mainhand);
  if (!mainHand) return;

  switch (mainHand.typeId) {
    case "hb_cb:guide_book_spawn_egg":
      let nearbyBackpacks = world.getDimension(player.dimension.id).getEntities({
        families: ["hb_cb_backpack"],
        location: player.location,
        tags: ["hb_cb:show_camp_box_hint"],
        minDistance: 0,
        maxDistance: 15,
      });
      if (nearbyBackpacks.length == 0) {
        player.runCommand('/titleraw @s actionbar {"rawtext":[{"translate":"hb_cb.guide_book.actionbar_hint"}]}');
      }
      break;
    default:
      break;
  }
}
