2013-09-29 20:16:47 +00:00
|
|
|
/**
|
|
|
|
* Copyright © 2013 tuxed <write@imaginarycode.com>
|
|
|
|
* This work is free. You can redistribute it and/or modify it under the
|
|
|
|
* terms of the Do What The Fuck You Want To Public License, Version 2,
|
|
|
|
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
|
|
|
|
*/
|
|
|
|
package com.imaginarycode.minecraft.redisbungee;
|
|
|
|
|
2013-12-27 20:40:58 +00:00
|
|
|
import com.google.common.collect.HashMultimap;
|
2014-01-02 05:14:58 +00:00
|
|
|
import com.google.common.collect.ImmutableMultimap;
|
2013-11-16 02:56:59 +00:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
2013-12-27 20:40:58 +00:00
|
|
|
import com.google.common.collect.Multimap;
|
2013-11-14 00:28:53 +00:00
|
|
|
import com.google.common.io.ByteStreams;
|
2014-01-02 05:14:58 +00:00
|
|
|
import lombok.NonNull;
|
2013-09-29 20:16:47 +00:00
|
|
|
import net.md_5.bungee.api.ServerPing;
|
|
|
|
import net.md_5.bungee.api.config.ServerInfo;
|
|
|
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
2013-12-03 20:39:36 +00:00
|
|
|
import net.md_5.bungee.api.event.*;
|
2013-09-29 20:16:47 +00:00
|
|
|
import net.md_5.bungee.api.plugin.Listener;
|
|
|
|
import net.md_5.bungee.api.plugin.Plugin;
|
2013-12-24 04:12:54 +00:00
|
|
|
import net.md_5.bungee.config.Configuration;
|
|
|
|
import net.md_5.bungee.config.ConfigurationProvider;
|
|
|
|
import net.md_5.bungee.config.YamlConfiguration;
|
2013-09-29 20:16:47 +00:00
|
|
|
import net.md_5.bungee.event.EventHandler;
|
2013-12-03 21:01:27 +00:00
|
|
|
import redis.clients.jedis.*;
|
2013-12-04 21:54:51 +00:00
|
|
|
import redis.clients.jedis.exceptions.JedisConnectionException;
|
2013-11-15 22:05:29 +00:00
|
|
|
import redis.clients.jedis.exceptions.JedisException;
|
2013-09-29 20:16:47 +00:00
|
|
|
|
|
|
|
import java.io.*;
|
2013-12-13 21:32:55 +00:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.UnknownHostException;
|
2013-10-15 00:40:32 +00:00
|
|
|
import java.util.*;
|
2013-09-29 20:16:47 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2013-12-04 21:54:51 +00:00
|
|
|
import java.util.logging.Level;
|
2013-09-29 20:16:47 +00:00
|
|
|
|
2014-01-02 05:14:58 +00:00
|
|
|
import static com.google.common.base.Preconditions.*;
|
|
|
|
|
2013-11-15 22:05:29 +00:00
|
|
|
/**
|
|
|
|
* The RedisBungee plugin.
|
|
|
|
* <p/>
|
|
|
|
* The only function of interest is {@link #getApi()}, which exposes some functions in this class.
|
|
|
|
*/
|
2013-12-13 21:32:55 +00:00
|
|
|
public final class RedisBungee extends Plugin implements Listener {
|
2013-11-14 00:28:53 +00:00
|
|
|
private static final ServerPing.PlayerInfo[] EMPTY_PLAYERINFO = new ServerPing.PlayerInfo[]{};
|
2013-12-24 04:12:54 +00:00
|
|
|
private static Configuration configuration;
|
2013-11-15 22:05:29 +00:00
|
|
|
private JedisPool pool;
|
|
|
|
private RedisBungee plugin;
|
|
|
|
private static RedisBungeeAPI api;
|
2013-11-15 22:55:57 +00:00
|
|
|
private PubSubListener psl = null;
|
2013-10-15 00:40:32 +00:00
|
|
|
|
2013-10-13 19:44:20 +00:00
|
|
|
/**
|
2013-11-15 22:05:29 +00:00
|
|
|
* Fetch the {@link RedisBungeeAPI} object created on plugin start.
|
2013-10-13 19:44:20 +00:00
|
|
|
*
|
2013-11-15 22:05:29 +00:00
|
|
|
* @return the {@link RedisBungeeAPI} object
|
2013-10-13 19:44:20 +00:00
|
|
|
*/
|
2013-11-15 22:05:29 +00:00
|
|
|
public static RedisBungeeAPI getApi() {
|
|
|
|
return api;
|
|
|
|
}
|
|
|
|
|
2013-12-24 05:15:06 +00:00
|
|
|
static Configuration getConfiguration() {
|
2013-11-15 22:55:57 +00:00
|
|
|
return configuration;
|
|
|
|
}
|
|
|
|
|
2014-01-02 05:14:58 +00:00
|
|
|
final Multimap<String, String> serversToPlayers() {
|
2013-12-27 20:40:58 +00:00
|
|
|
Multimap<String, String> serverToPlayers = HashMultimap.create();
|
2014-01-02 05:14:58 +00:00
|
|
|
for (String p : getPlayers()) {
|
|
|
|
ServerInfo si = getServerFor(p);
|
2013-12-27 20:40:58 +00:00
|
|
|
if (si != null)
|
|
|
|
serverToPlayers.put(si.getName(), p);
|
|
|
|
}
|
2014-01-02 05:14:58 +00:00
|
|
|
return ImmutableMultimap.copyOf(serverToPlayers);
|
2013-12-27 20:40:58 +00:00
|
|
|
}
|
|
|
|
|
2013-12-24 05:15:06 +00:00
|
|
|
final int getCount() {
|
2013-12-04 21:54:51 +00:00
|
|
|
int c = plugin.getProxy().getOnlineCount();
|
|
|
|
if (pool != null) {
|
|
|
|
Jedis rsc = pool.getResource();
|
|
|
|
try {
|
2013-12-24 04:12:54 +00:00
|
|
|
for (String i : configuration.getStringList("linked-servers")) {
|
|
|
|
if (i.equals(configuration.getString("server-id"))) continue;
|
2013-12-04 21:54:51 +00:00
|
|
|
if (rsc.exists("server:" + i + ":playerCount"))
|
|
|
|
c += Integer.valueOf(rsc.get("server:" + i + ":playerCount"));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
pool.returnResource(rsc);
|
2013-10-29 23:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
2013-10-15 00:40:32 +00:00
|
|
|
}
|
|
|
|
|
2013-12-24 05:15:06 +00:00
|
|
|
final Set<String> getPlayers() {
|
2013-11-14 00:28:53 +00:00
|
|
|
Set<String> players = new HashSet<>();
|
2013-09-29 20:16:47 +00:00
|
|
|
for (ProxiedPlayer pp : plugin.getProxy().getPlayers()) {
|
|
|
|
players.add(pp.getName());
|
|
|
|
}
|
|
|
|
if (pool != null) {
|
|
|
|
Jedis rsc = pool.getResource();
|
|
|
|
try {
|
2013-12-24 04:12:54 +00:00
|
|
|
for (String i : configuration.getStringList("linked-servers")) {
|
|
|
|
if (i.equals(configuration.getString("server-id"))) continue;
|
2013-11-16 02:56:59 +00:00
|
|
|
players.addAll(rsc.smembers("server:" + i + ":usersOnline"));
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
pool.returnResource(rsc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ImmutableSet.copyOf(players);
|
|
|
|
}
|
|
|
|
|
2014-01-02 05:14:58 +00:00
|
|
|
final Set<String> getPlayersOnServer(@NonNull String server) {
|
|
|
|
return ImmutableSet.copyOf(serversToPlayers().get(server));
|
2013-12-27 20:40:58 +00:00
|
|
|
}
|
|
|
|
|
2014-01-02 05:14:58 +00:00
|
|
|
final ServerInfo getServerFor(@NonNull String name) {
|
2013-09-29 20:16:47 +00:00
|
|
|
ServerInfo server = null;
|
|
|
|
if (plugin.getProxy().getPlayer(name) != null) return plugin.getProxy().getPlayer(name).getServer().getInfo();
|
|
|
|
if (pool != null) {
|
|
|
|
Jedis tmpRsc = pool.getResource();
|
|
|
|
try {
|
|
|
|
if (tmpRsc.hexists("player:" + name, "server"))
|
|
|
|
server = plugin.getProxy().getServerInfo(tmpRsc.hget("player:" + name, "server"));
|
|
|
|
} finally {
|
|
|
|
pool.returnResource(tmpRsc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
2014-01-02 05:14:58 +00:00
|
|
|
final long getLastOnline(@NonNull String name) {
|
2013-11-14 01:25:09 +00:00
|
|
|
long time = -1L;
|
|
|
|
if (plugin.getProxy().getPlayer(name) != null) return 0;
|
2013-09-29 20:16:47 +00:00
|
|
|
if (pool != null) {
|
|
|
|
Jedis tmpRsc = pool.getResource();
|
|
|
|
try {
|
|
|
|
if (tmpRsc.hexists("player:" + name, "online"))
|
|
|
|
time = Long.valueOf(tmpRsc.hget("player:" + name, "online"));
|
|
|
|
} finally {
|
|
|
|
pool.returnResource(tmpRsc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2014-01-02 05:14:58 +00:00
|
|
|
final InetAddress getIpAddress(@NonNull String name) {
|
2013-12-13 21:32:55 +00:00
|
|
|
if (plugin.getProxy().getPlayer(name) != null)
|
|
|
|
return plugin.getProxy().getPlayer(name).getAddress().getAddress();
|
|
|
|
InetAddress ia = null;
|
|
|
|
if (pool != null) {
|
|
|
|
Jedis tmpRsc = pool.getResource();
|
|
|
|
try {
|
|
|
|
if (tmpRsc.hexists("player:" + name, "ip"))
|
|
|
|
ia = InetAddress.getByName(tmpRsc.hget("player:" + name, "ip"));
|
|
|
|
} catch (UnknownHostException ignored) {
|
|
|
|
// Best to just return null
|
|
|
|
} finally {
|
|
|
|
pool.returnResource(tmpRsc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ia;
|
|
|
|
}
|
|
|
|
|
2014-01-02 05:14:58 +00:00
|
|
|
final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
|
|
|
|
checkArgument(configuration.getStringList("linked-servers").contains(proxyId) || proxyId.equals("allservers"), "proxyId is invalid");
|
2014-01-02 00:05:55 +00:00
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
try {
|
|
|
|
jedis.publish("redisbungee-" + proxyId, command);
|
|
|
|
} finally {
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-29 20:16:47 +00:00
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
|
|
|
plugin = this;
|
|
|
|
try {
|
|
|
|
loadConfig();
|
|
|
|
} catch (IOException e) {
|
2014-01-02 05:14:58 +00:00
|
|
|
throw new RuntimeException("Unable to load/save config", e);
|
2013-12-07 02:42:03 +00:00
|
|
|
} catch (JedisConnectionException e) {
|
|
|
|
throw new RuntimeException("Unable to connect to your Redis server!", e);
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
|
|
|
if (pool != null) {
|
|
|
|
Jedis tmpRsc = pool.getResource();
|
|
|
|
try {
|
2013-12-24 04:12:54 +00:00
|
|
|
tmpRsc.set("server:" + configuration.getString("server-id") + ":playerCount", "0"); // reset
|
|
|
|
if (tmpRsc.scard("server:" + configuration.getString("server-id") + ":usersOnline") > 0) {
|
|
|
|
for (String member : tmpRsc.smembers("server:" + configuration.getString("server-id") + ":usersOnline"))
|
2013-12-18 17:10:32 +00:00
|
|
|
cleanUpPlayer(member, tmpRsc);
|
2013-12-03 21:01:27 +00:00
|
|
|
}
|
2013-09-29 20:16:47 +00:00
|
|
|
} finally {
|
|
|
|
pool.returnResource(tmpRsc);
|
|
|
|
}
|
2013-10-31 23:07:37 +00:00
|
|
|
getProxy().getScheduler().schedule(this, new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Jedis rsc = pool.getResource();
|
|
|
|
try {
|
2013-12-27 20:40:58 +00:00
|
|
|
rsc.set("server:" + configuration.getString("server-id") + ":playerCount", String.valueOf(getProxy().getOnlineCount()));
|
2013-10-31 23:07:37 +00:00
|
|
|
} finally {
|
|
|
|
pool.returnResource(rsc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 1, 3, TimeUnit.SECONDS);
|
2013-11-15 22:55:57 +00:00
|
|
|
getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.GlistCommand());
|
|
|
|
getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.FindCommand());
|
|
|
|
getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.LastSeenCommand());
|
2013-12-13 21:32:55 +00:00
|
|
|
getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.IpCommand());
|
2013-09-29 20:16:47 +00:00
|
|
|
getProxy().getPluginManager().registerListener(this, this);
|
2013-11-15 22:05:29 +00:00
|
|
|
api = new RedisBungeeAPI(this);
|
|
|
|
psl = new PubSubListener();
|
2013-11-26 19:51:52 +00:00
|
|
|
psl.start();
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
2013-10-18 21:14:58 +00:00
|
|
|
if (pool != null) {
|
2013-11-15 22:05:29 +00:00
|
|
|
// Poison the PubSub listener
|
|
|
|
psl.poison();
|
|
|
|
getProxy().getScheduler().cancel(this);
|
2013-10-18 21:14:58 +00:00
|
|
|
Jedis tmpRsc = pool.getResource();
|
|
|
|
try {
|
2013-12-24 04:12:54 +00:00
|
|
|
tmpRsc.set("server:" + configuration.getString("server-id") + ":playerCount", "0"); // reset
|
|
|
|
if (tmpRsc.scard("server:" + configuration.getString("server-id") + ":usersOnline") > 0) {
|
|
|
|
for (String member : tmpRsc.smembers("server:" + configuration.getString("server-id") + ":usersOnline"))
|
2013-12-18 17:10:32 +00:00
|
|
|
cleanUpPlayer(member, tmpRsc);
|
2013-12-03 21:01:27 +00:00
|
|
|
}
|
2013-10-18 21:14:58 +00:00
|
|
|
} finally {
|
|
|
|
pool.returnResource(tmpRsc);
|
|
|
|
}
|
2013-10-19 22:25:38 +00:00
|
|
|
pool.destroy();
|
2013-10-18 21:14:58 +00:00
|
|
|
}
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 02:42:03 +00:00
|
|
|
private void loadConfig() throws IOException, JedisConnectionException {
|
2013-09-29 20:16:47 +00:00
|
|
|
if (!getDataFolder().exists()) {
|
|
|
|
getDataFolder().mkdir();
|
|
|
|
}
|
|
|
|
|
2013-10-19 22:25:38 +00:00
|
|
|
File file = new File(getDataFolder(), "config.yml");
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
file.createNewFile();
|
|
|
|
try (InputStream in = getResourceAsStream("example_config.yml");
|
|
|
|
OutputStream out = new FileOutputStream(file)) {
|
2013-11-14 00:28:53 +00:00
|
|
|
ByteStreams.copy(in, out);
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 04:12:54 +00:00
|
|
|
configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
|
2013-10-19 22:25:38 +00:00
|
|
|
|
2013-12-24 04:12:54 +00:00
|
|
|
String redisServer = configuration.getString("redis-server", "localhost");
|
|
|
|
int redisPort = configuration.getInt("redis-port", 6379);
|
|
|
|
String redisPassword = configuration.getString("redis-password");
|
2013-09-29 20:16:47 +00:00
|
|
|
|
2013-12-24 04:12:54 +00:00
|
|
|
if (redisPassword != null && (redisPassword.equals("") || redisPassword.equals("none"))) {
|
|
|
|
redisPassword = null;
|
2013-11-16 02:56:59 +00:00
|
|
|
}
|
2013-09-29 20:16:47 +00:00
|
|
|
|
2013-12-24 04:12:54 +00:00
|
|
|
// Configuration sanity checks.
|
2014-01-02 05:14:58 +00:00
|
|
|
if (configuration.get("server-id") == null || configuration.getString("server-id").equals("")) {
|
2013-12-24 04:12:54 +00:00
|
|
|
throw new RuntimeException("server-id is not specified in the configuration or is empty");
|
|
|
|
}
|
2013-11-15 22:55:57 +00:00
|
|
|
|
2014-01-02 05:14:58 +00:00
|
|
|
if (configuration.getStringList("linked-servers").isEmpty()) {
|
2013-12-24 04:12:54 +00:00
|
|
|
throw new RuntimeException("linked-servers is not specified in the configuration or is empty");
|
2013-12-03 21:01:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-29 20:16:47 +00:00
|
|
|
if (redisServer != null) {
|
|
|
|
if (!redisServer.equals("")) {
|
2013-12-03 21:01:27 +00:00
|
|
|
pool = new JedisPool(new JedisPoolConfig(), redisServer, redisPort, Protocol.DEFAULT_TIMEOUT, redisPassword);
|
2013-12-04 21:54:51 +00:00
|
|
|
// Test the connection
|
2013-12-07 02:27:28 +00:00
|
|
|
Jedis rsc = null;
|
2013-12-04 21:54:51 +00:00
|
|
|
try {
|
2013-12-07 02:27:28 +00:00
|
|
|
rsc = pool.getResource();
|
2013-12-04 21:54:51 +00:00
|
|
|
rsc.exists(String.valueOf(System.currentTimeMillis()));
|
|
|
|
getLogger().log(Level.INFO, "Successfully connected to Redis.");
|
|
|
|
} catch (JedisConnectionException e) {
|
2013-12-07 02:27:28 +00:00
|
|
|
if (rsc != null)
|
|
|
|
pool.returnBrokenResource(rsc);
|
2013-12-04 21:54:51 +00:00
|
|
|
pool.destroy();
|
|
|
|
pool = null;
|
|
|
|
rsc = null;
|
2013-12-07 02:42:03 +00:00
|
|
|
throw e;
|
2013-12-04 21:54:51 +00:00
|
|
|
} finally {
|
|
|
|
if (rsc != null && pool != null) {
|
|
|
|
pool.returnResource(rsc);
|
|
|
|
}
|
|
|
|
}
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
2013-12-24 04:12:54 +00:00
|
|
|
} else {
|
|
|
|
throw new RuntimeException("No redis server specified!");
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-15 17:07:42 +00:00
|
|
|
@EventHandler
|
|
|
|
public void onPreLogin(PreLoginEvent event) {
|
|
|
|
if (pool != null) {
|
|
|
|
Jedis rsc = pool.getResource();
|
|
|
|
try {
|
2013-12-24 04:12:54 +00:00
|
|
|
for (String server : configuration.getStringList("linked-servers")) {
|
2013-12-15 17:07:42 +00:00
|
|
|
if (rsc.sismember("server:" + server + ":usersOnline", event.getConnection().getName())) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
event.setCancelReason("You are already logged on to this server.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
pool.returnResource(rsc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 20:39:36 +00:00
|
|
|
@EventHandler
|
|
|
|
public void onPlayerConnect(final PostLoginEvent event) {
|
2013-12-04 21:54:51 +00:00
|
|
|
if (pool != null) {
|
|
|
|
Jedis rsc = pool.getResource();
|
|
|
|
try {
|
2013-12-24 04:12:54 +00:00
|
|
|
rsc.sadd("server:" + configuration.getString("server-id", "") + ":usersOnline", event.getPlayer().getName());
|
2013-12-04 21:54:51 +00:00
|
|
|
rsc.hset("player:" + event.getPlayer().getName(), "online", "0");
|
2013-12-13 21:32:55 +00:00
|
|
|
rsc.hset("player:" + event.getPlayer().getName(), "ip", event.getPlayer().getAddress().getAddress().getHostAddress());
|
2013-12-04 21:54:51 +00:00
|
|
|
} finally {
|
|
|
|
pool.returnResource(rsc);
|
|
|
|
}
|
2013-11-16 03:09:04 +00:00
|
|
|
}
|
|
|
|
// 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.
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
2013-11-16 02:56:59 +00:00
|
|
|
public void onPlayerDisconnect(final PlayerDisconnectEvent event) {
|
2013-11-16 03:09:04 +00:00
|
|
|
if (pool != null) {
|
|
|
|
Jedis rsc = pool.getResource();
|
|
|
|
try {
|
2014-01-02 05:14:58 +00:00
|
|
|
rsc.hset("player:" + event.getPlayer().getName(), "online", String.valueOf(System.currentTimeMillis()));
|
2013-12-18 17:10:32 +00:00
|
|
|
cleanUpPlayer(event.getPlayer().getName(), rsc);
|
2013-11-16 03:09:04 +00:00
|
|
|
} finally {
|
|
|
|
pool.returnResource(rsc);
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
2013-11-16 03:09:04 +00:00
|
|
|
}
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
2013-11-16 02:56:59 +00:00
|
|
|
public void onServerChange(final ServerConnectedEvent event) {
|
2013-11-16 03:09:04 +00:00
|
|
|
if (pool != null) {
|
|
|
|
Jedis rsc = pool.getResource();
|
|
|
|
try {
|
|
|
|
rsc.hset("player:" + event.getPlayer().getName(), "server", event.getServer().getInfo().getName());
|
|
|
|
} finally {
|
|
|
|
pool.returnResource(rsc);
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
2013-11-16 03:09:04 +00:00
|
|
|
}
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPing(ProxyPingEvent event) {
|
|
|
|
ServerPing old = event.getResponse();
|
2013-10-29 02:42:26 +00:00
|
|
|
ServerPing reply = new ServerPing();
|
2013-12-24 04:12:54 +00:00
|
|
|
if (configuration.getBoolean("player-list-in-ping", false)) {
|
2013-11-16 02:56:59 +00:00
|
|
|
Set<String> players = getPlayers();
|
|
|
|
ServerPing.PlayerInfo[] info = new ServerPing.PlayerInfo[players.size()];
|
|
|
|
int idx = 0;
|
|
|
|
for (String player : players) {
|
|
|
|
info[idx] = new ServerPing.PlayerInfo(player, "");
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
reply.setPlayers(new ServerPing.Players(old.getPlayers().getMax(), players.size(), info));
|
|
|
|
} else {
|
|
|
|
reply.setPlayers(new ServerPing.Players(old.getPlayers().getMax(), getCount(), EMPTY_PLAYERINFO));
|
|
|
|
}
|
2013-10-29 02:42:26 +00:00
|
|
|
reply.setDescription(old.getDescription());
|
|
|
|
reply.setFavicon(old.getFavicon());
|
|
|
|
reply.setVersion(old.getVersion());
|
|
|
|
event.setResponse(reply);
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|
2013-11-15 22:05:29 +00:00
|
|
|
|
2013-12-18 17:10:32 +00:00
|
|
|
private void cleanUpPlayer(String player, Jedis rsc) {
|
2013-12-24 04:12:54 +00:00
|
|
|
rsc.srem("server:" + configuration.getString("server-id") + ":usersOnline", player);
|
2013-12-18 17:10:32 +00:00
|
|
|
rsc.hdel("player:" + player, "server");
|
|
|
|
rsc.hdel("player:" + player, "ip");
|
|
|
|
}
|
|
|
|
|
2013-11-26 19:51:52 +00:00
|
|
|
private class PubSubListener extends Thread {
|
2013-11-15 22:05:29 +00:00
|
|
|
private Jedis rsc;
|
|
|
|
private JedisPubSubHandler jpsh;
|
|
|
|
|
2013-11-26 19:51:52 +00:00
|
|
|
private PubSubListener() {
|
|
|
|
super("RedisBungee PubSub Listener");
|
|
|
|
}
|
|
|
|
|
2013-11-15 22:05:29 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
rsc = pool.getResource();
|
|
|
|
jpsh = new JedisPubSubHandler();
|
2013-12-24 04:12:54 +00:00
|
|
|
rsc.subscribe(jpsh, "redisbungee-" + configuration.getString("server-id"), "redisbungee-allservers");
|
2013-11-15 22:05:29 +00:00
|
|
|
} catch (JedisException | ClassCastException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void poison() {
|
|
|
|
jpsh.unsubscribe();
|
|
|
|
pool.returnResource(rsc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class JedisPubSubHandler extends JedisPubSub {
|
|
|
|
@Override
|
|
|
|
public void onMessage(String s, String s2) {
|
|
|
|
String cmd;
|
|
|
|
if (s2.startsWith("/")) {
|
|
|
|
cmd = s2.substring(1);
|
|
|
|
} else {
|
|
|
|
cmd = s2;
|
|
|
|
}
|
2013-11-15 22:09:03 +00:00
|
|
|
getLogger().info("Invoking command from PubSub: /" + s2);
|
2013-12-24 04:42:56 +00:00
|
|
|
getProxy().getPluginManager().dispatchCommand(RedisBungeeCommandSender.instance, cmd);
|
2013-11-15 22:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPMessage(String s, String s2, String s3) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSubscribe(String s, int i) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUnsubscribe(String s, int i) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPUnsubscribe(String s, int i) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPSubscribe(String s, int i) {
|
|
|
|
}
|
|
|
|
}
|
2013-09-29 20:16:47 +00:00
|
|
|
}
|