2021-03-03 22:52:40 +01:00
|
|
|
package net.minestom.server.command.builder.arguments.minecraft;
|
|
|
|
|
|
|
|
import com.google.gson.JsonParseException;
|
2021-03-24 13:14:31 +01:00
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
2023-03-25 14:29:20 +01:00
|
|
|
import net.minestom.server.command.CommandSender;
|
2021-03-03 22:52:40 +01:00
|
|
|
import net.minestom.server.command.builder.arguments.Argument;
|
|
|
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2021-03-24 13:14:31 +01:00
|
|
|
public class ArgumentComponent extends Argument<Component> {
|
2021-03-03 22:52:40 +01:00
|
|
|
|
|
|
|
public static final int INVALID_JSON_ERROR = 1;
|
|
|
|
|
|
|
|
public ArgumentComponent(@NotNull String id) {
|
|
|
|
super(id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
@Override
|
2023-03-25 14:29:20 +01:00
|
|
|
public Component parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException {
|
2021-03-03 22:52:40 +01:00
|
|
|
try {
|
2021-03-24 13:14:31 +01:00
|
|
|
return GsonComponentSerializer.gson().deserialize(input);
|
2021-03-03 22:52:40 +01:00
|
|
|
} catch (JsonParseException e) {
|
|
|
|
throw new ArgumentSyntaxException("Invalid JSON", input, INVALID_JSON_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-07-02 05:39:39 +02:00
|
|
|
public String parser() {
|
|
|
|
return "minecraft:component";
|
2021-03-03 22:52:40 +01:00
|
|
|
}
|
2021-04-25 10:42:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return String.format("Component<%s>", getId());
|
|
|
|
}
|
2021-03-03 22:52:40 +01:00
|
|
|
}
|