2022-07-26 06:58:00 +00:00
|
|
|
package com.imaginarycode.minecraft.redisbungee.api.tasks;
|
|
|
|
|
2022-07-26 08:49:44 +00:00
|
|
|
import com.imaginarycode.minecraft.redisbungee.api.util.player.PlayerUtils;
|
2022-07-26 06:58:00 +00:00
|
|
|
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
2022-07-26 13:47:18 +00:00
|
|
|
import redis.clients.jedis.UnifiedJedis;
|
2022-07-26 06:58:00 +00:00
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
public abstract class IntegrityCheckTask extends RedisTask<Void> {
|
|
|
|
|
|
|
|
public static int INTERVAL = 30;
|
|
|
|
public static TimeUnit TIMEUNIT = TimeUnit.SECONDS;
|
|
|
|
|
|
|
|
|
|
|
|
public IntegrityCheckTask(RedisBungeePlugin<?> plugin) {
|
|
|
|
super(plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-07-26 13:47:18 +00:00
|
|
|
public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
|
2022-07-26 06:58:00 +00:00
|
|
|
try {
|
|
|
|
Set<String> players = plugin.getLocalPlayersAsUuidStrings();
|
2022-07-26 13:47:18 +00:00
|
|
|
Set<String> playersInRedis = unifiedJedis.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
|
2022-07-26 06:58:00 +00:00
|
|
|
List<String> lagged = plugin.getCurrentProxiesIds(true);
|
|
|
|
|
|
|
|
// Clean up lagged players.
|
|
|
|
for (String s : lagged) {
|
2022-07-26 13:47:18 +00:00
|
|
|
Set<String> laggedPlayers = unifiedJedis.smembers("proxy:" + s + ":usersOnline");
|
|
|
|
unifiedJedis.del("proxy:" + s + ":usersOnline");
|
2022-07-26 06:58:00 +00:00
|
|
|
if (!laggedPlayers.isEmpty()) {
|
|
|
|
plugin.logInfo("Cleaning up lagged proxy " + s + " (" + laggedPlayers.size() + " players)...");
|
|
|
|
for (String laggedPlayer : laggedPlayers) {
|
2022-07-26 13:47:18 +00:00
|
|
|
PlayerUtils.cleanUpPlayer(laggedPlayer, unifiedJedis, true);
|
2022-07-26 06:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-07-26 13:47:18 +00:00
|
|
|
if (unifiedJedis.sismember("proxy:" + proxyId + ":usersOnline", member)) {
|
2022-07-26 06:58:00 +00:00
|
|
|
// Just clean up the set.
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2022-07-26 13:47:18 +00:00
|
|
|
PlayerUtils.cleanUpPlayer(member, unifiedJedis, false);
|
2022-07-26 06:58:00 +00:00
|
|
|
plugin.logWarn("Player found in set that was not found locally and globally: " + member);
|
|
|
|
} else {
|
2022-07-26 13:47:18 +00:00
|
|
|
unifiedJedis.srem("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline", member);
|
2022-07-26 06:58:00 +00:00
|
|
|
plugin.logWarn("Player found in set that was not found locally, but is on another proxy: " + member);
|
|
|
|
}
|
|
|
|
}
|
2022-07-26 13:47:18 +00:00
|
|
|
// due unifiedJedis does not support pipelined.
|
2022-07-26 06:58:00 +00:00
|
|
|
//Pipeline pipeline = jedis.pipelined();
|
|
|
|
|
|
|
|
for (String player : absentInRedis) {
|
|
|
|
// Player not online according to Redis but not BungeeCord.
|
2022-07-26 13:47:18 +00:00
|
|
|
handlePlatformPlayer(player, unifiedJedis);
|
2022-07-26 06:58:00 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
|
|
plugin.logFatal("Unable to fix up stored player data");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-26 13:47:18 +00:00
|
|
|
public abstract void handlePlatformPlayer(String player, UnifiedJedis unifiedJedis);
|
2022-07-26 06:58:00 +00:00
|
|
|
|
|
|
|
}
|