> For the complete documentation index, see [llms.txt](https://openjs.wiki.gd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openjs.wiki.gd/apis-and-tools/services/inventoryapi.md).

# InventoryApi

### Overview

<table data-header-hidden><thead><tr><th>Functions:</th></tr></thead><tbody><tr><td><pre class="language-javascript"><code class="lang-javascript">createItem(data) ⇒ ItemStack 
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">constructInventory(type, title) ⇒ InventoryUI 
</code></pre></td></tr><tr><td><p></p><pre class="language-javascript"><code class="lang-javascript">setItemLore(item, lore)
</code></pre></td></tr><tr><td><p></p><pre class="language-javascript"><code class="lang-javascript">setItemName(item, name)
</code></pre></td></tr></tbody></table>

### API Reference

#### `createItem()`

Creates an [ItemStack](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/ItemStack.html) from a JavaScript object.

```js
const item = inventoryApi.createItem({
    id: "minecraft:diamond",
    name: "§b§lExample Item",
    lore: [
        "§7This is an example",
        "§7Created via InventoryApi"
    ],
    amount: 1
})
```

**Parameters:**

* `data` *(Object)*\
  Item configuration object:

| Key      | Type                  | Description                         |
| -------- | --------------------- | ----------------------------------- |
| `id`     | `String`              | Minecraft item ID                   |
| `name`   | `String`              | Display name (supports color codes) |
| `lore`   | `String[]`            | Item lore                           |
| `amount` | `Number` *(optional)* | Item amount (default: `1`)          |

**Returns:**

* `ItemStack` ⇒ [`org.bukkit.inventory.ItemStack`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/ItemStack.html)

***

### `constructInventory()`

Creates an interactive inventory UI.

```js
const menu = inventoryApi.constructInventory("single", "§8Example Menu")
```

**Parameters:**

* `type` *(String)*
* `title` *(String)*\
  Inventory title (supports color codes)

**Returns:**

* `InventoryUI` ⇒ Managed inventory interface

***

### `setItemLore()`

Overwrites the item's lore with the new one.

```js
inventoryApi.setItemLore(item, ["§8Example lore"])
```

**Parameters:**

* `item` *(ItemStack)*
* `lore` *(String\[])*

***

### `setItemName()`

Overwrites the item's name with the new one.

```js
inventoryApi.setItemName(item, "New Item Name")
```

**Parameters:**

* `item` *(ItemStack)*
* `name` *(String)*

***

### InventoryUI API

The object returned by `constructInventory()` exposes the following methods.

***

### `setSlot()`

Sets an item in a specific slot.

```js
menu.setSlot(0, item)
```

**Parameters:**

* `slot` *(Number)*
* `item` *(ItemStack)*

{% hint style="info" %}
**`menu.setSlot(slot, null)`** to clear the slot
{% endhint %}

***

### `getSlot()`

Returns an item from a specific slot.

```js
menu.getSlot(0)
```

**Parameters:**

* `slot` *(Number)*

**Returns:**

* `item` *(ItemStack)* **or** `null`

***

### `setTitle()`

Set and update the inventory title.

```js
menu.setTitle("New Title")
```

**Parameters:**

* `title` *(String)*

***

### `copy()`

Creates a deep copy of the inventory UI.

```js
const playerMenu = menu.copy()
```

**Returns:**

* `InventoryUI`

***

### `show()`

Opens the inventory for a player.

```js
menu.show(player)
```

**Parameters:**

* `player` *(Player)*

***

### `hide()`

Closes the inventory for a player.

```js
menu.hide(player)
```

**Parameters:**

* `player` *(Player)*

***

### `destroy()`

Destroys the inventory and unregisters all listeners.

```js
menu.destroy()
```

{% hint style="warning" %}
Always call `destroy()` when the inventory is no longer needed.
{% endhint %}

***

### `onLeftClick()`

Registers a left-click handler.

```js
menu.onLeftClick(function(player, slot, event) {
    event.setCancelled(true)
})
```

**Returns:** *(function handler)*

* `player` ⇒ [`Player`](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/Player.html)
* `slot` ⇒ `Number`
* `event` ⇒ [`InventoryClickEvent`](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/inventory/InventoryClickEvent.html)

***

### `onRightClick()`

Registers a right-click handler.

```js
menu.onRightClick(function(player, slot, event) {
    event.setCancelled(true)
})
```

**Returns:** *(function handler)*

* `player` ⇒ [`Player`](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/Player.html)
* `slot` ⇒ `Number`
* `event` ⇒ [`InventoryClickEvent`](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/inventory/InventoryClickEvent.html)

***

### `onItemPlaced()`

Called when an item is placed into the inventory.

```js
menu.onItemPlaced(function(player, slot, event) {
    event.setCancelled(true)
})
```

**Returns:** *(function handler)*

* `player` ⇒ [`Player`](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/Player.html)
* `slot` ⇒ `Number`
* `event` ⇒ [`InventoryClickEvent`](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/inventory/InventoryClickEvent.html)

***

### `onClosed()`

Called when the inventory is closed.

```js
menu.onClosed(function(player, event) {
    menu.destroy()
})
```

**Returns:**

* `player` ⇒ [`Player`](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/Player.html)
* `event` ⇒ [`InventoryCloseEvent`](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/inventory/InventoryCloseEvent.html)

***

### Example:

{% code title="Example Menu with close button" %}

```js
const inventoryApi = Services.get("InventoryApi")
const template = inventoryApi.constructInventory("single", "§8Example Menu")
const closeItem = inventoryApi.createItem({
    id: "minecraft:barrier",
    name: "§cClose"
})

template.setSlot(8, closeItem)

addCommand("showMenu", {
    onCommand: function(player) {
        let menu = template.copy()

        menu.onLeftClick(function(_, slot, event) {
            event.setCancelled(true)
            if(slot === 8) {
                menu.hide(player)
                player.sendMessage("§cClosed custom Menu!")
            }
        })

        menu.onClosed(function() {
            menu.destroy()
        })

        menu.show(player)
    }
})
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openjs.wiki.gd/apis-and-tools/services/inventoryapi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
