mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-11-23 04:28:01 +00:00
Enhanced server listing API (now more efficient) and fixed count issues.
This commit is contained in:
parent
5a6d2b15bb
commit
f685661462
@ -117,10 +117,8 @@ public final class RedisBungee extends Plugin {
|
|||||||
|
|
||||||
final Multimap<String, UUID> serversToPlayers() {
|
final Multimap<String, UUID> serversToPlayers() {
|
||||||
ImmutableMultimap.Builder<String, UUID> multimapBuilder = ImmutableMultimap.builder();
|
ImmutableMultimap.Builder<String, UUID> multimapBuilder = ImmutableMultimap.builder();
|
||||||
for (UUID p : getPlayers()) {
|
for (ServerInfo info : getProxy().getServers().values()) {
|
||||||
String name = dataManager.getServer(p);
|
multimapBuilder.putAll(info.getName(), getPlayersOnServer(info.getName()));
|
||||||
if (name != null)
|
|
||||||
multimapBuilder.put(name, p);
|
|
||||||
}
|
}
|
||||||
return multimapBuilder.build();
|
return multimapBuilder.build();
|
||||||
}
|
}
|
||||||
@ -130,23 +128,12 @@ public final class RedisBungee extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final int getCurrentCount() {
|
final int getCurrentCount() {
|
||||||
int c = getProxy().getOnlineCount();
|
int c = 0;
|
||||||
if (pool != null) {
|
if (pool != null) {
|
||||||
Jedis rsc = pool.getResource();
|
Jedis rsc = pool.getResource();
|
||||||
try {
|
try {
|
||||||
List<String> serverIds = getServerIds();
|
for (String i : getServerIds()) {
|
||||||
Map<String, String> counts = rsc.hgetAll("playerCounts");
|
c += rsc.scard("proxy:" + i + ":usersOnline");
|
||||||
for (Map.Entry<String, String> entry : counts.entrySet()) {
|
|
||||||
if (!serverIds.contains(entry.getKey()))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (entry.getKey().equals(serverId)) continue;
|
|
||||||
|
|
||||||
try {
|
|
||||||
c += Integer.valueOf(entry.getValue());
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
rsc.hset("playerCounts", entry.getKey(), "0");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (JedisConnectionException e) {
|
} catch (JedisConnectionException e) {
|
||||||
// Redis server has disappeared!
|
// Redis server has disappeared!
|
||||||
@ -207,8 +194,29 @@ public final class RedisBungee extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Set<UUID> getPlayersOnServer(@NonNull String server) {
|
final Set<UUID> getPlayersOnServer(@NonNull String server) {
|
||||||
checkArgument(getProxy().getServerInfo(server) != null, "server doesn't exist");
|
ServerInfo info = getProxy().getServerInfo(server);
|
||||||
return ImmutableSet.copyOf(serversToPlayers().get(server));
|
checkArgument(info != null, "server doesn't exist");
|
||||||
|
|
||||||
|
ImmutableSet.Builder<UUID> setBuilder = ImmutableSet.builder();
|
||||||
|
|
||||||
|
Jedis jedis = pool.getResource();
|
||||||
|
try {
|
||||||
|
for (String s : jedis.smembers("server:" + server + ":players")) {
|
||||||
|
try {
|
||||||
|
setBuilder.add(UUID.fromString(s));
|
||||||
|
} catch (IllegalArgumentException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (JedisConnectionException e) {
|
||||||
|
// Redis server has disappeared!
|
||||||
|
getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e);
|
||||||
|
pool.returnBrokenResource(jedis);
|
||||||
|
throw new RuntimeException("Unable to publish command", e);
|
||||||
|
} finally {
|
||||||
|
pool.returnResource(jedis);
|
||||||
|
}
|
||||||
|
|
||||||
|
return setBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
|
final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
|
||||||
@ -252,7 +260,6 @@ public final class RedisBungee extends Plugin {
|
|||||||
if (pool != null) {
|
if (pool != null) {
|
||||||
Jedis tmpRsc = pool.getResource();
|
Jedis tmpRsc = pool.getResource();
|
||||||
try {
|
try {
|
||||||
tmpRsc.hset("playerCounts", serverId, "0"); // reset
|
|
||||||
tmpRsc.hset("heartbeats", serverId, String.valueOf(System.currentTimeMillis()));
|
tmpRsc.hset("heartbeats", serverId, String.valueOf(System.currentTimeMillis()));
|
||||||
} finally {
|
} finally {
|
||||||
pool.returnResource(tmpRsc);
|
pool.returnResource(tmpRsc);
|
||||||
@ -265,13 +272,10 @@ public final class RedisBungee extends Plugin {
|
|||||||
public void run() {
|
public void run() {
|
||||||
Jedis rsc = pool.getResource();
|
Jedis rsc = pool.getResource();
|
||||||
try {
|
try {
|
||||||
Pipeline pipeline = rsc.pipelined();
|
rsc.hset("heartbeats", serverId, String.valueOf(System.currentTimeMillis()));
|
||||||
pipeline.hset("playerCounts", serverId, String.valueOf(getProxy().getOnlineCount()));
|
|
||||||
pipeline.hset("heartbeats", serverId, String.valueOf(System.currentTimeMillis()));
|
|
||||||
pipeline.sync();
|
|
||||||
} catch (JedisConnectionException e) {
|
} catch (JedisConnectionException e) {
|
||||||
// Redis server has disappeared!
|
// Redis server has disappeared!
|
||||||
getLogger().log(Level.SEVERE, "Unable to update proxy counts - did your Redis server go away?", e);
|
getLogger().log(Level.SEVERE, "Unable to update heartbeat - did your Redis server go away?", e);
|
||||||
pool.returnBrokenResource(rsc);
|
pool.returnBrokenResource(rsc);
|
||||||
} finally {
|
} finally {
|
||||||
pool.returnResource(rsc);
|
pool.returnResource(rsc);
|
||||||
@ -341,7 +345,6 @@ public final class RedisBungee extends Plugin {
|
|||||||
consumer.stop();
|
consumer.stop();
|
||||||
Jedis tmpRsc = pool.getResource();
|
Jedis tmpRsc = pool.getResource();
|
||||||
try {
|
try {
|
||||||
tmpRsc.hdel("playerCounts", serverId);
|
|
||||||
tmpRsc.hdel("heartbeats", serverId);
|
tmpRsc.hdel("heartbeats", serverId);
|
||||||
if (tmpRsc.scard("proxy:" + serverId + ":usersOnline") > 0) {
|
if (tmpRsc.scard("proxy:" + serverId + ":usersOnline") > 0) {
|
||||||
Set<String> players = tmpRsc.smembers("proxy:" + serverId + ":usersOnline");
|
Set<String> players = tmpRsc.smembers("proxy:" + serverId + ":usersOnline");
|
||||||
|
@ -59,6 +59,10 @@ public class RedisBungeeConsumer implements Runnable {
|
|||||||
PlayerLoggedOffConsumerEvent event1 = (PlayerLoggedOffConsumerEvent) event;
|
PlayerLoggedOffConsumerEvent event1 = (PlayerLoggedOffConsumerEvent) event;
|
||||||
Pipeline pipeline = jedis.pipelined();
|
Pipeline pipeline = jedis.pipelined();
|
||||||
jedis.hset("player:" + event1.getPlayer().getUniqueId().toString(), "online", String.valueOf(System.currentTimeMillis()));
|
jedis.hset("player:" + event1.getPlayer().getUniqueId().toString(), "online", String.valueOf(System.currentTimeMillis()));
|
||||||
|
|
||||||
|
if (event1.getPlayer().getServer() != null)
|
||||||
|
pipeline.srem("server:" + event1.getPlayer().getServer().getInfo().getName() + ":players", event1.getPlayer().getUniqueId().toString());
|
||||||
|
|
||||||
RedisUtil.cleanUpPlayer(event1.getPlayer().getUniqueId().toString(), pipeline);
|
RedisUtil.cleanUpPlayer(event1.getPlayer().getUniqueId().toString(), pipeline);
|
||||||
pipeline.publish("redisbungee-data", RedisBungee.getGson().toJson(new DataManager.DataManagerMessage(event1.getPlayer().getUniqueId(), DataManager.DataManagerMessage.Action.LEAVE)));
|
pipeline.publish("redisbungee-data", RedisBungee.getGson().toJson(new DataManager.DataManagerMessage(event1.getPlayer().getUniqueId(), DataManager.DataManagerMessage.Action.LEAVE)));
|
||||||
pipeline.sync();
|
pipeline.sync();
|
||||||
@ -66,6 +70,11 @@ public class RedisBungeeConsumer implements Runnable {
|
|||||||
PlayerChangedServerConsumerEvent event1 = (PlayerChangedServerConsumerEvent) event;
|
PlayerChangedServerConsumerEvent event1 = (PlayerChangedServerConsumerEvent) event;
|
||||||
Pipeline pipeline = jedis.pipelined();
|
Pipeline pipeline = jedis.pipelined();
|
||||||
pipeline.hset("player:" + event1.getPlayer().getUniqueId().toString(), "server", event1.getNewServer().getName());
|
pipeline.hset("player:" + event1.getPlayer().getUniqueId().toString(), "server", event1.getNewServer().getName());
|
||||||
|
|
||||||
|
if (event1.getOldServer() != null)
|
||||||
|
pipeline.srem("server:" + event1.getOldServer().getName() + ":players", event1.getPlayer().getUniqueId().toString());
|
||||||
|
|
||||||
|
pipeline.sadd("server:" + event1.getNewServer().getName() + ":players", event1.getPlayer().getUniqueId().toString());
|
||||||
pipeline.publish("redisbungee-data", RedisBungee.getGson().toJson(new DataManager.DataManagerMessage(event1.getPlayer().getUniqueId(), DataManager.DataManagerMessage.Action.SERVER_CHANGE)));
|
pipeline.publish("redisbungee-data", RedisBungee.getGson().toJson(new DataManager.DataManagerMessage(event1.getPlayer().getUniqueId(), DataManager.DataManagerMessage.Action.SERVER_CHANGE)));
|
||||||
pipeline.sync();
|
pipeline.sync();
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,15 @@ import redis.clients.jedis.Pipeline;
|
|||||||
|
|
||||||
class RedisUtil {
|
class RedisUtil {
|
||||||
public static void cleanUpPlayer(String player, Jedis rsc) {
|
public static void cleanUpPlayer(String player, Jedis rsc) {
|
||||||
rsc.srem("proxy:" + RedisBungee.getApi().getServerId() + ":usersOnline", player);
|
String server = rsc.hget("player:" + player, "server");
|
||||||
rsc.hdel("player:" + player, "server");
|
|
||||||
rsc.hdel("player:" + player, "ip");
|
Pipeline pipeline = rsc.pipelined();
|
||||||
rsc.hdel("player:" + player, "proxy");
|
|
||||||
|
if (server != null)
|
||||||
|
pipeline.srem("server:" + server + ":players", player);
|
||||||
|
|
||||||
|
cleanUpPlayer(player, pipeline);
|
||||||
|
pipeline.sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void cleanUpPlayer(String player, Pipeline rsc) {
|
public static void cleanUpPlayer(String player, Pipeline rsc) {
|
||||||
|
@ -6,14 +6,19 @@
|
|||||||
*/
|
*/
|
||||||
package com.imaginarycode.minecraft.redisbungee.consumerevents;
|
package com.imaginarycode.minecraft.redisbungee.consumerevents;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import net.md_5.bungee.api.config.ServerInfo;
|
import net.md_5.bungee.api.config.ServerInfo;
|
||||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Getter
|
@Getter
|
||||||
public class PlayerChangedServerConsumerEvent implements ConsumerEvent {
|
public class PlayerChangedServerConsumerEvent implements ConsumerEvent {
|
||||||
private final ProxiedPlayer player;
|
private final ProxiedPlayer player;
|
||||||
|
private final ServerInfo oldServer;
|
||||||
private final ServerInfo newServer;
|
private final ServerInfo newServer;
|
||||||
|
|
||||||
|
public PlayerChangedServerConsumerEvent(ProxiedPlayer player, ServerInfo newServer) {
|
||||||
|
this.player = player;
|
||||||
|
this.newServer = newServer;
|
||||||
|
this.oldServer = player.getServer() != null ? player.getServer().getInfo() : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user