mirror of
				https://github.com/proxiodev/RedisBungee.git
				synced 2025-11-04 01:28:02 +00:00 
			
		
		
		
	More robust error checking.
This commit is contained in:
		
							parent
							
								
									b604434091
								
							
						
					
					
						commit
						5a28cd9ede
					
				@ -7,10 +7,8 @@
 | 
				
			|||||||
package com.imaginarycode.minecraft.redisbungee;
 | 
					package com.imaginarycode.minecraft.redisbungee;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.google.common.base.Joiner;
 | 
					import com.google.common.base.Joiner;
 | 
				
			||||||
import com.google.common.collect.HashMultimap;
 | 
					import com.google.common.collect.*;
 | 
				
			||||||
import com.google.common.collect.ImmutableMultimap;
 | 
					import com.google.common.io.ByteArrayDataInput;
 | 
				
			||||||
import com.google.common.collect.ImmutableSet;
 | 
					 | 
				
			||||||
import com.google.common.collect.Multimap;
 | 
					 | 
				
			||||||
import com.google.common.io.ByteArrayDataOutput;
 | 
					import com.google.common.io.ByteArrayDataOutput;
 | 
				
			||||||
import com.google.common.io.ByteStreams;
 | 
					import com.google.common.io.ByteStreams;
 | 
				
			||||||
import lombok.NonNull;
 | 
					import lombok.NonNull;
 | 
				
			||||||
@ -44,11 +42,11 @@ import static com.google.common.base.Preconditions.*;
 | 
				
			|||||||
 * The only function of interest is {@link #getApi()}, which exposes some functions in this class.
 | 
					 * The only function of interest is {@link #getApi()}, which exposes some functions in this class.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public final class RedisBungee extends Plugin implements Listener {
 | 
					public final class RedisBungee extends Plugin implements Listener {
 | 
				
			||||||
    private static final ServerPing.PlayerInfo[] EMPTY_PLAYERINFO = new ServerPing.PlayerInfo[]{};
 | 
					 | 
				
			||||||
    private static Configuration configuration;
 | 
					    private static Configuration configuration;
 | 
				
			||||||
    private JedisPool pool;
 | 
					    private JedisPool pool;
 | 
				
			||||||
    private static RedisBungeeAPI api;
 | 
					    private static RedisBungeeAPI api;
 | 
				
			||||||
    private PubSubListener psl = null;
 | 
					    private PubSubListener psl = null;
 | 
				
			||||||
 | 
					    private static List<String> serverIds;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Fetch the {@link RedisBungeeAPI} object created on plugin start.
 | 
					     * Fetch the {@link RedisBungeeAPI} object created on plugin start.
 | 
				
			||||||
@ -63,14 +61,18 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
        return configuration;
 | 
					        return configuration;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    static List<String> getServerIds() {
 | 
				
			||||||
 | 
					        return serverIds;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    final Multimap<String, String> serversToPlayers() {
 | 
					    final Multimap<String, String> serversToPlayers() {
 | 
				
			||||||
        Multimap<String, String> serverToPlayers = HashMultimap.create();
 | 
					        ImmutableMultimap.Builder<String, String> multimapBuilder = ImmutableMultimap.builder();
 | 
				
			||||||
        for (String p : getPlayers()) {
 | 
					        for (String p : getPlayers()) {
 | 
				
			||||||
            ServerInfo si = getServerFor(p);
 | 
					            ServerInfo si = getServerFor(p);
 | 
				
			||||||
            if (si != null)
 | 
					            if (si != null)
 | 
				
			||||||
                serverToPlayers.put(si.getName(), p);
 | 
					                multimapBuilder = multimapBuilder.put(si.getName(), p);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return ImmutableMultimap.copyOf(serverToPlayers);
 | 
					        return multimapBuilder.build();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    final int getCount() {
 | 
					    final int getCount() {
 | 
				
			||||||
@ -78,10 +80,15 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
        if (pool != null) {
 | 
					        if (pool != null) {
 | 
				
			||||||
            Jedis rsc = pool.getResource();
 | 
					            Jedis rsc = pool.getResource();
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                for (String i : configuration.getStringList("linked-servers")) {
 | 
					                for (String i : serverIds) {
 | 
				
			||||||
                    if (i.equals(configuration.getString("server-id"))) continue;
 | 
					                    if (i.equals(configuration.getString("server-id"))) continue;
 | 
				
			||||||
                    if (rsc.exists("server:" + i + ":playerCount"))
 | 
					                    if (rsc.exists("server:" + i + ":playerCount"))
 | 
				
			||||||
 | 
					                        try {
 | 
				
			||||||
                            c += Integer.valueOf(rsc.get("server:" + i + ":playerCount"));
 | 
					                            c += Integer.valueOf(rsc.get("server:" + i + ":playerCount"));
 | 
				
			||||||
 | 
					                        } catch (NumberFormatException e) {
 | 
				
			||||||
 | 
					                            getLogger().severe("I found a funny number for " + i + "'s player count. Resetting it to 0.");
 | 
				
			||||||
 | 
					                            rsc.set("server:" + i + ":playerCount", "0");
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            } finally {
 | 
					            } finally {
 | 
				
			||||||
                pool.returnResource(rsc);
 | 
					                pool.returnResource(rsc);
 | 
				
			||||||
@ -91,26 +98,28 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    final Set<String> getLocalPlayers() {
 | 
					    final Set<String> getLocalPlayers() {
 | 
				
			||||||
        Set<String> players = new HashSet<>();
 | 
					        ImmutableSet.Builder<String> setBuilder = ImmutableSet.builder();
 | 
				
			||||||
        for (ProxiedPlayer pp : getProxy().getPlayers())
 | 
					        for (ProxiedPlayer pp : getProxy().getPlayers())
 | 
				
			||||||
            players.add(pp.getName());
 | 
					            setBuilder = setBuilder.add(pp.getName());
 | 
				
			||||||
        return players;
 | 
					        return setBuilder.build();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    final Set<String> getPlayers() {
 | 
					    final Set<String> getPlayers() {
 | 
				
			||||||
        Set<String> players = getLocalPlayers();
 | 
					        ImmutableSet.Builder<String> setBuilder = ImmutableSet.<String>builder().addAll(getLocalPlayers());
 | 
				
			||||||
        if (pool != null) {
 | 
					        if (pool != null) {
 | 
				
			||||||
            Jedis rsc = pool.getResource();
 | 
					            Jedis rsc = pool.getResource();
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                for (String i : configuration.getStringList("linked-servers")) {
 | 
					                for (String i : serverIds) {
 | 
				
			||||||
                    if (i.equals(configuration.getString("server-id"))) continue;
 | 
					                    if (i.equals(configuration.getString("server-id"))) continue;
 | 
				
			||||||
                    players.addAll(rsc.smembers("server:" + i + ":usersOnline"));
 | 
					                    Set<String> users = rsc.smembers("server:" + i + ":usersOnline");
 | 
				
			||||||
 | 
					                    if (users != null && !users.isEmpty())
 | 
				
			||||||
 | 
					                        setBuilder = setBuilder.addAll(users);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            } finally {
 | 
					            } finally {
 | 
				
			||||||
                pool.returnResource(rsc);
 | 
					                pool.returnResource(rsc);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return ImmutableSet.copyOf(players);
 | 
					        return setBuilder.build();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    final Set<String> getPlayersOnServer(@NonNull String server) {
 | 
					    final Set<String> getPlayersOnServer(@NonNull String server) {
 | 
				
			||||||
@ -140,7 +149,28 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
            Jedis tmpRsc = pool.getResource();
 | 
					            Jedis tmpRsc = pool.getResource();
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                if (tmpRsc.hexists("player:" + name, "online"))
 | 
					                if (tmpRsc.hexists("player:" + name, "online"))
 | 
				
			||||||
 | 
					                    try {
 | 
				
			||||||
                        time = Long.valueOf(tmpRsc.hget("player:" + name, "online"));
 | 
					                        time = Long.valueOf(tmpRsc.hget("player:" + name, "online"));
 | 
				
			||||||
 | 
					                    } catch (NumberFormatException e) {
 | 
				
			||||||
 | 
					                        getLogger().info("I found a funny number for when " + name + " was last online!");
 | 
				
			||||||
 | 
					                        boolean found = false;
 | 
				
			||||||
 | 
					                        for (String proxyId : serverIds) {
 | 
				
			||||||
 | 
					                            if (proxyId.equals(configuration.getString("server-id"))) continue;
 | 
				
			||||||
 | 
					                            if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", name)) {
 | 
				
			||||||
 | 
					                                found = true;
 | 
				
			||||||
 | 
					                                break;
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                        String value = "0";
 | 
				
			||||||
 | 
					                        if (!found) {
 | 
				
			||||||
 | 
					                            value = String.valueOf(System.currentTimeMillis());
 | 
				
			||||||
 | 
					                            getLogger().info(name + " isn't online. Setting to current time.");
 | 
				
			||||||
 | 
					                        } else {
 | 
				
			||||||
 | 
					                            getLogger().info(name + " is online. Setting to 0. Please check your BungeeCord instances.");
 | 
				
			||||||
 | 
					                            getLogger().info("If they are working properly, and this error does not resolve in a few minutes, please let Tux know!");
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                        tmpRsc.hset("player:" + name, "online", value);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
            } finally {
 | 
					            } finally {
 | 
				
			||||||
                pool.returnResource(tmpRsc);
 | 
					                pool.returnResource(tmpRsc);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@ -167,7 +197,7 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
 | 
					    final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
 | 
				
			||||||
        checkArgument(configuration.getStringList("linked-servers").contains(proxyId) || proxyId.equals("allservers"), "proxyId is invalid");
 | 
					        checkArgument(serverIds.contains(proxyId) || proxyId.equals("allservers"), "proxyId is invalid");
 | 
				
			||||||
        Jedis jedis = pool.getResource();
 | 
					        Jedis jedis = pool.getResource();
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            jedis.publish("redisbungee-" + proxyId, command);
 | 
					            jedis.publish("redisbungee-" + proxyId, command);
 | 
				
			||||||
@ -193,7 +223,7 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
                    for (String member : tmpRsc.smembers("server:" + configuration.getString("server-id") + ":usersOnline")) {
 | 
					                    for (String member : tmpRsc.smembers("server:" + configuration.getString("server-id") + ":usersOnline")) {
 | 
				
			||||||
                        // Are they simply on a different proxy?
 | 
					                        // Are they simply on a different proxy?
 | 
				
			||||||
                        boolean found = false;
 | 
					                        boolean found = false;
 | 
				
			||||||
                        for (String proxyId : configuration.getStringList("linked-servers")) {
 | 
					                        for (String proxyId : serverIds) {
 | 
				
			||||||
                            if (proxyId.equals(configuration.getString("server-id"))) continue;
 | 
					                            if (proxyId.equals(configuration.getString("server-id"))) continue;
 | 
				
			||||||
                            if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", member)) {
 | 
					                            if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", member)) {
 | 
				
			||||||
                                found = true;
 | 
					                                found = true;
 | 
				
			||||||
@ -240,7 +270,7 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
                            if (!players.contains(member)) {
 | 
					                            if (!players.contains(member)) {
 | 
				
			||||||
                                // Are they simply on a different proxy?
 | 
					                                // Are they simply on a different proxy?
 | 
				
			||||||
                                boolean found = false;
 | 
					                                boolean found = false;
 | 
				
			||||||
                                for (String proxyId : configuration.getStringList("linked-servers")) {
 | 
					                                for (String proxyId : serverIds) {
 | 
				
			||||||
                                    if (proxyId.equals(configuration.getString("server-id"))) continue;
 | 
					                                    if (proxyId.equals(configuration.getString("server-id"))) continue;
 | 
				
			||||||
                                    if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", member)) {
 | 
					                                    if (tmpRsc.sismember("server:" + proxyId + ":usersOnline", member)) {
 | 
				
			||||||
                                        // Just clean up the set.
 | 
					                                        // Just clean up the set.
 | 
				
			||||||
@ -260,7 +290,7 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
                        pool.returnResource(tmpRsc);
 | 
					                        pool.returnResource(tmpRsc);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }, 1, 5, TimeUnit.MINUTES);
 | 
					            }, 1, 3, TimeUnit.MINUTES);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        getProxy().registerChannel("RedisBungee");
 | 
					        getProxy().registerChannel("RedisBungee");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -319,6 +349,8 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
            throw new RuntimeException("linked-servers is not specified in the configuration or is empty");
 | 
					            throw new RuntimeException("linked-servers is not specified in the configuration or is empty");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        serverIds = ImmutableList.copyOf(configuration.getStringList("linked-servers"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (redisServer != null) {
 | 
					        if (redisServer != null) {
 | 
				
			||||||
            if (!redisServer.equals("")) {
 | 
					            if (!redisServer.equals("")) {
 | 
				
			||||||
                pool = new JedisPool(new JedisPoolConfig(), redisServer, redisPort, Protocol.DEFAULT_TIMEOUT, redisPassword);
 | 
					                pool = new JedisPool(new JedisPoolConfig(), redisServer, redisPort, Protocol.DEFAULT_TIMEOUT, redisPassword);
 | 
				
			||||||
@ -351,7 +383,7 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
        if (pool != null) {
 | 
					        if (pool != null) {
 | 
				
			||||||
            Jedis rsc = pool.getResource();
 | 
					            Jedis rsc = pool.getResource();
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                for (String server : configuration.getStringList("linked-servers")) {
 | 
					                for (String server : serverIds) {
 | 
				
			||||||
                    if (rsc.sismember("server:" + server + ":usersOnline", event.getConnection().getName())) {
 | 
					                    if (rsc.sismember("server:" + server + ":usersOnline", event.getConnection().getName())) {
 | 
				
			||||||
                        event.setCancelled(true);
 | 
					                        event.setCancelled(true);
 | 
				
			||||||
                        event.setCancelReason("You are already logged on to this server.");
 | 
					                        event.setCancelReason("You are already logged on to this server.");
 | 
				
			||||||
@ -419,7 +451,7 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            reply.setPlayers(new ServerPing.Players(old.getPlayers().getMax(), players.size(), info));
 | 
					            reply.setPlayers(new ServerPing.Players(old.getPlayers().getMax(), players.size(), info));
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            reply.setPlayers(new ServerPing.Players(old.getPlayers().getMax(), getCount(), EMPTY_PLAYERINFO));
 | 
					            reply.setPlayers(new ServerPing.Players(old.getPlayers().getMax(), getCount(), null));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        reply.setDescription(old.getDescription());
 | 
					        reply.setDescription(old.getDescription());
 | 
				
			||||||
        reply.setFavicon(old.getFavicon());
 | 
					        reply.setFavicon(old.getFavicon());
 | 
				
			||||||
@ -430,9 +462,8 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
    @EventHandler
 | 
					    @EventHandler
 | 
				
			||||||
    public void onPluginMessage(PluginMessageEvent event) {
 | 
					    public void onPluginMessage(PluginMessageEvent event) {
 | 
				
			||||||
        if (event.getTag().equals("RedisBungee") && event.getSender() instanceof Server) {
 | 
					        if (event.getTag().equals("RedisBungee") && event.getSender() instanceof Server) {
 | 
				
			||||||
            DataInput in = ByteStreams.newDataInput(event.getData());
 | 
					            ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            try {
 | 
					 | 
				
			||||||
            String subchannel = in.readUTF();
 | 
					            String subchannel = in.readUTF();
 | 
				
			||||||
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
 | 
					            ByteArrayDataOutput out = ByteStreams.newDataOutput();
 | 
				
			||||||
            String type;
 | 
					            String type;
 | 
				
			||||||
@ -480,9 +511,6 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            ((Server) event.getSender()).sendData("RedisBungee", out.toByteArray());
 | 
					            ((Server) event.getSender()).sendData("RedisBungee", out.toByteArray());
 | 
				
			||||||
            } catch (IOException e) {
 | 
					 | 
				
			||||||
                throw new RuntimeException("Unable to process plugin message from " + event.getSender().getAddress(), e);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -524,6 +552,7 @@ public final class RedisBungee extends Plugin implements Listener {
 | 
				
			|||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                cmd = s2;
 | 
					                cmd = s2;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            if (s2.trim().length() == 0) return;
 | 
				
			||||||
            getLogger().info("Invoking command from PubSub: /" + s2);
 | 
					            getLogger().info("Invoking command from PubSub: /" + s2);
 | 
				
			||||||
            getProxy().getPluginManager().dispatchCommand(RedisBungeeCommandSender.instance, cmd);
 | 
					            getProxy().getPluginManager().dispatchCommand(RedisBungeeCommandSender.instance, cmd);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user