Added Items#toLookupString

This commit is contained in:
Auxilor
2022-03-27 13:19:48 +01:00
parent e1ffc851a3
commit bde546efcb
10 changed files with 137 additions and 2 deletions

View File

@@ -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>

View File

@@ -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;
}
}

View 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)