* 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.
This commit is contained in:
Tux 2013-12-13 16:32:55 -05:00
parent 14aba0ffb3
commit cacea73f2e
4 changed files with 69 additions and 28 deletions

11
pom.xml
View File

@ -68,7 +68,6 @@
<includes>
<include>redis.clients:jedis</include>
<include>commons-pool:commons-pool</include>
<include>org.apache.commons:commons-lang3</include>
</includes>
</artifactSet>
<relocations>
@ -82,11 +81,6 @@
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.jedisutil
</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.commons</pattern>
<shadedPattern>com.imaginarycode.minecraft.redisbungee.internal.commons
</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
@ -140,11 +134,6 @@
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -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;
* <p/>
* 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<String> getPlayers() {
protected final Set<String> getPlayers() {
Set<String> 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);
}

View File

@ -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.
* <p/>
* Note that this function returns an immutable {@link java.util.Set}.
* <strong>Note that this function returns an immutable {@link java.util.Set}.</strong>
*
* @return a Set with all players found
*/
public Set<String> getPlayersOnline() {
public final Set<String> 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);
}
}

View File

@ -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.");
}
}
}
}