mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-11-22 20:28:00 +00:00
Better guard against failed redis connection attempts. Also reformat code.
This commit is contained in:
parent
8c824cc397
commit
080dab084a
9
pom.xml
9
pom.xml
@ -58,15 +58,18 @@
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>redis.clients.jedis</pattern>
|
||||
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedis</shadedPattern>
|
||||
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedis
|
||||
</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>redis.clients.util</pattern>
|
||||
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedisutil</shadedPattern>
|
||||
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedisutil
|
||||
</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>org.apache.commons</pattern>
|
||||
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.commons</shadedPattern>
|
||||
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.commons
|
||||
</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
</configuration>
|
||||
|
@ -18,11 +18,13 @@ import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import redis.clients.jedis.*;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* The RedisBungee plugin.
|
||||
@ -52,10 +54,10 @@ public class RedisBungee extends Plugin implements Listener {
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
int c = plugin.getProxy().getOnlineCount();
|
||||
if (pool != null) {
|
||||
Jedis rsc = pool.getResource();
|
||||
int c = 0;
|
||||
try {
|
||||
c = plugin.getProxy().getOnlineCount();
|
||||
for (String i : getConfiguration().getLinkedServers()) {
|
||||
if (i.equals(configuration.getServerId())) continue;
|
||||
if (rsc.exists("server:" + i + ":playerCount"))
|
||||
@ -64,6 +66,7 @@ public class RedisBungee extends Plugin implements Listener {
|
||||
} finally {
|
||||
pool.returnResource(rsc);
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
@ -249,12 +252,29 @@ public class RedisBungee extends Plugin implements Listener {
|
||||
if (redisServer != null) {
|
||||
if (!redisServer.equals("")) {
|
||||
pool = new JedisPool(new JedisPoolConfig(), redisServer, redisPort, Protocol.DEFAULT_TIMEOUT, redisPassword);
|
||||
// Test the connection
|
||||
Jedis rsc = pool.getResource();
|
||||
try {
|
||||
rsc.exists(String.valueOf(System.currentTimeMillis()));
|
||||
getLogger().log(Level.INFO, "Successfully connected to Redis.");
|
||||
} catch (JedisConnectionException e) {
|
||||
pool.returnBrokenResource(rsc);
|
||||
getLogger().log(Level.WARNING, "Failed to connect to your Redis server! RedisBungee will still work, albeit with reduced functionality.", e);
|
||||
pool.destroy();
|
||||
pool = null;
|
||||
rsc = null;
|
||||
} finally {
|
||||
if (rsc != null && pool != null) {
|
||||
pool.returnResource(rsc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPreLogin(PreLoginEvent event) {
|
||||
if (pool != null) {
|
||||
Jedis rsc = pool.getResource();
|
||||
try {
|
||||
if (rsc.hexists("player:" + event.getConnection().getName(), "server")) {
|
||||
@ -265,9 +285,11 @@ public class RedisBungee extends Plugin implements Listener {
|
||||
pool.returnResource(rsc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerConnect(final PostLoginEvent event) {
|
||||
if (pool != null) {
|
||||
Jedis rsc = pool.getResource();
|
||||
try {
|
||||
rsc.sadd("server:" + configuration.getServerId() + ":usersOnline", event.getPlayer().getName());
|
||||
@ -275,6 +297,7 @@ public class RedisBungee extends Plugin implements Listener {
|
||||
} finally {
|
||||
pool.returnResource(rsc);
|
||||
}
|
||||
}
|
||||
// I used to have a task that eagerly waited for the user to be connected.
|
||||
// Well, upon further inspection of BungeeCord's source code, this turned
|
||||
// out to not be needed at all, since ServerConnectedEvent is called anyway.
|
||||
@ -329,7 +352,6 @@ public class RedisBungee extends Plugin implements Listener {
|
||||
}
|
||||
|
||||
private class PubSubListener extends Thread {
|
||||
|
||||
private Jedis rsc;
|
||||
private JedisPubSubHandler jpsh;
|
||||
|
||||
|
@ -15,7 +15,7 @@ import java.util.Collection;
|
||||
|
||||
/**
|
||||
* This class is the CommandSender that RedisBungee uses to dispatch commands to BungeeCord.
|
||||
*
|
||||
* <p/>
|
||||
* It inherits all permissions of the console command sender. Sending messages and modifying permissions are no-ops.
|
||||
*
|
||||
* @author tuxed
|
||||
|
@ -21,7 +21,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* This class contains subclasses that are used for the commands RedisBungee overrides or includes: /glist, /find and /lastseen.
|
||||
*
|
||||
* <p/>
|
||||
* All classes use the {@link RedisBungeeAPI}.
|
||||
*
|
||||
* @author tuxed
|
||||
|
Loading…
Reference in New Issue
Block a user