mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-12-23 02:28:01 +00:00
Change Jedis -> JedisPooled, and make tasks use UnifiedJedis
since JedisCluster, JedisPooled are Childern of UnifiedJedis
This commit is contained in:
parent
f303f2c202
commit
ee76fa0b3d
@ -6,7 +6,7 @@ import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@ -365,10 +365,11 @@ public class RedisBungeeAPI {
|
||||
* @return {@link Jedis}
|
||||
* @since 0.7.0
|
||||
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE}
|
||||
* @see #getJedisPool()
|
||||
*/
|
||||
public Jedis requestJedis() {
|
||||
if (getMode() == RedisBungeeMode.SINGLE) {
|
||||
return ((JedisSummoner) this.plugin.getSummoner()).obtainResource();
|
||||
return getJedisPool().getResource();
|
||||
} else {
|
||||
throw new IllegalStateException("Mode is not " + RedisBungeeMode.SINGLE);
|
||||
}
|
||||
@ -380,17 +381,22 @@ public class RedisBungeeAPI {
|
||||
* @return {@link JedisPool}
|
||||
* @since 0.6.5
|
||||
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE}
|
||||
* @throws IllegalStateException if JedisPool compatibility mode is disabled in the config
|
||||
*/
|
||||
public JedisPool getJedisPool() {
|
||||
if (getMode() == RedisBungeeMode.SINGLE) {
|
||||
return ((JedisSummoner) this.plugin.getSummoner()).getJedisPool();
|
||||
JedisPool jedisPool = ((JedisPooledSummoner) this.plugin.getSummoner()).getCompatibilityJedisPool();
|
||||
if (jedisPool == null) {
|
||||
throw new IllegalStateException("JedisPool compatibility mode is disabled");
|
||||
}
|
||||
return jedisPool;
|
||||
} else {
|
||||
throw new IllegalStateException("Mode is not " + RedisBungeeMode.SINGLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This gives you instance of Jedis Cluster
|
||||
* This gives you instance of JedisCluster
|
||||
* WARNING DO NOT USE {@link JedisCluster#close()} it will break the functionally
|
||||
*
|
||||
* @return {@link redis.clients.jedis.JedisCluster}
|
||||
@ -406,7 +412,23 @@ public class RedisBungeeAPI {
|
||||
}
|
||||
|
||||
/**
|
||||
* returns Summoner class responsible for Single Jedis {@link Jedis}, Cluster Jedis {@link redis.clients.jedis.JedisCluster} handling
|
||||
* This gives you instance of JedisPooled
|
||||
* WARNING: DO NOT USE {@link redis.clients.jedis.JedisPooled#close()} it will break the functionally
|
||||
*
|
||||
* @return {@link redis.clients.jedis.JedisPooled}
|
||||
* @since 0.8.0
|
||||
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE}
|
||||
*/
|
||||
public JedisCluster requestJedisPooled() {
|
||||
if (getMode() == RedisBungeeMode.SINGLE) {
|
||||
return ((JedisClusterSummoner) this.plugin.getSummoner()).obtainResource();
|
||||
} else {
|
||||
throw new IllegalStateException("Mode is not " + RedisBungeeMode.SINGLE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns Summoner class responsible for Single Jedis {@link redis.clients.jedis.JedisPooled} with {@link JedisPool}, Cluster Jedis {@link redis.clients.jedis.JedisCluster} handling
|
||||
*
|
||||
* @return {@link Summoner}
|
||||
* @since 0.8.0
|
||||
|
@ -9,8 +9,7 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.InetAddress;
|
||||
@ -53,14 +52,8 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
|
||||
try {
|
||||
return serverCache.get(uuid, new RedisTask<String>(plugin.getApi()) {
|
||||
@Override
|
||||
public String jedisTask(Jedis jedis) {
|
||||
return Objects.requireNonNull(jedis.hget("player:" + uuid, "server"), "user not found");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String clusterJedisTask(JedisCluster jedisCluster) {
|
||||
return Objects.requireNonNull(jedisCluster.hget("player:" + uuid, "server"), "user not found");
|
||||
public String unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
return Objects.requireNonNull(unifiedJedis.hget("player:" + uuid, "server"), "user not found");
|
||||
|
||||
}
|
||||
});
|
||||
@ -82,13 +75,8 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
|
||||
try {
|
||||
return proxyCache.get(uuid, new RedisTask<String>(plugin.getApi()) {
|
||||
@Override
|
||||
public String jedisTask(Jedis jedis) {
|
||||
return Objects.requireNonNull(jedis.hget("player:" + uuid, "proxy"), "user not found");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String clusterJedisTask(JedisCluster jedisCluster) {
|
||||
return Objects.requireNonNull(jedisCluster.hget("player:" + uuid, "proxy"), "user not found");
|
||||
public String unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
return Objects.requireNonNull(unifiedJedis.hget("player:" + uuid, "proxy"), "user not found");
|
||||
}
|
||||
});
|
||||
} catch (ExecutionException | UncheckedExecutionException e) {
|
||||
@ -108,16 +96,8 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
|
||||
try {
|
||||
return ipCache.get(uuid, new RedisTask<InetAddress>(plugin.getApi()) {
|
||||
@Override
|
||||
public InetAddress jedisTask(Jedis jedis) {
|
||||
String result = jedis.hget("player:" + uuid, "ip");
|
||||
if (result == null)
|
||||
throw new NullPointerException("user not found");
|
||||
return InetAddresses.forString(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetAddress clusterJedisTask(JedisCluster jedisCluster) {
|
||||
String result = jedisCluster.hget("player:" + uuid, "ip");
|
||||
public InetAddress unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
String result = unifiedJedis.hget("player:" + uuid, "ip");
|
||||
if (result == null)
|
||||
throw new NullPointerException("user not found");
|
||||
return InetAddresses.forString(result);
|
||||
@ -139,15 +119,10 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
|
||||
|
||||
try {
|
||||
return lastOnlineCache.get(uuid, new RedisTask<Long>(plugin.getApi()) {
|
||||
@Override
|
||||
public Long jedisTask(Jedis jedis) {
|
||||
String result = jedis.hget("player:" + uuid, "online");
|
||||
return result == null ? -1 : Long.parseLong(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long clusterJedisTask(JedisCluster jedisCluster) {
|
||||
String result = jedisCluster.hget("player:" + uuid, "online");
|
||||
public Long unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
String result = unifiedJedis.hget("player:" + uuid, "online");
|
||||
return result == null ? -1 : Long.parseLong(result);
|
||||
}
|
||||
});
|
||||
|
@ -3,6 +3,7 @@ package com.imaginarycode.minecraft.redisbungee.api;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -24,22 +25,12 @@ public class PubSubListener implements Runnable {
|
||||
public void run() {
|
||||
RedisTask<Void> subTask = new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
jpsh = new JedisPubSubHandler(plugin);
|
||||
addedChannels.add("redisbungee-" + plugin.getConfiguration().getProxyId());
|
||||
addedChannels.add("redisbungee-allservers");
|
||||
addedChannels.add("redisbungee-data");
|
||||
jedis.subscribe(jpsh, addedChannels.toArray(new String[0]));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
jpsh = new JedisPubSubHandler(plugin);
|
||||
addedChannels.add("redisbungee-" + plugin.getConfiguration().getProxyId());
|
||||
addedChannels.add("redisbungee-allservers");
|
||||
addedChannels.add("redisbungee-data");
|
||||
jedisCluster.subscribe(jpsh, addedChannels.toArray(new String[0]));
|
||||
unifiedJedis.subscribe(jpsh, addedChannels.toArray(new String[0]));
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
@ -12,8 +12,6 @@ import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
@ -50,34 +48,17 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
|
||||
default int getCurrentCount() {
|
||||
return new RedisTask<Long>(this) {
|
||||
@Override
|
||||
public Long jedisTask(Jedis jedis) {
|
||||
public Long unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
long total = 0;
|
||||
long redisTime = getRedisTime(jedis.time());
|
||||
Map<String, String> heartBeats = jedis.hgetAll("heartbeats");
|
||||
long redisTime = getRedisTime(unifiedJedis);
|
||||
Map<String, String> heartBeats = unifiedJedis.hgetAll("heartbeats");
|
||||
for (Map.Entry<String, String> stringStringEntry : heartBeats.entrySet()) {
|
||||
String k = stringStringEntry.getKey();
|
||||
String v = stringStringEntry.getValue();
|
||||
|
||||
long heartbeatTime = Long.parseLong(v);
|
||||
if (heartbeatTime + 30 >= redisTime) {
|
||||
total = total + jedis.scard("proxy:" + k + ":usersOnline");
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long clusterJedisTask(JedisCluster jedisCluster) {
|
||||
long total = 0;
|
||||
long redisTime = getRedisTime(jedisCluster);
|
||||
Map<String, String> heartBeats = jedisCluster.hgetAll("heartbeats");
|
||||
for (Map.Entry<String, String> stringStringEntry : heartBeats.entrySet()) {
|
||||
String k = stringStringEntry.getKey();
|
||||
String v = stringStringEntry.getValue();
|
||||
|
||||
long heartbeatTime = Long.parseLong(v);
|
||||
if (heartbeatTime + 30 >= redisTime) {
|
||||
total = total + jedisCluster.scard("proxy:" + k + ":usersOnline");
|
||||
total = total + unifiedJedis.scard("proxy:" + k + ":usersOnline");
|
||||
}
|
||||
}
|
||||
return total;
|
||||
@ -92,7 +73,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
|
||||
default Set<UUID> getPlayers() {
|
||||
return new RedisTask<Set<UUID>>(this) {
|
||||
@Override
|
||||
public Set<UUID> jedisTask(Jedis jedis) {
|
||||
public Set<UUID> unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
ImmutableSet.Builder<UUID> setBuilder = ImmutableSet.builder();
|
||||
try {
|
||||
List<String> keys = new ArrayList<>();
|
||||
@ -100,34 +81,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
|
||||
keys.add("proxy:" + i + ":usersOnline");
|
||||
}
|
||||
if (!keys.isEmpty()) {
|
||||
Set<String> users = jedis.sunion(keys.toArray(new String[0]));
|
||||
if (users != null && !users.isEmpty()) {
|
||||
for (String user : users) {
|
||||
try {
|
||||
setBuilder = setBuilder.add(UUID.fromString(user));
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (JedisConnectionException e) {
|
||||
// Redis server has disappeared!
|
||||
logFatal("Unable to get connection from pool - did your Redis server go away?");
|
||||
throw new RuntimeException("Unable to get all players online", e);
|
||||
}
|
||||
return setBuilder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> clusterJedisTask(JedisCluster jedisCluster) {
|
||||
ImmutableSet.Builder<UUID> setBuilder = ImmutableSet.builder();
|
||||
try {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String i : getProxiesIds()) {
|
||||
keys.add("proxy:" + i + ":usersOnline");
|
||||
}
|
||||
if (!keys.isEmpty()) {
|
||||
Set<String> users = jedisCluster.sunion(keys.toArray(new String[0]));
|
||||
Set<String> users = unifiedJedis.sunion(keys.toArray(new String[0]));
|
||||
if (users != null && !users.isEmpty()) {
|
||||
for (String user : users) {
|
||||
try {
|
||||
@ -156,28 +110,12 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
|
||||
default Multimap<String, UUID> serversToPlayers() {
|
||||
return new RedisTask<Multimap<String, UUID>>(this) {
|
||||
@Override
|
||||
public Multimap<String, UUID> jedisTask(Jedis jedis) {
|
||||
public Multimap<String, UUID> unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
|
||||
for (String serverId : getProxiesIds()) {
|
||||
Set<String> players = jedis.smembers("proxy:" + serverId + ":usersOnline");
|
||||
Set<String> players = unifiedJedis.smembers("proxy:" + serverId + ":usersOnline");
|
||||
for (String player : players) {
|
||||
String playerServer = jedis.hget("player:" + player, "server");
|
||||
if (playerServer == null) {
|
||||
continue;
|
||||
}
|
||||
builder.put(playerServer, UUID.fromString(player));
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Multimap<String, UUID> clusterJedisTask(JedisCluster jedisCluster) {
|
||||
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
|
||||
for (String serverId : getProxiesIds()) {
|
||||
Set<String> players = jedisCluster.smembers("proxy:" + serverId + ":usersOnline");
|
||||
for (String player : players) {
|
||||
String playerServer = jedisCluster.hget("player:" + player, "server");
|
||||
String playerServer = unifiedJedis.hget("player:" + player, "server");
|
||||
if (playerServer == null) {
|
||||
continue;
|
||||
}
|
||||
@ -193,18 +131,8 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
|
||||
checkArgument(getProxiesIds().contains(proxyId), proxyId + " is not a valid proxy ID");
|
||||
return new RedisTask<Set<UUID>>(this) {
|
||||
@Override
|
||||
public Set<UUID> jedisTask(Jedis jedis) {
|
||||
Set<String> users = jedis.smembers("proxy:" + proxyId + ":usersOnline");
|
||||
ImmutableSet.Builder<UUID> builder = ImmutableSet.builder();
|
||||
for (String user : users) {
|
||||
builder.add(UUID.fromString(user));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> clusterJedisTask(JedisCluster jedisCluster) {
|
||||
Set<String> users = jedisCluster.smembers("proxy:" + proxyId + ":usersOnline");
|
||||
public Set<UUID> unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
Set<String> users = unifiedJedis.smembers("proxy:" + proxyId + ":usersOnline");
|
||||
ImmutableSet.Builder<UUID> builder = ImmutableSet.builder();
|
||||
for (String user : users) {
|
||||
builder.add(UUID.fromString(user));
|
||||
@ -224,11 +152,11 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
|
||||
default List<String> getCurrentProxiesIds(boolean lagged) {
|
||||
return new RedisTask<List<String>>(this) {
|
||||
@Override
|
||||
public List<String> jedisTask(Jedis jedis) {
|
||||
public List<String> unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
try {
|
||||
long time = getRedisTime(jedis.time());
|
||||
long time = getRedisTime(unifiedJedis);
|
||||
ImmutableList.Builder<String> servers = ImmutableList.builder();
|
||||
Map<String, String> heartbeats = jedis.hgetAll("heartbeats");
|
||||
Map<String, String> heartbeats = unifiedJedis.hgetAll("heartbeats");
|
||||
for (Map.Entry<String, String> entry : heartbeats.entrySet()) {
|
||||
try {
|
||||
long stamp = Long.parseLong(entry.getValue());
|
||||
@ -236,33 +164,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
|
||||
servers.add(entry.getKey());
|
||||
} else if (time > stamp + RedisUtil.PROXY_TIMEOUT) {
|
||||
logWarn(entry.getKey() + " is " + (time - stamp) + " seconds behind! (Time not synchronized or server down?) and was removed from heartbeat.");
|
||||
jedis.hdel("heartbeats", entry.getKey());
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
}
|
||||
return servers.build();
|
||||
} catch (JedisConnectionException e) {
|
||||
logFatal("Unable to fetch server IDs");
|
||||
e.printStackTrace();
|
||||
return Collections.singletonList(getConfiguration().getProxyId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> clusterJedisTask(JedisCluster jedisCluster) {
|
||||
try {
|
||||
long time = getRedisTime(jedisCluster);
|
||||
ImmutableList.Builder<String> servers = ImmutableList.builder();
|
||||
Map<String, String> heartbeats = jedisCluster.hgetAll("heartbeats");
|
||||
for (Map.Entry<String, String> entry : heartbeats.entrySet()) {
|
||||
try {
|
||||
long stamp = Long.parseLong(entry.getValue());
|
||||
if (lagged ? time >= stamp + RedisUtil.PROXY_TIMEOUT : time <= stamp + RedisUtil.PROXY_TIMEOUT) {
|
||||
servers.add(entry.getKey());
|
||||
} else if (time > stamp + RedisUtil.PROXY_TIMEOUT) {
|
||||
logWarn(entry.getKey() + " is " + (time - stamp) + " seconds behind! (Time not synchronized or server down?) and was removed from heartbeat.");
|
||||
jedisCluster.hdel("heartbeats", entry.getKey());
|
||||
unifiedJedis.hdel("heartbeats", entry.getKey());
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
@ -282,21 +184,9 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
|
||||
default void sendChannelMessage(String channel, String message) {
|
||||
new RedisTask<Void>(this) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
try {
|
||||
jedis.publish(channel, message);
|
||||
} catch (JedisConnectionException e) {
|
||||
// Redis server has disappeared!
|
||||
logFatal("Unable to get connection from pool - did your Redis server go away?");
|
||||
throw new RuntimeException("Unable to publish channel message", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
try {
|
||||
jedisCluster.publish(channel, message);
|
||||
unifiedJedis.publish(channel, message);
|
||||
} catch (JedisConnectionException e) {
|
||||
// Redis server has disappeared!
|
||||
logFatal("Unable to get connection from pool - did your Redis server go away?");
|
||||
@ -354,14 +244,8 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
|
||||
if (!getDataManager().handleKick(playerUniqueId, message)) {
|
||||
new RedisTask<Void>(this) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
PayloadUtils.kickPlayerPayload(playerUniqueId, message, jedis);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
PayloadUtils.kickPlayerPayload(playerUniqueId, message, jedisCluster);
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
PayloadUtils.kickPlayerPayload(playerUniqueId, message, unifiedJedis);
|
||||
return null;
|
||||
}
|
||||
}.execute();
|
||||
|
@ -5,7 +5,7 @@ import com.google.common.reflect.TypeToken;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
|
||||
import ninja.leaping.configurate.ConfigurationNode;
|
||||
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
|
||||
@ -102,9 +102,15 @@ public interface ConfigLoader {
|
||||
if (redisServer != null && redisServer.isEmpty()) {
|
||||
throw new RuntimeException("No redis server specified");
|
||||
}
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
config.setMaxTotal(maxConnections);
|
||||
summoner = new JedisSummoner(new JedisPool(config, redisServer, redisPort, 0, redisPassword, useSSL));
|
||||
JedisPool jedisPool = null;
|
||||
if (node.getNode("enable-jedis-pool-compatibility").getBoolean(true)) {
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
config.setMaxTotal(maxConnections);
|
||||
jedisPool = new JedisPool(config, redisServer, redisPort, 0, redisPassword, useSSL);
|
||||
}
|
||||
GenericObjectPoolConfig<Connection> poolConfig = new GenericObjectPoolConfig<>();
|
||||
poolConfig.setMaxTotal(maxConnections);
|
||||
summoner = new JedisPooledSummoner(new JedisPooled(poolConfig, redisServer, redisPort, 0, redisPassword, useSSL), jedisPool);
|
||||
redisBungeeMode = RedisBungeeMode.SINGLE;
|
||||
}
|
||||
plugin.logInfo("Successfully connected to Redis.");
|
||||
|
@ -6,7 +6,6 @@ import java.io.IOException;
|
||||
|
||||
public class JedisClusterSummoner implements Summoner<JedisCluster> {
|
||||
public final JedisCluster jedisCluster;
|
||||
private boolean closed = false;
|
||||
|
||||
public JedisClusterSummoner(JedisCluster jedisCluster) {
|
||||
this.jedisCluster = jedisCluster;
|
||||
@ -15,19 +14,14 @@ public class JedisClusterSummoner implements Summoner<JedisCluster> {
|
||||
jedisCluster.del("random_data");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JedisCluster obtainResource() {
|
||||
return jedisCluster;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return !closed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.closed = true;
|
||||
jedisCluster.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JedisCluster obtainResource() {
|
||||
return this.jedisCluster;
|
||||
}
|
||||
}
|
||||
|
@ -2,14 +2,17 @@ package com.imaginarycode.minecraft.redisbungee.api.summoners;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPooled;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class JedisSummoner implements Summoner<Jedis> {
|
||||
public class JedisPooledSummoner implements Summoner<JedisPooled> {
|
||||
|
||||
private final JedisPooled jedisPooled;
|
||||
private final JedisPool jedisPool;
|
||||
|
||||
public JedisSummoner(JedisPool jedisPool) {
|
||||
public JedisPooledSummoner(JedisPooled jedisPooled, JedisPool jedisPool) {
|
||||
this.jedisPooled = jedisPooled;
|
||||
this.jedisPool = jedisPool;
|
||||
try (Jedis jedis = this.jedisPool.getResource()) {
|
||||
// Test the connection to make sure configuration is right
|
||||
@ -18,22 +21,18 @@ public class JedisSummoner implements Summoner<Jedis> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Jedis obtainResource() {
|
||||
return jedisPool.getResource();
|
||||
public JedisPooled obtainResource() {
|
||||
return this.jedisPooled;
|
||||
}
|
||||
|
||||
public JedisPool getJedisPool() {
|
||||
public JedisPool getCompatibilityJedisPool() {
|
||||
return this.jedisPool;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return !jedisPool.isClosed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.jedisPool.close();
|
||||
this.jedisPooled.close();
|
||||
|
||||
}
|
||||
}
|
@ -14,7 +14,5 @@ public interface Summoner<P> extends Closeable {
|
||||
|
||||
P obtainResource();
|
||||
|
||||
boolean isAvailable();
|
||||
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.imaginarycode.minecraft.redisbungee.api.tasks;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@ -19,32 +20,12 @@ public class HeartbeatTask extends RedisTask<Void>{
|
||||
this.globalPlayerCount = globalPlayerCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
try {
|
||||
long redisTime = plugin.getRedisTime(jedis.time());
|
||||
jedis.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime));
|
||||
} catch (JedisConnectionException e) {
|
||||
// Redis server has disappeared!
|
||||
plugin.logFatal("Unable to update heartbeat - did your Redis server go away?");
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
plugin.updateProxiesIds();
|
||||
globalPlayerCount.set(plugin.getCurrentCount());
|
||||
} catch (Throwable e) {
|
||||
plugin.logFatal("Unable to update data - did your Redis server go away?");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
try {
|
||||
long redisTime = plugin.getRedisTime(jedisCluster);
|
||||
jedisCluster.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime));
|
||||
long redisTime = plugin.getRedisTime(unifiedJedis);
|
||||
unifiedJedis.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime));
|
||||
} catch (JedisConnectionException e) {
|
||||
// Redis server has disappeared!
|
||||
plugin.logFatal("Unable to update heartbeat - did your Redis server go away?");
|
||||
|
@ -5,6 +5,7 @@ import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@ -15,9 +16,10 @@ public class InitialUtils {
|
||||
public static void checkRedisVersion(RedisBungeePlugin<?> plugin) {
|
||||
new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
// This is more portable than INFO <section>
|
||||
String info = jedis.info();
|
||||
|
||||
String info = new String((byte[]) unifiedJedis.sendCommand(Protocol.Command.INFO));
|
||||
for (String s : info.split("\r\n")) {
|
||||
if (s.startsWith("redis_version:")) {
|
||||
String version = s.split(":")[1];
|
||||
@ -26,30 +28,7 @@ public class InitialUtils {
|
||||
plugin.logFatal("Your version of Redis (" + version + ") is not at least version 3.0 RedisBungee requires a newer version of Redis.");
|
||||
throw new RuntimeException("Unsupported Redis version detected");
|
||||
}
|
||||
long uuidCacheSize = jedis.hlen("uuid-cache");
|
||||
if (uuidCacheSize > 750000) {
|
||||
plugin.logInfo("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
// This is more portable than INFO <section>
|
||||
|
||||
String info = new String((byte[]) jedisCluster.sendCommand(Protocol.Command.INFO));
|
||||
for (String s : info.split("\r\n")) {
|
||||
if (s.startsWith("redis_version:")) {
|
||||
String version = s.split(":")[1];
|
||||
plugin.logInfo(version + " <- redis version");
|
||||
if (!RedisUtil.isRedisVersionRight(version)) {
|
||||
plugin.logFatal("Your version of Redis (" + version + ") is not at least version 3.0 RedisBungee requires a newer version of Redis.");
|
||||
throw new RuntimeException("Unsupported Redis version detected");
|
||||
}
|
||||
long uuidCacheSize = jedisCluster.hlen("uuid-cache");
|
||||
long uuidCacheSize = unifiedJedis.hlen("uuid-cache");
|
||||
if (uuidCacheSize > 750000) {
|
||||
plugin.logInfo("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
|
||||
}
|
||||
@ -65,7 +44,7 @@ public class InitialUtils {
|
||||
public static void checkIfRecovering(RedisBungeePlugin<?> plugin, Path dataFolder) {
|
||||
new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
Path crashFile = dataFolder.resolve("restarted_from_crash.txt");
|
||||
if (Files.exists(crashFile)) {
|
||||
try {
|
||||
@ -74,34 +53,10 @@ public class InitialUtils {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
plugin.logInfo("crash file was deleted");
|
||||
} else if (jedis.hexists("heartbeats", plugin.getConfiguration().getProxyId())) {
|
||||
} else if (unifiedJedis.hexists("heartbeats", plugin.getConfiguration().getProxyId())) {
|
||||
try {
|
||||
long value = Long.parseLong(jedis.hget("heartbeats", plugin.getConfiguration().getProxyId()));
|
||||
long redisTime = plugin.getRedisTime(jedis.time());
|
||||
if (redisTime < value + RedisUtil.PROXY_TIMEOUT) {
|
||||
logImposter(plugin);
|
||||
throw new RuntimeException("Possible impostor instance!");
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
Path crashFile = dataFolder.resolve("restarted_from_crash.txt");
|
||||
if (Files.exists(crashFile)) {
|
||||
try {
|
||||
Files.delete(crashFile);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
plugin.logInfo("crash file was deleted");
|
||||
} else if (jedisCluster.hexists("heartbeats", plugin.getConfiguration().getProxyId())) {
|
||||
try {
|
||||
long value = Long.parseLong(jedisCluster.hget("heartbeats", plugin.getConfiguration().getProxyId()));
|
||||
long redisTime = plugin.getRedisTime(jedisCluster);
|
||||
long value = Long.parseLong(unifiedJedis.hget("heartbeats", plugin.getConfiguration().getProxyId()));
|
||||
long redisTime = plugin.getRedisTime(unifiedJedis);
|
||||
|
||||
if (redisTime < value + RedisUtil.PROXY_TIMEOUT) {
|
||||
logImposter(plugin);
|
||||
|
@ -2,9 +2,7 @@ package com.imaginarycode.minecraft.redisbungee.api.tasks;
|
||||
|
||||
import com.imaginarycode.minecraft.redisbungee.api.util.player.PlayerUtils;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -22,20 +20,20 @@ public abstract class IntegrityCheckTask extends RedisTask<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
try {
|
||||
Set<String> players = plugin.getLocalPlayersAsUuidStrings();
|
||||
Set<String> playersInRedis = jedis.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
|
||||
Set<String> playersInRedis = unifiedJedis.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
|
||||
List<String> lagged = plugin.getCurrentProxiesIds(true);
|
||||
|
||||
// Clean up lagged players.
|
||||
for (String s : lagged) {
|
||||
Set<String> laggedPlayers = jedis.smembers("proxy:" + s + ":usersOnline");
|
||||
jedis.del("proxy:" + s + ":usersOnline");
|
||||
Set<String> laggedPlayers = unifiedJedis.smembers("proxy:" + s + ":usersOnline");
|
||||
unifiedJedis.del("proxy:" + s + ":usersOnline");
|
||||
if (!laggedPlayers.isEmpty()) {
|
||||
plugin.logInfo("Cleaning up lagged proxy " + s + " (" + laggedPlayers.size() + " players)...");
|
||||
for (String laggedPlayer : laggedPlayers) {
|
||||
PlayerUtils.cleanUpPlayer(laggedPlayer, jedis, true);
|
||||
PlayerUtils.cleanUpPlayer(laggedPlayer, unifiedJedis, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -49,85 +47,26 @@ public abstract class IntegrityCheckTask extends RedisTask<Void> {
|
||||
boolean found = false;
|
||||
for (String proxyId : plugin.getProxiesIds()) {
|
||||
if (proxyId.equals(plugin.getConfiguration().getProxyId())) continue;
|
||||
if (jedis.sismember("proxy:" + proxyId + ":usersOnline", member)) {
|
||||
if (unifiedJedis.sismember("proxy:" + proxyId + ":usersOnline", member)) {
|
||||
// Just clean up the set.
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
PlayerUtils.cleanUpPlayer(member, jedis, false);
|
||||
plugin.logWarn("Player found in set that was not found locally and globally: " + member);
|
||||
} else {
|
||||
jedis.srem("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline", member);
|
||||
plugin.logWarn("Player found in set that was not found locally, but is on another proxy: " + member);
|
||||
}
|
||||
}
|
||||
|
||||
Pipeline pipeline = jedis.pipelined();
|
||||
|
||||
for (String player : absentInRedis) {
|
||||
// Player not online according to Redis but not BungeeCord.
|
||||
plugin.logWarn("Player " + player + " is on the proxy but not in Redis.");
|
||||
handlePlatformPlayer(player, pipeline);
|
||||
}
|
||||
|
||||
pipeline.sync();
|
||||
} catch (Throwable e) {
|
||||
plugin.logFatal("Unable to fix up stored player data");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
try {
|
||||
Set<String> players = plugin.getLocalPlayersAsUuidStrings();
|
||||
Set<String> playersInRedis = jedisCluster.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
|
||||
List<String> lagged = plugin.getCurrentProxiesIds(true);
|
||||
|
||||
// Clean up lagged players.
|
||||
for (String s : lagged) {
|
||||
Set<String> laggedPlayers = jedisCluster.smembers("proxy:" + s + ":usersOnline");
|
||||
jedisCluster.del("proxy:" + s + ":usersOnline");
|
||||
if (!laggedPlayers.isEmpty()) {
|
||||
plugin.logInfo("Cleaning up lagged proxy " + s + " (" + laggedPlayers.size() + " players)...");
|
||||
for (String laggedPlayer : laggedPlayers) {
|
||||
PlayerUtils.cleanUpPlayer(laggedPlayer, jedisCluster, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> absentLocally = new HashSet<>(playersInRedis);
|
||||
absentLocally.removeAll(players);
|
||||
Set<String> absentInRedis = new HashSet<>(players);
|
||||
absentInRedis.removeAll(playersInRedis);
|
||||
|
||||
for (String member : absentLocally) {
|
||||
boolean found = false;
|
||||
for (String proxyId : plugin.getProxiesIds()) {
|
||||
if (proxyId.equals(plugin.getConfiguration().getProxyId())) continue;
|
||||
if (jedisCluster.sismember("proxy:" + proxyId + ":usersOnline", member)) {
|
||||
// Just clean up the set.
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
PlayerUtils.cleanUpPlayer(member, jedisCluster, false);
|
||||
PlayerUtils.cleanUpPlayer(member, unifiedJedis, false);
|
||||
plugin.logWarn("Player found in set that was not found locally and globally: " + member);
|
||||
} else {
|
||||
jedisCluster.srem("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline", member);
|
||||
unifiedJedis.srem("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline", member);
|
||||
plugin.logWarn("Player found in set that was not found locally, but is on another proxy: " + member);
|
||||
}
|
||||
}
|
||||
// due JedisCluster does not support pipelined.
|
||||
// due unifiedJedis does not support pipelined.
|
||||
//Pipeline pipeline = jedis.pipelined();
|
||||
|
||||
for (String player : absentInRedis) {
|
||||
// Player not online according to Redis but not BungeeCord.
|
||||
handlePlatformPlayer(player, jedisCluster);
|
||||
handlePlatformPlayer(player, unifiedJedis);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
plugin.logFatal("Unable to fix up stored player data");
|
||||
@ -137,7 +76,6 @@ public abstract class IntegrityCheckTask extends RedisTask<Void> {
|
||||
}
|
||||
|
||||
|
||||
public abstract void handlePlatformPlayer(String player, JedisCluster jedisCluster);
|
||||
public abstract void handlePlatformPlayer(String player, UnifiedJedis unifiedJedis);
|
||||
|
||||
public abstract void handlePlatformPlayer(String player, Pipeline pipeline);
|
||||
}
|
||||
|
@ -3,11 +3,11 @@ package com.imaginarycode.minecraft.redisbungee.api.tasks;
|
||||
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@ -15,7 +15,6 @@ public abstract class RedisTask<V> implements Runnable, Callable<V> {
|
||||
|
||||
protected final Summoner<?> summoner;
|
||||
protected final RedisBungeeAPI api;
|
||||
protected Jedis jedis;
|
||||
protected RedisBungeePlugin<?> plugin;
|
||||
|
||||
@Override
|
||||
@ -34,25 +33,7 @@ public abstract class RedisTask<V> implements Runnable, Callable<V> {
|
||||
this.summoner = api.getSummoner();
|
||||
}
|
||||
|
||||
// way to reuse jedis inside another RedisTask object
|
||||
public RedisTask(RedisBungeeAPI api, Jedis jedis) {
|
||||
this.api = api;
|
||||
this.summoner = api.getSummoner();
|
||||
this.jedis = jedis;
|
||||
}
|
||||
|
||||
// way to reuse jedis inside another RedisTask object
|
||||
public RedisTask(RedisBungeePlugin<?> plugin, Jedis jedis) {
|
||||
this.plugin = plugin;
|
||||
this.api = plugin.getApi();
|
||||
this.summoner = api.getSummoner();
|
||||
this.jedis = jedis;
|
||||
}
|
||||
|
||||
|
||||
public abstract V jedisTask(Jedis jedis);
|
||||
|
||||
public abstract V clusterJedisTask(JedisCluster jedisCluster);
|
||||
public abstract V unifiedJedisTask(UnifiedJedis unifiedJedis);
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
@ -60,19 +41,13 @@ public abstract class RedisTask<V> implements Runnable, Callable<V> {
|
||||
}
|
||||
|
||||
public V execute(){
|
||||
// JedisCluster, JedisPooled in fact is just UnifiedJedis does not need new instance since its single instance anyway.
|
||||
if (api.getMode() == RedisBungeeMode.SINGLE) {
|
||||
if (this.jedis != null){
|
||||
return this.jedisTask(this.jedis);
|
||||
}
|
||||
JedisSummoner jedisSummoner = (JedisSummoner) summoner;
|
||||
try (Jedis newJedis = jedisSummoner.obtainResource()) {
|
||||
return this.jedisTask(newJedis);
|
||||
}
|
||||
|
||||
JedisPooledSummoner jedisSummoner = (JedisPooledSummoner) summoner;
|
||||
return this.unifiedJedisTask(jedisSummoner.obtainResource());
|
||||
} else if (api.getMode() == RedisBungeeMode.CLUSTER) {
|
||||
// Jedis cluster does not need new instance since its single instance anyways.
|
||||
JedisClusterSummoner jedisClusterSummoner = (JedisClusterSummoner) summoner;
|
||||
return this.clusterJedisTask(jedisClusterSummoner.obtainResource());
|
||||
return this.unifiedJedisTask(jedisClusterSummoner.obtainResource());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.imaginarycode.minecraft.redisbungee.api.util.player.PlayerUtils;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@ -12,23 +13,12 @@ public class ShutdownUtils {
|
||||
public static void shutdownCleanup(RedisBungeePlugin<?> plugin) {
|
||||
new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
jedis.hdel("heartbeats", plugin.getConfiguration().getProxyId());
|
||||
if (jedis.scard("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline") > 0) {
|
||||
Set<String> players = jedis.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
unifiedJedis.hdel("heartbeats", plugin.getConfiguration().getProxyId());
|
||||
if (unifiedJedis.scard("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline") > 0) {
|
||||
Set<String> players = unifiedJedis.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
|
||||
for (String member : players)
|
||||
PlayerUtils.cleanUpPlayer(member, jedis, true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
jedisCluster.hdel("heartbeats", plugin.getConfiguration().getProxyId());
|
||||
if (jedisCluster.scard("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline") > 0) {
|
||||
Set<String> players = jedisCluster.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
|
||||
for (String member : players)
|
||||
PlayerUtils.cleanUpPlayer(member, jedisCluster, true);
|
||||
PlayerUtils.cleanUpPlayer(member, unifiedJedis, true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import com.imaginarycode.minecraft.redisbungee.api.AbstractDataManager;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.UUID;
|
||||
@ -13,76 +14,30 @@ import java.util.UUID;
|
||||
public class PayloadUtils {
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
public static void playerJoinPayload(UUID uuid, Pipeline pipeline, InetAddress inetAddress) {
|
||||
pipeline.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.JOIN,
|
||||
new AbstractDataManager.LoginPayload(inetAddress))));
|
||||
}
|
||||
public static void playerJoinPayload(UUID uuid, JedisCluster jedisCluster, InetAddress inetAddress) {
|
||||
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.JOIN,
|
||||
new AbstractDataManager.LoginPayload(inetAddress))));
|
||||
}
|
||||
public static void playerJoinPayload(UUID uuid, Jedis jedis, InetAddress inetAddress) {
|
||||
jedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
public static void playerJoinPayload(UUID uuid, UnifiedJedis unifiedJedis, InetAddress inetAddress) {
|
||||
unifiedJedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.JOIN,
|
||||
new AbstractDataManager.LoginPayload(inetAddress))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void playerQuitPayload(String uuid, Jedis jedis, long timestamp) {
|
||||
jedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
public static void playerQuitPayload(String uuid, UnifiedJedis unifiedJedis, long timestamp) {
|
||||
unifiedJedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
UUID.fromString(uuid), RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.LEAVE,
|
||||
new AbstractDataManager.LogoutPayload(timestamp))));
|
||||
}
|
||||
|
||||
|
||||
public static void playerQuitPayload(String uuid, JedisCluster jedisCluster, long timestamp) {
|
||||
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
UUID.fromString(uuid), RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.LEAVE,
|
||||
new AbstractDataManager.LogoutPayload(timestamp))));
|
||||
}
|
||||
|
||||
public static void playerQuitPayload(String uuid, Pipeline pipeline, long timestamp) {
|
||||
pipeline.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
UUID.fromString(uuid), RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.LEAVE,
|
||||
new AbstractDataManager.LogoutPayload(timestamp))));
|
||||
}
|
||||
|
||||
|
||||
public static void playerServerChangePayload(UUID uuid, Jedis jedis, String newServer, String oldServer) {
|
||||
jedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.SERVER_CHANGE,
|
||||
new AbstractDataManager.ServerChangePayload(newServer, oldServer))));
|
||||
}
|
||||
|
||||
public static void playerServerChangePayload(UUID uuid, Pipeline pipeline, String newServer, String oldServer) {
|
||||
pipeline.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.SERVER_CHANGE,
|
||||
new AbstractDataManager.ServerChangePayload(newServer, oldServer))));
|
||||
}
|
||||
public static void playerServerChangePayload(UUID uuid, JedisCluster jedisCluster, String newServer, String oldServer) {
|
||||
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
public static void playerServerChangePayload(UUID uuid, UnifiedJedis unifiedJedis, String newServer, String oldServer) {
|
||||
unifiedJedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.SERVER_CHANGE,
|
||||
new AbstractDataManager.ServerChangePayload(newServer, oldServer))));
|
||||
}
|
||||
|
||||
|
||||
public static void kickPlayerPayload(UUID uuid, String message, Pipeline pipeline) {
|
||||
pipeline.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.KICK,
|
||||
new AbstractDataManager.KickPayload(message))));
|
||||
}
|
||||
|
||||
public static void kickPlayerPayload(UUID uuid, String message, Jedis jedis) {
|
||||
jedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.KICK,
|
||||
new AbstractDataManager.KickPayload(message))));
|
||||
}
|
||||
|
||||
public static void kickPlayerPayload(UUID uuid, String message, JedisCluster jedisCluster) {
|
||||
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
public static void kickPlayerPayload(UUID uuid, String message, UnifiedJedis unifiedJedis) {
|
||||
unifiedJedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
|
||||
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.KICK,
|
||||
new AbstractDataManager.KickPayload(message))));
|
||||
}
|
||||
|
@ -1,37 +1,14 @@
|
||||
package com.imaginarycode.minecraft.redisbungee.api.util.player;
|
||||
|
||||
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import static com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils.playerQuitPayload;
|
||||
|
||||
public class PlayerUtils {
|
||||
|
||||
|
||||
public static void cleanUpPlayer(String uuid, Jedis rsc, boolean firePayload) {
|
||||
rsc.srem("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", uuid);
|
||||
rsc.hdel("player:" + uuid, "server", "ip", "proxy");
|
||||
long timestamp = System.currentTimeMillis();
|
||||
rsc.hset("player:" + uuid, "online", String.valueOf(timestamp));
|
||||
if (firePayload) {
|
||||
playerQuitPayload(uuid, rsc, timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
public static void cleanUpPlayer(String uuid, Pipeline rsc, boolean firePayload) {
|
||||
rsc.srem("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", uuid);
|
||||
rsc.hdel("player:" + uuid, "server", "ip", "proxy");
|
||||
long timestamp = System.currentTimeMillis();
|
||||
rsc.hset("player:" + uuid, "online", String.valueOf(timestamp));
|
||||
if (firePayload) {
|
||||
playerQuitPayload(uuid, rsc, timestamp);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void cleanUpPlayer(String uuid, JedisCluster rsc, boolean firePayload) {
|
||||
public static void cleanUpPlayer(String uuid, UnifiedJedis rsc, boolean firePayload) {
|
||||
rsc.srem("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", uuid);
|
||||
rsc.hdel("player:" + uuid, "server", "ip", "proxy");
|
||||
long timestamp = System.currentTimeMillis();
|
||||
|
@ -11,6 +11,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
|
||||
import java.util.*;
|
||||
@ -74,17 +75,17 @@ public final class UUIDTranslator {
|
||||
}
|
||||
RedisTask<UUID> redisTask = new RedisTask<UUID>(plugin.getApi()) {
|
||||
@Override
|
||||
public UUID jedisTask(Jedis jedis) {
|
||||
String stored = jedis.hget("uuid-cache", player.toLowerCase());
|
||||
public UUID unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
String stored = unifiedJedis.hget("uuid-cache", player.toLowerCase());
|
||||
if (stored != null) {
|
||||
// Found an entry value. Deserialize it.
|
||||
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
|
||||
|
||||
// Check for expiry:
|
||||
if (entry.expired()) {
|
||||
jedis.hdel("uuid-cache", player.toLowerCase());
|
||||
unifiedJedis.hdel("uuid-cache", player.toLowerCase());
|
||||
// Doesn't hurt to also remove the UUID entry as well.
|
||||
jedis.hdel("uuid-cache", entry.getUuid().toString());
|
||||
unifiedJedis.hdel("uuid-cache", entry.getUuid().toString());
|
||||
} else {
|
||||
nameToUuidMap.put(player.toLowerCase(), entry);
|
||||
uuidToNameMap.put(entry.getUuid(), entry);
|
||||
@ -105,46 +106,7 @@ public final class UUIDTranslator {
|
||||
}
|
||||
for (Map.Entry<String, UUID> entry : uuidMap1.entrySet()) {
|
||||
if (entry.getKey().equalsIgnoreCase(player)) {
|
||||
persistInfo(entry.getKey(), entry.getValue(), jedis);
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID clusterJedisTask(JedisCluster jedisCluster) {
|
||||
String stored = jedisCluster.hget("uuid-cache", player.toLowerCase());
|
||||
if (stored != null) {
|
||||
// Found an entry value. Deserialize it.
|
||||
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
|
||||
|
||||
// Check for expiry:
|
||||
if (entry.expired()) {
|
||||
jedisCluster.hdel("uuid-cache", player.toLowerCase());
|
||||
// Doesn't hurt to also remove the UUID entry as well.
|
||||
jedisCluster.hdel("uuid-cache", entry.getUuid().toString());
|
||||
} else {
|
||||
nameToUuidMap.put(player.toLowerCase(), entry);
|
||||
uuidToNameMap.put(entry.getUuid(), entry);
|
||||
return entry.getUuid();
|
||||
}
|
||||
}
|
||||
|
||||
// That didn't work. Let's ask Mojang.
|
||||
if (!expensiveLookups || !plugin.isOnlineMode())
|
||||
return null;
|
||||
|
||||
Map<String, UUID> uuidMap1;
|
||||
try {
|
||||
uuidMap1 = new UUIDFetcher(Collections.singletonList(player)).call();
|
||||
} catch (Exception e) {
|
||||
plugin.logFatal("Unable to fetch UUID from Mojang for " + player);
|
||||
return null;
|
||||
}
|
||||
for (Map.Entry<String, UUID> entry : uuidMap1.entrySet()) {
|
||||
if (entry.getKey().equalsIgnoreCase(player)) {
|
||||
persistInfo(entry.getKey(), entry.getValue(), jedisCluster);
|
||||
persistInfo(entry.getKey(), entry.getValue(), unifiedJedis);
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
@ -178,57 +140,18 @@ public final class UUIDTranslator {
|
||||
|
||||
RedisTask<String> redisTask = new RedisTask<String>(plugin.getApi()) {
|
||||
@Override
|
||||
public String jedisTask(Jedis jedis) {
|
||||
String stored = jedis.hget("uuid-cache", player.toString());
|
||||
public String unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
String stored = unifiedJedis.hget("uuid-cache", player.toString());
|
||||
if (stored != null) {
|
||||
// Found an entry value. Deserialize it.
|
||||
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
|
||||
|
||||
// Check for expiry:
|
||||
if (entry.expired()) {
|
||||
jedis.hdel("uuid-cache", player.toString());
|
||||
unifiedJedis.hdel("uuid-cache", player.toString());
|
||||
// Doesn't hurt to also remove the named entry as well.
|
||||
// TODO: Since UUIDs are fixed, we could look up the name and see if the UUID matches.
|
||||
jedis.hdel("uuid-cache", entry.getName());
|
||||
} else {
|
||||
nameToUuidMap.put(entry.getName().toLowerCase(), entry);
|
||||
uuidToNameMap.put(player, entry);
|
||||
return entry.getName();
|
||||
}
|
||||
}
|
||||
|
||||
if (!expensiveLookups || !plugin.isOnlineMode())
|
||||
return null;
|
||||
|
||||
// That didn't work. Let's ask Mojang. This call may fail, because Mojang is insane.
|
||||
String name;
|
||||
try {
|
||||
List<String> nameHist = NameFetcher.nameHistoryFromUuid(player);
|
||||
name = Iterables.getLast(nameHist, null);
|
||||
} catch (Exception e) {
|
||||
plugin.logFatal("Unable to fetch name from Mojang for " + player);
|
||||
return null;
|
||||
}
|
||||
if (name != null) {
|
||||
persistInfo(name, player, jedis);
|
||||
return name;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String clusterJedisTask(JedisCluster jedisCluster) {
|
||||
String stored = jedisCluster.hget("uuid-cache", player.toString());
|
||||
if (stored != null) {
|
||||
// Found an entry value. Deserialize it.
|
||||
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
|
||||
|
||||
// Check for expiry:
|
||||
if (entry.expired()) {
|
||||
jedisCluster.hdel("uuid-cache", player.toString());
|
||||
// Doesn't hurt to also remove the named entry as well.
|
||||
// TODO: Since UUIDs are fixed, we could look up the name and see if the UUID matches.
|
||||
jedisCluster.hdel("uuid-cache", entry.getName());
|
||||
unifiedJedis.hdel("uuid-cache", entry.getName());
|
||||
} else {
|
||||
nameToUuidMap.put(entry.getName().toLowerCase(), entry);
|
||||
uuidToNameMap.put(player, entry);
|
||||
@ -250,7 +173,7 @@ public final class UUIDTranslator {
|
||||
}
|
||||
|
||||
if (name != null) {
|
||||
persistInfo(name, player, jedisCluster);
|
||||
persistInfo(name, player, unifiedJedis);
|
||||
return name;
|
||||
}
|
||||
|
||||
@ -268,22 +191,10 @@ public final class UUIDTranslator {
|
||||
}
|
||||
}
|
||||
|
||||
public void persistInfo(String name, UUID uuid, Jedis jedis) {
|
||||
public void persistInfo(String name, UUID uuid, UnifiedJedis unifiedJedis) {
|
||||
addToMaps(name, uuid);
|
||||
String json = gson.toJson(uuidToNameMap.get(uuid));
|
||||
jedis.hmset("uuid-cache", ImmutableMap.of(name.toLowerCase(), json, uuid.toString(), json));
|
||||
}
|
||||
|
||||
public void persistInfo(String name, UUID uuid, Pipeline pipeline) {
|
||||
addToMaps(name, uuid);
|
||||
String json = gson.toJson(uuidToNameMap.get(uuid));
|
||||
pipeline.hmset("uuid-cache", ImmutableMap.of(name.toLowerCase(), json, uuid.toString(), json));
|
||||
}
|
||||
|
||||
public void persistInfo(String name, UUID uuid, JedisCluster jedisCluster) {
|
||||
addToMaps(name, uuid);
|
||||
String json = gson.toJson(uuidToNameMap.get(uuid));
|
||||
jedisCluster.hmset("uuid-cache", ImmutableMap.of(name.toLowerCase(), json, uuid.toString(), json));
|
||||
unifiedJedis.hmset("uuid-cache", ImmutableMap.of(name.toLowerCase(), json, uuid.toString(), json));
|
||||
}
|
||||
|
||||
private static class CachedUUIDEntry {
|
||||
|
@ -5,6 +5,7 @@ import net.md_5.bungee.api.connection.PendingConnection;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -13,43 +14,23 @@ import static com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUt
|
||||
|
||||
public class BungeePlayerUtils {
|
||||
|
||||
public static void createPlayer(ProxiedPlayer player, Pipeline pipeline, boolean fireEvent) {
|
||||
createPlayer(player.getPendingConnection(), pipeline, fireEvent);
|
||||
public static void createPlayer(ProxiedPlayer player, UnifiedJedis unifiedJedis, boolean fireEvent) {
|
||||
createPlayer(player.getPendingConnection(), unifiedJedis, fireEvent);
|
||||
if (player.getServer() != null)
|
||||
pipeline.hset("player:" + player.getUniqueId().toString(), "server", player.getServer().getInfo().getName());
|
||||
unifiedJedis.hset("player:" + player.getUniqueId().toString(), "server", player.getServer().getInfo().getName());
|
||||
}
|
||||
|
||||
public static void createPlayer(PendingConnection connection, Pipeline pipeline, boolean fireEvent) {
|
||||
public static void createPlayer(PendingConnection connection, UnifiedJedis unifiedJedis, boolean fireEvent) {
|
||||
Map<String, String> playerData = new HashMap<>(4);
|
||||
playerData.put("online", "0");
|
||||
playerData.put("ip", connection.getAddress().getAddress().getHostAddress());
|
||||
playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId());
|
||||
|
||||
pipeline.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", connection.getUniqueId().toString());
|
||||
pipeline.hmset("player:" + connection.getUniqueId().toString(), playerData);
|
||||
unifiedJedis.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", connection.getUniqueId().toString());
|
||||
unifiedJedis.hmset("player:" + connection.getUniqueId().toString(), playerData);
|
||||
|
||||
if (fireEvent) {
|
||||
playerJoinPayload(connection.getUniqueId(), pipeline, connection.getAddress().getAddress());
|
||||
}
|
||||
}
|
||||
|
||||
public static void createPlayer(ProxiedPlayer player, JedisCluster jedisCluster, boolean fireEvent) {
|
||||
createPlayer(player.getPendingConnection(), jedisCluster, fireEvent);
|
||||
if (player.getServer() != null)
|
||||
jedisCluster.hset("player:" + player.getUniqueId().toString(), "server", player.getServer().getInfo().getName());
|
||||
}
|
||||
|
||||
public static void createPlayer(PendingConnection connection, JedisCluster jedisCluster, boolean fireEvent) {
|
||||
Map<String, String> playerData = new HashMap<>(4);
|
||||
playerData.put("online", "0");
|
||||
playerData.put("ip", connection.getAddress().getAddress().getHostAddress());
|
||||
playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId());
|
||||
|
||||
jedisCluster.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", connection.getUniqueId().toString());
|
||||
jedisCluster.hmset("player:" + connection.getUniqueId().toString(), playerData);
|
||||
|
||||
if (fireEvent) {
|
||||
playerJoinPayload(connection.getUniqueId(), jedisCluster, connection.getAddress().getAddress());
|
||||
playerJoinPayload(connection.getUniqueId(), unifiedJedis, connection.getAddress().getAddress());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ import net.md_5.bungee.event.EventHandler;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.*;
|
||||
@ -40,7 +41,7 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
|
||||
event.registerIntent((Plugin) plugin);
|
||||
plugin.executeAsync(new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
try {
|
||||
if (event.isCancelled()) {
|
||||
return null;
|
||||
@ -60,41 +61,7 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
|
||||
}
|
||||
|
||||
for (String s : plugin.getProxiesIds()) {
|
||||
if (jedis.sismember("proxy:" + s + ":usersOnline", event.getConnection().getUniqueId().toString())) {
|
||||
event.setCancelled(true);
|
||||
// TODO: Make it accept a BaseComponent[] like everything else.
|
||||
event.setCancelReason(ALREADY_LOGGED_IN);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
event.completeIntent((Plugin) plugin);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
try {
|
||||
if (event.isCancelled()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// We make sure they aren't trying to use an existing player's name.
|
||||
// This is problematic for online-mode servers as they always disconnect old clients.
|
||||
if (plugin.isOnlineMode()) {
|
||||
ProxiedPlayer player = (ProxiedPlayer) plugin.getPlayer(event.getConnection().getName());
|
||||
|
||||
if (player != null) {
|
||||
event.setCancelled(true);
|
||||
// TODO: Make it accept a BaseComponent[] like everything else.
|
||||
event.setCancelReason(ONLINE_MODE_RECONNECT);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
for (String s : plugin.getProxiesIds()) {
|
||||
if (jedisCluster.sismember("proxy:" + s + ":usersOnline", event.getConnection().getUniqueId().toString())) {
|
||||
if (unifiedJedis.sismember("proxy:" + s + ":usersOnline", event.getConnection().getUniqueId().toString())) {
|
||||
event.setCancelled(true);
|
||||
// TODO: Make it accept a BaseComponent[] like everything else.
|
||||
event.setCancelReason(ALREADY_LOGGED_IN);
|
||||
@ -114,24 +81,9 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
|
||||
public void onPostLogin(PostLoginEvent event) {
|
||||
plugin.executeAsync(new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
// this code was moved out from login event due being async..
|
||||
// and it can be cancelled but it will show as false in redis-bungee
|
||||
// which will register the player into the redis database.
|
||||
Pipeline pipeline = jedis.pipelined();
|
||||
plugin.getUuidTranslator().persistInfo(event.getPlayer().getName(), event.getPlayer().getUniqueId(), pipeline);
|
||||
BungeePlayerUtils.createPlayer(event.getPlayer(), pipeline, true);
|
||||
pipeline.sync();
|
||||
// the end of moved code.
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
|
||||
// due no support to pipeline in jedis cluster just use the instance instead.
|
||||
plugin.getUuidTranslator().persistInfo(event.getPlayer().getName(), event.getPlayer().getUniqueId(), jedisCluster);
|
||||
BungeePlayerUtils.createPlayer(event.getPlayer(), jedisCluster, true);
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
plugin.getUuidTranslator().persistInfo(event.getPlayer().getName(), event.getPlayer().getUniqueId(), unifiedJedis);
|
||||
BungeePlayerUtils.createPlayer(event.getPlayer(), unifiedJedis, true);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
@ -142,17 +94,8 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
|
||||
public void onPlayerDisconnect(PlayerDisconnectEvent event) {
|
||||
plugin.executeAsync(new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
Pipeline pipeline = jedis.pipelined();
|
||||
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), pipeline, true);
|
||||
pipeline.sync();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
// Due some reason JedisCluster does not support pipeline, use instance instead
|
||||
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), jedisCluster, true);
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), unifiedJedis, true);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
@ -166,16 +109,9 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
|
||||
final String oldServer = event.getPlayer().getServer() == null ? null : event.getPlayer().getServer().getInfo().getName();
|
||||
plugin.executeAsync(new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
jedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", event.getServer().getInfo().getName());
|
||||
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), jedis, currentServer, oldServer);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
jedisCluster.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", event.getServer().getInfo().getName());
|
||||
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), jedisCluster, currentServer, oldServer);
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
unifiedJedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", event.getServer().getInfo().getName());
|
||||
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), unifiedJedis, currentServer, oldServer);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
@ -221,21 +221,12 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
|
||||
|
||||
IntegrityCheckTask integrityCheckTask = new IntegrityCheckTask(this) {
|
||||
@Override
|
||||
public void handlePlatformPlayer(String player, JedisCluster jedis) {
|
||||
public void handlePlatformPlayer(String player, UnifiedJedis unifiedJedis) {
|
||||
ProxiedPlayer proxiedPlayer = ProxyServer.getInstance().getPlayer(UUID.fromString(player));
|
||||
if (proxiedPlayer == null)
|
||||
return; // We'll deal with it later.
|
||||
|
||||
BungeePlayerUtils.createPlayer(proxiedPlayer, jedis, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePlatformPlayer(String player, Pipeline pipeline) {
|
||||
ProxiedPlayer proxiedPlayer = ProxyServer.getInstance().getPlayer(UUID.fromString(player));
|
||||
if (proxiedPlayer == null)
|
||||
return; // We'll deal with it later.
|
||||
|
||||
BungeePlayerUtils.createPlayer(proxiedPlayer, pipeline, false);
|
||||
BungeePlayerUtils.createPlayer(proxiedPlayer, unifiedJedis, false);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -30,6 +30,7 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.*;
|
||||
@ -47,7 +48,7 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
|
||||
public void onLogin(LoginEvent event, Continuation continuation) {
|
||||
plugin.executeAsync(new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
try {
|
||||
if (!event.getResult().isAllowed()) {
|
||||
return null;
|
||||
@ -65,37 +66,7 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
|
||||
}
|
||||
|
||||
for (String s : plugin.getProxiesIds()) {
|
||||
if (jedis.sismember("proxy:" + s + ":usersOnline", event.getPlayer().getUniqueId().toString())) {
|
||||
event.setResult(ResultedEvent.ComponentResult.denied(serializer.deserialize(ALREADY_LOGGED_IN)));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
continuation.resume();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
try {
|
||||
if (!event.getResult().isAllowed()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// We make sure they aren't trying to use an existing player's name.
|
||||
// This is problematic for online-mode servers as they always disconnect old clients.
|
||||
if (plugin.isOnlineMode()) {
|
||||
Player player = (Player) plugin.getPlayer(event.getPlayer().getUsername());
|
||||
|
||||
if (player != null) {
|
||||
event.setResult(ResultedEvent.ComponentResult.denied(serializer.deserialize(ONLINE_MODE_RECONNECT)));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
for (String s : plugin.getProxiesIds()) {
|
||||
if (jedisCluster.sismember("proxy:" + s + ":usersOnline", event.getPlayer().getUniqueId().toString())) {
|
||||
if (unifiedJedis.sismember("proxy:" + s + ":usersOnline", event.getPlayer().getUniqueId().toString())) {
|
||||
event.setResult(ResultedEvent.ComponentResult.denied(serializer.deserialize(ALREADY_LOGGED_IN)));
|
||||
return null;
|
||||
}
|
||||
@ -114,18 +85,9 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
|
||||
public void onPostLogin(PostLoginEvent event) {
|
||||
plugin.executeAsync(new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
Pipeline pipeline = jedis.pipelined();
|
||||
plugin.getUuidTranslator().persistInfo(event.getPlayer().getUsername(), event.getPlayer().getUniqueId(), pipeline);
|
||||
VelocityPlayerUtils.createPlayer(event.getPlayer(), pipeline, true);
|
||||
pipeline.sync();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
plugin.getUuidTranslator().persistInfo(event.getPlayer().getUsername(), event.getPlayer().getUniqueId(), jedisCluster);
|
||||
VelocityPlayerUtils.createPlayer(event.getPlayer(), jedisCluster, true);
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
plugin.getUuidTranslator().persistInfo(event.getPlayer().getUsername(), event.getPlayer().getUniqueId(), unifiedJedis);
|
||||
VelocityPlayerUtils.createPlayer(event.getPlayer(), unifiedJedis, true);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
@ -136,16 +98,8 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
|
||||
public void onPlayerDisconnect(DisconnectEvent event) {
|
||||
plugin.executeAsync(new RedisTask<Void>(plugin) {
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
Pipeline pipeline = jedis.pipelined();
|
||||
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), pipeline, true);
|
||||
pipeline.sync();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), jedisCluster, true);
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), unifiedJedis, true);
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -159,18 +113,10 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
|
||||
final String currentServer = event.getServer().getServerInfo().getName();
|
||||
final String oldServer = event.getPreviousServer().map(serverConnection -> serverConnection.getServerInfo().getName()).orElse(null);
|
||||
plugin.executeAsync(new RedisTask<Void>(plugin) {
|
||||
|
||||
@Override
|
||||
public Void jedisTask(Jedis jedis) {
|
||||
jedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", currentServer);
|
||||
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), jedis, currentServer, oldServer);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void clusterJedisTask(JedisCluster jedisCluster) {
|
||||
jedisCluster.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", currentServer);
|
||||
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), jedisCluster, currentServer, oldServer);
|
||||
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
||||
unifiedJedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", currentServer);
|
||||
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), unifiedJedis, currentServer, oldServer);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
@ -244,19 +244,11 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
|
||||
|
||||
IntegrityCheckTask integrityCheckTask = new IntegrityCheckTask(this) {
|
||||
@Override
|
||||
public void handlePlatformPlayer(String player, JedisCluster jedisCluster) {
|
||||
public void handlePlatformPlayer(String player, UnifiedJedis unifiedJedis) {
|
||||
Player playerProxied = getProxy().getPlayer(UUID.fromString(player)).orElse(null);
|
||||
if (playerProxied == null)
|
||||
return; // We'll deal with it later.
|
||||
VelocityPlayerUtils.createPlayer(playerProxied, jedisCluster, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePlatformPlayer(String player, Pipeline pipeline) {
|
||||
Player playerProxied = getProxy().getPlayer(UUID.fromString(player)).orElse(null);
|
||||
if (playerProxied == null)
|
||||
return; // We'll deal with it later.
|
||||
VelocityPlayerUtils.createPlayer(playerProxied, pipeline, false);
|
||||
VelocityPlayerUtils.createPlayer(playerProxied, unifiedJedis, false);
|
||||
}
|
||||
};
|
||||
integrityCheck = getProxy().getScheduler().buildTask(this, integrityCheckTask::execute).repeat(30, TimeUnit.SECONDS).schedule();
|
||||
|
@ -4,6 +4,7 @@ import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ServerConnection;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.UnifiedJedis;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -12,38 +13,20 @@ import java.util.Optional;
|
||||
import static com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils.playerJoinPayload;
|
||||
|
||||
public class VelocityPlayerUtils {
|
||||
|
||||
protected static void createPlayer(Player player, Pipeline pipeline, boolean fireEvent) {
|
||||
protected static void createPlayer(Player player, UnifiedJedis unifiedJedis, boolean fireEvent) {
|
||||
Optional<ServerConnection> server = player.getCurrentServer();
|
||||
server.ifPresent(serverConnection -> pipeline.hset("player:" + player.getUniqueId().toString(), "server", serverConnection.getServerInfo().getName()));
|
||||
server.ifPresent(serverConnection -> unifiedJedis.hset("player:" + player.getUniqueId().toString(), "server", serverConnection.getServerInfo().getName()));
|
||||
|
||||
Map<String, String> playerData = new HashMap<>(4);
|
||||
playerData.put("online", "0");
|
||||
playerData.put("ip", player.getRemoteAddress().getHostName());
|
||||
playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId());
|
||||
|
||||
pipeline.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", player.getUniqueId().toString());
|
||||
pipeline.hmset("player:" + player.getUniqueId().toString(), playerData);
|
||||
unifiedJedis.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", player.getUniqueId().toString());
|
||||
unifiedJedis.hmset("player:" + player.getUniqueId().toString(), playerData);
|
||||
|
||||
if (fireEvent) {
|
||||
playerJoinPayload(player.getUniqueId(), pipeline, player.getRemoteAddress().getAddress());
|
||||
}
|
||||
}
|
||||
|
||||
protected static void createPlayer(Player player, JedisCluster jedisCluster, boolean fireEvent) {
|
||||
Optional<ServerConnection> server = player.getCurrentServer();
|
||||
server.ifPresent(serverConnection -> jedisCluster.hset("player:" + player.getUniqueId().toString(), "server", serverConnection.getServerInfo().getName()));
|
||||
|
||||
Map<String, String> playerData = new HashMap<>(4);
|
||||
playerData.put("online", "0");
|
||||
playerData.put("ip", player.getRemoteAddress().getHostName());
|
||||
playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId());
|
||||
|
||||
jedisCluster.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", player.getUniqueId().toString());
|
||||
jedisCluster.hmset("player:" + player.getUniqueId().toString(), playerData);
|
||||
|
||||
if (fireEvent) {
|
||||
playerJoinPayload(player.getUniqueId(), jedisCluster, player.getRemoteAddress().getAddress());
|
||||
playerJoinPayload(player.getUniqueId(), unifiedJedis, player.getRemoteAddress().getAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user