currently refactoring a lot.
This commit is contained in:
parent
fe0b04c7d1
commit
9aa138058c
32 changed files with 484 additions and 254 deletions
|
|
@ -2,8 +2,9 @@ package de.kentoj.kencommandapi;
|
|||
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import de.kentoj.kencommandapi.api.CommandNode;
|
||||
import de.kentoj.kencommandapi.internal.parser.CommandParser;
|
||||
import de.kentoj.kencommandapi.api.CommandHandler;
|
||||
import de.kentoj.kencommandapi.api.node.RootCommandNode;
|
||||
import de.kentoj.kencommandapi.api.parser.CommandParser;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
|
@ -12,13 +13,17 @@ import java.util.Arrays;
|
|||
public class CommandAPI {
|
||||
|
||||
private final ProxyServer server;
|
||||
private final CommandParser<CommandSource> commandParser = new CommandParser<>(CommandSource::hasPermission);
|
||||
private final CommandHandler<CommandSource> commandHandler = new CommandHandler<>(
|
||||
new CommandParser<>(CommandSource::hasPermission),
|
||||
CommandSource::sendMessage,
|
||||
CommandSource::hasPermission
|
||||
);
|
||||
|
||||
public void register(CommandNode<CommandSource> rootNode) {
|
||||
public void register(RootCommandNode<CommandSource> rootNode) {
|
||||
var cm = server.getCommandManager();
|
||||
var meta = cm.metaBuilder(rootNode.getNames()[0])
|
||||
.aliases(Arrays.copyOfRange(rootNode.getNames(), 1, rootNode.getNames().length - 1))
|
||||
.build();
|
||||
cm.register(meta, new VelocityCommand(commandParser, rootNode));
|
||||
cm.register(meta, new VelocityCommand(commandHandler, rootNode));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
@ -3,13 +3,11 @@ package de.kentoj.kencommandapi.suggestionprovider;
|
|||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import de.kentoj.kencommandapi.api.invocation.CommandContext;
|
||||
import de.kentoj.kencommandapi.api.suggestion.SuggestionProvider;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class PlayerSuggestionProvider implements SuggestionProvider<CommandSource> {
|
||||
|
|
@ -17,10 +15,10 @@ public class PlayerSuggestionProvider implements SuggestionProvider<CommandSourc
|
|||
private final ProxyServer proxyServer;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Set<String>> suggest(CommandContext<CommandSource> context) {
|
||||
public CompletableFuture<List<String>> suggest(CommandSource __) {
|
||||
var list = proxyServer.getAllPlayers().stream()
|
||||
.map(Player::getUsername)
|
||||
.collect(Collectors.toSet());
|
||||
.toList();
|
||||
return CompletableFuture.completedFuture(list);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ public class PlayerArgumentType implements ArgumentType<CommandSource, Player> {
|
|||
@Override
|
||||
public Player parseInput(String string) {
|
||||
var player = server.getPlayer(string).orElse(null);
|
||||
if (player == null) throw new IllegalArgumentException("Player not online -- " + string);
|
||||
if (player == null) throw new IllegalArgumentException("Player not online");
|
||||
return player;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue