Creating Custom Items

Learn how to create custom items from YAML configuration using the ItemBuilder class with support for placeholders, mathematical expressions, display conditions, custom commands, and NBT data.

The ItemBuilder class provides powerful methods for creating custom items from YAML configuration files with support for placeholders, mathematical expressions, display conditions, custom commands, and more.

Table of Contents

Basic Usage

Creating a Simple Item

import me.yleoft.zAPI.item.ItemBuilder;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class Example {
    public ItemStack createCustomItem(Player player, YamlConfiguration config) {
        // Create item from config at path "items.my_item"
        return ItemBuilder.createItem(player, config, "items.my_item");
    }
}

With Custom Placeholders

Creating Items from Config

Method Signatures

Configuration Path

The path parameter points to the item's location in the config:

Item Properties

Configuration Keys

All item properties are defined using standard keys:

Key
Type
Description

material

String/List

Item material

amount

Integer

Stack size

name

String

Display name

lore

List

Lore lines

enchantments

List

Enchantments

unbreakable

Boolean

Unbreakable flag

itemflags

List

Item flags

pickable

Boolean

Can be picked up

commands

List

Click commands

display-condition

String

Show condition

placeholders

Section

Custom placeholders

Material Types

Standard Materials

Random Selection

Player Heads

By Player Name:

By Base64 Texture:

By URL:

Auto-detect:

Display Name and Lore

Names and lore support MiniMessage formatting and placeholders:

Enchantments

Format: ENCHANTMENT_NAME:LEVEL

Placeholders work in enchantment levels:

Item Flags

Commands

Commands execute when the item is clicked in an inventory:

Command prefixes:

  • [CON] - Execute as console

  • [INV] - Inventory action (close)

  • [ITEM] - Item action (give)

  • [chance] - Random execution (e.g., [75] for 75%)

Pickable Property

Controls whether players can pick up the item from inventories:

When false, the item is marked with NBT data preventing pickup.

Placeholder System

Built-in Placeholders

Placeholder
Description
Example

%player%

Player name

Steve

%online%

Online players

42

%uuid%

Player UUID

069a79f4...

%world%

Current world

world

%slot%

Slot number

5

%currentitem%

Item index

1

Creating Slot Placeholders

Custom Placeholders in Config

Define custom placeholders that are parsed once and reused:

Placeholder Processing Order

Placeholders are processed in this specific order:

  1. Custom placeholders (from placeholders: section)

  2. Mathematical expressions ({math: ...})

  3. PlaceholderAPI placeholders

This allows you to build on previous placeholders:

Merging Placeholder Maps

Mathematical Expressions

Mathematical expressions are wrapped in {math: ...} tags and evaluated during placeholder processing.

Basic Operations

With Placeholders

Supported Functions

Function
Description
Example

sqrt(x)

Square root

{math: sqrt(16)} → 4

round(x)

Round to nearest

{math: round(3.7)} → 4

roundDown(x)

Floor function

{math: roundDown(3.7)} → 3

Complex Expressions

Format of Results

  • Whole numbers: No decimal point (e.g., 5 not 5.0)

  • Decimals: Up to 2 decimal places, trailing zeros removed (e.g., 3.14 not 3.1400)

Display Conditions

Display conditions determine whether an item should be created/shown based on a boolean expression.

Evaluating Conditions

Condition Syntax

Comparison Operators

Operator
Description
Example

==

Equal to

%level%==10

!=

Not equal

%world%!=nether

>

Greater than

%balance%>1000

<

Less than

%homes%<5

>=

Greater or equal

%level%>=20

<=

Less or equal

%health%<=10

Basic Conditions

With Math Expressions

In Configuration

Advanced Features

Parsing Placeholder Definitions

Parse custom placeholders from a config section:

Replacing Placeholders in Existing Items

Modify an existing ItemStack's name and lore:

Creating Items from Material Strings

Directly create items from material strings (including heads):

Parsing Material Names

Parse material names to Material enum:

Complete Examples

Example 1: Dynamic Weapon

Example 2: Multi-Slot Home Items

Example 3: Conditional Shop Item

Example 4: Skill Tree Item

Best Practices

  1. Always provide player context when possible for proper placeholder resolution

  2. Use display conditions to hide items that shouldn't be shown

  3. Set pickable: false for GUI items you don't want players to take

  4. Validate math expressions before using in production

  5. Cache computed values when creating multiple similar items

  6. Use custom placeholders to avoid repeating complex expressions

  7. Test with null players if items need to work without player context

  8. Leverage PlaceholderAPI for dynamic external data

  9. Format numbers appropriately using math functions like round()

  10. Use meaningful placeholder names for maintainability

Performance Tips

  1. Parse placeholders once and reuse for multiple items

  2. Avoid redundant item creation - cache when possible

  3. Use batch operations when creating many items

  4. Pre-compute static values before loops

  5. Minimize PlaceholderAPI calls by caching results

Troubleshooting

Item Not Created

  • Check if display condition evaluates to false

  • Verify material name is valid

  • Ensure config path exists

Placeholders Not Replacing

  • Verify placeholder spelling

  • Check if PlaceholderAPI is installed (for PAPI placeholders)

  • Ensure custom placeholders are defined in config

Math Not Evaluating

  • Confirm expression is wrapped in {math: ...}

  • Check for syntax errors

  • Verify all placeholders return valid numbers

Commands Not Executing

  • Test command manually first

  • Check command prefix syntax

  • Verify player permissions

See Also

Last updated

Was this helpful?