This commit is contained in:
kento2 2026-06-29 18:49:42 +02:00
parent 254af80fde
commit 72f7625b19
53 changed files with 679 additions and 756 deletions

View file

@ -1,13 +1,12 @@
plugins {
id("java")
id("java-library")
id("maven-publish")
id("de.kentoj.scrow.scrow-repository")
id("de.kentoj.scrow.base-dependencies")
id("de.kentoj.scrow.java-library")
}
group = "de.kentoj.scrow"
version = rootProject.version
repositories {
mavenCentral()
maven {
name = "papermc"
url = uri("https://repo.papermc.io/repository/maven-public/")
@ -20,4 +19,15 @@ dependencies {
compileOnly("com.velocitypowered:velocity-api:3.5.0-SNAPSHOT")
api(project(":kencommandapi-core"))
}
}
publishing {
publications {
create<MavenPublication>("KenCommandAPI") {
groupId = rootProject.group.toString()
version = rootProject.version.toString()
artifactId = project.name
from(components["java"])
}
}
}

View file

@ -0,0 +1,24 @@
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 lombok.RequiredArgsConstructor;
import java.util.Arrays;
@RequiredArgsConstructor
public class CommandAPI {
private final ProxyServer server;
private final CommandParser<CommandSource> commandParser = new CommandParser<>(CommandSource::hasPermission);
public void register(CommandNode<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));
}
}

View file

@ -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());
}
}

View file

@ -1,22 +0,0 @@
package de.kentoj.kencommandapi;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
import de.kentoj.kencommandapi.impl.processing.CommandContextImpl;
import java.util.Map;
public class VelocityCommandContext extends CommandContextImpl<CommandSource, VelocityCommandContext> {
VelocityCommandContext(
CommandSource sender,
Map<String, ParsedArgument<CommandSource, VelocityCommandContext, ?>> parsedArguments
) {
super(sender, parsedArguments);
}
public Player getPlayerSender() {
return (Player) getSender();
}
}

View file

@ -1,44 +0,0 @@
package de.kentoj.kencommandapi;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.api.nodes.CommandNode;
import de.kentoj.kencommandapi.api.processing.CommandException;
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
import de.kentoj.kencommandapi.impl.AbstractKenCommandApi;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.Arrays;
import java.util.Map;
@RequiredArgsConstructor
public class VelocityKenCommandApi extends AbstractKenCommandApi<CommandSource, VelocityCommandContext> {
private final ProxyServer server;
@Override
public VelocityCommandContext createContext(CommandSource sender, Map<String, ParsedArgument<CommandSource, VelocityCommandContext, ?>> parsedArguments) {
return new VelocityCommandContext(sender, parsedArguments);
}
@Override
public void sendErrorMessage(CommandSource sender, CommandException ex) {
var cause = (ex.getCause() != null) ? ": " + ex.getCause().getMessage() : "";
sender.sendMessage(Component.text(ex.getMessage() + cause).color(NamedTextColor.RED));
}
@Override
public boolean hasPermission(CommandSource commandSource, String permission) {
return commandSource.hasPermission(permission);
}
public void register(CommandNode<CommandSource, VelocityCommandContext> 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(this, rootNode));
}
}

View file

@ -3,8 +3,8 @@ 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.VelocityCommandContext;
import de.kentoj.kencommandapi.api.argument.SuggestionProvider;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.suggestion.SuggestionProvider;
import lombok.RequiredArgsConstructor;
import java.util.Set;
@ -12,12 +12,12 @@ import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@RequiredArgsConstructor
public class PlayerSuggestionProvider implements SuggestionProvider<CommandSource, VelocityCommandContext> {
public class PlayerSuggestionProvider implements SuggestionProvider<CommandSource> {
private final ProxyServer proxyServer;
@Override
public CompletableFuture<Set<String>> suggest(VelocityCommandContext context) {
public CompletableFuture<Set<String>> suggest(CommandContext<CommandSource> context) {
var list = proxyServer.getAllPlayers().stream()
.map(Player::getUsername)
.collect(Collectors.toSet());

View file

@ -3,27 +3,25 @@ package de.kentoj.kencommandapi.type;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.VelocityCommandContext;
import de.kentoj.kencommandapi.api.argument.ArgumentType;
import de.kentoj.kencommandapi.api.argument.SuggestionProvider;
import de.kentoj.kencommandapi.api.processing.CommandException;
import de.kentoj.kencommandapi.api.suggestion.SuggestionProvider;
import de.kentoj.kencommandapi.suggestionprovider.PlayerSuggestionProvider;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class PlayerArgumentType implements ArgumentType<CommandSource, VelocityCommandContext, Player> {
public class PlayerArgumentType implements ArgumentType<CommandSource, Player> {
private final ProxyServer server;
@Override
public Player parseInput(String string) {
var player = server.getPlayer(string).orElse(null);
if (player == null) throw new CommandException("Player not online -- " + string);
if (player == null) throw new IllegalArgumentException("Player not online -- " + string);
return player;
}
@Override
public SuggestionProvider<CommandSource, VelocityCommandContext> defaultSuggestionProvider() {
public SuggestionProvider<CommandSource> defaultSuggestionProvider() {
return new PlayerSuggestionProvider(server);
}
}