mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-30 16:19:03 +01:00
Improve ItemStack#editMeta (#6502)
This commit is contained in:
parent
150389bf71
commit
7f7961ea23
1 changed files with 22 additions and 3 deletions
|
@ -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<? super ItemMeta> 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.
|
||||
+ * <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);
|
||||
+ return true;
|
||||
+ }
|
||||
|
|
Loading…
Reference in a new issue