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:
parent
3ba41c18e7
commit
6e99cf9390
4 changed files with 135 additions and 4 deletions
|
|
@ -55,6 +55,7 @@ public class LoHub extends JavaPlugin {
|
||||||
|
|
||||||
registerListeners();
|
registerListeners();
|
||||||
registerCommands();
|
registerCommands();
|
||||||
|
registerPluginChannels();
|
||||||
|
|
||||||
regionManager.createHubRegion();
|
regionManager.createHubRegion();
|
||||||
|
|
||||||
|
|
@ -90,6 +91,15 @@ public class LoHub extends JavaPlugin {
|
||||||
getCommand("chatlock").setExecutor(new com.loki.lohub.commands.ChatLockCommand(this));
|
getCommand("chatlock").setExecutor(new com.loki.lohub.commands.ChatLockCommand(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void registerPluginChannels() {
|
||||||
|
getServer().getMessenger().registerIncomingPluginChannel(
|
||||||
|
this,
|
||||||
|
com.loki.lohub.listeners.ServerSwitchMessageListener.getChannel(),
|
||||||
|
new com.loki.lohub.listeners.ServerSwitchMessageListener(this)
|
||||||
|
);
|
||||||
|
getLogger().info("Plugin messaging channels registered");
|
||||||
|
}
|
||||||
|
|
||||||
public ConfigManager getConfigManager() {
|
public ConfigManager getConfigManager() {
|
||||||
return configManager;
|
return configManager;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
package com.loki.lohub.listeners;
|
||||||
|
|
||||||
|
import com.google.common.io.ByteArrayDataInput;
|
||||||
|
import com.google.common.io.ByteStreams;
|
||||||
|
import com.loki.lohub.LoHub;
|
||||||
|
import com.loki.lohub.utils.TextUtil;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class ServerSwitchMessageListener implements PluginMessageListener {
|
||||||
|
|
||||||
|
private final LoHub plugin;
|
||||||
|
private static final String CHANNEL = "lohub:switch";
|
||||||
|
|
||||||
|
public ServerSwitchMessageListener(LoHub plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte @NotNull [] message) {
|
||||||
|
if (!channel.equals(CHANNEL)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteArrayDataInput in = ByteStreams.newDataInput(message);
|
||||||
|
String subChannel = in.readUTF();
|
||||||
|
|
||||||
|
if (subChannel.equals("ServerSwitch")) {
|
||||||
|
String playerName = in.readUTF();
|
||||||
|
String fromServer = in.readUTF();
|
||||||
|
String toServer = in.readUTF();
|
||||||
|
|
||||||
|
handleServerSwitch(playerName, fromServer, toServer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleServerSwitch(String playerName, String fromServer, String toServer) {
|
||||||
|
if (!plugin.getConfig().getBoolean("server_switch_messages.enabled", true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String messageTemplate = plugin.getConfig().getString("server_switch_messages.switch_message",
|
||||||
|
"&#FFD700%player% &#AAAAAA connected to BCB77%server%");
|
||||||
|
|
||||||
|
String serverDisplayName = getServerDisplayName(toServer);
|
||||||
|
String formattedMessage = messageTemplate
|
||||||
|
.replace("%player%", playerName)
|
||||||
|
.replace("%server%", serverDisplayName);
|
||||||
|
|
||||||
|
Component component = Component.text(TextUtil.colorize(formattedMessage));
|
||||||
|
Bukkit.broadcast(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getServerDisplayName(String serverName) {
|
||||||
|
String path = "server_switch_messages.server_names." + serverName;
|
||||||
|
return plugin.getConfig().getString(path, serverName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getChannel() {
|
||||||
|
return CHANNEL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -166,6 +166,28 @@ join_leave_messages:
|
||||||
# Сообщение при выходе с сервера (не при переключении на другой сервер)
|
# Сообщение при выходе с сервера (не при переключении на другой сервер)
|
||||||
quit_message: "&#FF6B6B- &#FFD700%player% &#AAAAAAвышел"
|
quit_message: "&#FF6B6B- &#FFD700%player% &#AAAAAAвышел"
|
||||||
|
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
|
||||||
|
# | SERVER SWITCH MESSAGES |
|
||||||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
|
||||||
|
|
||||||
|
server_switch_messages:
|
||||||
|
# Показывать сообщения о переключении между серверами
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
# Шаблон сообщения при переключении
|
||||||
|
# %player% - имя игрока
|
||||||
|
# %server% - название сервера
|
||||||
|
switch_message: "&#FFD700%player% &#AAAAAAподключился к BCB77%server%"
|
||||||
|
|
||||||
|
# Отображаемые названия серверов
|
||||||
|
# Если сервер не указан, будет использовано его техническое имя
|
||||||
|
server_names:
|
||||||
|
hub: "Хабу"
|
||||||
|
grief: "Грифу"
|
||||||
|
survival: "Выживанию"
|
||||||
|
creative: "Креативу"
|
||||||
|
minigames: "Мини-играм"
|
||||||
|
|
||||||
join_events:
|
join_events:
|
||||||
- "[MESSAGE] &#FFD700╔═══════════════════════════════╗"
|
- "[MESSAGE] &#FFD700╔═══════════════════════════════╗"
|
||||||
- "[MESSAGE] &r"
|
- "[MESSAGE] &r"
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,21 @@
|
||||||
package com.loki.lovelocity.listeners;
|
package com.loki.lovelocity.listeners;
|
||||||
|
|
||||||
|
import com.google.common.io.ByteArrayDataOutput;
|
||||||
|
import com.google.common.io.ByteStreams;
|
||||||
import com.loki.lovelocity.LoVelocity;
|
import com.loki.lovelocity.LoVelocity;
|
||||||
import com.velocitypowered.api.event.PostOrder;
|
import com.velocitypowered.api.event.PostOrder;
|
||||||
import com.velocitypowered.api.event.Subscribe;
|
import com.velocitypowered.api.event.Subscribe;
|
||||||
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
||||||
import com.velocitypowered.api.event.player.ServerConnectedEvent;
|
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 {
|
public class ServerSwitchListener {
|
||||||
|
|
||||||
private final LoVelocity plugin;
|
private final LoVelocity plugin;
|
||||||
|
private static final String CHANNEL = "lohub:switch";
|
||||||
|
|
||||||
public ServerSwitchListener(LoVelocity plugin) {
|
public ServerSwitchListener(LoVelocity plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
|
@ -16,13 +23,40 @@ public class ServerSwitchListener {
|
||||||
|
|
||||||
@Subscribe(order = PostOrder.LAST)
|
@Subscribe(order = PostOrder.LAST)
|
||||||
public void onServerConnected(ServerConnectedEvent event) {
|
public void onServerConnected(ServerConnectedEvent event) {
|
||||||
// Velocity doesn't show join/leave messages by default
|
Player player = event.getPlayer();
|
||||||
// They are handled by backend servers (Paper/Spigot)
|
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)
|
@Subscribe(order = PostOrder.LAST)
|
||||||
public void onDisconnect(DisconnectEvent event) {
|
public void onDisconnect(DisconnectEvent event) {
|
||||||
// Velocity doesn't show join/leave messages by default
|
// Player left the network - handled by backend server
|
||||||
// They are handled by backend servers (Paper/Spigot)
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue