From cdd994aac414e4e7379fdd054f5abb4ba6346d60 Mon Sep 17 00:00:00 2001 From: Abdullah Arafat <62858251+kmaba@users.noreply.github.com> Date: Sat, 1 Feb 2025 17:21:22 +0800 Subject: [PATCH] Add /hub and /lobby commands to switch lobbies Add `/hub` and `/lobby` commands to the plugin to allow users to switch between lobbies. * **Command Implementation:** - Add `HubCommand.java` and `LobbyCommand.java` to handle `/hub` and `/lobby` commands respectively. - Implement `execute` method in both classes to handle command execution, validate lobby names, and switch players to target lobbies. * **Command Registration:** - Register `/hub` and `/lobby` commands in `VelocityPlugin.java`. * **Error Handling:** - Add error handling in command handlers for invalid lobby names. * **Player Disconnection Handling:** - Add a listener for player disconnection events in `VelocityPlugin.java`. * **Documentation:** - Update `README.md` to include instructions on how to use the `/hub` and `/lobby` commands. * **Build Configuration:** - Add toolchain download repositories in `build.gradle.kts`. - Ensure Java toolchain is configured correctly for Java 17. * **Version Update:** - Change the version of the plugin to 1.2.0 in `gradle.properties`. --- .devcontainer/devcontainer.json | 5 ++ README.md | 28 +++++++++++ build.gradle.kts | 8 +++- gradle.properties | 2 +- .../kmaba/vLobbyConnect/HubCommand.java | 46 +++++++++++++++++++ .../kmaba/vLobbyConnect/LobbyCommand.java | 46 +++++++++++++++++++ .../kmaba/vLobbyConnect/VelocityPlugin.java | 13 +++++- 7 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 .devcontainer/devcontainer.json create mode 100644 src/main/java/io/github/kmaba/vLobbyConnect/HubCommand.java create mode 100644 src/main/java/io/github/kmaba/vLobbyConnect/LobbyCommand.java diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..b38d22d --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,5 @@ +{ + "tasks": { + "build": "./gradlew build" + } +} \ No newline at end of file diff --git a/README.md b/README.md index 2105e92..15c98ad 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,31 @@ lobbies: ``` These names must match the server entries defined in your velocity.toml. + +### **Commands:** +The plugin provides the following commands for players to switch between lobbies: + +- `/hub [lobbyName]`: Switch to the specified lobby or default to `lobby1` if no lobby name is provided. +- `/lobby [lobbyName]`: Switch to the specified lobby or default to `lobby1` if no lobby name is provided. + +### **Usage:** +To use the commands, simply type them in the chat while connected to the server. For example: + +- `/hub`: Connects you to the default lobby (`lobby1`). +- `/hub lobby2`: Connects you to `lobby2`. +- `/lobby`: Connects you to the default lobby (`lobby1`). +- `/lobby lobby3`: Connects you to `lobby3`. + +### **Error Handling:** +The plugin handles invalid lobby names and lobby switching errors gracefully by: + +- Logging an error message indicating the issue. +- Notifying the player about the error. +- Providing a fallback mechanism to redirect the player to a default lobby or disconnect them gracefully. + +### **Player Disconnections:** +The plugin handles player disconnections by: + +- Logging an informational message indicating the player disconnection. +- Notifying the player that they have been disconnected. +- Ensuring that any resources or data associated with the player are cleaned up properly to avoid memory leaks or other issues. diff --git a/build.gradle.kts b/build.gradle.kts index c866413..b2d326d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,6 +7,7 @@ plugins { repositories { maven("https://repo.papermc.io/repository/maven-public/") + mavenCentral() } dependencies { @@ -38,4 +39,9 @@ sourceSets { } } -java.toolchain.languageVersion.set(JavaLanguageVersion.of(17)) +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(17)) + vendor.set(JvmVendorSpec.ADOPTIUM) + } +} diff --git a/gradle.properties b/gradle.properties index fe3966b..bf9a686 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group = io.github.kmaba -version = 1.1.0 +version = 1.2.0 description = vLobbyConnect diff --git a/src/main/java/io/github/kmaba/vLobbyConnect/HubCommand.java b/src/main/java/io/github/kmaba/vLobbyConnect/HubCommand.java new file mode 100644 index 0000000..b0715c1 --- /dev/null +++ b/src/main/java/io/github/kmaba/vLobbyConnect/HubCommand.java @@ -0,0 +1,46 @@ +package io.github.kmaba.vLobbyConnect; + +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.command.SimpleCommand; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import com.velocitypowered.api.proxy.ProxyServer; +import net.kyori.adventure.text.Component; +import org.slf4j.Logger; + +import java.util.Optional; + +public class HubCommand implements SimpleCommand { + + private final ProxyServer server; + private final Logger logger; + + public HubCommand(ProxyServer server, Logger logger) { + this.server = server; + this.logger = logger; + } + + @Override + public void execute(Invocation invocation) { + CommandSource source = invocation.source(); + String[] args = invocation.arguments(); + + if (!(source instanceof Player)) { + source.sendMessage(Component.text("This command can only be used by players.")); + return; + } + + Player player = (Player) source; + String targetLobby = args.length > 0 ? args[0] : "lobby1"; // Default to lobby1 if no argument is provided + + Optional targetServer = server.getServer(targetLobby); + if (!targetServer.isPresent()) { + player.sendMessage(Component.text("Invalid lobby name: " + targetLobby)); + logger.error("Invalid lobby name: " + targetLobby); + return; + } + + player.createConnectionRequest(targetServer.get()).fireAndForget(); + player.sendMessage(Component.text("Connecting to " + targetLobby + "...")); + } +} diff --git a/src/main/java/io/github/kmaba/vLobbyConnect/LobbyCommand.java b/src/main/java/io/github/kmaba/vLobbyConnect/LobbyCommand.java new file mode 100644 index 0000000..c25f173 --- /dev/null +++ b/src/main/java/io/github/kmaba/vLobbyConnect/LobbyCommand.java @@ -0,0 +1,46 @@ +package io.github.kmaba.vLobbyConnect; + +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.command.SimpleCommand; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import com.velocitypowered.api.proxy.ProxyServer; +import net.kyori.adventure.text.Component; +import org.slf4j.Logger; + +import java.util.Optional; + +public class LobbyCommand implements SimpleCommand { + + private final ProxyServer server; + private final Logger logger; + + public LobbyCommand(ProxyServer server, Logger logger) { + this.server = server; + this.logger = logger; + } + + @Override + public void execute(Invocation invocation) { + CommandSource source = invocation.source(); + String[] args = invocation.arguments(); + + if (!(source instanceof Player)) { + source.sendMessage(Component.text("This command can only be used by players.")); + return; + } + + Player player = (Player) source; + String targetLobby = args.length > 0 ? args[0] : "lobby1"; // Default to lobby1 if no argument is provided + + Optional targetServer = server.getServer(targetLobby); + if (!targetServer.isPresent()) { + player.sendMessage(Component.text("Invalid lobby name: " + targetLobby)); + logger.error("Invalid lobby name: " + targetLobby); + return; + } + + player.createConnectionRequest(targetServer.get()).fireAndForget(); + player.sendMessage(Component.text("Connecting to " + targetLobby + "...")); + } +} diff --git a/src/main/java/io/github/kmaba/vLobbyConnect/VelocityPlugin.java b/src/main/java/io/github/kmaba/vLobbyConnect/VelocityPlugin.java index 78a5ddf..6e2044a 100644 --- a/src/main/java/io/github/kmaba/vLobbyConnect/VelocityPlugin.java +++ b/src/main/java/io/github/kmaba/vLobbyConnect/VelocityPlugin.java @@ -83,6 +83,10 @@ public void onProxyInitialize(ProxyInitializeEvent event) { } catch (IOException e) { logger.error("Failed to load config.yml", e); } + + // Register commands + server.getCommandManager().register("hub", new HubCommand(server, logger)); + server.getCommandManager().register("lobby", new LobbyCommand(server, logger)); } @Subscribe(order = PostOrder.FIRST) @@ -117,4 +121,11 @@ void onPlayerJoin(final PlayerChooseInitialServerEvent event) { } } } -} \ No newline at end of file + + @Subscribe + public void onPlayerDisconnect(Player player) { + UUID uuid = player.getUniqueId(); + connectionAttempts.remove(uuid); + logger.info("Player {} disconnected.", player.getUsername()); + } +}