diff --git a/lohub/src/main/java/com/loki/lohub/LoHub.java b/lohub/src/main/java/com/loki/lohub/LoHub.java index 79318ee..ee15901 100644 --- a/lohub/src/main/java/com/loki/lohub/LoHub.java +++ b/lohub/src/main/java/com/loki/lohub/LoHub.java @@ -55,6 +55,7 @@ public class LoHub extends JavaPlugin { registerListeners(); registerCommands(); + registerPluginChannels(); regionManager.createHubRegion(); @@ -90,6 +91,15 @@ public class LoHub extends JavaPlugin { 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() { return configManager; } diff --git a/lohub/src/main/java/com/loki/lohub/listeners/ServerSwitchMessageListener.java b/lohub/src/main/java/com/loki/lohub/listeners/ServerSwitchMessageListener.java new file mode 100644 index 0000000..f65881d --- /dev/null +++ b/lohub/src/main/java/com/loki/lohub/listeners/ServerSwitchMessageListener.java @@ -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; + } +} diff --git a/lohub/src/main/resources/config.yml b/lohub/src/main/resources/config.yml index 5e80b49..476977a 100644 --- a/lohub/src/main/resources/config.yml +++ b/lohub/src/main/resources/config.yml @@ -166,6 +166,28 @@ join_leave_messages: # Сообщение при выходе с сервера (не при переключении на другой сервер) 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: - "[MESSAGE] &#FFD700╔═══════════════════════════════╗" - "[MESSAGE] &r" diff --git a/lovelocity/src/main/java/com/loki/lovelocity/listeners/ServerSwitchListener.java b/lovelocity/src/main/java/com/loki/lovelocity/listeners/ServerSwitchListener.java index 601cd7c..0b22391 100644 --- a/lovelocity/src/main/java/com/loki/lovelocity/listeners/ServerSwitchListener.java +++ b/lovelocity/src/main/java/com/loki/lovelocity/listeners/ServerSwitchListener.java @@ -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 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); + }); } }