Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ItemUtils#changeItemMeta, #setItem #7217

Open
wants to merge 5 commits into
base: dev/feature
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +35,7 @@

import java.util.HashMap;
import java.util.UUID;
import java.util.function.Consumer;

/**
* Miscellaneous static utility methods related to items.
Expand Down Expand Up @@ -212,14 +209,13 @@ 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(Object object) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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 +357,38 @@ public static boolean isGlass(Material material) {
return false;
}
}

/**
* 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
* @param <T>
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
*/
public static <T extends ItemMeta> ItemStack changeItemMeta(ItemStack itemStack, Consumer<T> metaChanger) {
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
//noinspection unchecked
T itemMeta = (T) itemStack.getItemMeta();
metaChanger.accept(itemMeta);
itemStack.setItemMeta(itemMeta);
return 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
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
*/
public static void setItem(Object object, ItemStack itemStack) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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);
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException("Object did not originate from a Slot, ItemType or ItemStack");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

}