Minestom wiki
Search…
Items

Overview

Items in Minestom are immutables, meaning that an ItemStack cannot change after being built. This provides us many benefits:
  • Thread safety, as you cannot change the same object from multiple threads.
  • No side effects where a change to an item would modify all inventories where the same object is present.
  • Reuse ItemStack in multiple places. For example, if all your players start with the same set of items, you could just store those as constants and add them to each player inventory to avoid a lot of allocation.
  • Related to the second point, it allows us to internally cache items packet (eg: window packet) to keep improving performance

API

1
// Constant air item, should be used instead of 'null
2
ItemStack air = ItemStack.AIR;
3
// Item with amount sets to 1
4
ItemStack stone = ItemStack.of(Material.STONE);
5
// Item with custom amount
6
ItemStack stoneStack = ItemStack.of(Material.STONE, 64);
Copied!
However, as items are immutable, creating complex objects require using a builder:
1
ItemStack item = ItemStack.builder(Material.STONE)
2
.displayName(Component.text("Display name!", NamedTextColor.GREEN))
3
.lore(Component.text("Line 1"), Component.text("Line 2"))
4
.meta(metaBuilder ->
5
metaBuilder.enchantment(Enchantment.EFFICIENCY, (short) 10)
6
.hideFlag(ItemHideFlag.HIDE_ENCHANTS))
7
.build();
Copied!
And some special methods have been made to modify an existing item easily:
1
// Set the amount to 5
2
item = item.withAmount(5);
3
// Set the amount based on the current one
4
item = item.withAmount(amount -> amount * 2);
5
// Same with various other fields
6
item = item.withDisplayName(Component.text("New display name!"));
7
8
// Start rebuilding the item
9
// More performant than the above if you need to modify multiple fields
10
item = item.with(builder -> {
11
builder.amount(32)
12
.displayName(Component.text("Again..."));
13
});
Copied!
Last modified 7mo ago
Copy link
Contents
Overview
API