Minestom/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentNbtCompoundTag.java

52 lines
1.6 KiB
Java
Raw Normal View History

package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
2020-10-24 16:58:27 +02:00
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTException;
2021-12-13 16:41:30 +01:00
import org.jglrxavpok.hephaistos.parser.SNBTParser;
import java.io.StringReader;
2020-10-17 11:29:05 +02:00
/**
* Argument used to retrieve a {@link NBTCompound} if you need key-value data.
* <p>
* Example: {display:{Name:"{\"text\":\"Sword of Power\"}"}}
*/
public class ArgumentNbtCompoundTag extends Argument<NBTCompound> {
public static final int INVALID_NBT = 1;
public ArgumentNbtCompoundTag(String id) {
super(id, true);
}
2020-10-24 16:58:27 +02:00
@NotNull
@Override
public NBTCompound parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException {
try {
NBT nbt = new SNBTParser(new StringReader(input)).parse();
if (!(nbt instanceof NBTCompound))
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
return (NBTCompound) nbt;
} catch (NBTException e) {
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
}
}
@Override
public String parser() {
return "minecraft:nbt_compound_tag";
}
@Override
public String toString() {
return String.format("NbtCompound<%s>", getId());
}
}