This commit is contained in:
kento2 2026-06-02 23:48:07 +02:00
parent c2aec2842a
commit 5cec83b992
34 changed files with 269 additions and 58 deletions

View file

@ -0,0 +1,23 @@
plugins {
id("java")
id("java-library")
}
group = "de.kentoj.scrow"
version = ""
repositories {
mavenCentral()
maven {
name = "papermc"
url = uri("https://repo.papermc.io/repository/maven-public/")
}
}
dependencies {
// https://docs.papermc.io/velocity/dev/creating-your-first-plugin/
annotationProcessor("com.velocitypowered:velocity-api:3.5.0-SNAPSHOT")
compileOnly("com.velocitypowered:velocity-api:3.5.0-SNAPSHOT")
api(project(":kencommandapi-core"))
}

View file

@ -0,0 +1,23 @@
package de.kentoj.kencommandapi;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import de.kentoj.kencommandapi.api.nodes.CommandNode;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class VelocityCommand implements SimpleCommand {
private final VelocityKenCommandApi commandApi;
private final CommandNode<CommandSource, VelocityCommandContext> rootNode;
@Override
public void execute(Invocation ctx) {
commandApi.invoke(rootNode, ctx.source(), ctx.arguments());
}
@Override
public boolean hasPermission(Invocation ctx) {
return commandApi.hasPermission(ctx.source(), rootNode.getPermission());
}
}

View file

@ -0,0 +1,22 @@
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

@ -0,0 +1,43 @@
package de.kentoj.kencommandapi;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.api.processing.CommandException;
import de.kentoj.kencommandapi.api.processing.ParsedArgument;
import de.kentoj.kencommandapi.api.nodes.CommandNode;
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.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(rootNode.getNames())
.build();
cm.register(meta, new VelocityCommand(this, rootNode));
}
}

View file

@ -0,0 +1,26 @@
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 lombok.RequiredArgsConstructor;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@RequiredArgsConstructor
public class PlayerSuggestionProvider implements SuggestionProvider<CommandSource, VelocityCommandContext> {
private final ProxyServer proxyServer;
@Override
public CompletableFuture<Set<String>> suggest(VelocityCommandContext context) {
var list = proxyServer.getAllPlayers().stream()
.map(Player::getUsername)
.collect(Collectors.toSet());
return CompletableFuture.completedFuture(list);
}
}

View file

@ -0,0 +1,29 @@
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.suggestionprovider.PlayerSuggestionProvider;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class PlayerArgumentType implements ArgumentType<CommandSource, VelocityCommandContext, 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);
return player;
}
@Override
public SuggestionProvider<CommandSource, VelocityCommandContext> defaultSuggestionProvider() {
return new PlayerSuggestionProvider(server);
}
}