> ## Documentation Index
> Fetch the complete documentation index at: https://docs.selvut.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Inventory Hotbar

> Find and select hotbar items from a script command.

Save as `hotbar_tools.lua`.

```lua theme={null}
Script.meta({
  name = "Hotbar Tools",
  description = "Small inventory and hotbar commands."
})

local fallbackItem = Setting.text("Fallback Item", {
  default = "minecraft:stone"
})

local function endsWith(text, suffix)
  return string.sub(text, -#suffix) == suffix
end

local function matchesItem(item, itemId)
  if item == nil then return false end
  local id = item.getId()
  return id == itemId or id == "minecraft:" .. itemId or endsWith(id, ":" .. itemId)
end

local function findHotbar(itemId)
  for slot = 0, 8 do
    if matchesItem(Inventory.hotbarSlot(slot), itemId) then
      return slot
    end
  end
  return -1
end

Script.command("held", {
  description = "Print the currently held item"
}, function()
  local item = Player.heldItem()
  if item == nil then
    Chat.log("Your hand is empty")
    return
  end
  Chat.log(item.getName() .. " x" .. item.getCount() .. " (" .. item.getId() .. ")")
end)

Script.command("holditem", {
  description = "Select an item from the hotbar",
  usage = "holditem <item>",
  parameters = {
    { name = "item", type = "string", required = false }
  }
}, function(args, item)
  local itemId = item or fallbackItem.get()
  local slot = findHotbar(itemId)
  if slot < 0 then
    Chat.log("No " .. itemId .. " in hotbar")
    return
  end
  Player.switchSlot(slot)
  Chat.log("Selected hotbar slot " .. (slot + 1))
end)
```

Example commands:

```txt theme={null}
;held
;holditem emerald
;holditem minecraft:oak_planks
```
