2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-04 20:38:02 +00:00

implement last server connect on join, closes #84

This commit is contained in:
mohammed jasem alaajel 2023-09-10 21:13:53 +04:00
parent 189026529a
commit d3803cfe4e
No known key found for this signature in database
4 changed files with 74 additions and 28 deletions

View File

@ -38,6 +38,7 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
protected final RedisBungeePlugin<P> plugin;
private final LoadingCache<UUID, String> serverCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getServerFromRedis);
private final LoadingCache<UUID, String> lastServerCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastServerFromRedis);
private final LoadingCache<UUID, String> proxyCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getProxyFromRedis);
private final LoadingCache<UUID, InetAddress> ipCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getIpAddressFromRedis);
private final LoadingCache<UUID, Long> lastOnlineCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastOnlineFromRedis);
@ -68,6 +69,7 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
protected void handleNetworkPlayerServerChange(IPlayerChangedServerNetworkEvent event) {
this.serverCache.invalidate(event.getUuid());
this.lastServerCache.invalidate(event.getUuid());
}
protected void handleNetworkPlayerQuit(IPlayerLeftNetworkEvent event) {
@ -190,6 +192,10 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
return unifiedJedis.hget("redis-bungee::player::" + uuid + "::data", "server");
}
protected String getLastServerFromRedis(UUID uuid) {
return unifiedJedis.hget("redis-bungee::player::" + uuid + "::data", "last-server");
}
protected InetAddress getIpAddressFromRedis(UUID uuid) {
String ip = unifiedJedis.hget("redis-bungee::player::" + uuid + "::data", "ip");
if (ip == null) return null;
@ -202,6 +208,9 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
return Long.parseLong(unixString);
}
public String getLastServerFor(UUID uuid) {
return this.lastServerCache.get(uuid);
}
public String getServerFor(UUID uuid) {
return this.serverCache.get(uuid);
}

View File

@ -4,13 +4,12 @@
# example:
# Format:
# message_id:
# <2 char language code>: "MiniMessage color and your text."
# lets assume we want to add arabic language.
# logged-in-other-location:
# en: "<color:red>You logged in from another location!"
# ar: "<color:red>لقد اتصلت من مكان اخر"
# errors:
# logged-in-other-location:
# en: "<color:red>You logged in from another location!"
# ar: "<color:red>لقد اتصلت من مكان اخر"
# 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: "<color:red>You logged in from another location!"
already-logged-in:
en: "<color:red>You are already logged in!"
server-not-found:
en: "<color:red>unable to connect you to the last server, because server %s was not found."
server-found:
en: "<color:green>Connecting you to %s..."
logged-in-other-location:
en: "<color:red>You logged in from another location!"
already-logged-in:
en: "<color:red>You are already logged in!"
# commands:

View File

@ -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);
}
}
}

View File

@ -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<UUID> 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<String, UUID> 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<String, String> human = HashMultimap.create();
for (Map.Entry<String, UUID> 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<RegisteredServer> 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);
}
}
}