diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java index 7d544df..2324d26 100644 --- a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java @@ -38,6 +38,7 @@ public abstract class PlayerDataManager plugin; private final LoadingCache serverCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getServerFromRedis); + private final LoadingCache lastServerCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastServerFromRedis); private final LoadingCache proxyCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getProxyFromRedis); 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); @@ -68,6 +69,7 @@ public abstract class PlayerDataManager: "MiniMessage color and your text." + # lets assume we want to add arabic language. -# logged-in-other-location: -# en: "You logged in from another location!" -# ar: "لقد اتصلت من مكان اخر" +# errors: +# logged-in-other-location: +# en: "You logged in from another location!" +# ar: "لقد اتصلت من مكان اخر" # Prefix if ever used. @@ -26,9 +25,15 @@ default-language: en # https://minecraft.fandom.com/wiki/Language use-client-language: true +messages: + logged-in-other-location: + en: "You logged in from another location!" + already-logged-in: + en: "You are already logged in!" + server-not-found: + en: "unable to connect you to the last server, because server %s was not found." + server-found: + en: "Connecting you to %s..." -logged-in-other-location: - en: "You logged in from another location!" -already-logged-in: - en: "You are already logged in!" \ No newline at end of file +# commands: diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java index ce298ff..faf447e 100644 --- a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java @@ -17,12 +17,15 @@ import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; +import net.kyori.adventure.text.Component; import net.md_5.bungee.api.AbstractReconnectHandler; +import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.Server; import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.api.event.ProxyPingEvent; +import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.event.EventHandler; @@ -146,5 +149,18 @@ public class RedisBungeeListener implements Listener { } } - + @EventHandler + public void onServerConnectEvent(ServerConnectEvent event) { + if (event.getReason() == ServerConnectEvent.Reason.JOIN_PROXY && plugin.configuration().handleReconnectToLastServer()) { + String lastServer = plugin.playerDataManager().getLastServerFor(event.getPlayer().getUniqueId()); + if (lastServer == null) return; + // sending connect message, todo: IMPLEMENT once lang system is finalized + ServerInfo serverInfo = ProxyServer.getInstance().getServerInfo(lastServer); + if (serverInfo == null) { + // sending failure message, todo: IMPLEMENT once lang system is finalized + return; + } + event.setTarget(serverInfo); + } + } } diff --git a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java index 35a9807..0e1003f 100644 --- a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java +++ b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java @@ -20,10 +20,13 @@ import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.connection.PluginMessageEvent; +import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent; import com.velocitypowered.api.event.proxy.ProxyPingEvent; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ServerConnection; +import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.ServerPing; +import net.kyori.adventure.text.Component; import java.util.*; import java.util.stream.Collectors; @@ -65,7 +68,7 @@ public class RedisBungeeListener { String type; switch (subchannel) { - case "PlayerList": + case "PlayerList" -> { out.writeUTF("PlayerList"); Set original = Collections.emptySet(); type = in.readUTF(); @@ -83,8 +86,8 @@ public class RedisBungeeListener { .map(uuid -> plugin.getUuidTranslator().getNameFromUuid(uuid, false)) .collect(Collectors.toSet()); out.writeUTF(Joiner.on(',').join(players)); - break; - case "PlayerCount": + } + case "PlayerCount" -> { out.writeUTF("PlayerCount"); type = in.readUTF(); if (type.equals("ALL")) { @@ -98,20 +101,18 @@ public class RedisBungeeListener { out.writeInt(0); } } - break; - case "LastOnline": + } + case "LastOnline" -> { String user = in.readUTF(); out.writeUTF("LastOnline"); out.writeUTF(user); out.writeLong(plugin.getAbstractRedisBungeeApi().getLastOnline(Objects.requireNonNull(plugin.getUuidTranslator().getTranslatedUuid(user, true)))); - break; - case "ServerPlayers": + } + case "ServerPlayers" -> { String type1 = in.readUTF(); out.writeUTF("ServerPlayers"); Multimap multimap = plugin.getAbstractRedisBungeeApi().getServerToPlayers(); - boolean includesUsers; - switch (type1) { case "COUNT" -> includesUsers = false; case "PLAYERS" -> includesUsers = true; @@ -120,9 +121,7 @@ public class RedisBungeeListener { return; } } - out.writeUTF(type1); - if (includesUsers) { Multimap human = HashMultimap.create(); for (Map.Entry entry : multimap.entries()) { @@ -132,19 +131,20 @@ public class RedisBungeeListener { } else { serializeMultiset(multimap.keys(), out); } - break; - case "Proxy": + } + case "Proxy" -> { out.writeUTF("Proxy"); out.writeUTF(plugin.configuration().getProxyId()); - break; - case "PlayerProxy": + } + case "PlayerProxy" -> { String username = in.readUTF(); out.writeUTF("PlayerProxy"); out.writeUTF(username); out.writeUTF(plugin.getAbstractRedisBungeeApi().getProxy(Objects.requireNonNull(plugin.getUuidTranslator().getTranslatedUuid(username, true)))); - break; - default: + } + default -> { return; + } } ((ServerConnection) event.getSource()).sendPluginMessage(event.getIdentifier(), out.toByteArray()); @@ -152,5 +152,21 @@ public class RedisBungeeListener { } + @Subscribe + public void onPlayerChooseInitialServerEvent(PlayerChooseInitialServerEvent event) { + if (plugin.configuration().handleReconnectToLastServer()) { + String lastServer = plugin.playerDataManager().getLastServerFor(event.getPlayer().getUniqueId()); + if (lastServer == null) return; + // sending connect message, todo: IMPLEMENT once lang system is finalized + Optional optionalRegisteredServer = ((RedisBungeeVelocityPlugin) plugin).getProxy().getServer(lastServer); + if (optionalRegisteredServer.isEmpty()) { + // sending failure message, todo: IMPLEMENT once lang system is finalized + return; + } + RegisteredServer server = optionalRegisteredServer.get(); + event.setInitialServer(server); + } + } + }