Creating Custom Inventories

Learn how to create custom inventory menus programmatically using the InventoryBuilder class with support for placeholders, mathematical expressions, and automatic command registration.

zAPI provides a powerful InventoryBuilder class that allows you to create custom inventories programmatically from YAML configuration files. This system supports dynamic placeholders, mathematical expressions, display conditions, and automatic command registration.

Table of Contents

Basic Usage

Creating a Simple Inventory

import me.yleoft.zAPI.inventory.InventoryBuilder;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

public class Example {
    public void openSimpleMenu(Player player) {
        // Create an inventory with a title and 3 rows
        Component title = TextFormatter.transform("<gold><bold>My Custom Menu");
        InventoryBuilder builder = new InventoryBuilder(title, 3);
        
        // Add items (we'll cover this in detail later)
        ItemStack item = new ItemStack(Material.DIAMOND);
        builder.setItem(0, item);
        
        // Build and open the inventory
        Inventory inventory = builder.build();
        player.openInventory(inventory);
    }
}

Creating from YAML Configuration

Constructor Options

The InventoryBuilder class offers several constructors:

1. Manual Constructor

  • title: Component - The inventory title

  • rows: int - Number of rows (1-6)

2. Config Constructor (Without Player)

  • config: YamlConfiguration - The configuration file

3. Config Constructor (With Player)

  • player: OfflinePlayer - Player for placeholder resolution

  • config: YamlConfiguration - The configuration file

4. Config Constructor (With Custom Placeholders)

  • player: OfflinePlayer - Player for placeholder resolution

  • config: YamlConfiguration - The configuration file

  • globalPlaceholders: Map<String, String> - Custom placeholders applied to ALL items

Available Placeholders

Inventory-Level Placeholders

These placeholders are automatically available to all items in the inventory:

Placeholder
Description
Example Value

%rows%

Number of rows in the inventory

3

%title%

Raw title string from config

My Menu

%command%

Command to open this inventory

openmenu

Slot-Level Placeholders

These placeholders are automatically available when items are placed in slots:

Placeholder
Description
Example Value

%slot%

The slot number (0-based)

5

%currentitem%

Item index for multi-slot items (1-based)

1

%player%

Player name

Steve

%online%

Number of online players

42

%uuid%

Player UUID

069a79f4-44e9-4726-a5be-fca90e38aaf5

%world%

Player's current world

world

PlaceholderAPI Support

All PlaceholderAPI placeholders are automatically supported when PlaceholderAPI is installed:

Custom Placeholders

You can define custom placeholders in your config that will be parsed and applied to all relevant fields. See the Inventory Configuration Guide for details.

Creating Inventories from Config

Automatic Command Registration

If your config includes a command field, zAPI will automatically register a command that opens the inventory:

The command is registered with:

  • Name from the command field

  • Permission handling if specified

  • Player-only restriction

  • Automatic inventory opening

Example Config with Command

When a player types /homes, the inventory will open automatically.

Inventory Properties

Getting and Setting Title

Getting Inventory Information

Item Management

Adding Items

Removing Items

Checking Items

Advanced Features

Using Global Placeholders

Global placeholders are applied to the entire inventory, including title and all items:

These placeholders are particularly useful for:

  • Pagination systems

  • Player profile viewers

  • Dynamic content based on context

Mathematical Expressions in Placeholders

You can use mathematical expressions in your placeholders using the {math: ...} syntax:

Supported operations:

  • Basic: +, -, *, /

  • Functions: sqrt(), round(), roundDown()

Copying an Inventory

Building the Final Inventory

Complete Example

Here's a complete example showing various features:

Best Practices

  1. Use player context when available - Always pass the player to constructors for proper placeholder resolution

  2. Validate row count - Ensure rows are between 1 and 6

  3. Handle null items - Always check if getItem() returns null

  4. Use global placeholders for shared data - When multiple items need the same placeholder values

  5. Separate logic from presentation - Keep config files focused on layout, handle dynamic content in code

  6. Cache built inventories when possible - If the inventory doesn't change, build once and reuse

See Also

Last updated

Was this helpful?