summaryrefslogtreecommitdiff
path: root/platform-velocity
diff options
context:
space:
mode:
authorSho Sakuma <me@m1sk9.dev>2026-02-06 18:43:36 +0900
committerSho Sakuma <me@m1sk9.dev>2026-02-06 20:33:37 +0900
commit2b539cb2cefd56ad4a8322b643d4735581e6804d (patch)
tree24e00d5f40f028323081e35ea7a9f2bf56f726c2 /platform-velocity
parent5edc4a18217aa9f330eef50372d48ab1eec5becf (diff)
downloadLunaticChat-2b539cb2cefd56ad4a8322b643d4735581e6804d.tar.gz
LunaticChat-2b539cb2cefd56ad4a8322b643d4735581e6804d.tar.bz2
LunaticChat-2b539cb2cefd56ad4a8322b643d4735581e6804d.zip
feat: Add cross-server chat
Diffstat (limited to 'platform-velocity')
-rw-r--r--platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/LunaticChat.kt10
-rw-r--r--platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/messaging/CrossServerChatRelay.kt53
-rw-r--r--platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/messaging/PluginMessageHandler.kt23
3 files changed, 83 insertions, 3 deletions
diff --git a/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/LunaticChat.kt b/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/LunaticChat.kt
index 1d7fb90..77b8c97 100644
--- a/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/LunaticChat.kt
+++ b/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/LunaticChat.kt
@@ -6,6 +6,7 @@ import com.velocitypowered.api.event.proxy.ProxyInitializeEvent
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent
import com.velocitypowered.api.plugin.Plugin
import com.velocitypowered.api.proxy.ProxyServer
+import dev.m1sk9.lunaticChat.velocity.messaging.CrossServerChatRelay
import dev.m1sk9.lunaticChat.velocity.messaging.PluginMessageHandler
import org.slf4j.Logger
@@ -29,11 +30,19 @@ class LunaticChat
private val logger: Logger,
) {
private var messageHandler: PluginMessageHandler? = null
+ private var crossServerChatRelay: CrossServerChatRelay? = null
@Subscribe
fun onProxyInitialization(event: ProxyInitializeEvent) {
logger.info("Initializing LunaticChat Velocity plugin")
+ // Initialize cross-server chat relay
+ crossServerChatRelay =
+ CrossServerChatRelay(
+ server = server,
+ logger = logger,
+ )
+
// Initialize plugin message handler
messageHandler =
PluginMessageHandler(
@@ -41,6 +50,7 @@ class LunaticChat
server = server,
logger = logger,
pluginVersion = "0.8.0",
+ crossServerChatRelay = crossServerChatRelay!!,
)
messageHandler?.initialize()
diff --git a/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/messaging/CrossServerChatRelay.kt b/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/messaging/CrossServerChatRelay.kt
new file mode 100644
index 0000000..bedc302
--- /dev/null
+++ b/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/messaging/CrossServerChatRelay.kt
@@ -0,0 +1,53 @@
+package dev.m1sk9.lunaticChat.velocity.messaging
+
+import com.velocitypowered.api.proxy.ProxyServer
+import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier
+import com.velocitypowered.api.proxy.server.RegisteredServer
+import dev.m1sk9.lunaticChat.engine.protocol.PluginMessage
+import dev.m1sk9.lunaticChat.engine.protocol.PluginMessageCodec
+import org.slf4j.Logger
+
+/**
+ * Relays global chat messages between servers
+ *
+ * When a Paper server sends a global chat message to Velocity,
+ * this class broadcasts it to all other connected servers.
+ */
+class CrossServerChatRelay(
+ private val server: ProxyServer,
+ private val logger: Logger,
+) {
+ companion object {
+ private val CHANNEL = MinecraftChannelIdentifier.create("lunaticchat", "main")
+ }
+
+ /**
+ * Relays a global chat message to all servers except the source
+ *
+ * @param message Global chat message to relay
+ * @param sourceServer The server that sent the message
+ */
+ fun relayGlobalMessage(
+ message: PluginMessage.GlobalChatMessage,
+ sourceServer: RegisteredServer,
+ ) {
+ try {
+ val encodedMessage = PluginMessageCodec.encode(message)
+ var relayCount = 0
+
+ server.allServers
+ .filter { it != sourceServer }
+ .forEach { targetServer ->
+ targetServer.sendPluginMessage(CHANNEL, encodedMessage)
+ relayCount++
+ }
+
+ logger.info(
+ "Relayed global chat message from ${message.serverName} to $relayCount servers " +
+ "(messageId=${message.messageId}, player=${message.playerName})",
+ )
+ } catch (e: Exception) {
+ logger.error("Failed to relay global chat message: ${e.message}", e)
+ }
+ }
+}
diff --git a/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/messaging/PluginMessageHandler.kt b/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/messaging/PluginMessageHandler.kt
index b235ba1..fb69fa4 100644
--- a/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/messaging/PluginMessageHandler.kt
+++ b/platform-velocity/src/main/kotlin/dev/m1sk9/lunaticChat/velocity/messaging/PluginMessageHandler.kt
@@ -18,6 +18,7 @@ class PluginMessageHandler(
private val server: ProxyServer,
private val logger: Logger,
private val pluginVersion: String,
+ private val crossServerChatRelay: CrossServerChatRelay,
) {
companion object {
private val CHANNEL = MinecraftChannelIdentifier.create("lunaticchat", "main")
@@ -46,15 +47,16 @@ class PluginMessageHandler(
}
try {
- val message = PluginMessageCodec.decode(event.data)
-
- when (message) {
+ when (val message = PluginMessageCodec.decode(event.data)) {
is PluginMessage.Handshake -> {
handleHandshake(source, message)
}
is PluginMessage.StatusRequest -> {
handleStatusRequest(source)
}
+ is PluginMessage.GlobalChatMessage -> {
+ handleGlobalChatMessage(source, message)
+ }
else -> {
logger.warn("Unexpected message type from Paper: ${message::class.simpleName}")
}
@@ -152,6 +154,21 @@ class PluginMessageHandler(
}
/**
+ * Handles global chat message and relays to other servers
+ */
+ private fun handleGlobalChatMessage(
+ connection: ServerConnection,
+ message: PluginMessage.GlobalChatMessage,
+ ) {
+ logger.info(
+ "Received global chat message from ${connection.serverInfo.name}: " +
+ "messageId=${message.messageId}, player=${message.playerName}",
+ )
+
+ crossServerChatRelay.relayGlobalMessage(message, connection.server)
+ }
+
+ /**
* Shutdown
*/
fun shutdown() {