net.minestom.server.command.builder.arguments.minecraft.ArgumentItemStack Maven / Gradle / Ivy
Show all versions of minestom-snapshots Show documentation
package net.minestom.server.command.builder.arguments.minecraft;
import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.TagStringIOExt;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.component.DataComponent;
import net.minestom.server.component.DataComponentMap;
import net.minestom.server.item.ItemComponent;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.item.component.CustomData;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
/**
* Argument which can be used to retrieve an {@link ItemStack} from its material and with NBT data.
*
* It is the same type as the one used in the /give command.
*
* Example: diamond_sword{display:{Name:"{\"text\":\"Sword of Power\"}"}}
*/
public class ArgumentItemStack extends Argument {
public static final int NO_MATERIAL = 1;
public static final int INVALID_NBT = 2;
public static final int INVALID_MATERIAL = 3;
public static final int INVALID_COMPONENT = 4;
public ArgumentItemStack(String id) {
super(id, true);
}
@NotNull
@Override
public ItemStack parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException {
return staticParse(input);
}
@Override
public String parser() {
return "minecraft:item_stack";
}
/**
* @deprecated use {@link Argument#parse(CommandSender, Argument)}
*/
@Deprecated
public static ItemStack staticParse(@NotNull String input) throws ArgumentSyntaxException {
var reader = new StringReader(input);
final Material material = Material.fromNamespaceId(reader.readNamespaceId());
if (material == null)
throw new ArgumentSyntaxException("Material is invalid", input, INVALID_MATERIAL);
if (!reader.hasMore()) {
return ItemStack.of(material); // Nothing else, we have our item
}
DataComponentMap.Builder components = DataComponentMap.builder();
// Parse the declared components
if (reader.peek() == '[') {
reader.consume('[');
do {
final NamespaceID componentId = reader.readNamespaceId();
final DataComponent> component = ItemComponent.fromNamespaceId(componentId);
if (component == null)
throw new ArgumentSyntaxException("Unknown item component", input, INVALID_COMPONENT);
reader.consume('=');
final BinaryTag nbt = reader.readTag();
BinaryTagSerializer.Context context = new BinaryTagSerializer.ContextWithRegistries(MinecraftServer.process(), false);
//noinspection unchecked
components.set((DataComponent