.
This commit is contained in:
parent
254af80fde
commit
72f7625b19
53 changed files with 679 additions and 756 deletions
|
|
@ -2,22 +2,47 @@ package de.kentoj.kencommandapi;
|
|||
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.command.SimpleCommand;
|
||||
import de.kentoj.kencommandapi.api.nodes.CommandNode;
|
||||
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 lombok.RequiredArgsConstructor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class VelocityCommand implements SimpleCommand {
|
||||
|
||||
private final VelocityKenCommandApi commandApi;
|
||||
private final CommandNode<CommandSource, VelocityCommandContext> rootNode;
|
||||
private final CommandParser<CommandSource> commandParser;
|
||||
private final CommandNode<CommandSource> rootNode;
|
||||
|
||||
@Override
|
||||
public void execute(Invocation ctx) {
|
||||
commandApi.invoke(rootNode, ctx.source(), ctx.arguments());
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(Invocation ctx) {
|
||||
return commandApi.hasPermission(ctx.source(), rootNode.getPermission());
|
||||
if (rootNode.getPermission() == null) return true;
|
||||
return ctx.source().hasPermission(rootNode.getPermission());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue