From 338297192cae2af6f38f81e8c50f117250686a7e Mon Sep 17 00:00:00 2001 From: Mohammed Alteneiji Date: Sat, 22 Feb 2025 17:57:06 +0400 Subject: [PATCH] reimpl kick api using serialized messages instead, Move language to own module, depenedcies fixes --- .../redisbungee/api/PlayerDataManager.java | 82 ++++++++----------- .../redisbungee/api/ProxyDataManager.java | 1 - .../redisbungee/api/RedisBungeePlugin.java | 7 -- build.gradle.kts | 0 commands/build.gradle.kts | 3 + .../commands/CommandRedisBungeeDebug.java | 8 +- .../commands/utils/AdventureBaseCommand.java | 9 +- lang/build.gradle.kts | 49 +++++++++++ .../config/lang}/LangConfigLoader.java | 4 +- .../config/lang}/LangConfiguration.java | 2 +- {api => lang}/src/main/resources/lang.yml | 0 .../redisbungee/BungeePlayerDataManager.java | 3 +- .../minecraft/redisbungee/RedisBungee.java | 2 - proxies/velocity/build.gradle.kts | 1 + .../redisbungee/RedisBungeeListener.java | 4 +- .../RedisBungeeVelocityPlugin.java | 26 ++---- .../VelocityPlayerDataManager.java | 44 ++++++++-- .../velocity/velocity-api/build.gradle.kts | 8 +- .../redisbungee/ApiPlatformSupport.java | 25 ++++++ .../minecraft/redisbungee/RedisBungeeAPI.java | 4 +- .../redisbungee/ServerObjectFetcher.java | 10 --- settings.gradle.kts | 1 + 22 files changed, 179 insertions(+), 114 deletions(-) delete mode 100644 build.gradle.kts create mode 100644 lang/build.gradle.kts rename {api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders => lang/src/main/java/net/limework/valiobungee/config/lang}/LangConfigLoader.java (94%) rename {api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config => lang/src/main/java/net/limework/valiobungee/config/lang}/LangConfiguration.java (98%) rename {api => lang}/src/main/resources/lang.yml (100%) create mode 100644 proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/ApiPlatformSupport.java delete mode 100644 proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/ServerObjectFetcher.java diff --git a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java index 6d5cc28..efb35ae 100644 --- a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java +++ b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java @@ -20,8 +20,6 @@ import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEv import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerLeftNetworkEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisPipelineTask; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.json.JSONComponentSerializer; import org.json.JSONObject; import redis.clients.jedis.ClusterPipeline; import redis.clients.jedis.Pipeline; @@ -45,7 +43,6 @@ public abstract class PlayerDataManager

{ private final LoadingCache ipCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getIpAddressFromRedis); private final LoadingCache lastOnlineCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastOnlineFromRedis); private final LoadingCache> serverToPlayersCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build(this::serversToPlayersBuilder); - private final JSONComponentSerializer COMPONENT_SERIALIZER = JSONComponentSerializer.json(); public PlayerDataManager(RedisBungeePlugin

plugin) { this.plugin = plugin; @@ -99,50 +96,33 @@ public abstract class PlayerDataManager

{ this.serverToPlayersCache.invalidate(SERVERS_TO_PLAYERS_KEY); } + protected void handlePubSubMessageEvent(IPubSubMessageEvent event) { - // kick api - if (event.getChannel().equals("redisbungee-kick")) { - JSONObject data = new JSONObject(event.getMessage()); - String proxy = data.getString("proxy"); - if (proxy.equals(this.proxyId)) { - return; + switch (event.getChannel()) { + case "redisbungee-serverchange" -> { + JSONObject data = new JSONObject(event.getMessage()); + UUID uuid = UUID.fromString(data.getString("uuid")); + String from = null; + if (data.has("from")) from = data.getString("from"); + String to = data.getString("to"); + plugin.fireEvent(plugin.createPlayerChangedServerNetworkEvent(uuid, from, to)); } - UUID uuid = UUID.fromString(data.getString("uuid")); - String message = data.getString("message"); - plugin.handlePlatformKick(uuid, COMPONENT_SERIALIZER.deserialize(message)); - return; - } - if (event.getChannel().equals("redisbungee-serverchange")) { - JSONObject data = new JSONObject(event.getMessage()); - String proxy = data.getString("proxy"); - if (proxy.equals(this.proxyId)) { - return; + case "redisbungee-player-join" -> { + JSONObject data = new JSONObject(event.getMessage()); + UUID uuid = UUID.fromString(data.getString("uuid")); + plugin.fireEvent(plugin.createPlayerJoinedNetworkEvent(uuid)); } - UUID uuid = UUID.fromString(data.getString("uuid")); - String from = null; - if (data.has("from")) from = data.getString("from"); - String to = data.getString("to"); - plugin.fireEvent(plugin.createPlayerChangedServerNetworkEvent(uuid, from, to)); - return; - } - if (event.getChannel().equals("redisbungee-player-join")) { - JSONObject data = new JSONObject(event.getMessage()); - String proxy = data.getString("proxy"); - if (proxy.equals(this.proxyId)) { - return; + case "redisbungee-player-leave" -> { + JSONObject data = new JSONObject(event.getMessage()); + UUID uuid = UUID.fromString(data.getString("uuid")); + plugin.fireEvent(plugin.createPlayerLeftNetworkEvent(uuid)); } - UUID uuid = UUID.fromString(data.getString("uuid")); - plugin.fireEvent(plugin.createPlayerJoinedNetworkEvent(uuid)); - return; - } - if (event.getChannel().equals("redisbungee-player-leave")) { - JSONObject data = new JSONObject(event.getMessage()); - String proxy = data.getString("proxy"); - if (proxy.equals(this.proxyId)) { - return; + case "redisbungee-player-kick" -> { + JSONObject data = new JSONObject(event.getMessage()); + UUID uuid = UUID.fromString(data.getString("uuid")); + String message = data.getString("serialized-message"); + handleSerializedKick(uuid, message); } - UUID uuid = UUID.fromString(data.getString("uuid")); - plugin.fireEvent(plugin.createPlayerLeftNetworkEvent(uuid)); } } @@ -158,14 +138,16 @@ public abstract class PlayerDataManager

{ handleServerChangeRedis(uuid, to); } - public void kickPlayer(UUID uuid, Component message) { - if (!plugin.handlePlatformKick(uuid, message)) { // handle locally before SENDING a message - JSONObject data = new JSONObject(); - data.put("proxy", this.proxyId); - data.put("uuid", uuid); - data.put("message", COMPONENT_SERIALIZER.serialize(message)); - plugin.proxyDataManager().sendChannelMessage("redisbungee-kick", data.toString()); - } + // must check if player is on the local proxy + protected abstract boolean handleSerializedKick(UUID player, String serializedMessage); + + public void serializedPlayerKick(UUID player, String serializedMessage) { + JSONObject data = new JSONObject(); + data.put("proxy", this.proxyId); + data.put("uuid", player); + data.put("serialized-message", serializedMessage); + if (!handleSerializedKick(player, serializedMessage)) + plugin.proxyDataManager().sendChannelMessage("redisbungee-player-kick", data.toString()); } private void handleServerChangeRedis(UUID uuid, String server) { diff --git a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/ProxyDataManager.java b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/ProxyDataManager.java index 07a5df3..5a2f9e9 100644 --- a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/ProxyDataManager.java +++ b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/ProxyDataManager.java @@ -103,7 +103,6 @@ public abstract class ProxyDataManager implements Runnable { public synchronized void sendChannelMessage(String channel, String message) { if (isClosed()) return; - this.plugin.fireEvent(this.plugin.createPubSubEvent(channel, message)); publishPayload(new PubSubPayload(this.proxyId, channel, message)); } diff --git a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/RedisBungeePlugin.java b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/RedisBungeePlugin.java index be13298..4ec08e1 100644 --- a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/RedisBungeePlugin.java +++ b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/RedisBungeePlugin.java @@ -11,12 +11,10 @@ package com.imaginarycode.minecraft.redisbungee.api; import com.imaginarycode.minecraft.redisbungee.AbstractRedisBungeeAPI; -import com.imaginarycode.minecraft.redisbungee.api.config.LangConfiguration; import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; import com.imaginarycode.minecraft.redisbungee.api.events.EventsPlatform; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator; -import net.kyori.adventure.text.Component; import java.net.InetAddress; import java.util.UUID; @@ -55,8 +53,6 @@ public interface RedisBungeePlugin

extends EventsPlatform { RedisBungeeConfiguration configuration(); - LangConfiguration langConfiguration(); - Summoner getSummoner(); RedisBungeeMode getRedisBungeeMode(); @@ -77,11 +73,8 @@ public interface RedisBungeePlugin

extends EventsPlatform { UUID getPlayerUUID(String player); - String getPlayerName(UUID player); - boolean handlePlatformKick(UUID uuid, Component message); - String getPlayerServerName(P player); boolean isPlayerOnAServer(P player); diff --git a/build.gradle.kts b/build.gradle.kts deleted file mode 100644 index e69de29..0000000 diff --git a/commands/build.gradle.kts b/commands/build.gradle.kts index 0d2c7e5..aedf8a6 100644 --- a/commands/build.gradle.kts +++ b/commands/build.gradle.kts @@ -5,6 +5,9 @@ plugins { dependencies { compileOnly(project(":RedisBungee-API")) implementation(libs.acf.core) + compileOnly(libs.adventure.api) + compileOnly(libs.adventure.miniMessage) + compileOnly(libs.adventure.gson) } description = "RedisBungee common commands" diff --git a/commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungeeDebug.java b/commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungeeDebug.java index e10973a..d1f6d09 100644 --- a/commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungeeDebug.java +++ b/commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungeeDebug.java @@ -33,16 +33,16 @@ public class CommandRedisBungeeDebug extends AdventureBaseCommand { @Description("kicks a player from the network by name") @Private public void kick(CommandIssuer issuer, String playerName) { - - plugin.getAbstractRedisBungeeApi().kickPlayer(playerName, Component.text("debug kick")); + String message = serializeMessage(Component.text("kicked using redisbungee api using name")); + plugin.playerDataManager().serializedPlayerKick(plugin.getUuidTranslator().getTranslatedUuid(playerName, false), message); } @Subcommand("kickByUUID") @Description("kicks a player from the network by UUID") @Private public void kick(CommandIssuer issuer, UUID uuid) { - - plugin.getAbstractRedisBungeeApi().kickPlayer(uuid, Component.text("debug kick")); + String message = serializeMessage(Component.text("kicked using redisbungee api using uuid")); + plugin.playerDataManager().serializedPlayerKick(uuid, message); } diff --git a/commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/AdventureBaseCommand.java b/commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/AdventureBaseCommand.java index 873e8ee..53b8b64 100644 --- a/commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/AdventureBaseCommand.java +++ b/commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/AdventureBaseCommand.java @@ -13,14 +13,21 @@ package com.imaginarycode.minecraft.redisbungee.commands.utils; import co.aikar.commands.BaseCommand; import co.aikar.commands.CommandIssuer; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; /** * this just dumb class that wraps the adventure stuff into base command */ public abstract class AdventureBaseCommand extends BaseCommand { - public static void sendMessage(CommandIssuer issuer, Component component) { + protected void sendMessage(CommandIssuer issuer, Component component) { CommandPlatformHelper.getPlatformHelper().sendMessage(issuer, component); } + private static final GsonComponentSerializer COMPONENT_SERIALIZER = GsonComponentSerializer.gson(); + + protected String serializeMessage(Component message) { + return COMPONENT_SERIALIZER.serialize(message); + } + } diff --git a/lang/build.gradle.kts b/lang/build.gradle.kts new file mode 100644 index 0000000..1ef8634 --- /dev/null +++ b/lang/build.gradle.kts @@ -0,0 +1,49 @@ +plugins { + `java-library` + `maven-publish` +} + +dependencies { + compileOnly(project(":RedisBungee-API")) + compileOnly(libs.adventure.api) + compileOnly(libs.adventure.miniMessage) +} + +description = "RedisBungee languages" + +java { + withJavadocJar() + withSourcesJar() +} + +tasks { + // thanks again for paper too + withType { + val options = options as StandardJavadocDocletOptions + options.use() + options.isDocFilesSubDirs = true + val adventureVersion = libs.adventure.plain.get().version + options.links( + "https://jd.advntr.dev/api/$adventureVersion" + ) + } + + compileJava { + options.encoding = Charsets.UTF_8.name() + options.release.set(17) + } + javadoc { + options.encoding = Charsets.UTF_8.name() + } + processResources { + filteringCharset = Charsets.UTF_8.name() + } +} + +publishing { + publications { + create("maven") { + from(components["java"]) + } + } +} diff --git a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/LangConfigLoader.java b/lang/src/main/java/net/limework/valiobungee/config/lang/LangConfigLoader.java similarity index 94% rename from api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/LangConfigLoader.java rename to lang/src/main/java/net/limework/valiobungee/config/lang/LangConfigLoader.java index ad30c42..14cf449 100644 --- a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/LangConfigLoader.java +++ b/lang/src/main/java/net/limework/valiobungee/config/lang/LangConfigLoader.java @@ -8,10 +8,10 @@ * http://www.eclipse.org/legal/epl-v10.html */ -package com.imaginarycode.minecraft.redisbungee.api.config.loaders; +package net.limework.valiobungee.config.lang; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; -import com.imaginarycode.minecraft.redisbungee.api.config.LangConfiguration; +import com.imaginarycode.minecraft.redisbungee.api.config.loaders.GenericConfigLoader; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import ninja.leaping.configurate.ConfigurationNode; diff --git a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/LangConfiguration.java b/lang/src/main/java/net/limework/valiobungee/config/lang/LangConfiguration.java similarity index 98% rename from api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/LangConfiguration.java rename to lang/src/main/java/net/limework/valiobungee/config/lang/LangConfiguration.java index 87aaa11..723f5d9 100644 --- a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/LangConfiguration.java +++ b/lang/src/main/java/net/limework/valiobungee/config/lang/LangConfiguration.java @@ -8,7 +8,7 @@ * http://www.eclipse.org/legal/epl-v10.html */ -package com.imaginarycode.minecraft.redisbungee.api.config; +package net.limework.valiobungee.config.lang; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; diff --git a/api/src/main/resources/lang.yml b/lang/src/main/resources/lang.yml similarity index 100% rename from api/src/main/resources/lang.yml rename to lang/src/main/resources/lang.yml diff --git a/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeePlayerDataManager.java b/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeePlayerDataManager.java index 8013ff3..a803aea 100644 --- a/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeePlayerDataManager.java +++ b/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeePlayerDataManager.java @@ -12,7 +12,6 @@ package com.imaginarycode.minecraft.redisbungee; import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; -import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent; @@ -75,7 +74,7 @@ public class BungeePlayerDataManager extends PlayerDataManager im event.completeIntent((Plugin) plugin); } else { if (plugin.configuration().kickWhenOnline()) { - kickPlayer(event.getConnection().getUniqueId(), plugin.langConfiguration().messages().loggedInFromOtherLocation()); + serializedPlayerKick(event.getConnection().getUniqueId(), plugin.langConfiguration().messages().loggedInFromOtherLocation()); // wait 3 seconds before releasing the event plugin.executeAsyncAfter(() -> event.completeIntent((Plugin) plugin), TimeUnit.SECONDS, 3); } else { diff --git a/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index 2a63876..a9fdecc 100644 --- a/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -46,8 +46,6 @@ import redis.clients.jedis.JedisPool; import java.io.IOException; import java.lang.reflect.Field; import java.net.InetAddress; -import java.sql.Date; -import java.time.Instant; import java.util.HashSet; import java.util.Set; import java.util.UUID; diff --git a/proxies/velocity/build.gradle.kts b/proxies/velocity/build.gradle.kts index e7296e7..81ac09b 100644 --- a/proxies/velocity/build.gradle.kts +++ b/proxies/velocity/build.gradle.kts @@ -10,6 +10,7 @@ dependencies { annotationProcessor(libs.platform.velocity) implementation(project(":RedisBungee-Commands")) implementation(libs.acf.velocity) + implementation(project(":RedisBungee-Lang")) } diff --git a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java index c94e0c2..8d6afcf 100644 --- a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java +++ b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java @@ -36,9 +36,9 @@ import static com.imaginarycode.minecraft.redisbungee.api.util.serialize.MultiMa public class RedisBungeeListener { - private final RedisBungeePlugin plugin; + private final RedisBungeeVelocityPlugin plugin; - public RedisBungeeListener(RedisBungeePlugin plugin) { + public RedisBungeeListener(RedisBungeeVelocityPlugin plugin) { this.plugin = plugin; } diff --git a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java index 72589bd..3d92d81 100644 --- a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java +++ b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java @@ -18,18 +18,14 @@ import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.commands.CommandLoader; import com.imaginarycode.minecraft.redisbungee.commands.utils.CommandPlatformHelper; -import com.imaginarycode.minecraft.redisbungee.api.config.LangConfiguration; import com.imaginarycode.minecraft.redisbungee.api.config.loaders.ConfigLoader; import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; -import com.imaginarycode.minecraft.redisbungee.api.config.loaders.LangConfigLoader; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNetworkEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerLeftNetworkEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.util.InitialUtils; -import com.imaginarycode.minecraft.redisbungee.api.util.uuid.NameFetcher; -import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDFetcher; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator; import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent; @@ -48,6 +44,8 @@ import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import com.velocitypowered.api.scheduler.ScheduledTask; import net.kyori.adventure.text.Component; +import net.limework.valiobungee.config.lang.LangConfiguration; +import net.limework.valiobungee.config.lang.LangConfigLoader; import org.slf4j.Logger; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -55,18 +53,15 @@ import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.nio.file.Path; -import java.sql.Date; import java.time.Duration; -import java.time.Instant; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.UUID; -import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @Plugin(id = "redisbungee", name = "RedisBungee", version = Constants.VERSION, url = "https://github.com/ProxioDev/RedisBungee", authors = {"astei", "ProxioDev"}) -public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, ConfigLoader, LangConfigLoader, ServerObjectFetcher { +public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, ConfigLoader, LangConfigLoader, ApiPlatformSupport { private final ProxyServer server; private final Logger logger; private final Path dataFolder; @@ -208,7 +203,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, Con return this.configuration; } - @Override public LangConfiguration langConfiguration() { return this.langConfiguration; } @@ -233,15 +227,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, Con return this.getProxy().getPlayer(player).map(Player::getUsername).orElse(null); } - - @Override - public boolean handlePlatformKick(UUID uuid, Component message) { - Player player = getPlayer(uuid); - if (player == null) return false; - player.disconnect(message); - return true; - } - @Override public String getPlayerServerName(Player player) { return player.getCurrentServer().map(serverConnection -> serverConnection.getServerInfo().getName()).orElse(null); @@ -356,6 +341,11 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, Con return server; } + @Override + public void kickPlayer(UUID player, Component message) { + this.playerDataManager.kickPlayer(player, message); + } + public Logger getLogger() { return logger; } diff --git a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityPlayerDataManager.java b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityPlayerDataManager.java index 95f35ce..00c5cfc 100644 --- a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityPlayerDataManager.java +++ b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityPlayerDataManager.java @@ -11,8 +11,6 @@ package com.imaginarycode.minecraft.redisbungee; import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager; -import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; -import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent; @@ -26,12 +24,18 @@ import com.velocitypowered.api.event.connection.PostLoginEvent; import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.proxy.Player; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import java.util.UUID; import java.util.concurrent.TimeUnit; public class VelocityPlayerDataManager extends PlayerDataManager { - public VelocityPlayerDataManager(RedisBungeePlugin plugin) { + + private final RedisBungeeVelocityPlugin vplugin; + + public VelocityPlayerDataManager(RedisBungeeVelocityPlugin plugin) { super(plugin); + this.vplugin = plugin; } @Subscribe @@ -66,6 +70,36 @@ public class VelocityPlayerDataManager extends PlayerDataManager { super.playerChangedServer(event.getPlayer().getUniqueId(), oldServer, currentServer); } + private final GsonComponentSerializer COMPONENT_SERIALIZER = GsonComponentSerializer.gson(); + + @Override + public boolean handleSerializedKick(UUID uuid, String serializedMessage) { + Player player = plugin.getPlayer(uuid); + if (player == null) return false; + // decode the adventure component + if (serializedMessage == null || serializedMessage.isEmpty()) { + // kick the player too even if the message is invalid + player.disconnect(Component.empty()); + plugin.logWarn("unable to decode serialized adventure component because its empty or null"); + } else { + try { + Component message = COMPONENT_SERIALIZER.deserialize(serializedMessage); + player.disconnect(message); + } catch (Exception e) { + plugin.logFatal("Kick message is invalid", e); + plugin.logFatal("The serialized kick message:"); + plugin.logFatal(serializedMessage); + // kick the player too even if the message is invalid + player.disconnect(Component.empty()); + } + } + return true; + } + + public void kickPlayer(UUID player, Component message) { + serializedPlayerKick(player, COMPONENT_SERIALIZER.serialize(message)); + } + @Subscribe public void onLoginEvent(LoginEvent event, Continuation continuation) { // check if online @@ -77,11 +111,11 @@ public class VelocityPlayerDataManager extends PlayerDataManager { continuation.resume(); } else { if (plugin.configuration().kickWhenOnline()) { - kickPlayer(event.getPlayer().getUniqueId(), plugin.langConfiguration().messages().loggedInFromOtherLocation()); + kickPlayer(event.getPlayer().getUniqueId(), vplugin.langConfiguration().messages().loggedInFromOtherLocation()); // wait 3 seconds before releasing the event plugin.executeAsyncAfter(continuation::resume, TimeUnit.SECONDS, 3); } else { - event.setResult(ResultedEvent.ComponentResult.denied(plugin.langConfiguration().messages().alreadyLoggedIn())); + event.setResult(ResultedEvent.ComponentResult.denied(vplugin.langConfiguration().messages().alreadyLoggedIn())); continuation.resume(); } } diff --git a/proxies/velocity/velocity-api/build.gradle.kts b/proxies/velocity/velocity-api/build.gradle.kts index 440601b..f432c44 100644 --- a/proxies/velocity/velocity-api/build.gradle.kts +++ b/proxies/velocity/velocity-api/build.gradle.kts @@ -10,15 +10,9 @@ dependencies { exclude("com.google.code.gson", "gson") exclude("org.spongepowered", "configurate-yaml") exclude("com.github.ben-manes.caffeine", "caffeine") - // exclude also adventure api - exclude("net.kyori", "adventure-api") - exclude("net.kyori", "adventure-text-serializer-gson") - exclude("net.kyori", "adventure-text-serializer-legacy") - exclude("net.kyori", "adventure-text-serializer-plain") - exclude("net.kyori", "adventure-text-minimessage") } + implementation(project(":RedisBungee-Lang")) compileOnly(libs.platform.velocity) - } description = "RedisBungee Velocity API" diff --git a/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/ApiPlatformSupport.java b/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/ApiPlatformSupport.java new file mode 100644 index 0000000..e5f7e1a --- /dev/null +++ b/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/ApiPlatformSupport.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2013-present RedisBungee contributors + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ + +package com.imaginarycode.minecraft.redisbungee; + +import com.velocitypowered.api.proxy.ProxyServer; +import net.kyori.adventure.text.Component; + +import java.util.UUID; + +// this class used to redirect calls to keep the implementation and api separate +public interface ApiPlatformSupport { + + ProxyServer getProxy(); + + void kickPlayer(UUID Player, Component message); + +} diff --git a/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java b/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java index 8d3dd5e..5368bc0 100644 --- a/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java +++ b/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java @@ -50,7 +50,7 @@ public class RedisBungeeAPI extends AbstractRedisBungeeAPI { public final ServerInfo getServerFor(@NonNull UUID player) { String serverName = this.getServerNameFor(player); if (serverName == null) return null; - return ((ServerObjectFetcher) this.plugin).getProxy().getServer(serverName).map((RegisteredServer::getServerInfo)).orElse(null); + return ((ApiPlatformSupport) this.plugin).getProxy().getServer(serverName).map((RegisteredServer::getServerInfo)).orElse(null); } /** @@ -73,7 +73,7 @@ public class RedisBungeeAPI extends AbstractRedisBungeeAPI { * @since 0.12.0 */ public void kickPlayer(UUID playerUUID, Component message) { - this.plugin.playerDataManager().kickPlayer(playerUUID, message); + ((ApiPlatformSupport) this.plugin).kickPlayer(playerUUID, message); } diff --git a/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/ServerObjectFetcher.java b/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/ServerObjectFetcher.java deleted file mode 100644 index 786361e..0000000 --- a/proxies/velocity/velocity-api/src/main/java/com/imaginarycode/minecraft/redisbungee/ServerObjectFetcher.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.imaginarycode.minecraft.redisbungee; - -import com.velocitypowered.api.proxy.ProxyServer; - -public interface ServerObjectFetcher { - - ProxyServer getProxy(); - - -} diff --git a/settings.gradle.kts b/settings.gradle.kts index 37fefc7..161073a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -13,6 +13,7 @@ fun configureProject(name: String, path: String) { } configureProject(":RedisBungee-API", "api") +configureProject(":RedisBungee-Lang", "lang") configureProject(":RedisBungee-Commands", "commands") configureProject(":RedisBungee-Bungee", "proxies/bungeecord/bungeecord-api")