From bdbd079a1476b6a4096a427645920281506268fb Mon Sep 17 00:00:00 2001 From: kento2 Date: Tue, 2 Jun 2026 21:34:53 +0200 Subject: [PATCH] going to rename --- README.md | 2 - .../kentoj/scrow/bukkit/CoreImplPlugin.java | 2 + .../scrow/bukkit/server/LobbyCommand.java | 26 ++++++++ core-velocity/build.gradle.kts | 6 ++ .../scrow/corevelocity/CoreVelocity.java | 16 ++++- .../corevelocity/command/OnlineCommand.java | 27 +++++++++ .../corevelocity/privmsg/LastTargetCache.java | 24 ++++++++ .../corevelocity/privmsg/MsgCommand.java | 59 +++++++++++++++++++ .../corevelocity/privmsg/PrivmsgHelper.java | 15 +++++ .../corevelocity/privmsg/ReplyCommand.java | 44 ++++++++++++++ .../corevelocity/privmsg/ReplyKenCommand.java | 10 ++++ 11 files changed, 228 insertions(+), 3 deletions(-) create mode 100644 core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/server/LobbyCommand.java create mode 100644 core-velocity/src/main/java/de/kentoj/scrow/corevelocity/command/OnlineCommand.java create mode 100644 core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/LastTargetCache.java create mode 100644 core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/MsgCommand.java create mode 100644 core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/PrivmsgHelper.java create mode 100644 core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/ReplyCommand.java create mode 100644 core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/ReplyKenCommand.java diff --git a/README.md b/README.md index d88995e..ebae924 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ TODO - timeout/error handling with reactive -- unregister server on shutdown -- infrastructure scripts/glue code w/ daemontools - djb's multilogd for logging(?) environment diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/CoreImplPlugin.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/CoreImplPlugin.java index 3d52edc..56541b0 100644 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/CoreImplPlugin.java +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/CoreImplPlugin.java @@ -3,6 +3,7 @@ package de.kentoj.scrow.bukkit; import de.kentoj.scrow.bukkit.crown.CrownCommand; import de.kentoj.scrow.bukkit.economy.CoinsCommand; import de.kentoj.scrow.bukkit.friends.command.FriendCommand; +import de.kentoj.scrow.bukkit.server.LobbyCommand; import de.kentoj.scrow.bukkit.server.PlayCommand; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -20,6 +21,7 @@ public class CoreImplPlugin extends JavaPlugin { ScrowAPI.getCommandManager().register(new CoinsCommand().getRootNode()); ScrowAPI.getCommandManager().register(new CrownCommand().getRootNode()); ScrowAPI.getCommandManager().register(new PlayCommand().getRootNode()); + ScrowAPI.getCommandManager().register(new LobbyCommand().getRootNode()); } registerServer(); diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/server/LobbyCommand.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/server/LobbyCommand.java new file mode 100644 index 0000000..c8ef0f3 --- /dev/null +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/server/LobbyCommand.java @@ -0,0 +1,26 @@ +package de.kentoj.scrow.bukkit.server; + +import de.kentoj.kencommandapi.BukkitCommandContext; +import de.kentoj.kencommandapi.api.processing.CommandExecutor; +import de.kentoj.kencommandapi.api.structure.node.CommandNode; +import de.kentoj.scrow.bukkit.ScrowAPI; +import lombok.Getter; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +public class LobbyCommand implements CommandExecutor { + + @Getter + private final CommandNode rootNode; + + public LobbyCommand() { + var cmds = ScrowAPI.getCommandManager(); + this.rootNode = cmds.createNode("lobby", "l", "hub"); + this.rootNode.setExecutor(this); + } + + @Override + public void execute(BukkitCommandContext ctx) { + Bukkit.dispatchCommand(ctx.getSender(), "play lobby"); + } +} diff --git a/core-velocity/build.gradle.kts b/core-velocity/build.gradle.kts index 76b7842..31ddb44 100644 --- a/core-velocity/build.gradle.kts +++ b/core-velocity/build.gradle.kts @@ -19,5 +19,11 @@ dependencies { annotationProcessor("com.velocitypowered:velocity-api:3.5.0-SNAPSHOT") compileOnly("com.velocitypowered:velocity-api:3.5.0-SNAPSHOT") + // https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine + implementation("com.github.ben-manes.caffeine:caffeine:3.2.4") + + // http://forge.kentoj.de/scrow/-/packages/maven/de.kentoj.scrow:kencommandapi-velocity + implementation("de.kentoj.scrow:kencommandapi-velocity") + implementation(project(":core-lib")) } \ No newline at end of file diff --git a/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/CoreVelocity.java b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/CoreVelocity.java index cae6960..7ca61ba 100644 --- a/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/CoreVelocity.java +++ b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/CoreVelocity.java @@ -1,10 +1,15 @@ package de.kentoj.scrow.corevelocity; import com.google.inject.Inject; +import com.velocitypowered.api.command.CommandManager; import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.proxy.ProxyServer; -import de.kentoj.scrowlib.servermanager.ServerManagerImpl; +import de.kentoj.scrow.corevelocity.command.OnlineCommand; +import de.kentoj.scrow.corevelocity.privmsg.LastTargetCache; +import de.kentoj.scrow.corevelocity.privmsg.MsgCommand; +import de.kentoj.scrow.corevelocity.privmsg.ReplyCommand; import de.kentoj.scrowlib.servermanager.ServerManager; +import de.kentoj.scrowlib.servermanager.ServerManagerImpl; import io.nats.client.Connection; import io.nats.client.Nats; import io.nats.client.Options; @@ -29,6 +34,15 @@ public class CoreVelocity { @Inject public CoreVelocity(ProxyServer server) { this.server = server; + var cmds = server.getCommandManager(); + { + cmds.register(OnlineCommand.commandMeta(cmds), new OnlineCommand(server)); + } + { + var lastTargetCache = new LastTargetCache(); + cmds.register(MsgCommand.commandMeta(cmds), new MsgCommand(lastTargetCache, server)); + cmds.register(ReplyCommand.commandMeta(cmds), new ReplyCommand(lastTargetCache, server)); + } try { var natsHost = System.getenv("HOST_NATS"); diff --git a/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/command/OnlineCommand.java b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/command/OnlineCommand.java new file mode 100644 index 0000000..cc0b8e3 --- /dev/null +++ b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/command/OnlineCommand.java @@ -0,0 +1,27 @@ +package de.kentoj.scrow.corevelocity.command; + +import com.velocitypowered.api.command.CommandManager; +import com.velocitypowered.api.command.CommandMeta; +import com.velocitypowered.api.command.SimpleCommand; +import com.velocitypowered.api.proxy.ProxyServer; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.Component; + +@RequiredArgsConstructor +public class OnlineCommand implements SimpleCommand { + + private final ProxyServer server; + + @Override + public void execute(Invocation invocation) { + int cnt = server.getPlayerCount(); + var msg = cnt != 1 + ? "There are currently " + cnt + " players online" + : "There is currently 1 player online"; + invocation.source().sendMessage(Component.text(msg)); + } + + public static CommandMeta commandMeta(CommandManager cmds) { + return cmds.metaBuilder("online").build(); + } +} diff --git a/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/LastTargetCache.java b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/LastTargetCache.java new file mode 100644 index 0000000..0ba877d --- /dev/null +++ b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/LastTargetCache.java @@ -0,0 +1,24 @@ +package de.kentoj.scrow.corevelocity.privmsg; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +public class LastTargetCache { + + private final Cache<@NotNull UUID, UUID> cache = Caffeine.newBuilder() + .expireAfterWrite(20, TimeUnit.MINUTES) + .build(); + + public @Nullable UUID getLastTarget(UUID uuid) { + return cache.getIfPresent(uuid); + } + + public void setLastTarget(UUID uuid, UUID target) { + cache.put(uuid, target); + } +} diff --git a/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/MsgCommand.java b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/MsgCommand.java new file mode 100644 index 0000000..b1158d6 --- /dev/null +++ b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/MsgCommand.java @@ -0,0 +1,59 @@ +package de.kentoj.scrow.corevelocity.privmsg; + +import com.velocitypowered.api.command.CommandManager; +import com.velocitypowered.api.command.CommandMeta; +import com.velocitypowered.api.command.SimpleCommand; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.Arrays; +import java.util.stream.Collectors; + +@RequiredArgsConstructor +public class MsgCommand implements SimpleCommand { + + private final LastTargetCache cache; + private final ProxyServer server; + + @Override + public void execute(Invocation invocation) { + if (!(invocation.source() instanceof Player player)) return; + + var targetName = invocation.arguments()[0]; + if (invocation.arguments().length < 2) { + player.sendMessage(Component.text("usage: ./msg ")); + return; + NamedTextColor + } + + var target = server.getPlayer(targetName).orElse(null); + if (target == null) { + player.sendMessage(Component.text("The player you last messaged is now offline.")); + return; + } + + if (target.getUniqueId().equals(player.getUniqueId())) { + player.sendMessage(Component.text("You can't message yourself.")); + return; + } + + var msg = Arrays.stream(invocation.arguments()) + .skip(1) + .collect(Collectors.joining(" ")); + cache.setLastTarget(player.getUniqueId(), target.getUniqueId()); + PrivmsgHelper.handle(player, target, msg); + } + + + @Override + public boolean hasPermission(Invocation invocation) { + return invocation.source().hasPermission("corevelocity.cmd.msg"); + } + + public static CommandMeta commandMeta(CommandManager cmds) { + return cmds.metaBuilder("msg").aliases("w").build(); + } +} diff --git a/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/PrivmsgHelper.java b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/PrivmsgHelper.java new file mode 100644 index 0000000..7c60151 --- /dev/null +++ b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/PrivmsgHelper.java @@ -0,0 +1,15 @@ +package de.kentoj.scrow.corevelocity.privmsg; + +import com.velocitypowered.api.proxy.Player; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import net.kyori.adventure.text.Component; + +@NoArgsConstructor(access = AccessLevel.NONE) +public class PrivmsgHelper { + + public static void handle(Player player, Player target, String msg) { + target.sendMessage(Component.text("[PRIVMSG] " + player.getUsername() + " -> You: " + msg)); + player.sendMessage(Component.text("[PRIVMSG] " + "you -> " + player.getUsername() + ": " + msg)); + } +} diff --git a/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/ReplyCommand.java b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/ReplyCommand.java new file mode 100644 index 0000000..b58d481 --- /dev/null +++ b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/ReplyCommand.java @@ -0,0 +1,44 @@ +package de.kentoj.scrow.corevelocity.privmsg; + +import com.velocitypowered.api.command.CommandManager; +import com.velocitypowered.api.command.CommandMeta; +import com.velocitypowered.api.command.RawCommand; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.Component; + +@RequiredArgsConstructor +public class ReplyCommand implements RawCommand { + + private final LastTargetCache cache; + private final ProxyServer server; + + @Override + public void execute(Invocation invocation) { + if (!(invocation.source() instanceof Player player)) return; + var targetId = cache.getLastTarget(player.getUniqueId()); + if (targetId == null) { + player.sendMessage(Component.text("You haven't msg'd anyone recently. Use '/msg ' first.")); + return; + } + + var target = server.getPlayer(targetId).orElse(null); + if (target == null) { + player.sendMessage(Component.text("The player you last messaged is now offline.")); + return; + } + + cache.setLastTarget(player.getUniqueId(), target.getUniqueId()); + PrivmsgHelper.handle(player, target, invocation.arguments()); + } + + @Override + public boolean hasPermission(Invocation invocation) { + return invocation.source().hasPermission("corevelocity.cmd.msg"); + } + + public static CommandMeta commandMeta(CommandManager cmds) { + return cmds.metaBuilder("reply").aliases("r").build(); + } +} diff --git a/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/ReplyKenCommand.java b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/ReplyKenCommand.java new file mode 100644 index 0000000..83db736 --- /dev/null +++ b/core-velocity/src/main/java/de/kentoj/scrow/corevelocity/privmsg/ReplyKenCommand.java @@ -0,0 +1,10 @@ +package de.kentoj.scrow.corevelocity.privmsg; + +import com.velocitypowered.api.proxy.ProxyServer; + +public class ReplyKenCommand { + + public ReplyKenCommand(ProxyServer server) { + + } +}