2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-01 02:58:02 +00:00

* Make the functions vemacs added to be only exposed in RedisBungeeAPI.

* Breaking API change: Last online times are now in milliseconds.
 * Error checking. Because why not?
This commit is contained in:
Tux 2014-01-02 00:14:58 -05:00
parent efb707cd94
commit bcdbf9ba54
3 changed files with 33 additions and 25 deletions

View File

@ -7,9 +7,11 @@
package com.imaginarycode.minecraft.redisbungee; package com.imaginarycode.minecraft.redisbungee;
import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import lombok.NonNull;
import net.md_5.bungee.api.ServerPing; import net.md_5.bungee.api.ServerPing;
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;
@ -31,6 +33,8 @@ import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import static com.google.common.base.Preconditions.*;
/** /**
* The RedisBungee plugin. * The RedisBungee plugin.
* <p/> * <p/>
@ -57,14 +61,14 @@ public final class RedisBungee extends Plugin implements Listener {
return configuration; return configuration;
} }
static Multimap<String, String> serversToPlayers() { final Multimap<String, String> serversToPlayers() {
Multimap<String, String> serverToPlayers = HashMultimap.create(); Multimap<String, String> serverToPlayers = HashMultimap.create();
for (String p : RedisBungee.getApi().getPlayersOnline()) { for (String p : getPlayers()) {
ServerInfo si = RedisBungee.getApi().getServerFor(p); ServerInfo si = getServerFor(p);
if (si != null) if (si != null)
serverToPlayers.put(si.getName(), p); serverToPlayers.put(si.getName(), p);
} }
return serverToPlayers; return ImmutableMultimap.copyOf(serverToPlayers);
} }
final int getCount() { final int getCount() {
@ -103,11 +107,11 @@ public final class RedisBungee extends Plugin implements Listener {
return ImmutableSet.copyOf(players); return ImmutableSet.copyOf(players);
} }
final Set<String> getPlayersOnServer(String server) { final Set<String> getPlayersOnServer(@NonNull String server) {
return new HashSet<>(serversToPlayers().get(server)); return ImmutableSet.copyOf(serversToPlayers().get(server));
} }
final ServerInfo getServerFor(String name) { final ServerInfo getServerFor(@NonNull String name) {
ServerInfo server = null; ServerInfo server = null;
if (plugin.getProxy().getPlayer(name) != null) return plugin.getProxy().getPlayer(name).getServer().getInfo(); if (plugin.getProxy().getPlayer(name) != null) return plugin.getProxy().getPlayer(name).getServer().getInfo();
if (pool != null) { if (pool != null) {
@ -122,11 +126,7 @@ public final class RedisBungee extends Plugin implements Listener {
return server; return server;
} }
private long getUnixTimestamp() { final long getLastOnline(@NonNull String name) {
return TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
final long getLastOnline(String name) {
long time = -1L; long time = -1L;
if (plugin.getProxy().getPlayer(name) != null) return 0; if (plugin.getProxy().getPlayer(name) != null) return 0;
if (pool != null) { if (pool != null) {
@ -141,7 +141,7 @@ public final class RedisBungee extends Plugin implements Listener {
return time; return time;
} }
final InetAddress getIpAddress(String name) { final InetAddress getIpAddress(@NonNull String name) {
if (plugin.getProxy().getPlayer(name) != null) if (plugin.getProxy().getPlayer(name) != null)
return plugin.getProxy().getPlayer(name).getAddress().getAddress(); return plugin.getProxy().getPlayer(name).getAddress().getAddress();
InetAddress ia = null; InetAddress ia = null;
@ -159,7 +159,8 @@ public final class RedisBungee extends Plugin implements Listener {
return ia; return ia;
} }
final void sendProxyCommand(String proxyId, String command) { final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
checkArgument(configuration.getStringList("linked-servers").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);
@ -174,7 +175,7 @@ public final class RedisBungee extends Plugin implements Listener {
try { try {
loadConfig(); loadConfig();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new RuntimeException("Unable to load/save config", e);
} catch (JedisConnectionException e) { } catch (JedisConnectionException e) {
throw new RuntimeException("Unable to connect to your Redis server!", e); throw new RuntimeException("Unable to connect to your Redis server!", e);
} }
@ -224,7 +225,6 @@ 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"))
cleanUpPlayer(member, tmpRsc); cleanUpPlayer(member, tmpRsc);
} }
} catch (JedisException | ClassCastException ignored) {
} finally { } finally {
pool.returnResource(tmpRsc); pool.returnResource(tmpRsc);
} }
@ -258,11 +258,11 @@ public final class RedisBungee extends Plugin implements Listener {
} }
// Configuration sanity checks. // Configuration sanity checks.
if (configuration.getString("server-id").equals("")) { if (configuration.get("server-id") == null || configuration.getString("server-id").equals("")) {
throw new RuntimeException("server-id is not specified in the configuration or is empty"); throw new RuntimeException("server-id is not specified in the configuration or is empty");
} }
if (configuration.getStringList("linked-servers").equals(Collections.EMPTY_LIST)) { if (configuration.getStringList("linked-servers").isEmpty()) {
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");
} }
@ -332,7 +332,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 {
rsc.hset("player:" + event.getPlayer().getName(), "online", String.valueOf(getUnixTimestamp())); rsc.hset("player:" + event.getPlayer().getName(), "online", String.valueOf(System.currentTimeMillis()));
cleanUpPlayer(event.getPlayer().getName(), rsc); cleanUpPlayer(event.getPlayer().getName(), rsc);
} finally { } finally {
pool.returnResource(rsc); pool.returnResource(rsc);

View File

@ -6,6 +6,7 @@
*/ */
package com.imaginarycode.minecraft.redisbungee; package com.imaginarycode.minecraft.redisbungee;
import com.google.common.collect.Multimap;
import lombok.NonNull; import lombok.NonNull;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
@ -36,7 +37,7 @@ public class RedisBungeeAPI {
/** /**
* Get the last time a player was on. If the player is currently online, this will return 0. If the player has not been recorded, * Get the last time a player was on. If the player is currently online, this will return 0. If the player has not been recorded,
* this will return -1. Otherwise it will return a value in seconds. * this will return -1. Otherwise it will return a value in milliseconds.
* *
* @param player a player name * @param player a player name
* @return the last time a player was on, if online returns a 0 * @return the last time a player was on, if online returns a 0
@ -67,6 +68,14 @@ public class RedisBungeeAPI {
return plugin.getPlayers(); return plugin.getPlayers();
} }
/**
* Get a full list of players on all servers.
* @return a immutable Multimap with all players found on this server
*/
public final Multimap<String, String> getServerToPlayers() {
return plugin.serversToPlayers();
}
/** /**
* Get a list of players on the server with the given name. * Get a list of players on the server with the given name.
* @param server a server name * @param server a server name
@ -99,7 +108,7 @@ public class RedisBungeeAPI {
* Sends a proxy command to all proxies. * Sends a proxy command to all proxies.
* @param command the command to send and execute * @param command the command to send and execute
*/ */
public final void sendProxyCommand(String command) { public final void sendProxyCommand(@NonNull String command) {
plugin.sendProxyCommand("allservers", command); plugin.sendProxyCommand("allservers", command);
} }
@ -108,7 +117,7 @@ public class RedisBungeeAPI {
* @param proxyId a proxy ID * @param proxyId a proxy ID
* @param command the command to send and execute * @param command the command to send and execute
*/ */
public final void sendProxyCommand(String proxyId, String command) { public final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
plugin.sendProxyCommand(proxyId, command); plugin.sendProxyCommand(proxyId, command);
} }
} }

View File

@ -19,7 +19,6 @@ import net.md_5.bungee.api.plugin.Command;
import java.net.InetAddress; import java.net.InetAddress;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
/** /**
* This class contains subclasses that are used for the commands RedisBungee overrides or includes: /glist, /find and /lastseen. * This class contains subclasses that are used for the commands RedisBungee overrides or includes: /glist, /find and /lastseen.
@ -47,7 +46,7 @@ class RedisBungeeCommands {
.append(" player(s) are currently online.").create(); .append(" player(s) are currently online.").create();
if (args.length > 0 && args[0].equals("showall")) { if (args.length > 0 && args[0].equals("showall")) {
if (RedisBungee.getConfiguration().getBoolean("canonical-glist", true)) { if (RedisBungee.getConfiguration().getBoolean("canonical-glist", true)) {
Multimap<String, String> serverToPlayers = RedisBungee.serversToPlayers(); Multimap<String, String> serverToPlayers = RedisBungee.getApi().getServerToPlayers();
for (String server : new TreeSet<>(serverToPlayers.keySet())) { for (String server : new TreeSet<>(serverToPlayers.keySet())) {
TextComponent serverName = new TextComponent(); TextComponent serverName = new TextComponent();
serverName.setColor(ChatColor.GREEN); serverName.setColor(ChatColor.GREEN);
@ -111,7 +110,7 @@ class RedisBungeeCommands {
sender.sendMessage(message); sender.sendMessage(message);
} else if (secs != -1) { } else if (secs != -1) {
message.setColor(ChatColor.BLUE); message.setColor(ChatColor.BLUE);
message.setText(args[0] + " was last online on " + new SimpleDateFormat().format(TimeUnit.SECONDS.toMillis(secs)) + "."); message.setText(args[0] + " was last online on " + new SimpleDateFormat().format(secs) + ".");
sender.sendMessage(message); sender.sendMessage(message);
} else { } else {
message.setColor(ChatColor.RED); message.setColor(ChatColor.RED);