Permalink
Browse files

Update to v5.0.4, fix a typo in JSON string builder, improve memory u…

…sage, fix command handling
  • Loading branch information...
1 parent 119e117 commit 424b7d262c425ae333516168e0b640a641c3500a @phyreman committed Feb 11, 2012
@@ -33,6 +33,10 @@
private static final String ON = "on";
private static final int DAY = 24;
+ // Constant nums
+ private static final int ZERO = 0;
+ private static final int ONE = 1;
+
public NetPlayerListener() {}
@EventHandler(priority = EventPriority.MONITOR)
@@ -47,16 +51,16 @@ public void onPlayerJoin(final PlayerJoinEvent e) {
if (!(new File(sf)).exists()) {
prop.set(ON, 1)
.set(SEEN, now)
- .set(SECONDS, 0)
- .set(MINUTES, 0)
- .set(HOURS, 0)
- .set(DAYS, 0)
- .set(BROKEN, 0)
- .set(PLACED, 0)
- .set(DEATHS, 0)
- .set(MOBS, 0)
- .set(PLAYERS, 0)
- .set(DISTANCE, 0)
+ .set(SECONDS, ZERO)
+ .set(MINUTES, ZERO)
+ .set(HOURS, ZERO)
+ .set(DAYS, ZERO)
+ .set(BROKEN, ZERO)
+ .set(PLACED, ZERO)
+ .set(DEATHS, ZERO)
+ .set(MOBS, ZERO)
+ .set(PLAYERS, ZERO)
+ .set(DISTANCE, ZERO)
.set(JOINDATE, now)
.set(IP, "")
.save();
@@ -84,9 +88,9 @@ public void onPlayerJoin(final PlayerJoinEvent e) {
// Update and save player data
if (Netstats.conf().getBool(IP)) {
String[] ips = e.getPlayer().getAddress().toString().split("/");
- prop.set(IP, ips[1].substring(0, ips[1].indexOf(':')));
+ prop.set(IP, ips[ONE].substring(ZERO, ips[ONE].indexOf(':')));
}
- prop.set(ON, 1)
+ prop.set(ON, ONE)
.set(SEEN, now)
.save();
}
@@ -103,7 +107,7 @@ public void onPlayerQuit(final PlayerQuitEvent e) {
.add(HOURS, prop.sub(MINUTES, MINHOUR))
.add(DAYS, prop.sub(HOURS, DAY))
.set(SEEN, now)
- .set(ON, 0)
+ .set(ON, ZERO)
.set(DISTANCE, Double.valueOf(new DecimalFormat("#.##").format(prop.getDouble(DISTANCE))))
.save();
}
@@ -119,7 +123,7 @@ public void onPlayerKick(final PlayerKickEvent e) {
.add(HOURS, prop.sub(MINUTES, MINHOUR))
.add(DAYS, prop.sub(HOURS, DAY))
.set(SEEN, now)
- .set(ON, 0)
+ .set(ON, ZERO)
.set(DISTANCE, Double.valueOf(new DecimalFormat("#.##").format(prop.getDouble(DISTANCE))))
.save();
}
@@ -25,13 +25,18 @@
private static final String PLAYERS = "players";
private static final String PLAYER = "player";
private static final String WORLDS = "worlds";
- private static final String STATS = ".stats";
+ private static final String DSTATS = ".stats";
+ private static final String STATS = "stats";
private static final String WORLD = "world";
private static final String UTF8 = "UTF-8";
private static final String NAME = "name";
private static final String PF = "pf";
private static final int OK = 200;
+ // Constant nums
+ private static final int ZERO = 0;
+ private static final int ONE = 1;
+
public NetServWorker () {}
// Generate the player list
@@ -40,18 +45,18 @@ public NetServWorker () {}
final Map<String, String> map = new HashMap<String, String>();
final String[] pairs = str.split("&");
for (String pair : pairs) {
- map.put(pair.split("=")[0], pair.split("=")[1]);
+ map.put(pair.split("=")[ZERO], pair.split("=")[ONE]);
}
if (map.get(PLAYERS).equals("all")) {
// Create filter to only list files with the stats extension
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
- return name.endsWith(STATS);
+ return name.endsWith(DSTATS);
}
};
for (final File f : new File(Netstats.vars(PF)).listFiles(filter)) {
- final String name = f.getName().split("\\.")[0];
+ final String name = f.getName().split("\\.")[ZERO];
if (!playerList.contains(name)) {
playerList.add(name);
}
@@ -61,7 +66,7 @@ public boolean accept(File dir, String name) {
playerList.add(p.getName());
}
} else {
- if (map.get(PLAYERS).indexOf(',') != -1) {
+ if (map.get(PLAYERS).indexOf(',') != -ONE) {
for (final String player : map.get(PLAYERS).split(",")) {
if (!playerList.contains(player)) {
playerList.add(player);
@@ -80,8 +85,8 @@ public boolean accept(File dir, String name) {
private JSONObject worldHandler(Map<String, String> map) {
final JSONObject worlds = new JSONObject();
final JSONObject players = new JSONObject();
- int wc = 0;
- int pc = 0;
+ int wc = ZERO;
+ int pc = ZERO;
// Specific worlds
String[] worldList = map.get(WORLDS).split(",");
for (final String world : worldList) {
@@ -90,7 +95,7 @@ private JSONObject worldHandler(Map<String, String> map) {
for (final String player : playerList) {
final JSONObject newPlayer = new JSONObject();
newPlayer.put(NAME, player);
- final Property playerProperty = new Property(Netstats.vars(PF)+'/'+player+'.'+world+STATS);
+ final Property playerProperty = new Property(Netstats.vars(PF)+'/'+player+'.'+world+DSTATS);
final JSONObject playerStats = new JSONObject();
for (final String stat : playerProperty.getKeys()) {
playerStats.put(stat, playerProperty.getString(stat));
@@ -115,30 +120,30 @@ private JSONObject allWorlds() {
public boolean accept(File dir, String name) {
for (final String player : playerList) {
if (name.contains(player)) {
- return name.endsWith(STATS);
+ return name.endsWith(DSTATS);
}
}
return false;
}
};
for (final File f : new File(Netstats.vars(PF)).listFiles(filter)) {
- final String world = f.getName().split("\\.")[1];
+ final String world = f.getName().split("\\.")[ONE];
if (!worldList.contains(world)) {
worldList.add(world);
}
}
final JSONObject worlds = new JSONObject();
final JSONObject players = new JSONObject();
- int wc = 0;
- int pc = 0;
+ int wc = ZERO;
+ int pc = ZERO;
// Now build the JSON response
for (final String world : worldList) {
final JSONObject newWorld = new JSONObject();
newWorld.put(NAME, world);
for (final String player : playerList) {
final JSONObject newPlayer = new JSONObject();
newPlayer.put(NAME, player);
- final String pfile = Netstats.vars(PF)+'/'+player+'.'+world+STATS;
+ final String pfile = Netstats.vars(PF)+'/'+player+'.'+world+DSTATS;
if (new File(pfile).exists()) {
final Property playerProperty = new Property(pfile);
final JSONObject playerStats = new JSONObject();
@@ -181,10 +186,10 @@ public void handle(HttpExchange exchange) throws IOException {
// Read the input as UTF-8
final StringBuilder buf = new StringBuilder();
int b;
- while ((b = exchange.getRequestBody().read()) != -1) {
+ while ((b = exchange.getRequestBody().read()) != -ONE) {
buf.append((char)b);
}
- if (buf.length() > 0) {
+ if (buf.length() > ZERO) {
// Reply to client
respond(jsonHandler(listPlayers(URLDecoder.decode(buf.toString(), UTF8))));
}
@@ -32,12 +32,14 @@
private static String lf; // Locales folder
// Constants
+ //TODO Replace with locale strings
private static final String DISTANCE = "distance";
private static final String PLAYERS = "players";
private static final String BROKEN = "broken";
private static final String DEATHS = "deaths";
private static final String PLACED = "placed";
- private static final String STATS = ".stats";
+ private static final String DSTATS = ".stats";
+ private static final String STATS = "stats";
private static final String TOTAL = "total";
private static final String ADMIN = "admin";
private static final String DELAY = "delay";
@@ -47,8 +49,14 @@
private static final String IP = "ip";
private static final int DPORT = 9999;
private static final int RUN = 600;
+
+ // Constant nums
+ private static final int ZERO = 0;
+ private static final int ONE = 1;
+ private static final int TWO = 2;
private static final int THREE = 3;
+ // Variables
private static int port;
private static short delay;
@@ -113,39 +121,40 @@ public static void setDelay(final short d) {
// Check if admin
private boolean isAdmin(CommandSender s) {
// Sender is either the console or the admin
- return (s instanceof ConsoleCommandSender || (s instanceof Player && ((Player)s).getName().equals(conf.getString("admin"))));
+ return (s instanceof ConsoleCommandSender || (s instanceof Player && ((Player)s).getName().equals(conf.getString(ADMIN))));
}
// List the different stats
private boolean listStats(final CommandSender s) {
// Store stats and description in RAM for the duration of the call
final Property stats = new Property(null)
- .set(BROKEN, "broken - Number of blocks broken")
- .set(DEATHS, " - Number of times player died")
- .set(DISTANCE, " - Distance traveled (meters)")
- .set(MOBS, " - Number of mobs killed")
- .set(PLACED, " - Number of blocks placed")
- .set(PLAYERS, " - Number of players killed")
- .set(TOTAL, " - Total playtime");
+ .set(BROKEN, "Number of blocks broken")
+ .set(DEATHS, "Number of times player died")
+ .set(DISTANCE, "Distance traveled (meters)")
+ .set(MOBS, "Number of mobs killed")
+ .set(PLACED, "Number of blocks placed")
+ .set(PLAYERS, "Number of players killed")
+ .set(TOTAL, "Total playtime");
// Only show stats currently being tracked
for (final String k : stats.getKeys()) {
if (conf.getBool(k)) {
- s.sendMessage(stats.getString(k));
+ s.sendMessage(k+" - "+stats.getString(k));
}
}
return true;
}
private String grammar(final Number n, final String time) {
- return (n.longValue() == 0) ? "" : ((n.longValue() > 1) ? n.longValue()+time+" " : "1"+time+" ");
+ return (n.longValue() == ZERO) ? "" : ((n.longValue() > ONE) ? n.longValue()+time+' ' : ONE+time+' ');
}
// Get a specific stat for specified user[:world]
private boolean getStats(final CommandSender s, final String stats, final String user) {
+
// Filename Filter
FilenameFilter filter = new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return name.endsWith(STATS) && name.toLowerCase().contains(user.toLowerCase());
+ public boolean accept(final File dir, final String name) {
+ return name.endsWith(DSTATS) && name.toLowerCase().contains(user.toLowerCase());
}
};
@@ -156,15 +165,15 @@ public boolean accept(File dir, String name) {
final String statList = "broken,placed,deaths,mobs,players,distance,total"; // Comma-delimited list of stats
final String fname = f.getName();
final String[] file = fname.split("\\.");
- final String pname = file[0];
- final String world = file[1];
+ final String pname = file[ZERO];
+ final String world = file[ONE];
final Property p = new Property(pf+'/'+fname);
s.sendMessage(ChatColor.GREEN+"Stats for "+pname+" in "+world);
for (final String stat : (stats.toLowerCase().equals("all") ? statList.split(",") : stats.split(","))) {
if (!stat.equals(TOTAL)) {
s.sendMessage(p.getString(stat)+' '+(stat.equals(DISTANCE) ? "meters" : stat));
} else {
- s.sendMessage((grammar(p.getLong("days"), "d")+grammar(p.getByte("hours"), "h")+grammar(p.getByte("minutes"), "m")+grammar(p.getByte("seconds"), "s")).trim()+".");
+ s.sendMessage((grammar(p.getLong("days"), "d")+grammar(p.getByte("hours"), "h")+grammar(p.getByte("minutes"), "m")+grammar(p.getByte("seconds"), "s")).trim()+'.');
}
}
}
@@ -173,18 +182,25 @@ public boolean accept(File dir, String name) {
// Handle incoming commands
private boolean cmdHandler(final CommandSender sender, final String[] args) {
- if (args.length == 2 && args[0].equalsIgnoreCase("get:config") && isAdmin(sender)) {
- sender.sendMessage(args[1]+" = "+conf.getString(args[1].toLowerCase()));
+ final int len = args.length;
+ if (len == ONE && args[ZERO].equals(STATS)) {
+ return listStats(sender);
+ } else if (len == TWO && args[ZERO].equalsIgnoreCase("get:config") && isAdmin(sender)) {
+ final String a1 = args[ONE];
+ sender.sendMessage(a1+" = "+conf.getString(a1.toLowerCase()));
return true;
- } else if (args.length == THREE && args[0].equalsIgnoreCase("set:config") && isAdmin(sender)) {
- conf.set(args[1], args[2])
+ } else if (len == THREE) {
+ final String a0 = args[ZERO];
+ final String a1 = args[ONE];
+ final String a2 = args[TWO];
+ if (a0.equalsIgnoreCase("set:config") && isAdmin(sender)) {
+ conf.set(a1, a2)
.save();
- return true;
- }
- if (args[0].equals("stats")) {
- return listStats(sender);
- } else if (args.length == THREE && args[0].equals("get:stat")) {
- return getStats(sender, args[1], args[2]);
+ return true;
+ }
+ if (a0.equals("get:stat")) {
+ return getStats(sender, a1, a2);
+ }
}
return false;
}
@@ -267,7 +283,7 @@ public void onEnable() {
// If /reload, add all players back to player list
for (final Player p : server.getOnlinePlayers()) {
- players.put(p.getName(), new Property(pf+'/'+p.getName()+'.'+p.getWorld().getName()+STATS));
+ players.put(p.getName(), new Property(pf+'/'+p.getName()+'.'+p.getWorld().getName()+DSTATS));
}
// Init the plugin manager
Oops, something went wrong.

0 comments on commit 424b7d2

Please sign in to comment.