From bcdbf9ba548270029de5c7bb43876999fb7e60ef Mon Sep 17 00:00:00 2001 From: Tux Date: Thu, 2 Jan 2014 00:14:58 -0500 Subject: [PATCH] * 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? --- .../minecraft/redisbungee/RedisBungee.java | 38 +++++++++---------- .../minecraft/redisbungee/RedisBungeeAPI.java | 15 ++++++-- .../redisbungee/RedisBungeeCommands.java | 5 +-- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index 9bf525b..0292431 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -7,9 +7,11 @@ package com.imaginarycode.minecraft.redisbungee; import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; import com.google.common.io.ByteStreams; +import lombok.NonNull; import net.md_5.bungee.api.ServerPing; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -31,6 +33,8 @@ import java.util.*; import java.util.concurrent.TimeUnit; import java.util.logging.Level; +import static com.google.common.base.Preconditions.*; + /** * The RedisBungee plugin. *

@@ -57,14 +61,14 @@ public final class RedisBungee extends Plugin implements Listener { return configuration; } - static Multimap serversToPlayers() { + final Multimap serversToPlayers() { Multimap serverToPlayers = HashMultimap.create(); - for (String p : RedisBungee.getApi().getPlayersOnline()) { - ServerInfo si = RedisBungee.getApi().getServerFor(p); + for (String p : getPlayers()) { + ServerInfo si = getServerFor(p); if (si != null) serverToPlayers.put(si.getName(), p); } - return serverToPlayers; + return ImmutableMultimap.copyOf(serverToPlayers); } final int getCount() { @@ -103,11 +107,11 @@ public final class RedisBungee extends Plugin implements Listener { return ImmutableSet.copyOf(players); } - final Set getPlayersOnServer(String server) { - return new HashSet<>(serversToPlayers().get(server)); + final Set getPlayersOnServer(@NonNull String server) { + return ImmutableSet.copyOf(serversToPlayers().get(server)); } - final ServerInfo getServerFor(String name) { + final ServerInfo getServerFor(@NonNull String name) { ServerInfo server = null; if (plugin.getProxy().getPlayer(name) != null) return plugin.getProxy().getPlayer(name).getServer().getInfo(); if (pool != null) { @@ -122,11 +126,7 @@ public final class RedisBungee extends Plugin implements Listener { return server; } - private long getUnixTimestamp() { - return TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS); - } - - final long getLastOnline(String name) { + final long getLastOnline(@NonNull String name) { long time = -1L; if (plugin.getProxy().getPlayer(name) != null) return 0; if (pool != null) { @@ -141,7 +141,7 @@ public final class RedisBungee extends Plugin implements Listener { return time; } - final InetAddress getIpAddress(String name) { + final InetAddress getIpAddress(@NonNull String name) { if (plugin.getProxy().getPlayer(name) != null) return plugin.getProxy().getPlayer(name).getAddress().getAddress(); InetAddress ia = null; @@ -159,7 +159,8 @@ public final class RedisBungee extends Plugin implements Listener { 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(); try { jedis.publish("redisbungee-" + proxyId, command); @@ -174,7 +175,7 @@ public final class RedisBungee extends Plugin implements Listener { try { loadConfig(); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException("Unable to load/save config", e); } catch (JedisConnectionException 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")) cleanUpPlayer(member, tmpRsc); } - } catch (JedisException | ClassCastException ignored) { } finally { pool.returnResource(tmpRsc); } @@ -258,11 +258,11 @@ public final class RedisBungee extends Plugin implements Listener { } // 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"); } - 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"); } @@ -332,7 +332,7 @@ public final class RedisBungee extends Plugin implements Listener { if (pool != null) { Jedis rsc = pool.getResource(); 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); } finally { pool.returnResource(rsc); diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java index 0cf771b..b78dced 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java @@ -6,6 +6,7 @@ */ package com.imaginarycode.minecraft.redisbungee; +import com.google.common.collect.Multimap; import lombok.NonNull; 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, - * 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 * @return the last time a player was on, if online returns a 0 @@ -67,6 +68,14 @@ public class RedisBungeeAPI { 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 getServerToPlayers() { + return plugin.serversToPlayers(); + } + /** * Get a list of players on the server with the given name. * @param server a server name @@ -99,7 +108,7 @@ public class RedisBungeeAPI { * Sends a proxy command to all proxies. * @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); } @@ -108,7 +117,7 @@ public class RedisBungeeAPI { * @param proxyId a proxy ID * @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); } } diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeCommands.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeCommands.java index f8e9e23..e2c3102 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeCommands.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeCommands.java @@ -19,7 +19,6 @@ import net.md_5.bungee.api.plugin.Command; import java.net.InetAddress; import java.text.SimpleDateFormat; 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. @@ -47,7 +46,7 @@ class RedisBungeeCommands { .append(" player(s) are currently online.").create(); if (args.length > 0 && args[0].equals("showall")) { if (RedisBungee.getConfiguration().getBoolean("canonical-glist", true)) { - Multimap serverToPlayers = RedisBungee.serversToPlayers(); + Multimap serverToPlayers = RedisBungee.getApi().getServerToPlayers(); for (String server : new TreeSet<>(serverToPlayers.keySet())) { TextComponent serverName = new TextComponent(); serverName.setColor(ChatColor.GREEN); @@ -111,7 +110,7 @@ class RedisBungeeCommands { sender.sendMessage(message); } else if (secs != -1) { 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); } else { message.setColor(ChatColor.RED);