Added Items#toLookupString
This commit is contained in:
@@ -115,6 +115,48 @@ public final class Items {
|
||||
REGISTRY.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn an ItemStack back into a lookup string.
|
||||
*
|
||||
* @param itemStack The ItemStack.
|
||||
* @return The lookup string.
|
||||
*/
|
||||
@NotNull
|
||||
public static String toLookupString(@Nullable final ItemStack itemStack) {
|
||||
if (itemStack == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
CustomItem customItem = getCustomItem(itemStack);
|
||||
|
||||
if (customItem != null) {
|
||||
builder.append(customItem.getKey());
|
||||
} else {
|
||||
builder.append(itemStack.getType().name().toLowerCase());
|
||||
}
|
||||
|
||||
if (itemStack.getAmount() > 1) {
|
||||
builder.append(" ")
|
||||
.append(itemStack.getAmount());
|
||||
}
|
||||
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
|
||||
if (meta != null) {
|
||||
for (LookupArgParser parser : ARG_PARSERS) {
|
||||
String parsed = parser.serializeBack(meta);
|
||||
if (parsed != null) {
|
||||
builder.append(" ")
|
||||
.append(parsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the backbone of the entire eco item system.
|
||||
* <p>
|
||||
|
||||
@@ -22,4 +22,14 @@ public interface LookupArgParser {
|
||||
*/
|
||||
@Nullable Predicate<ItemStack> parseArguments(@NotNull String[] args,
|
||||
@NotNull ItemMeta meta);
|
||||
|
||||
/**
|
||||
* Serialize the item back to a string.
|
||||
*
|
||||
* @param meta The ItemMeta.
|
||||
* @return The string, or null if not required.
|
||||
*/
|
||||
default @Nullable String serializeBack(@NotNull final ItemMeta meta) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
11
eco-api/src/main/kotlin/com/willfp/eco/core/items/Items.kt
Normal file
11
eco-api/src/main/kotlin/com/willfp/eco/core/items/Items.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
@file:JvmName("ItemsExtensions")
|
||||
|
||||
package com.willfp.eco.core.items
|
||||
|
||||
import org.bukkit.inventory.ItemStack
|
||||
|
||||
/**
|
||||
* @see Items.toLookupString
|
||||
*/
|
||||
fun ItemStack?.toLookupString(): String =
|
||||
Items.toLookupString(this)
|
||||
Reference in New Issue
Block a user