From 8400a7987a6eb114f9f0b6657d19f0d24afc3231 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Fri, 15 Apr 2022 12:54:08 +1000 Subject: [PATCH] #975: Add method to read ItemStack input By: Parker Hawke --- .../inventory/CraftItemFactory.java | 24 +++++++++++++++++++ .../craftbukkit/inventory/CraftMetaItem.java | 7 ++++++ 2 files changed, 31 insertions(+) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java index 15b2226671..5b40a1b2a2 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java @@ -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); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index 92005c2df3..fd3fdefe06 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -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());