Skip to content

Commit

Permalink
ItemUtils: Add meta and holder utility methods (#7217)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAbsolutionism authored Jan 1, 2025
1 parent d60fe83 commit d8664fc
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 deletions src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 <T>
* @return the updated item
*/
public static <T extends ItemMeta> ItemStack changeItemMeta(@NotNull Class<T> metaClass, @NotNull ItemStack itemStack, @NotNull Consumer<T> 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.");
}

}

0 comments on commit d8664fc

Please sign in to comment.