currently refactoring a lot.

This commit is contained in:
kento2 2026-07-04 01:04:01 +02:00
parent fe0b04c7d1
commit 9aa138058c
32 changed files with 484 additions and 254 deletions

View file

@ -2,42 +2,22 @@ package de.kentoj.kencommandapi;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import de.kentoj.kencommandapi.api.CommandNode;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.invocation.CommandExecutor;
import de.kentoj.kencommandapi.internal.parser.CommandParser;
import de.kentoj.kencommandapi.internal.parser.ParseResult;
import de.kentoj.kencommandapi.api.CommandHandler;
import de.kentoj.kencommandapi.api.node.RootCommandNode;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@RequiredArgsConstructor
public class VelocityCommand implements SimpleCommand {
private final CommandParser<CommandSource> commandParser;
private final CommandNode<CommandSource> rootNode;
private final CommandHandler<CommandSource> commandHandler;
private final RootCommandNode<CommandSource> rootNode;
@Override
public void execute(Invocation invocation) {
var parseResult = commandParser.parseLiteral(rootNode, invocation.source(), invocation.arguments());
if (parseResult instanceof ParseResult.Success<CommandSource>) {
var ctx = new CommandContext<>(invocation.source(), ((ParseResult.Success<CommandSource>) parseResult).getParsedArguments());
assert parseResult.getLiteral().getExecutor() != null;
try {
var result = parseResult.getLiteral().getExecutor().execute(ctx);
if (result.getError() != null)
invocation.source().sendMessage(Component.text(result.getError()).color(NamedTextColor.RED));
} catch (Exception e) {
invocation.source().sendMessage(Component.text("ERROR ")
.append(Component.text(e.getMessage()))
.color(NamedTextColor.DARK_RED)
.decorate(TextDecoration.BOLD));
}
} else {
invocation.source().sendMessage(Component.text(parseResult.getMessage()).color(NamedTextColor.RED));
}
commandHandler.invoke(rootNode, invocation.source(), invocation.arguments());
}
@Override
@ -45,4 +25,9 @@ public class VelocityCommand implements SimpleCommand {
if (rootNode.getPermission() == null) return true;
return ctx.source().hasPermission(rootNode.getPermission());
}
}
@Override
public CompletableFuture<List<String>> suggestAsync(Invocation invocation) {
return commandHandler.getSuggestions(rootNode, invocation.source(), invocation.arguments());
}
}