This commit is contained in:
kento2 2026-08-06 22:05:43 +02:00
parent 2ea2eb5d1e
commit ad2717b6d2
48 changed files with 255 additions and 252 deletions

View file

@ -9,7 +9,6 @@ java_library(
deps = [
"//core-lib:core",
artifact("com.velocitypowered:velocity-api"),
artifact("org.projectlombok:lombok"),
artifact("de.kentoj.scrow:kencommandapi-velocity"),
],
exports = [
@ -17,18 +16,16 @@ java_library(
artifact("de.kentoj.scrow:kencommandapi-velocity"),
artifact("org.mongodb:bson"),
],
plugins = [ "//third_party:lombok_plugin" ],
)
java_binary(
name = "plugin",
srcs = glob(["plugin/main/**/*.java"]),
main_class = "none",
resources = glob(["plugin/main/resources/**"], allow_empty = True),
create_executable = False,
deps = [
":api",
artifact("com.velocitypowered:velocity-api"),
artifact("com.google.inject:guice"),
artifact("io.nats:jnats"),
],
plugins = [ "//third_party:lombok_plugin" ],
)

View file

@ -7,26 +7,17 @@ import de.kentoj.scrowlib.instancemanager.InstanceManager;
import de.kentoj.scrowlib.messaging.MessageBroker;
import de.kentoj.scrowlib.player.NetworkPlayer;
import de.kentoj.scrowlib.player.NetworkPlayerFactory;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.jdbi.v3.core.Jdbi;
import org.jetbrains.annotations.ApiStatus;
import java.util.UUID;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ScrowAPI {
@Getter
private static Jdbi jdbi;
@Getter
private static CommandAPI<CommandSource> commands;
@Getter
private static CommandAPI<Player> playerCommands;
@Getter
private static MessageBroker messageBroker;
@Getter
private static InstanceManager instanceManager;
private static NetworkPlayerFactory playerFactory;
@ -39,33 +30,56 @@ public class ScrowAPI {
}
@ApiStatus.Internal
public static void setMessageBroker(MessageBroker messageBroker) {
public static void initMessageBroker(MessageBroker messageBroker) {
ScrowAPI.messageBroker = messageBroker;
}
@ApiStatus.Internal
public static void setInstanceManager(InstanceManager instanceManager) {
public static void initInstanceManager(InstanceManager instanceManager) {
ScrowAPI.instanceManager = instanceManager;
}
@ApiStatus.Internal
public static void setJdbi(Jdbi jdbi) {
public static void initJdbi(Jdbi jdbi) {
ScrowAPI.jdbi = jdbi;
}
@ApiStatus.Internal
public static void setPlayerFactory(NetworkPlayerFactory playerFactory) {
public static void initPlayerFactory(NetworkPlayerFactory playerFactory) {
ScrowAPI.playerFactory = playerFactory;
}
@ApiStatus.Internal
public static void setPlayerCommands(CommandAPI<Player> playerCommands) {
public static void initPlayerCommands(CommandAPI<Player> playerCommands) {
ScrowAPI.playerCommands = playerCommands;
}
@ApiStatus.Internal
public static void setCommands(CommandAPI<CommandSource> commands) {
public static void initCommands(CommandAPI<CommandSource> commands) {
ScrowAPI.commands = commands;
}
}
public static Jdbi jdbi() {
return jdbi;
}
public static CommandAPI<CommandSource> commands() {
return commands;
}
public static CommandAPI<Player> playerCommands() {
return playerCommands;
}
public static MessageBroker messageBroker() {
return messageBroker;
}
public static InstanceManager instanceManager() {
return instanceManager;
}
public static NetworkPlayerFactory playerFactory() {
return playerFactory;
}
}

View file

@ -13,18 +13,15 @@ import de.kentoj.scrow.corevelocity.privmsg.LastTargetCache;
import de.kentoj.scrow.corevelocity.privmsg.MsgCommand;
import de.kentoj.scrow.corevelocity.privmsg.PrivMsgHandler;
import de.kentoj.scrow.corevelocity.privmsg.ReplyCommand;
import de.kentoj.scrow.generated.BuildInfo;
import de.kentoj.scrow.velocity.ScrowAPI;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.format.NamedTextColor;
@Slf4j
@Plugin(
id = "corevelocity",
name = "CoreVelocity",
authors = {"gradlehater57"},
version = BuildInfo.VERSION // may not resolve until building for the first time
version = "0"
)
public final class CoreVelocityPlugin {
@ -35,13 +32,12 @@ public final class CoreVelocityPlugin {
private void onInit(ProxyInitializeEvent __) {
ScrowAPISurface.initScrowAPI(server);
var cmds = ScrowAPI.getCommandApi();
var lastTargetCache = new LastTargetCache();
var style = new ScrowMessageStyle("MSG", NamedTextColor.LIGHT_PURPLE);
var privmsgHelper = new PrivMsgHandler(style);
cmds.register(new ReplyCommand(server, lastTargetCache, privmsgHelper, style).getRootLiteral());
cmds.register(new MsgCommand(server, lastTargetCache, privmsgHelper, style).getRootLiteral());
cmds.register(new OnlineCommand(server).getRootLiteral());
ScrowAPI.playerCommands().register(new ReplyCommand(server, lastTargetCache, privmsgHelper, style).rootLiteral());
ScrowAPI.playerCommands().register(new MsgCommand(server, lastTargetCache, privmsgHelper, style).rootLiteral());
ScrowAPI.commands().register(new OnlineCommand(server).rootLiteral());
var registerWatchdog = new ServerRegisterWatchdog(server);
var sendPlayerWatchdog = new SendPlayerWatchdog(server);

View file

@ -1,5 +1,7 @@
package de.kentoj.scrow.corevelocity;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.CommandAPI;
import de.kentoj.scrow.velocity.ScrowAPI;
@ -9,20 +11,18 @@ import de.kentoj.scrowlib.messaging.NatsMessageBroker;
import de.kentoj.scrowlib.player.NetworkPlayerImpl;
import io.nats.client.Nats;
import io.nats.client.Options;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jdbi.v3.core.Jdbi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Properties;
@Slf4j
@NoArgsConstructor(access = AccessLevel.NONE)
public class ScrowAPISurface {
public static void initScrowAPI(ProxyServer server) {
private static final Logger log = LoggerFactory.getLogger(ScrowAPISurface.class);
public static void initScrowAPI(ProxyServer server) {
var natsHost = System.getenv("HOST_NATS");
if (natsHost != null) {
try {
@ -30,13 +30,13 @@ public class ScrowAPISurface {
.server(natsHost)
.pedantic()
.build();
ScrowAPI.setMessageBroker(new NatsMessageBroker(Nats.connect(options)));
ScrowAPI.initMessageBroker(new NatsMessageBroker(Nats.connect(options)));
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
} else {
log.error("HOST_NATS not set -> Using in-memory dummy message broker");
ScrowAPI.setMessageBroker(new InMemoyMessageBroker());
ScrowAPI.initMessageBroker(new InMemoyMessageBroker());
}
var hostPostgres = System.getenv().get("HOST_POSTGRES");
@ -45,7 +45,7 @@ public class ScrowAPISurface {
Class.forName("org.postgresql.Driver", true, ScrowAPISurface.class.getClassLoader());
var props = new Properties();
props.setProperty("user", "postgres");
ScrowAPI.setJdbi(Jdbi.create(hostPostgres, props));
ScrowAPI.initJdbi(Jdbi.create(hostPostgres, props));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
@ -53,9 +53,10 @@ public class ScrowAPISurface {
log.error("HOST_POSTGRES not set -> Using in-memory database");
}
ScrowAPI.setInstanceManager(new InstanceManagerImpl(ScrowAPI.getMessageBroker()));
ScrowAPI.setCommandApi(new CommandAPI(server));
ScrowAPI.setPlayerFactory(playerId -> new NetworkPlayerImpl(ScrowAPI.getMessageBroker(), playerId));
ScrowAPI.initInstanceManager(new InstanceManagerImpl(ScrowAPI.messageBroker()));
ScrowAPI.initCommands(new CommandAPI<>(server, CommandSource.class));
ScrowAPI.initPlayerCommands(new CommandAPI<>(server, Player.class));
ScrowAPI.initPlayerFactory(playerId -> new NetworkPlayerImpl(ScrowAPI.messageBroker(), playerId));
}
public static void destroyScrowAPI() {

View file

@ -9,30 +9,30 @@ import de.kentoj.kencommandapi.api.invocation.SyncCommandExecutor;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class OnlineCommand implements SyncCommandExecutor<CommandSource> {
public final class OnlineCommand {
@Getter
private final RootLiteral<CommandSource> rootLiteral;
private final ScrowMessageStyle style = ScrowMessageStyle.GENERIC;
private final ProxyServer server;
public OnlineCommand(ProxyServer server) {
this.server = server;
rootLiteral = Literal.rootLiteral(ScrowMessageStyle.GENERIC, "online");
rootLiteral.setExecutor(this);
rootLiteral = Literal.<CommandSource>builder("online")
.withSyncExecutor(this::executeSync)
.asRootLiteral(ScrowMessageStyle.GENERIC);
}
@Override
public Result<Object, String> executeSync(CommandContext<CommandSource> ctx) {
private Result<Object, String> executeSync(CommandContext<CommandSource> ctx) {
int cnt = server.getPlayerCount();
var msg = cnt != 1
? "There are currently " + cnt + " players online."
: "There is currently 1 player online.";
ctx.getSender().sendMessage(style.ok(msg));
ctx.sender().sendMessage(style.ok(msg));
return Results.success(new Object());
}
public RootLiteral<CommandSource> rootLiteral() {
return rootLiteral;
}
}

View file

@ -9,7 +9,7 @@ import org.bson.Document;
public class FriendNotifications {
private final MessageBroker messageBroker = ScrowAPI.getMessageBroker();
private final MessageBroker messageBroker = ScrowAPI.messageBroker();
@Subscribe
private void onConnect(PostLoginEvent ev) {

View file

@ -3,8 +3,6 @@ package de.kentoj.scrow.corevelocity.network;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.scrow.velocity.ScrowAPI;
import de.kentoj.scrowlib.messaging.MessageBroker;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import java.util.UUID;
@ -12,15 +10,17 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@Slf4j
@RequiredArgsConstructor
public class SendPlayerWatchdog {
private final ExecutorService EXECUTOR = Executors.newVirtualThreadPerTaskExecutor();
private final MessageBroker messageBroker = ScrowAPI.getMessageBroker();
private final MessageBroker messageBroker = ScrowAPI.messageBroker();
private final ProxyServer server;
public SendPlayerWatchdog(ProxyServer server) {
this.server = server;
}
public void listen() {
messageBroker.handle("player.send", doc -> sendPlayer(
doc.get("playerId", UUID.class),

View file

@ -4,31 +4,35 @@ import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import de.kentoj.scrow.velocity.ScrowAPI;
import de.kentoj.scrowlib.messaging.MessageBroker;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
@Log
@RequiredArgsConstructor
public class ServerRegisterWatchdog {
private final MessageBroker messageBroker = ScrowAPI.getMessageBroker();
private static final Logger log = LoggerFactory.getLogger(ServerRegisterWatchdog.class);
private final MessageBroker messageBroker = ScrowAPI.messageBroker();
private final ProxyServer server;
public ServerRegisterWatchdog(ProxyServer server) {
this.server = server;
}
public void listen() {
messageBroker.subscribe("event.instance.up", doc -> {
var handle = doc.getString("handle");
var port = doc.getInteger("port");
server.unregisterServer(new ServerInfo(handle, new InetSocketAddress(port)));
log.info("registered server " + handle);
log.info("registered server {}", handle);
});
messageBroker.subscribe("event.instance.down", doc -> {
var handle = doc.getString("handle");
var port = doc.getInteger("port");
server.unregisterServer(new ServerInfo(handle, new InetSocketAddress(port)));
log.info("unregistered server " + handle);
log.info("unregistered server {}", handle);
});
}
}

View file

@ -5,13 +5,13 @@ import com.google.common.cache.CacheBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class LastTargetCache {
private final Cache<@NotNull UUID, @NotNull UUID> cache = CacheBuilder.newBuilder()
.expireAfterAccess(20, TimeUnit.MINUTES)
.expireAfterAccess(Duration.ofMinutes(20))
.build();
public @Nullable UUID getLastTarget(UUID uuid) {

View file

@ -5,22 +5,19 @@ import com.leakyabstractions.result.core.Results;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.api.argument.CommandArgument;
import de.kentoj.kencommandapi.api.argument.CommandArgumentSpec;
import de.kentoj.kencommandapi.api.argument.types.StringArgumentType;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.invocation.SyncCommandExecutor;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.kencommandapi.type.PlayerArgumentType;
import lombok.Getter;
public class MsgCommand implements SyncCommandExecutor<CommandSource> {
public class MsgCommand {
@Getter
private final RootLiteral<CommandSource> rootLiteral;
private final CommandArgument<CommandSource, Player> targetArg;
private final CommandArgument<CommandSource, String> msgArg;
private final RootLiteral<Player> rootLiteral;
private final CommandArgumentSpec<Player, Player> targetArg;
private final CommandArgumentSpec<Player, String> msgArg;
private final LastTargetCache cache;
private final PrivMsgHandler privMsgHandler;
@ -29,26 +26,29 @@ public class MsgCommand implements SyncCommandExecutor<CommandSource> {
this.cache = cache;
this.privMsgHandler = helper;
targetArg = CommandArgument.arg("target", new PlayerArgumentType(server));
msgArg = CommandArgument.arg("msg", new StringArgumentType<>(true));
targetArg = CommandArgumentSpec.builder("target", new PlayerArgumentType<Player>(server)).build();
msgArg = CommandArgumentSpec.builder("msg", new StringArgumentType<Player>(true)).build();
rootLiteral = Literal.rootLiteral(style, "msg", "w", "privmsg");
rootLiteral.setExecutor(this);
rootLiteral.addArgument(targetArg);
rootLiteral.addArgument(msgArg);
rootLiteral = Literal.<Player>builder("msg", "w", "privmsg")
.withArgument(targetArg)
.withArgument(msgArg)
.withSyncExecutor(this::executeSync)
.asRootLiteral(style);
}
@Override
public Result<Object, String> executeSync(CommandContext<CommandSource> ctx) {
private Result<Object, String> executeSync(CommandContext<Player> ctx) {
var target = ctx.getArg(targetArg);
var msg = ctx.getArg(msgArg);
var sender = (Player) ctx.getSender();
var isMessagingSelf = target.getUniqueId().equals(sender.getUniqueId());
var isMessagingSelf = target.getUniqueId().equals(ctx.sender().getUniqueId());
if (isMessagingSelf) return Results.failure("You can't message yourself.");
cache.setLastTarget(sender.getUniqueId(), target.getUniqueId());
privMsgHandler.handle(sender, target, msg);
cache.setLastTarget(ctx.sender().getUniqueId(), target.getUniqueId());
privMsgHandler.handle(ctx.sender(), target, msg);
return Results.success(new Object());
}
public RootLiteral<Player> rootLiteral() {
return rootLiteral;
}
}

View file

@ -2,16 +2,18 @@ package de.kentoj.scrow.corevelocity.privmsg;
import com.velocitypowered.api.proxy.Player;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import static net.kyori.adventure.text.Component.text;
@RequiredArgsConstructor
public class PrivMsgHandler {
private final ScrowMessageStyle style;
public PrivMsgHandler(ScrowMessageStyle style) {
this.style = style;
}
public void handle(Player player, Player target, String msg) {
target.sendMessage(style.ok(text(target.getUsername() + " -> You: ").append(Component.text(msg))));
player.sendMessage(style.ok(text("You -> " + target.getUsername() + ": ").append(Component.text(msg))));

View file

@ -5,20 +5,18 @@ import com.leakyabstractions.result.core.Results;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.api.argument.CommandArgument;
import de.kentoj.kencommandapi.api.argument.CommandArgumentSpec;
import de.kentoj.kencommandapi.api.argument.types.StringArgumentType;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.invocation.SyncCommandExecutor;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import lombok.Getter;
import de.kentoj.kencommandapi.type.PlayerArgumentType;
public class ReplyCommand implements SyncCommandExecutor<CommandSource> {
public class ReplyCommand {
@Getter
private final RootLiteral<CommandSource> rootLiteral;
private final CommandArgument<CommandSource, String> msgArg;
private final RootLiteral<Player> rootLiteral;
private final CommandArgumentSpec<Player, String> msgArg;
private final LastTargetCache cache;
private final ProxyServer server;
@ -29,16 +27,15 @@ public class ReplyCommand implements SyncCommandExecutor<CommandSource> {
this.server = server;
this.privMsgHandler = privMsgHandler;
msgArg = CommandArgument.arg("msg", new StringArgumentType<>(true));
rootLiteral = Literal.rootLiteral(style, "reply", "r");
rootLiteral.setExecutor(this);
rootLiteral.addArgument(msgArg);
msgArg = CommandArgumentSpec.builder("msg", new StringArgumentType<Player>(true)).build();
rootLiteral = Literal.<Player>builder("reply", "r")
.withArgument(msgArg)
.withSyncExecutor(this::executeSync)
.asRootLiteral(style);
}
@Override
public Result<Object, String> executeSync(CommandContext<CommandSource> ctx) {
var sender = (Player) ctx.getSender();
var targetId = cache.getLastTarget(sender.getUniqueId());
private Result<Object, String> executeSync(CommandContext<Player> ctx) {
var targetId = cache.getLastTarget(ctx.sender().getUniqueId());
if (targetId == null)
return Results.failure("You didn't messaged anyone recently. Use /msg first");
var target = server.getPlayer(targetId).orElse(null);
@ -46,7 +43,11 @@ public class ReplyCommand implements SyncCommandExecutor<CommandSource> {
if (target == null)
return Results.failure("The player you last messaged is no longer online.");
privMsgHandler.handle(sender, target, msg);
privMsgHandler.handle(ctx.sender(), target, msg);
return Results.success(new Object());
}
public RootLiteral<Player> rootLiteral() {
return rootLiteral;
}
}