feat: Add server switch broadcast messages

LoVelocity:
- Track server switches (player moving between servers)
- Send plugin messages to hub about switches
- Log server switches

LoHub:
- Receive server switch messages via plugin messaging
- Broadcast to hub chat when player switches servers
- Configurable server display names
- Message format: 'Player connected to Server'

Config:
- server_switch_messages.enabled
- server_switch_messages.switch_message
- server_switch_messages.server_names (hub, grief, survival, etc)

Example: 'Player connected to Grief' when switching from hub to grief server
This commit is contained in:
loki5512344 2026-02-24 14:03:49 +01:00
parent 3ba41c18e7
commit 6e99cf9390
4 changed files with 135 additions and 4 deletions

View file

@ -1,14 +1,21 @@
package com.loki.lovelocity.listeners;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.loki.lovelocity.LoVelocity;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Optional;
public class ServerSwitchListener {
private final LoVelocity plugin;
private static final String CHANNEL = "lohub:switch";
public ServerSwitchListener(LoVelocity plugin) {
this.plugin = plugin;
@ -16,13 +23,40 @@ public class ServerSwitchListener {
@Subscribe(order = PostOrder.LAST)
public void onServerConnected(ServerConnectedEvent event) {
// Velocity doesn't show join/leave messages by default
// They are handled by backend servers (Paper/Spigot)
Player player = event.getPlayer();
RegisteredServer toServer = event.getServer();
Optional<RegisteredServer> previousServer = event.getPreviousServer();
// If player is switching from another server (not first join)
if (previousServer.isPresent()) {
String fromServerName = previousServer.get().getServerInfo().getName();
String toServerName = toServer.getServerInfo().getName();
// Send plugin message to hub server about server switch
sendServerSwitchMessage(player, fromServerName, toServerName);
plugin.getLogger().info(player.getUsername() + " switched from " + fromServerName + " to " + toServerName);
}
}
@Subscribe(order = PostOrder.LAST)
public void onDisconnect(DisconnectEvent event) {
// Velocity doesn't show join/leave messages by default
// They are handled by backend servers (Paper/Spigot)
// Player left the network - handled by backend server
}
private void sendServerSwitchMessage(Player player, String fromServer, String toServer) {
// Create plugin message
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("ServerSwitch");
out.writeUTF(player.getUsername());
out.writeUTF(fromServer);
out.writeUTF(toServer);
byte[] data = out.toByteArray();
// Send to hub server (assuming hub server name is "hub")
plugin.getServer().getServer("hub").ifPresent(hubServer -> {
hubServer.sendPluginMessage(() -> CHANNEL, data);
});
}
}