#975: Add method to read ItemStack input

By: Parker Hawke <hawkeboyz2@hotmail.com>
This commit is contained in:
CraftBukkit/Spigot 2022-04-15 12:54:08 +10:00
parent 9340d95f92
commit 8400a7987a
2 changed files with 31 additions and 0 deletions

View File

@ -1,5 +1,10 @@
package org.bukkit.craftbukkit.inventory;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.arguments.item.ArgumentParserItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.item.Item;
import org.apache.commons.lang.Validate;
import org.bukkit.Color;
import org.bukkit.Material;
@ -340,6 +345,25 @@ public final class CraftItemFactory implements ItemFactory {
return DEFAULT_LEATHER_COLOR;
}
@Override
public ItemStack createItemStack(String input) throws IllegalArgumentException {
try {
ArgumentParserItemStack arg = new ArgumentParserItemStack(new StringReader(input), false).parse(); // false = no tags
Item item = arg.getItem();
net.minecraft.world.item.ItemStack nmsItemStack = new net.minecraft.world.item.ItemStack(item);
NBTTagCompound nbt = arg.getNbt();
if (nbt != null) {
nmsItemStack.setTag(nbt);
}
return CraftItemStack.asCraftMirror(nmsItemStack);
} catch (CommandSyntaxException ex) {
throw new IllegalArgumentException("Could not parse ItemStack: " + input, ex);
}
}
@Override
public Material updateMaterial(ItemMeta meta, Material material) throws IllegalArgumentException {
return ((CraftMetaItem) meta).updateMaterial(material);

View File

@ -1057,6 +1057,13 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
return removed > 0;
}
@Override
public String getAsString() {
NBTTagCompound tag = new NBTTagCompound();
applyToItem(tag);
return tag.toString();
}
@Override
public CustomItemTagContainer getCustomTagContainer() {
return new DeprecatedCustomTagContainer(this.getPersistentDataContainer());