Add API to ItemUtils to retrieve an NMS item based on a given Bukkit item

This commit is contained in:
Alexander
2021-05-24 06:06:43 +01:00
parent 981c6e9e4c
commit 763d1c2821

View File

@@ -14,6 +14,7 @@ import net.kyori.adventure.text.Component;
import org.apache.commons.collections4.CollectionUtils;
import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
@@ -171,6 +172,26 @@ public final class ItemUtils {
return MetaUtils.areMetasEqual(former.getItemMeta(), latter.getItemMeta());
}
/**
* Returns the NMS version of a given item, preferring the item's craft handle but will fall back upon creating an
* NMS copy.
*
* @param item The item to get the NMS version of.
* @return The NMS version, either handle or copy.
*/
public static net.minecraft.server.v1_16_R3.ItemStack getNMSItemStack(final ItemStack item) {
if (item == null) {
return null;
}
if (item instanceof CraftItemStack) {
final var handle = ((CraftItemStack) item).getHandle();
if (handle != null) {
return handle;
}
}
return CraftItemStack.asNMSCopy(item);
}
/**
* Decrements an item's amount, or returns null if the amount reaches zero.
*