Skip to main content

Item Guide

The Item class is used to handle all item-related operations on the server. Below, the main functions and usage of the Item class will be described.

Item class overview

Located in the cn.nukkit.item package.

Provides a large number of methods for creating and modifying items in the game, which can be blocks, tools, food, etc.

Constructors

The Item class provides several constructors that allow the creation of item instances from different parameters.

Example code:

// Import the Item class
import cn.nukkit.item.Item;

// Create the item by item ID and quantity
Item diamond = Item.get(Item.DIAMOND, 0, 1); // create 1 diamond
// Give the player a diamond
player.giveItem(diamond);
// Or
player.getInventory().addItem(diamond);

// Create items via namespaces (recommended!)
Item apple = Item.fromString("minecraft:apple");

Code explanation:

warning

Always use the Item.get() or Item.fromString() method to get an item, rather than instantiating the item object new ItemDiamond(id, meta, count)!

  • Item.DIAMOND: This is the item ID of the diamond, each item has a unique ID.
  • 0: This is the item's variant number, for most items this will be 0.
  • 1: This represents the number of the item.

Main Methods

Below are some commonly used methods and examples in the Item class.

getId()

Returns the ID of the item.

int id = diamond.getId(); // Get the ID of the diamond
// Output: 264

getNamespaceId()

Returns the namespace of the item.

String namespace = diamond.getNamespaceId(); // Get the namespace of the diamond
// Output: minecraft:diamond

getCount()

Returns the quantity of the item.

int count = diamond.getCount(); // Get the quantity of the diamond
// Output: 1

getName()

Returns the name of the item.

String name = diamond.getName(); // Get the name of the diamond
// Output: Diamond

getEnchantmentLevel(int id)

Get the enchantment level of the item. You can find relevant enchantment IDs in cn.nukkit.item.enchantment.Enchantment.

int level = diamond.getEnchantmentLevel(Enchantment.ID_DAMAGE_ALL); // Get the diamond's sharpness level. If it's not enchanted, return 0.
// Output: 0

Handling item properties

Attributes of items can be managed through various setter and getter methods.

Setting the number of items

diamond.setCount(3); // Set the number of diamonds to 3

Example Code

  1. Setting Basic Item Properties
Item diamond = Item.get(Item.DIAMOND, 0, 1);
// You can continue to add more lines
diamond.setLore("Line 1", "Line 2");
// If you do not want the text to be italicized, you can add "§r" at the beginning of the text to reset the format
diamond.setCustomName("Custom Name of the Item");
// Add Sharpness 5 and Protection 4 enchantments to the item
diamond.addEnchantment(Enchantment.get(Enchantment.ID_DAMAGE_ALL).setLevel(5), Enchantment.get(Enchantment.ID_PROTECTION_ALL).setLevel(4));
  1. Infinite Durability for Items
// Achieved through nbt
CompoundTag tag = diamond.hasCompoundTag() ? diamond.getNamedTag() : new CompoundTag();
tag.putByte("Unbreakable", 1);
// After modifying nbt, you need to setNamedTag for it to take effect
diamond.setNamedTag(tag);
  1. Item Lock Modes

Nukkit-MOT provides the Item.ItemLockMode class, and developers can use setItemLockMode(ItemLockMode lockMode) to set the lock mode of an item.

  • Item.ItemLockMode.LOCK_NONE

Remove item lock

  • Item.ItemLockMode.LOCK_IN_INVENTORY

lock in inventory the texture used over the item.

Prevents the item from being removed from the player's inventory, dropped, or crafted with.

  • Item.ItemLockMode.LOCK_IN_SLOT

lock in slot the texture used over the item.

Prevents the item from being moved or removed from its slot in the player's inventory, dropped, and by extension, crafted with.

note

Supported since 1.16.100 (protocol version 419)

diamond.setItemLockMode(Item.ItemLockMode.LOCK_IN_INVENTORY);