From cacea73f2ebbb4d826378c4a5e61a658b0a1b3be Mon Sep 17 00:00:00 2001 From: Tux Date: Fri, 13 Dec 2013 16:32:55 -0500 Subject: [PATCH] * Don't shade in commons-lang anymore, replaced with SimpleDateFormat instead. * Add IP address logging support. * Add /ip command with permission node redisbungee.command.ip. * Mark some stuff as final so that the JVM can optimize things a little more. --- pom.xml | 11 ------- .../minecraft/redisbungee/RedisBungee.java | 33 ++++++++++++++++--- .../minecraft/redisbungee/RedisBungeeAPI.java | 24 ++++++++++---- .../redisbungee/RedisBungeeCommands.java | 29 ++++++++++++---- 4 files changed, 69 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index 052383b..8871e36 100644 --- a/pom.xml +++ b/pom.xml @@ -68,7 +68,6 @@ redis.clients:jedis commons-pool:commons-pool - org.apache.commons:commons-lang3 @@ -82,11 +81,6 @@ com.imaginarycode.minecraft.redisbungee.internal.jedisutil - - org.apache.commons - com.imaginarycode.minecraft.redisbungee.internal.commons - - @@ -140,11 +134,6 @@ jar provided - - org.apache.commons - commons-lang3 - 3.1 - org.projectlombok lombok diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index 414f9f4..7100123 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -22,6 +22,8 @@ import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; import java.io.*; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -31,7 +33,7 @@ import java.util.logging.Level; *

* The only function of interest is {@link #getApi()}, which exposes some functions in this class. */ -public 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 RedisBungeeCommandSender commandSender = new RedisBungeeCommandSender(); private static RedisBungeeConfiguration configuration = new RedisBungeeConfiguration(); @@ -53,7 +55,7 @@ public class RedisBungee extends Plugin implements Listener { return configuration; } - public int getCount() { + protected final int getCount() { int c = plugin.getProxy().getOnlineCount(); if (pool != null) { Jedis rsc = pool.getResource(); @@ -70,7 +72,7 @@ public class RedisBungee extends Plugin implements Listener { return c; } - public Set getPlayers() { + protected final Set getPlayers() { Set players = new HashSet<>(); for (ProxiedPlayer pp : plugin.getProxy().getPlayers()) { players.add(pp.getName()); @@ -89,7 +91,7 @@ public class RedisBungee extends Plugin implements Listener { return ImmutableSet.copyOf(players); } - public ServerInfo getServerFor(String name) { + protected final ServerInfo getServerFor(String name) { ServerInfo server = null; if (plugin.getProxy().getPlayer(name) != null) return plugin.getProxy().getPlayer(name).getServer().getInfo(); if (pool != null) { @@ -108,7 +110,7 @@ public class RedisBungee extends Plugin implements Listener { return TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS); } - public long getLastOnline(String name) { + protected final long getLastOnline(String name) { long time = -1L; if (plugin.getProxy().getPlayer(name) != null) return 0; if (pool != null) { @@ -123,6 +125,24 @@ public class RedisBungee extends Plugin implements Listener { return time; } + protected final InetAddress getIpAddress(String name) { + 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; + } + @Override public void onEnable() { plugin = this; @@ -158,6 +178,7 @@ public class RedisBungee extends Plugin implements Listener { getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.GlistCommand()); getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.FindCommand()); getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.LastSeenCommand()); + getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.IpCommand()); getProxy().getPluginManager().registerListener(this, this); api = new RedisBungeeAPI(this); psl = new PubSubListener(); @@ -298,6 +319,7 @@ public class RedisBungee extends Plugin implements Listener { try { rsc.sadd("server:" + configuration.getServerId() + ":usersOnline", event.getPlayer().getName()); rsc.hset("player:" + event.getPlayer().getName(), "online", "0"); + rsc.hset("player:" + event.getPlayer().getName(), "ip", event.getPlayer().getAddress().getAddress().getHostAddress()); } finally { pool.returnResource(rsc); } @@ -315,6 +337,7 @@ public class RedisBungee extends Plugin implements Listener { rsc.srem("server:" + configuration.getServerId() + ":usersOnline", event.getPlayer().getName()); rsc.hset("player:" + event.getPlayer().getName(), "online", String.valueOf(getUnixTimestamp())); rsc.hdel("player:" + event.getPlayer().getName(), "server"); + rsc.hdel("player:" + event.getPlayer().getName(), "ip"); } 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 3f91aad..b532db5 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeAPI.java @@ -8,8 +8,11 @@ package com.imaginarycode.minecraft.redisbungee; import net.md_5.bungee.api.config.ServerInfo; +import java.net.InetAddress; import java.util.Set; +import lombok.NonNull; + /** * This class exposes some internal RedisBungee functions. You obtain an instance of this object by invoking {@link RedisBungee#getApi()}. * @@ -28,7 +31,7 @@ public class RedisBungeeAPI { * * @return a count of all players found */ - public int getPlayerCount() { + public final int getPlayerCount() { return plugin.getCount(); } @@ -39,7 +42,7 @@ public class RedisBungeeAPI { * @param player a player name * @return the last time a player was on, if online returns a 0 */ - public long getLastOnline(String player) { + public final long getLastOnline(@NonNull String player) { return plugin.getLastOnline(player); } @@ -50,18 +53,18 @@ public class RedisBungeeAPI { * @param player a player name * @return a {@link net.md_5.bungee.api.config.ServerInfo} for the server the player is on. */ - public ServerInfo getServerFor(String player) { + public final ServerInfo getServerFor(@NonNull String player) { return plugin.getServerFor(player); } /** * Get a combined list of players on this network. *

- * Note that this function returns an immutable {@link java.util.Set}. + * Note that this function returns an immutable {@link java.util.Set}. * * @return a Set with all players found */ - public Set getPlayersOnline() { + public final Set getPlayersOnline() { return plugin.getPlayers(); } @@ -71,7 +74,16 @@ public class RedisBungeeAPI { * @param player a player name * @return if the server is online */ - public boolean isPlayerOnline(String player) { + public final boolean isPlayerOnline(@NonNull String player) { return getLastOnline(player) == 0; } + + /** + * Get the {@link java.net.InetAddress} associated with this player. + * + * @return an {@link java.net.InetAddress} if the player is online, null otherwise + */ + public final InetAddress getPlayerIp(@NonNull String player) { + return plugin.getIpAddress(player); + } } diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeCommands.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeCommands.java index c8ea9f2..d547fb2 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeCommands.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeCommands.java @@ -14,10 +14,9 @@ import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.plugin.Command; -import org.apache.commons.lang3.time.FastDateFormat; -import java.util.Set; -import java.util.TreeSet; +import java.net.InetAddress; +import java.text.SimpleDateFormat; import java.util.concurrent.TimeUnit; /** @@ -82,8 +81,6 @@ public class RedisBungeeCommands { } public static class LastSeenCommand extends Command { - FastDateFormat format = FastDateFormat.getInstance(); - protected LastSeenCommand() { super("lastseen", "redisbungee.command.lastseen"); } @@ -95,7 +92,7 @@ public class RedisBungeeCommands { if (secs == 0) { sender.sendMessage(ChatColor.GREEN + args[0] + " is currently online."); } else if (secs != -1) { - sender.sendMessage(ChatColor.BLUE + args[0] + " was last online on " + format.format(TimeUnit.SECONDS.toMillis(secs)) + "."); + sender.sendMessage(ChatColor.BLUE + args[0] + " was last online on " + new SimpleDateFormat().format(TimeUnit.SECONDS.toMillis(secs)) + "."); } else { sender.sendMessage(ChatColor.RED + args[0] + " has never been online."); } @@ -104,4 +101,24 @@ public class RedisBungeeCommands { } } } + + public static class IpCommand extends Command { + protected IpCommand() { + super("ip", "redisbungee.command.ip", "playerip"); + } + + @Override + public void execute(CommandSender sender, String[] args) { + if (args.length > 0) { + InetAddress ia = RedisBungee.getApi().getPlayerIp(args[0]); + if (ia != null) { + sender.sendMessage(ChatColor.GREEN + args[0] + " is connected from " + ia.toString() + "."); + } else { + sender.sendMessage(ChatColor.RED + "No such player found."); + } + } else { + sender.sendMessage(ChatColor.RED + "You must specify a player name."); + } + } + } }