2020-10-17 08:18:52 +02:00
|
|
|
package net.minestom.server.command.builder.arguments.minecraft;
|
|
|
|
|
2024-03-17 21:07:29 +01:00
|
|
|
import net.kyori.adventure.nbt.BinaryTag;
|
|
|
|
import net.kyori.adventure.nbt.CompoundBinaryTag;
|
|
|
|
import net.kyori.adventure.nbt.TagStringIO;
|
2023-03-25 14:29:20 +01:00
|
|
|
import net.minestom.server.command.CommandSender;
|
2020-10-17 08:18:52 +02:00
|
|
|
import net.minestom.server.command.builder.arguments.Argument;
|
2021-01-08 03:07:37 +01:00
|
|
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
2020-10-24 16:58:27 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2020-10-17 08:18:52 +02:00
|
|
|
|
2024-03-17 21:07:29 +01:00
|
|
|
import java.io.IOException;
|
2020-10-17 08:18:52 +02:00
|
|
|
|
2020-10-17 11:29:05 +02:00
|
|
|
/**
|
2024-03-17 21:07:29 +01:00
|
|
|
* Argument used to retrieve a {@link CompoundBinaryTag} if you need key-value data.
|
2020-10-17 11:29:05 +02:00
|
|
|
* <p>
|
|
|
|
* Example: {display:{Name:"{\"text\":\"Sword of Power\"}"}}
|
|
|
|
*/
|
2024-03-17 21:07:29 +01:00
|
|
|
public class ArgumentNbtCompoundTag extends Argument<CompoundBinaryTag> {
|
2020-10-17 08:18:52 +02:00
|
|
|
|
|
|
|
public static final int INVALID_NBT = 1;
|
|
|
|
|
|
|
|
public ArgumentNbtCompoundTag(String id) {
|
|
|
|
super(id, true);
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:58:27 +02:00
|
|
|
@NotNull
|
2020-10-17 08:18:52 +02:00
|
|
|
@Override
|
2024-03-17 21:07:29 +01:00
|
|
|
public CompoundBinaryTag parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException {
|
2020-10-17 08:18:52 +02:00
|
|
|
try {
|
2024-03-17 21:07:29 +01:00
|
|
|
BinaryTag nbt = TagStringIO.get().asCompound(input);
|
2021-01-08 03:07:37 +01:00
|
|
|
|
2024-03-17 21:07:29 +01:00
|
|
|
if (!(nbt instanceof CompoundBinaryTag compound))
|
2021-01-08 03:07:37 +01:00
|
|
|
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
|
|
|
|
|
2024-03-17 21:07:29 +01:00
|
|
|
return compound;
|
|
|
|
} catch (IOException e) {
|
2021-01-08 03:07:37 +01:00
|
|
|
throw new ArgumentSyntaxException("NBTCompound is invalid", input, INVALID_NBT);
|
2020-10-17 08:18:52 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 00:25:10 +01:00
|
|
|
|
|
|
|
@Override
|
2022-07-02 05:39:39 +02:00
|
|
|
public String parser() {
|
|
|
|
return "minecraft:nbt_compound_tag";
|
2021-02-09 00:25:10 +01:00
|
|
|
}
|
2021-04-25 10:42:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return String.format("NbtCompound<%s>", getId());
|
|
|
|
}
|
2020-10-17 08:18:52 +02:00
|
|
|
}
|