implement velocity platform
Some checks failed
/ deploy (push) Has been cancelled
/ test (push) Has been cancelled

This commit is contained in:
kento2 2026-08-25 19:15:27 +02:00
parent fb2f1f1859
commit 1b1a496ab1
9 changed files with 1984 additions and 25 deletions

23
velocity/BUILD.bazel Normal file
View file

@ -0,0 +1,23 @@
load("@rules_jvm_external//:defs.bzl", "artifact", "java_export")
load("//:defs.bzl", "GROUP", "VERSION")
java_export(
name = "velocity",
maven_coordinates = GROUP + ":commands-velocity:" + VERSION,
srcs = glob(["site/lab0x13/scrow/commands/**/*.java"]),
visibility = ["//visibility:public"],
deps = [
"//common:common-lib",
artifact("org.jetbrains:annotations"),
artifact("com.google.guava:guava"),
artifact("net.kyori:adventure-api"),
artifact("com.leakyabstractions:result-api"),
artifact("com.leakyabstractions:result"),
artifact("com.velocitypowered:velocity-api"),
],
exports = [
"//common:common-lib",
artifact("com.leakyabstractions:result-api"),
artifact("com.leakyabstractions:result"),
]
)

View file

@ -0,0 +1,7 @@
import com.velocitypowered.api.command.CommandSource;
@FunctionalInterface
public interface CommandContextFactory<C extends VelocityCommandContext<C>> {
C createContext(CommandSource sender);
}

View file

@ -0,0 +1,40 @@
import com.velocitypowered.api.command.RawCommand;
import site.lab0x13.scrow.commands.CommandHandler;
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
import java.util.Arrays;
import java.util.List;
public class VelocityCommand<C extends VelocityCommandContext<C>> implements RawCommand {
private final CommandHandler<C> commandHandler;
private final CommandContextFactory<C> commandContextFactory;
private final RootLiteral<C> rootLiteral;
public VelocityCommand(CommandHandler<C> commandHandler, CommandContextFactory<C> commandContextFactory, RootLiteral<C> rootLiteral) {
this.commandHandler = commandHandler;
this.commandContextFactory = commandContextFactory;
this.rootLiteral = rootLiteral;
}
@Override
public void execute(Invocation invocation) {
var args = Arrays.stream(invocation.arguments().split(" "))
.filter(a -> !a.isEmpty());
var ctx = commandContextFactory.createContext(invocation.source());
commandHandler.invoke(rootLiteral, ctx, args);
}
@Override
public boolean hasPermission(Invocation ctx) {
if (rootLiteral.permission() == null) return true;
return ctx.source().hasPermission(rootLiteral.permission());
}
@Override
public List<String> suggest(Invocation invocation) {
var args = invocation.arguments().split(" ");
var ctx = commandContextFactory.createContext(invocation.source());
return commandHandler.suggest(rootLiteral, ctx, args, invocation.arguments().endsWith(" "));
}
}

View file

@ -0,0 +1,21 @@
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import site.lab0x13.scrow.commands.model.AbstractCommandContext;
import site.lab0x13.scrow.commands.model.CommandContext;
public class VelocityCommandContext<C extends CommandContext<C>> extends AbstractCommandContext<C> {
private final CommandSource sender;
public VelocityCommandContext(CommandSource sender) {
this.sender = sender;
}
public CommandSource sender() {
return sender;
}
public Player player() {
return (Player) sender();
}
}

View file

@ -0,0 +1,14 @@
import net.kyori.adventure.text.Component;
import site.lab0x13.scrow.commands.PlatformAdapter;
public class VelocityPlatform<C extends VelocityCommandContext<C>> implements PlatformAdapter<C> {
@Override
public boolean checkPermission(C ctx, String permission) {
return ctx.sender().hasPermission(permission);
}
@Override
public void sendMessage(C ctx, Component message) {
ctx.sender().sendMessage(message);
}
}