Minestom/src/main/java/net/minestom/server/command/builder/arguments/minecraft/ArgumentComponent.java
mworzala 8335afdf47 hollow-cube/sender-in-command-arg-parsing
Signed-off-by: mworzala <mattheworzala@gmail.com>

fix default argument issue

(cherry picked from commit a7440639c8541faeb91155c53ce3a1f4d60df127)

Add sender to command parse chain

(cherry picked from commit 853307891d178abdc7036f8c809c52909cdca327)

(cherry picked from commit 1268cf16c0)
2024-02-09 14:27:43 -05:00

39 lines
1.2 KiB
Java

package net.minestom.server.command.builder.arguments.minecraft;
import com.google.gson.JsonParseException;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import org.jetbrains.annotations.NotNull;
public class ArgumentComponent extends Argument<Component> {
public static final int INVALID_JSON_ERROR = 1;
public ArgumentComponent(@NotNull String id) {
super(id, true);
}
@NotNull
@Override
public Component parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException {
try {
return GsonComponentSerializer.gson().deserialize(input);
} catch (JsonParseException e) {
throw new ArgumentSyntaxException("Invalid JSON", input, INVALID_JSON_ERROR);
}
}
@Override
public String parser() {
return "minecraft:component";
}
@Override
public String toString() {
return String.format("Component<%s>", getId());
}
}