package net.minestom.server.item; import net.kyori.adventure.text.Component; import net.minestom.server.item.rule.VanillaStackingRule; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.UUID; import java.util.function.Consumer; import java.util.function.IntUnaryOperator; import java.util.function.UnaryOperator; /** * Represents an immutable item to be placed inside {@link net.minestom.server.inventory.PlayerInventory}, * {@link net.minestom.server.inventory.Inventory} or even on the ground {@link net.minestom.server.entity.ItemEntity}. *
* An item stack cannot be null, {@link ItemStack#AIR} should be used instead.
*/
public class ItemStack {
/**
* Constant AIR item. Should be used instead of 'null'.
*/
public static final @NotNull ItemStack AIR = ItemStack.of(Material.AIR);
private final UUID uuid = UUID.randomUUID();
private final StackingRule stackingRule = new VanillaStackingRule(64);
private final Material material;
private final int amount;
private final ItemMeta meta;
protected ItemStack(@NotNull Material material, int amount, @NotNull ItemMeta meta) {
this.material = material;
this.amount = amount;
this.meta = meta;
}
@Contract(value = "_ -> new", pure = true)
public static @NotNull ItemBuilder builder(@NotNull Material material) {
return new ItemBuilder(material);
}
@Contract(value = "_ ,_ -> new", pure = true)
public static @NotNull ItemStack of(@NotNull Material material, int amount) {
return builder(material).amount(amount).build();
}
@Contract(value = "_ -> new", pure = true)
public static @NotNull ItemStack of(@NotNull Material material) {
return of(material, 1);
}
@Contract(pure = true)
public @NotNull UUID getUuid() {
return uuid;
}
@Contract(pure = true)
public @NotNull Material getMaterial() {
return material;
}
@Contract(value = "_, -> new", pure = true)
public @NotNull ItemStack with(@NotNull Consumer<@NotNull ItemBuilder> builderConsumer) {
var builder = builder();
builderConsumer.accept(builder);
return builder.build();
}
@Contract(pure = true)
public int getAmount() {
return amount;
}
@Contract(value = "_, -> new", pure = true)
public @NotNull ItemStack withAmount(int amount) {
return builder().amount(amount).build();
}
@Contract(value = "_, -> new", pure = true)
public @NotNull ItemStack withAmount(@NotNull IntUnaryOperator intUnaryOperator) {
return withAmount(intUnaryOperator.applyAsInt(amount));
}
@Contract(value = "_, _ -> new", pure = true)
public