From 7f7961ea23a8083a32519e886c434ec8615e435b Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Thu, 30 Sep 2021 17:32:25 -0700 Subject: [PATCH] Improve ItemStack#editMeta (#6502) --- patches/api/ItemStack-editMeta.patch | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/patches/api/ItemStack-editMeta.patch b/patches/api/ItemStack-editMeta.patch index 0df0b07da0..392bcab303 100644 --- a/patches/api/ItemStack-editMeta.patch +++ b/patches/api/ItemStack-editMeta.patch @@ -27,9 +27,28 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + * @return {@code true} if the edit was successful, {@code false} otherwise + */ + public boolean editMeta(final @NotNull java.util.function.Consumer consumer) { -+ final ItemMeta meta = this.getItemMeta(); -+ if (meta != null) { -+ consumer.accept(meta); ++ return editMeta(ItemMeta.class, consumer); ++ } ++ ++ /** ++ * Edits the {@link ItemMeta} of this stack if the meta is of the specified type. ++ *

++ * The {@link java.util.function.Consumer} must only interact ++ * with this stack's {@link ItemMeta} through the provided {@link ItemMeta} instance. ++ * Calling this method or any other meta-related method of the {@link ItemStack} class ++ * (such as {@link #getItemMeta()}, {@link #addItemFlags(ItemFlag...)}, {@link #lore()}, etc.) ++ * from inside the consumer is disallowed and will produce undefined results or exceptions. ++ *

++ * ++ * @param metaClass the type of meta to edit ++ * @param consumer the meta consumer ++ * @param the meta type ++ * @return {@code true} if the edit was successful, {@code false} otherwise ++ */ ++ public boolean editMeta(final @NotNull Class metaClass, final @NotNull java.util.function.Consumer<@NotNull ? super M> consumer) { ++ final @Nullable ItemMeta meta = this.getItemMeta(); ++ if (metaClass.isInstance(meta)) { ++ consumer.accept((M) meta); + this.setItemMeta(meta); + return true; + }