Permalink
Browse files

Remove lingering debug code and further improve memory usage

  • Loading branch information...
1 parent bf416b2 commit 119e1176c0a6c6d67187c12cdb082aa22329df71 @phyreman committed Feb 4, 2012
@@ -24,13 +24,11 @@ public void onBlockBreak(final BlockBreakEvent e) {
if (e.isCancelled()) {
return;
}
- Netstats.info("Block was broken!");
- final long now = System.nanoTime(); // Grab time at start of action (for accuracy)
- final Property prop = Netstats.players().get(e.getPlayer().getName())
-
+ // Get the time
+ final long now = System.nanoTime();
// Add 1 to broken
- .inc("broken");
+ final Property prop = Netstats.players().get(e.getPlayer().getName()).inc("broken");
// Add everything up
prop.add(SECONDS, ((now-prop.getLong(SEEN))/SEC)%SEC)
@@ -7,11 +7,11 @@
public class NetBlockPlaceListener implements Listener {
// Constants
- private static final String SEEN = "seen";
private static final String SECONDS = "seconds";
private static final String MINUTES = "minutes";
private static final String HOURS = "hours";
private static final String DAYS = "days";
+ private static final String SEEN = "seen";
private static final int SEC = 1000000000;
private static final int MINHOUR = 60;
private static final int DAY = 24;
@@ -25,11 +25,10 @@ public void onBlockPlace(final BlockPlaceEvent e) {
return;
}
+ // Get the time
final long now = System.nanoTime();
- final Property prop = Netstats.players().get(e.getPlayer().getName())
-
// Add 1 to placed
- .inc("placed");
+ final Property prop = Netstats.players().get(e.getPlayer().getName()).inc("placed");
// Add everything up
prop.add(SECONDS, ((now-prop.getLong(SEEN))/SEC)%SEC)
@@ -19,9 +19,12 @@
private static final long THRESHOLD = 500; // ms between damage & death
private static final String SECONDS = "seconds";
private static final String MINUTES = "minutes";
+ private static final String PLAYERS = "players";
+ private static final String DEATHS = "deaths";
private static final String HOURS = "hours";
private static final String DAYS = "days";
private static final String SEEN = "seen";
+ private static final String MOBS = "mobs";
private static final int SEC = 1000000000;
private static final int MINHOUR = 60;
private static final int DAY = 24;
@@ -32,9 +35,9 @@ public NetEntityListener() {}
public void onEntityDeath(final EntityDeathEvent e) {
final Entity en = e.getEntity();
final int id = en.getEntityId();
- if (en instanceof Player && conf.getBool("deaths")) {
+ if (en instanceof Player && conf.getBool(DEATHS)) {
// Add 1 to deaths
- Netstats.players().get(((Player)en).getName()).inc("deaths");
+ Netstats.players().get(((Player)en).getName()).inc(DEATHS);
}
// An entity just died, if it was attacked (didn't fall, drown, or burn to death)
if (attacks.containsKey(id)) {
@@ -66,9 +69,9 @@ public void onEntityDamage(final EntityDamageEvent e) {
private void entDeath(Entity victim, Player damager) {
final long now = System.nanoTime();
- if (victim instanceof Monster) {
+ if (victim instanceof Monster && conf.getBool(MOBS)) {
// Player killed a monster, save it to player's stats file
- final Property prop = Netstats.players().get(damager.getName()).inc("mobs");
+ final Property prop = Netstats.players().get(damager.getName()).inc(MOBS);
// Add everything up
prop.add(SECONDS, ((now-prop.getLong(SEEN))/SEC)%SEC)
@@ -77,10 +80,10 @@ private void entDeath(Entity victim, Player damager) {
.add(DAYS, prop.sub(HOURS, DAY))
.set(SEEN, now)
.save();
- } else if ((victim instanceof Player) && (conf.getBool("players"))) {
+ } else if ((victim instanceof Player) && conf.getBool(PLAYERS)) {
final Player p = (Player)victim;
// Player killed a player, save it to player's stats file
- final Property prop = Netstats.players().get(p.getName()).inc("players");
+ final Property prop = Netstats.players().get(p.getName()).inc(PLAYERS);
// Add everything up
prop.add(SECONDS, ((now-prop.getLong(SEEN))/SEC)%SEC)
@@ -15,22 +15,22 @@
private final String pf = Netstats.vars("pf");
// Constants
- private static final String ON = "on";
- private static final String SEEN = "seen";
+ private static final String DISTANCE = "distance";
+ private static final String JOINDATE = "joindate";
+ private static final String PLAYERS = "players";
private static final String SECONDS = "seconds";
private static final String MINUTES = "minutes";
- private static final String HOURS = "hours";
- private static final String DAYS = "days";
private static final String BROKEN = "broken";
private static final String PLACED = "placed";
private static final String DEATHS = "deaths";
+ private static final String HOURS = "hours";
+ private static final String DAYS = "days";
+ private static final String SEEN = "seen";
private static final String MOBS = "mobs";
- private static final String PLAYERS = "players";
- private static final String DISTANCE = "distance";
- private static final String JOINDATE = "joindate";
- private static final String IP = "ip";
private static final int SEC = 1000000000;
private static final int MINHOUR = 60;
+ private static final String IP = "ip";
+ private static final String ON = "on";
private static final int DAY = 24;
public NetPlayerListener() {}
@@ -7,14 +7,12 @@
import org.bukkit.event.player.PlayerMoveEvent;
public class NetPlayerMoveListener implements Listener {
- private static final String DISTANCE = "distance";
-
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerMove(final PlayerMoveEvent e) {
if (e.isCancelled()) {
return;
}
final Player p = e.getPlayer();
- Netstats.players().get(p.getName()).add(DISTANCE, e.getTo().distance(e.getFrom()));
+ Netstats.players().get(p.getName()).add("distance", e.getTo().distance(e.getFrom()));
}
}
@@ -12,6 +12,7 @@
private static final String SECONDS = "seconds";
private static final String MINUTES = "minutes";
private static final String HOURS = "hours";
+ private static final String FORMAT = "#.##";
private static final String DAYS = "days";
private static final String SEEN = "seen";
private static final int SEC = 1000000000;
@@ -35,7 +36,7 @@ public void run() {
.add(HOURS, prop.sub(MINUTES, MINHOUR))
.add(DAYS, prop.sub(HOURS, DAY))
.set(SEEN, now)
- .set(DISTANCE, Double.valueOf(new DecimalFormat("#.##").format(prop.getDouble(DISTANCE))))
+ .set(DISTANCE, Double.valueOf(new DecimalFormat(FORMAT).format(prop.getDouble(DISTANCE))))
.save();
}
}
@@ -57,7 +58,7 @@ public void out() {
.add(HOURS, prop.sub(MINUTES, MINHOUR))
.add(DAYS, prop.sub(HOURS, DAY))
.set(SEEN, now)
- .set(DISTANCE, Double.valueOf(new DecimalFormat("#.##").format(prop.getDouble(DISTANCE))))
+ .set(DISTANCE, Double.valueOf(new DecimalFormat(FORMAT).format(prop.getDouble(DISTANCE))))
.save();
}
}
@@ -32,18 +32,19 @@
private static String lf; // Locales folder
// Constants
- public static final String DISTANCE = "distance";
- public static final String PLAYERS = "players";
- public static final String BROKEN = "broken";
- public static final String DEATHS = "deaths";
- public static final String PLACED = "placed";
- public static final String TOTAL = "total";
- public static final String ADMIN = "admin";
- public static final String DELAY = "delay";
- public static final String MOBS = "mobs";
- public static final String PORT = "port";
- public static final String IP = "ip";
+ 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 TOTAL = "total";
+ private static final String ADMIN = "admin";
+ private static final String DELAY = "delay";
+ private static final String MOBS = "mobs";
+ private static final String PORT = "port";
private static final short DDELAY = 30;
+ private static final String IP = "ip";
private static final int DPORT = 9999;
private static final int RUN = 600;
private static final int THREE = 3;
@@ -61,15 +62,19 @@ private void mkd(final String... d) {
}
}
+ private static String brace(final String str) {
+ return '['+str+"] ";
+ }
+
// Override method for logging info/severe to auto-include the prefix [Netstats]
public static void info(final String msg) {
- log.info('['+name+"] "+msg);
+ log.info(brace(name)+msg);
}
public static void severe(final String msg, final Exception e) {
- log.log(Level.SEVERE, '['+name+"] "+msg, e);
+ log.log(Level.SEVERE, brace(name)+msg, e);
}
public static void warning(final String msg, final Exception e) {
- log.log(Level.WARNING, '['+name+"] "+msg, e);
+ log.log(Level.WARNING, brace(name)+msg, e);
}
// Getter/setter methods
@@ -140,7 +145,7 @@ private boolean getStats(final CommandSender s, final String stats, final String
// Filename Filter
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
- return name.endsWith(".stats") && name.toLowerCase().contains(user.toLowerCase());
+ return name.endsWith(STATS) && name.toLowerCase().contains(user.toLowerCase());
}
};
@@ -262,7 +267,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()+STATS));
}
// Init the plugin manager
@@ -276,10 +281,10 @@ public void onEnable() {
// Register block events (Break, Place)
- if (conf.getBool("broken")) {
+ if (conf.getBool(BROKEN)) {
pm.registerEvents(new NetBlockBreakListener(), this);
}
- if (conf.getBool("placed")) {
+ if (conf.getBool(PLACED)) {
pm.registerEvents(new NetBlockPlaceListener(), this);
}
@@ -18,6 +18,8 @@
import java.util.Set;
public final class Property {
+ private static final String UTF8 = "UTF-8";
+
private Map<String, String> props = new LinkedHashMap<String, String>();
private Map<String, String> keys = new HashMap<String, String>();
private String file;
@@ -36,7 +38,7 @@ public Property(final String file) {
public Property load() {
BufferedReader br = null;
try {
- br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
+ br = new BufferedReader(new InputStreamReader(new FileInputStream(file),UTF8));
String line;
byte cc = 0; // # of comments
int delim;
@@ -79,7 +81,7 @@ public Property save() {
BufferedWriter bw = null;
try {
// Construct the BufferedWriter object
- bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
+ bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),UTF8));
// Save all the props one at a time, only if there's data to write
if (props.size() > 0) {
@@ -164,13 +166,13 @@ public Property inc(final String key) {
// Add given number to given key
public Property add(final String key, final Number n) {
- props.put(key.toLowerCase(), new BigDecimal((String)props.get(key.toLowerCase())).add(new BigDecimal(""+n)).toString());
+ props.put(key.toLowerCase(), new BigDecimal((String)props.get(key.toLowerCase())).add(new BigDecimal(n.toString())).toString());
return this;
}
// Subtract given number from given key
public Number sub(final String key, final Number n) {
- final Number num = new BigDecimal((String)props.get(key.toLowerCase())).subtract(new BigDecimal(""+n));
+ final Number num = new BigDecimal((String)props.get(key.toLowerCase())).subtract(new BigDecimal(n.toString()));
if (num.intValue() >= 0) {
props.put(key, num.toString());
return 1;

0 comments on commit 119e117

Please sign in to comment.