Commits

Parker Hawke authored and md_5 committed fa99e752ae2
#1007: Add ItemMeta#getAsComponentString()
No tags

src/main/java/org/bukkit/inventory/meta/ItemMeta.java

Modified
3 3 import com.google.common.collect.Multimap;
4 4 import java.util.Collection;
5 5 import java.util.List;
6 6 import java.util.Map;
7 7 import java.util.Set;
8 8 import org.bukkit.attribute.Attribute;
9 9 import org.bukkit.attribute.AttributeModifier;
10 10 import org.bukkit.configuration.serialization.ConfigurationSerializable;
11 11 import org.bukkit.enchantments.Enchantment;
12 12 import org.bukkit.inventory.EquipmentSlot;
13 +import org.bukkit.inventory.ItemFactory;
13 14 import org.bukkit.inventory.ItemFlag;
14 15 import org.bukkit.inventory.ItemRarity;
15 16 import org.bukkit.inventory.meta.components.FoodComponent;
16 17 import org.bukkit.inventory.meta.tags.CustomItemTagContainer;
17 18 import org.bukkit.persistence.PersistentDataHolder;
18 19 import org.jetbrains.annotations.ApiStatus;
19 20 import org.jetbrains.annotations.NotNull;
20 21 import org.jetbrains.annotations.Nullable;
21 22
22 23 /**
530 531 * @return if any attribute modifiers were remove
531 532 *
532 533 * @throws NullPointerException if the Attribute is null
533 534 * @throws NullPointerException if the AttributeModifier is null
534 535 *
535 536 * @see AttributeModifier#getUniqueId()
536 537 */
537 538 boolean removeAttributeModifier(@NotNull Attribute attribute, @NotNull AttributeModifier modifier);
538 539
539 540 /**
540 - * Get this ItemMeta as an NBT string.
541 + * Get this ItemMeta as an NBT string. If this ItemMeta does not have any
542 + * NBT, then {@code "{}"} will be returned.
541 543 * <p>
542 - * This string should not be relied upon as a serializable value. If
543 - * serialization is desired, the {@link ConfigurationSerializable} API
544 - * should be used instead.
544 + * This string should <strong>NEVER</strong> be relied upon as a serializable value. If
545 + * serialization is desired, the {@link ConfigurationSerializable} API should be used
546 + * instead.
545 547 *
546 548 * @return the NBT string
547 549 */
548 550 @NotNull
549 551 String getAsString();
550 552
553 + /**
554 + * Get this ItemMeta as a component-compliant string. If this ItemMeta does
555 + * not contain any components, then {@code "[]"} will be returned.
556 + * <p>
557 + * The result of this method should yield a string representing the components
558 + * altered by this ItemMeta instance. When passed to {@link ItemFactory#createItemStack(String)}
559 + * with a prepended item type, it will create an ItemStack that has an ItemMeta
560 + * matching this ItemMeta instance exactly. Note that this method returns <strong>
561 + * ONLY</strong> the components and cannot be passed to createItemStack() alone.
562 + * An example may look something like this:
563 + * <pre>
564 + * ItemStack itemStack = // ... an item stack obtained from somewhere
565 + * ItemMeta itemMeta = itemStack.getItemMeta();
566 + *
567 + * String components = itemMeta.getAsComponentString(); // example: "[minecraft:damage=53]"
568 + * String itemTypeKey = itemStack.getType().getKey().toString(); // example: "minecraft:diamond_sword"
569 + * String itemAsString = itemTypeKey + components; // results in: "minecraft:diamond_sword[minecraft:damage=53]"
570 + *
571 + * ItemStack recreatedItemStack = Bukkit.getItemFactory().createItemStack(itemAsString);
572 + * assert itemStack.isSimilar(recreatedItemStack); // Should be true*
573 + * </pre>
574 + * <p>
575 + * *Components not represented or explicitly overridden by this ItemMeta instance
576 + * will not be included in the resulting string and therefore may result in ItemStacks
577 + * that do not match <em>exactly</em>. For example, if {@link #setDisplayName(String)}
578 + * is not set, then the custom name component will not be included. Or if this ItemMeta
579 + * is a PotionMeta, it will not include any components related to lodestone compasses,
580 + * banners, or books, etc., only components modifiable by a PotionMeta instance.
581 + * <p>
582 + * This string should <strong>NEVER</strong> be relied upon as a serializable value. If
583 + * serialization is desired, the {@link ConfigurationSerializable} API should be used
584 + * instead.
585 + *
586 + * @return the component-compliant string
587 + */
588 + @NotNull
589 + String getAsComponentString();
590 +
551 591 /**
552 592 * Returns a public custom tag container capable of storing tags on the
553 593 * item.
554 594 *
555 595 * Those tags will be sent to the client with all of their content, so the
556 596 * client is capable of reading them. This will result in the player seeing
557 597 * a NBT Tag notification on the item.
558 598 *
559 599 * These tags can also be modified by the client once in creative mode
560 600 *

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut