Improve ItemStack#editMeta (#6502)

This commit is contained in:
Jake Potrebic 2021-09-30 17:32:25 -07:00 committed by GitHub
parent 425edfa5d7
commit 6847f5781f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 5 deletions

View File

@ -5,10 +5,10 @@ Subject: [PATCH] ItemStack#editMeta
diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java
index 86bd9f14de5c1ff3d797955be1af56e5efcac884..59a026d80b0a0a4890becf98efdbe5325b2c622a 100644 index 86bd9f14de5c1ff3d797955be1af56e5efcac884..56072cb4d32ca8a09023be08a5a832c2c108379a 100644
--- a/src/main/java/org/bukkit/inventory/ItemStack.java --- a/src/main/java/org/bukkit/inventory/ItemStack.java
+++ b/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/src/main/java/org/bukkit/inventory/ItemStack.java
@@ -543,6 +543,31 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, net.kyor @@ -543,6 +543,50 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, net.kyor
return result.ensureServerConversions(); // Paper return result.ensureServerConversions(); // Paper
} }
@ -27,9 +27,28 @@ index 86bd9f14de5c1ff3d797955be1af56e5efcac884..59a026d80b0a0a4890becf98efdbe532
+ * @return {@code true} if the edit was successful, {@code false} otherwise + * @return {@code true} if the edit was successful, {@code false} otherwise
+ */ + */
+ public boolean editMeta(final @NotNull java.util.function.Consumer<? super ItemMeta> consumer) { + public boolean editMeta(final @NotNull java.util.function.Consumer<? super ItemMeta> consumer) {
+ final ItemMeta meta = this.getItemMeta(); + return editMeta(ItemMeta.class, consumer);
+ if (meta != null) { + }
+ consumer.accept(meta); +
+ /**
+ * Edits the {@link ItemMeta} of this stack if the meta is of the specified type.
+ * <p>
+ * 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.
+ * </p>
+ *
+ * @param metaClass the type of meta to edit
+ * @param consumer the meta consumer
+ * @param <M> the meta type
+ * @return {@code true} if the edit was successful, {@code false} otherwise
+ */
+ public <M extends ItemMeta> boolean editMeta(final @NotNull Class<M> 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); + this.setItemMeta(meta);
+ return true; + return true;
+ } + }