refactor ig

This commit is contained in:
kento2 2026-08-04 02:30:41 +02:00
parent ef32f9dbc4
commit 336ac4cad5
54 changed files with 528 additions and 444 deletions

15
velocity/BUILD Normal file
View file

@ -0,0 +1,15 @@
load("@rules_jvm_external//:defs.bzl", "artifact", "java_export")
java_export(
name = "velocity",
maven_coordinates = "de.kentoj.scrow:kencommandapi-velocity:0.34-SNAPSHOT",
srcs = glob(["src/main/**/*.java"]),
deps = [
"//core",
artifact("org.jetbrains:annotations"),
artifact("com.velocitypowered:velocity-api"),
],
exports = [
"//core",
],
)

View file

@ -0,0 +1,28 @@
package de.kentoj.kencommandapi;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.api.CommandHandler;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import java.util.Arrays;
public class CommandAPI<T extends CommandSource> {
private final ProxyServer server;
private final Class<T> senderType;
private final CommandHandler<T> commandHandler = new CommandHandler<>(T::sendMessage, T::hasPermission);
public CommandAPI(ProxyServer server, Class<T> senderType) {
this.server = server;
this.senderType = senderType;
}
public void register(RootLiteral<T> rootNode) {
var cm = server.getCommandManager();
var meta = cm.metaBuilder(rootNode.names()[0])
.aliases(Arrays.copyOfRange(rootNode.names(), 1, rootNode.names().length))
.build();
cm.register(meta, new VelocityCommand<>(commandHandler, rootNode, senderType));
}
}

View file

@ -0,0 +1,53 @@
package de.kentoj.kencommandapi;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.RawCommand;
import de.kentoj.kencommandapi.api.CommandHandler;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class VelocityCommand<T extends CommandSource> implements RawCommand {
private final CommandHandler<T> commandHandler;
private final RootLiteral<T> rootNode;
private final Class<T> senderType;
public VelocityCommand(CommandHandler<T> commandHandler, RootLiteral<T> rootNode, Class<T> senderType) {
this.commandHandler = commandHandler;
this.rootNode = rootNode;
this.senderType = senderType;
}
@Override
public void execute(Invocation invocation) {
if (!senderType.isInstance(invocation.source())) {
invocation.source().sendMessage(MessageStyle.PLAIN.err("You can't use that command"));
return;
}
T sender = senderType.cast(invocation.source());
var args = Arrays.stream(invocation.arguments().split(" "))
.filter(a -> !a.isEmpty());
commandHandler.invoke(rootNode, sender, args);
}
@Override
public boolean hasPermission(Invocation ctx) {
if (rootNode.permission() == null) return true;
return ctx.source().hasPermission(rootNode.permission());
}
@Override
public List<String> suggest(Invocation invocation) {
if (!senderType.isInstance(invocation.source()))
return Collections.emptyList();
T sender = senderType.cast(invocation.source());
var args = invocation.arguments().split(" ");
return commandHandler.getSuggestions(rootNode, sender, args, invocation.arguments().endsWith(" "));
}
}

View file

@ -0,0 +1,24 @@
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.suggestion.SuggestionProvider;
import java.util.List;
public final class PlayerSuggestionProvider implements SuggestionProvider<CommandSource> {
private final ProxyServer proxyServer;
public PlayerSuggestionProvider(ProxyServer proxyServer) {
this.proxyServer = proxyServer;
}
@Override
public List<String> suggest(CommandSource __) {
return proxyServer.getAllPlayers().stream()
.map(Player::getUsername)
.toList();
}
}

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.api.argument.ArgumentType;
import de.kentoj.kencommandapi.api.suggestion.SuggestionProvider;
import de.kentoj.kencommandapi.suggestionprovider.PlayerSuggestionProvider;
public class PlayerArgumentType implements ArgumentType<CommandSource, Player> {
private final ProxyServer server;
public PlayerArgumentType(ProxyServer server) {
this.server = server;
}
@Override
public Player parseInputUnchecked(String input) {
var player = server.getPlayer(input).orElse(null);
if (player == null) throw new IllegalArgumentException("Player not online");
return player;
}
@Override
public SuggestionProvider<CommandSource> getDefaultSuggestionProvider() {
return new PlayerSuggestionProvider(server);
}
}