diff --git a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java index ae37c272750..b27265f5ae3 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java @@ -18,10 +18,12 @@ import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.SkullMeta; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.UUID; +import java.util.function.Consumer; /** * Miscellaneous static utility methods related to items. @@ -202,14 +204,14 @@ public static Material asItem(Material type) { * @param object Object to convert * @return ItemStack from slot/itemtype */ - @Nullable - public static ItemStack asItemStack(Object object) { - if (object instanceof ItemType) - return ((ItemType) object).getRandom(); - else if (object instanceof Slot) - return ((Slot) object).getItem(); - else if (object instanceof ItemStack) - return ((ItemStack) object); + public static @Nullable ItemStack asItemStack(@Nullable Object object) { + if (object instanceof ItemType itemType) { + return itemType.getRandom(); + } else if (object instanceof Slot slot) { + return slot.getItem(); + } else if (object instanceof ItemStack itemStack) { + return itemStack; + } return null; } @@ -361,4 +363,46 @@ public static boolean isGlass(Material material) { return false; } } + + /** + * Applies a provided {@code Consumer} to the meta of the provided {@code ItemStack} and returns the updated {@code ItemStack} (with updated {@code ItemMeta}). + * + * @param itemStack the item whose meta is to be changed using the provided Consumer + * @param metaChanger a consumer to update the meta of the provided ItemStack + * @param + * @return the updated item + */ + public static ItemStack changeItemMeta(@NotNull Class metaClass, @NotNull ItemStack itemStack, @NotNull Consumer metaChanger) { + ItemMeta originalMeta = itemStack.getItemMeta(); + if (metaClass.isInstance(originalMeta)) { + //noinspection unchecked + T itemMeta = (T) originalMeta; + metaChanger.accept(itemMeta); + itemStack.setItemMeta(itemMeta); + } + return itemStack; + } + + /** + * Updates the provided object's ({@code Slot}, {@code ItemType}, {@code ItemStack}) {@link ItemMeta} by setting it to the provided {@code ItemStack}. + * + * @param object the object to update + * @param itemMeta the {@link ItemMeta} to change to + * @see #asItemStack(Object) + */ + public static void setItemMeta(Object object, @NotNull ItemMeta itemMeta) { + if (object instanceof Slot slot) { + ItemStack itemStack = slot.getItem(); + if (itemStack == null) + return; + itemStack.setItemMeta(itemMeta); + slot.setItem(itemStack); + } else if (object instanceof ItemType itemType) { + itemType.setItemMeta(itemMeta); + } else if (object instanceof ItemStack itemStack) { + itemStack.setItemMeta(itemMeta); + } + throw new IllegalArgumentException("Object was not a Slot, ItemType or ItemStack."); + } + }