Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tasks": {
"build": "./gradlew build"
}
}
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 7 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {

repositories {
maven("https://repo.papermc.io/repository/maven-public/")
mavenCentral()
}

dependencies {
Expand Down Expand Up @@ -38,4 +39,9 @@ sourceSets {
}
}

java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
vendor.set(JvmVendorSpec.ADOPTIUM)
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group = io.github.kmaba
version = 1.1.0
version = 1.2.0
description = vLobbyConnect
46 changes: 46 additions & 0 deletions src/main/java/io/github/kmaba/vLobbyConnect/HubCommand.java
Original file line number Diff line number Diff line change
@@ -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<RegisteredServer> 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 + "..."));
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/github/kmaba/vLobbyConnect/LobbyCommand.java
Original file line number Diff line number Diff line change
@@ -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<RegisteredServer> 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 + "..."));
}
}
13 changes: 12 additions & 1 deletion src/main/java/io/github/kmaba/vLobbyConnect/VelocityPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -117,4 +121,11 @@ void onPlayerJoin(final PlayerChooseInitialServerEvent event) {
}
}
}
}

@Subscribe
public void onPlayerDisconnect(Player player) {
UUID uuid = player.getUniqueId();
connectionAttempts.remove(uuid);
logger.info("Player {} disconnected.", player.getUsername());
}
}
Loading