From ad47aac9ae8e21438f24cdd8505fc7d86b471016 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Fri, 15 Nov 2024 18:08:34 -0500 Subject: [PATCH 1/4] Starter Commit --- .../ch/njol/skript/bukkitutil/ItemUtils.java | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java index a74a604664d..136f0e5b6e3 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java @@ -21,11 +21,7 @@ import ch.njol.skript.Skript; import ch.njol.skript.aliases.ItemType; import ch.njol.skript.util.slot.Slot; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.OfflinePlayer; -import org.bukkit.Tag; -import org.bukkit.TreeType; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.type.Fence; @@ -39,6 +35,7 @@ import java.util.HashMap; import java.util.UUID; +import java.util.function.Consumer; /** * Miscellaneous static utility methods related to items. @@ -214,12 +211,12 @@ public static Material asItem(Material type) { */ @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); + 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 +358,36 @@ public static boolean isGlass(Material material) { return false; } } + + /** + * Changes the ItemMeta of the provided ItemStack with the provided Consumer + * @param itemStack ItemStack's ItemMeta to be changed + * @param metaChanger Consumer to change the ItemMeta + * @return ItemStack with the updated ItemMeta + * @param + */ + public static ItemStack changeItemMeta(ItemStack itemStack, Consumer metaChanger) { + //noinspection unchecked + T itemMeta = (T) itemStack.getItemMeta(); + metaChanger.accept(itemMeta); + itemStack.setItemMeta(itemMeta); + return itemStack; + } + + /** + * Set's the item back from its original location, see {@link ItemUtils#asItemStack(Object)} + * @param object The original object used to determine it's origin + * @param itemStack The ItemStack to update the original location + */ + public static void setItem(Object object, ItemStack itemStack) { + ItemMeta itemMeta = itemStack.getItemMeta(); + if (object instanceof Slot slot) { + slot.setItem(itemStack); + } else if (object instanceof ItemType itemType) { + itemType.setItemMeta(itemMeta); + } else if (object instanceof ItemStack itemStack1) { + itemStack1.setItemMeta(itemMeta); + } + } + } From 3648e2ced5054311371d2fb6a63a6d22a171c86b Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Sat, 16 Nov 2024 12:50:11 -0500 Subject: [PATCH 2/4] Requested Changes --- src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java index 136f0e5b6e3..271fe27769a 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java @@ -209,8 +209,7 @@ public static Material asItem(Material type) { * @param object Object to convert * @return ItemStack from slot/itemtype */ - @Nullable - public static ItemStack asItemStack(Object object) { + public static @Nullable ItemStack asItemStack(Object object) { if (object instanceof ItemType itemType) return itemType.getRandom(); else if (object instanceof Slot slot) @@ -360,7 +359,7 @@ public static boolean isGlass(Material material) { } /** - * Changes the ItemMeta of the provided ItemStack with the provided Consumer + * Applies {@code metaChanger} to the meta of {@code itemStack} and returns the itemStack with the updated ItemMeta. * @param itemStack ItemStack's ItemMeta to be changed * @param metaChanger Consumer to change the ItemMeta * @return ItemStack with the updated ItemMeta @@ -375,7 +374,8 @@ public static ItemStack changeItemMeta(ItemStack itemStack, } /** - * Set's the item back from its original location, see {@link ItemUtils#asItemStack(Object)} + * Updates the provided, slot, itemtype or item stack in {@code object} by applying {@code itemStack} to it. + * See {@see asItemStack} * @param object The original object used to determine it's origin * @param itemStack The ItemStack to update the original location */ @@ -388,6 +388,7 @@ public static void setItem(Object object, ItemStack itemStack) { } else if (object instanceof ItemStack itemStack1) { itemStack1.setItemMeta(itemMeta); } + throw new IllegalArgumentException("Object did not originate from a Slot, ItemType or ItemStack"); } } From 5284dc95389be00ea41544ef961026c3a8620995 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Sat, 16 Nov 2024 22:54:19 -0500 Subject: [PATCH 3/4] Update ItemUtils --- .../ch/njol/skript/bukkitutil/ItemUtils.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java index 271fe27769a..b6f81c41394 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java @@ -359,11 +359,11 @@ public static boolean isGlass(Material material) { } /** - * Applies {@code metaChanger} to the meta of {@code itemStack} and returns the itemStack with the updated ItemMeta. - * @param itemStack ItemStack's ItemMeta to be changed - * @param metaChanger Consumer to change the ItemMeta - * @return ItemStack with the updated ItemMeta + * 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(ItemStack itemStack, Consumer metaChanger) { //noinspection unchecked @@ -374,10 +374,11 @@ public static ItemStack changeItemMeta(ItemStack itemStack, } /** - * Updates the provided, slot, itemtype or item stack in {@code object} by applying {@code itemStack} to it. - * See {@see asItemStack} - * @param object The original object used to determine it's origin - * @param itemStack The ItemStack to update the original location + * Updates the provided object ({@code Slot}, {@code ItemType}, {@code ItemStack}) by setting it to the provided {@code ItemStack}. + * + * @param object the object to update + * @param itemStack the item to set the object to + * @see #asItemStack(Object) */ public static void setItem(Object object, ItemStack itemStack) { ItemMeta itemMeta = itemStack.getItemMeta(); @@ -388,7 +389,7 @@ public static void setItem(Object object, ItemStack itemStack) { } else if (object instanceof ItemStack itemStack1) { itemStack1.setItemMeta(itemMeta); } - throw new IllegalArgumentException("Object did not originate from a Slot, ItemType or ItemStack"); + throw new IllegalArgumentException("Object was not a Slot, ItemType or ItemStack."); } } From 2dcf5b6640ae0afd051f3f77349e0467aab4791c Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Fri, 22 Nov 2024 23:36:20 -0500 Subject: [PATCH 4/4] Changes --- src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java index b6f81c41394..dd12104d812 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java @@ -31,6 +31,7 @@ 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; @@ -209,7 +210,7 @@ public static Material asItem(Material type) { * @param object Object to convert * @return ItemStack from slot/itemtype */ - public static @Nullable ItemStack asItemStack(Object object) { + public static @Nullable ItemStack asItemStack(@Nullable Object object) { if (object instanceof ItemType itemType) return itemType.getRandom(); else if (object instanceof Slot slot) @@ -360,12 +361,13 @@ public static boolean isGlass(Material material) { /** * 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(ItemStack itemStack, Consumer metaChanger) { + public static ItemStack changeItemMeta(@NotNull ItemStack itemStack, @NotNull Consumer metaChanger) { //noinspection unchecked T itemMeta = (T) itemStack.getItemMeta(); metaChanger.accept(itemMeta); @@ -380,7 +382,7 @@ public static ItemStack changeItemMeta(ItemStack itemStack, * @param itemStack the item to set the object to * @see #asItemStack(Object) */ - public static void setItem(Object object, ItemStack itemStack) { + public static void setItem(@Nullable Object object, @NotNull ItemStack itemStack) { ItemMeta itemMeta = itemStack.getItemMeta(); if (object instanceof Slot slot) { slot.setItem(itemStack);