-
Notifications
You must be signed in to change notification settings - Fork 9
Items
Mariusz Matyszczak edited this page Mar 5, 2023
·
1 revision
With its simple and intuitive interface, the Item class allows you to easily specify the properties of your item, including its name, lore, durability, and enchantments. Whether you're creating a new weapon, tool, or decorative item, the Item class provides a solid foundation that you can build upon to create your perfect item.
Basic usage of the Item class.
// one liner Item pumpkinPie = Item.of(Material.PUMPKIN_PIE, 1, "Pumpkin Pie"); // builder Item builderPie = Item.of(Material.PUMPKIN_PIE) .amount(1) .name("Pumpkin Pie");
Of course, everything supports colors, including HEX support.
// glowing diamond with colorful descriptions Item diamond = Item.of(Material.DIAMOND) .amount(1) // not necessary, by default it's always 1 .name("&bGlowing Diamond") .enchant(Enchantment.DURABILITY, 1) // for the glow .hideItemFlags(true) // to hide the enchantment .lore(List.of( "{#00bbff}This is a glowing diamond!", "�bbffIt's very shiny!", "&b&lIt's very expensive!") );
By default Item aims to eliminate the usage of ItemMeta, however, if you'd like to edit it you can do it easily.
// the item meta way Item item = Item.of(Material.DIAMOND, 1, "&bGlowing Diamond") .meta(meta -> { meta.addEnchant(Enchantment.DURABILITY, 1, true); meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); });
You may attach tags to your item.
// item with tags Item frog = Item.of(Material.FISHING_ROD) .name("&aFrog Rod") .lore(List.of("&aThis is a frog rod!")) .setTag("frog", "hello", DataType.STRING); // attaching some string value // check if the item has a tag if (frog.hasTag("frog", DataType.STRING)) { // yep it's a frog for sure Optional<String> optional = frog.getTag("frog", DataType.STRING); String value = optional.get(); // "hello" } // get the optional value Optional<String> optional = frog.getTag("frog", DataType.STRING); optional.ifPresent(value -> { // do something with the value });
The Item class natively wraps an ItemStack instance so you can grab it anytime.
// golden apple Item goldenApple = Item.of(Material.GOLDEN_APPLE, 1, "&6Golden Apple"); ItemStack itemStack = goldenApple.get(); // get the item stack