Optimisations mostly in the form of stringbuilder.append

This commit is contained in:
md_5
2012-07-28 15:20:19 +10:00
parent 61e9643b90
commit 1921d0c77c
6 changed files with 58 additions and 63 deletions

View File

@@ -45,45 +45,45 @@ public class BlockChange implements LookupCacheElement
public String toString() {
final StringBuilder msg = new StringBuilder();
if (date > 0)
msg.append(formatter.format(date) + " ");
msg.append(formatter.format(date)).append(" ");
if (playerName != null)
msg.append(playerName + " ");
msg.append(playerName).append(" ");
if (signtext != null) {
final String action = type == 0 ? "destroyed " : "created ";
if (!signtext.contains("\0"))
msg.append(action + signtext);
msg.append(action).append(signtext);
else
msg.append(action + materialName(type != 0 ? type : replaced) + " [" + signtext.replace("\0", "] [") + "]");
msg.append(action).append(materialName(type != 0 ? type : replaced)).append(" [").append(signtext.replace("\0", "] [")).append("]");
} else if (type == replaced) {
if (type == 0)
msg.append("did an unspecified action");
else if (ca != null) {
if (ca.itemType == 0 || ca.itemAmount == 0)
msg.append("looked inside " + materialName(type));
msg.append("looked inside ").append(materialName(type));
else if (ca.itemAmount < 0)
msg.append("took " + -ca.itemAmount + "x " + materialName(ca.itemType, ca.itemData));
msg.append("took ").append(-ca.itemAmount).append("x ").append(materialName(ca.itemType, ca.itemData));
else
msg.append("put in " + ca.itemAmount + "x " + materialName(ca.itemType, ca.itemData));
msg.append("put in ").append(ca.itemAmount).append("x ").append(materialName(ca.itemType, ca.itemData));
} else if (type == 23 || type == 54 || type == 61 || type == 62)
msg.append("opened " + materialName(type));
msg.append("opened ").append(materialName(type));
else if (type == 64 || type == 71 || type == 96 || type == 107)
msg.append((data == 0 ? "opened" : "closed") + " " + materialName(type));
msg.append(data == 0 ? "opened" : "closed").append(" ").append(materialName(type));
else if (type == 69)
msg.append("switched " + materialName(type));
msg.append("switched ").append(materialName(type));
else if (type == 77)
msg.append("pressed " + materialName(type));
msg.append("pressed ").append(materialName(type));
else if (type == 92)
msg.append("ate a piece of " + materialName(type));
msg.append("ate a piece of ").append(materialName(type));
else if (type == 25 || type == 93 || type == 94)
msg.append("changed " + materialName(type));
msg.append("changed ").append(materialName(type));
} else if (type == 0)
msg.append("destroyed " + materialName(replaced, data));
msg.append("destroyed ").append(materialName(replaced, data));
else if (replaced == 0)
msg.append("created " + materialName(type, data));
msg.append("created ").append(materialName(type, data));
else
msg.append("replaced " + materialName(replaced, (byte)0) + " with " + materialName(type, data));
msg.append("replaced ").append(materialName(replaced, (byte)0)).append(" with ").append(materialName(type, data));
if (loc != null)
msg.append(" at " + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ());
msg.append(" at ").append(loc.getBlockX()).append(":").append(loc.getBlockY()).append(":").append(loc.getBlockZ());
return msg.toString();
}

View File

@@ -406,9 +406,9 @@ public class CommandsHandler implements CommandExecutor
params.needType = true;
params.needData = true;
params.needPlayer = true;
if (params.types.size() == 0 || Block.inList(params.types, 63) || Block.inList(params.types, 68))
if (params.types.isEmpty() || Block.inList(params.types, 63) || Block.inList(params.types, 68))
params.needSignText = true;
if (params.bct == BlockChangeType.CHESTACCESS || params.types.size() == 0 || Block.inList(params.types, 23) || Block.inList(params.types, 54) || Block.inList(params.types, 61) || Block.inList(params.types, 62))
if (params.bct == BlockChangeType.CHESTACCESS || params.types.isEmpty() || Block.inList(params.types, 23) || Block.inList(params.types, 54) || Block.inList(params.types, 61) || Block.inList(params.types, 62))
params.needChestAccess = true;
}
conn = logblock.getConnection();
@@ -463,9 +463,9 @@ public class CommandsHandler implements CommandExecutor
params.needType = true;
params.needData = true;
params.needPlayer = true;
if (params.types.size() == 0 || Block.inList(params.types, 63) || Block.inList(params.types, 68))
if (params.types.isEmpty() || Block.inList(params.types, 63) || Block.inList(params.types, 68))
params.needSignText = true;
if (params.types.size() == 0 || Block.inList(params.types, 23) || Block.inList(params.types, 54) || Block.inList(params.types, 61) || Block.inList(params.types, 62))
if (params.types.isEmpty() || Block.inList(params.types, 23) || Block.inList(params.types, 54) || Block.inList(params.types, 61) || Block.inList(params.types, 62))
params.needChestAccess = true;
}
conn = logblock.getConnection();

View File

@@ -28,7 +28,7 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import de.diddiz.util.Block;
public class QueryParams implements Cloneable
public final class QueryParams implements Cloneable
{
private static final Set<Integer> keywords = new HashSet<Integer>(Arrays.asList("player".hashCode(), "area".hashCode(), "selection".hashCode(), "sel".hashCode(), "block".hashCode(), "type".hashCode(), "sum".hashCode(), "destroyed".hashCode(), "created".hashCode(), "chestaccess".hashCode(), "all".hashCode(), "time".hashCode(), "since".hashCode(), "before".hashCode(), "limit".hashCode(), "world".hashCode(), "asc".hashCode(), "desc".hashCode(), "last".hashCode(), "coords".hashCode(), "silent".hashCode(), "chat".hashCode(), "search".hashCode(), "match".hashCode(), "loc".hashCode(), "location".hashCode()));
public BlockChangeType bct = BlockChangeType.BOTH;
@@ -136,7 +136,7 @@ public class QueryParams implements Cloneable
final String[] blocknames = new String[types.size()];
for (int i = 0; i < types.size(); i++)
blocknames[i] = materialName(types.get(i).getBlock());
title.append(listing(blocknames, ", ", " and ") + " ");
title.append(listing(blocknames, ", ", " and ")).append(" ");
} else
title.append("block ");
if (bct == BlockChangeType.CREATED)
@@ -147,33 +147,33 @@ public class QueryParams implements Cloneable
title.append("changes ");
}
if (players.size() > 10)
title.append((excludePlayersMode ? "without" : "from") + " many players ");
title.append(excludePlayersMode ? "without" : "from").append(" many players ");
else if (!players.isEmpty())
title.append((excludePlayersMode ? "without" : "from") + " player" + (players.size() != 1 ? "s" : "") + " " + listing(players.toArray(new String[players.size()]), ", ", " and ") + " ");
title.append(excludePlayersMode ? "without" : "from").append(" player").append(players.size() != 1 ? "s" : "").append(" ").append(listing(players.toArray(new String[players.size()]), ", ", " and ")).append(" ");
if (match != null && match.length() > 0)
title.append("matching '" + match + "' ");
title.append("matching '").append(match).append("' ");
if (before > 0 && since > 0)
title.append("between " + since + " and " + before + " minutes ago ");
title.append("between ").append(since).append(" and ").append(before).append(" minutes ago ");
else if (since > 0)
title.append("in the last " + since + " minutes ");
title.append("in the last ").append(since).append(" minutes ");
else if (before > 0)
title.append("more than " + before * -1 + " minutes ago ");
title.append("more than ").append(before * -1).append(" minutes ago ");
if (loc != null) {
if (radius > 0)
title.append("within " + radius + " blocks of " + (prepareToolQuery ? "clicked block" : "you") + " ");
title.append("within ").append(radius).append(" blocks of ").append(prepareToolQuery ? "clicked block" : "you").append(" ");
else if (radius == 0)
title.append("at " + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ() + " ");
title.append("at ").append(loc.getBlockX()).append(":").append(loc.getBlockY()).append(":").append(loc.getBlockZ()).append(" ");
} else if (sel != null)
title.append(prepareToolQuery ? "at double chest " : "inside selection ");
else if (prepareToolQuery)
if (radius > 0)
title.append("within " + radius + " blocks of clicked block ");
title.append("within ").append(radius).append(" blocks of clicked block ");
else if (radius == 0)
title.append("at clicked block ");
if (world != null && !(sel != null && prepareToolQuery))
title.append("in " + friendlyWorldname(world.getName()) + " ");
title.append("in ").append(friendlyWorldname(world.getName())).append(" ");
if (sum != SummarizationMode.NONE)
title.append("summed up by " + (sum == SummarizationMode.TYPES ? "blocks" : "players") + " ");
title.append("summed up by ").append(sum == SummarizationMode.TYPES ? "blocks" : "players").append(" ");
title.deleteCharAt(title.length() - 1);
title.setCharAt(0, String.valueOf(title.charAt(0)).toUpperCase().toCharArray()[0]);
return title.toString();
@@ -189,9 +189,9 @@ public class QueryParams implements Cloneable
if (match != null && match.length() > 0) {
final boolean unlike = match.startsWith("-");
if (match.length() > 3 && !unlike || match.length() > 4)
where.append("MATCH (message) AGAINST ('" + match + "' IN BOOLEAN MODE) AND ");
where.append("MATCH (message) AGAINST ('").append(match).append("' IN BOOLEAN MODE) AND ");
else
where.append("message " + (unlike ? "NOT " : "") + "LIKE '%" + (unlike ? match.substring(1) : match) + "%' AND ");
where.append("message ").append(unlike ? "NOT " : "").append("LIKE '%").append(unlike ? match.substring(1) : match).append("%' AND ");
}
} else {
switch (blockChangeType) {
@@ -199,9 +199,9 @@ public class QueryParams implements Cloneable
if (!types.isEmpty()) {
where.append('(');
for (final Block block : types) {
where.append("((type = " + block.getBlock() + " OR replaced = " + block.getBlock());
where.append("((type = ").append(block.getBlock()).append(" OR replaced = ").append(block.getBlock());
if (block.getData() != -1) {
where.append(") AND data = " + block.getData());
where.append(") AND data = ").append(block.getData());
} else {
where.append(")");
}
@@ -215,9 +215,9 @@ public class QueryParams implements Cloneable
if (!types.isEmpty()) {
where.append('(');
for (final Block block : types) {
where.append("((type = " + block.getBlock() + " OR replaced = " + block.getBlock());
where.append("((type = ").append(block.getBlock()).append(" OR replaced = ").append(block.getBlock());
if (block.getData() != -1) {
where.append(") AND data = " + block.getData());
where.append(") AND data = ").append(block.getData());
} else {
where.append(")");
}
@@ -232,9 +232,9 @@ public class QueryParams implements Cloneable
if (!types.isEmpty()) {
where.append('(');
for (final Block block : types) {
where.append("((type = " + block.getBlock());
where.append("((type = ").append(block.getBlock());
if (block.getData() != -1) {
where.append(") AND data = " + block.getData());
where.append(") AND data = ").append(block.getData());
} else {
where.append(")");
}
@@ -250,9 +250,9 @@ public class QueryParams implements Cloneable
if (!types.isEmpty()) {
where.append('(');
for (final Block block : types) {
where.append("((replaced = " + block.getBlock());
where.append("((replaced = ").append(block.getBlock());
if (block.getData() != -1) {
where.append(") AND data = " + block.getData());
where.append(") AND data = ").append(block.getData());
} else {
where.append(")");
}
@@ -269,9 +269,9 @@ public class QueryParams implements Cloneable
if (!types.isEmpty()) {
where.append('(');
for (final Block block : types) {
where.append("((itemtype = " + block.getBlock());
where.append("((itemtype = ").append(block.getBlock());
if (block.getData() != -1) {
where.append(") AND itemdata = " + block.getData());
where.append(") AND itemdata = ").append(block.getData());
} else {
where.append(")");
}
@@ -284,26 +284,26 @@ public class QueryParams implements Cloneable
}
if (loc != null) {
if (radius == 0)
where.append("x = '" + loc.getBlockX() + "' AND y = '" + loc.getBlockY() + "' AND z = '" + loc.getBlockZ() + "' AND ");
where.append("x = '").append(loc.getBlockX()).append("' AND y = '").append(loc.getBlockY()).append("' AND z = '").append(loc.getBlockZ()).append("' AND ");
else if (radius > 0)
where.append("x > '" + (loc.getBlockX() - radius) + "' AND x < '" + (loc.getBlockX() + radius) + "' AND z > '" + (loc.getBlockZ() - radius) + "' AND z < '" + (loc.getBlockZ() + radius) + "' AND ");
where.append("x > '").append(loc.getBlockX() - radius).append("' AND x < '").append(loc.getBlockX() + radius).append("' AND z > '").append(loc.getBlockZ() - radius).append("' AND z < '").append(loc.getBlockZ() + radius).append("' AND ");
} else if (sel != null)
where.append("x >= '" + sel.getMinimumPoint().getBlockX() + "' AND x <= '" + sel.getMaximumPoint().getBlockX() + "' AND y >= '" + sel.getMinimumPoint().getBlockY() + "' AND y <= '" + sel.getMaximumPoint().getBlockY() + "' AND z >= '" + sel.getMinimumPoint().getBlockZ() + "' AND z <= '" + sel.getMaximumPoint().getBlockZ() + "' AND ");
where.append("x >= '").append(sel.getMinimumPoint().getBlockX()).append("' AND x <= '").append(sel.getMaximumPoint().getBlockX()).append("' AND y >= '").append(sel.getMinimumPoint().getBlockY()).append("' AND y <= '").append(sel.getMaximumPoint().getBlockY()).append("' AND z >= '").append(sel.getMinimumPoint().getBlockZ()).append("' AND z <= '").append(sel.getMaximumPoint().getBlockZ()).append("' AND ");
}
if (!players.isEmpty() && sum != SummarizationMode.PLAYERS)
if (!excludePlayersMode) {
where.append('(');
for (final String playerName : players)
where.append("playername = '" + playerName + "' OR ");
where.append("playername = '").append(playerName).append("' OR ");
where.delete(where.length() - 4, where.length());
where.append(") AND ");
} else
for (final String playerName : players)
where.append("playername != '" + playerName + "' AND ");
where.append("playername != '").append(playerName).append("' AND ");
if (since > 0)
where.append("date > date_sub(now(), INTERVAL " + since + " MINUTE) AND ");
where.append("date > date_sub(now(), INTERVAL ").append(since).append(" MINUTE) AND ");
if (before > 0)
where.append("date < date_sub(now(), INTERVAL " + before + " MINUTE) AND ");
where.append("date < date_sub(now(), INTERVAL ").append(before).append(" MINUTE) AND ");
if (where.length() > 6)
where.delete(where.length() - 4, where.length());
else

View File

@@ -3,11 +3,9 @@ package de.diddiz.LogBlock;
import static de.diddiz.LogBlock.config.Config.getLoggedWorlds;
import static de.diddiz.LogBlock.config.Config.isLogging;
import static de.diddiz.util.BukkitUtils.friendlyWorldname;
import static de.diddiz.util.Utils.readURL;
import static org.bukkit.Bukkit.getLogger;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;

View File

@@ -126,9 +126,6 @@ public class Config
config.set(e.getKey(), e.getValue());
logblock.saveConfig();
url = "jdbc:mysql://" + config.getString("mysql.host") + ":" + config.getInt("mysql.port") + "/" + getStringIncludingInts(config, "mysql.database");
String username = config.getString("mysql.user");
if (username == null)
username = String.valueOf(config.getInt("mysql.user"));
user = getStringIncludingInts(config, "mysql.user");
password = getStringIncludingInts(config, "mysql.password");
delayBetweenRuns = config.getInt("consumer.delayBetweenRuns", 6);
@@ -201,7 +198,7 @@ public class Config
}
final List<String> loggedWorlds = config.getStringList("loggedWorlds");
worldConfigs = new HashMap<String, WorldConfig>();
if (loggedWorlds.size() == 0)
if (loggedWorlds.isEmpty())
throw new DataFormatException("No worlds configured");
for (final String world : loggedWorlds)
worldConfigs.put(world, new WorldConfig(new File(logblock.getDataFolder(), friendlyWorldname(world) + ".yml")));

View File

@@ -81,8 +81,8 @@ public class Utils
return entries[0];
final StringBuilder builder = new StringBuilder(entries[0]);
for (int i = 1; i < len - 1; i++)
builder.append(delimiter + entries[i]);
builder.append(finalDelimiter + entries[len - 1]);
builder.append(delimiter).append(entries[i]);
builder.append(finalDelimiter).append(entries[len - 1]);
return builder.toString();
}
@@ -94,8 +94,8 @@ public class Utils
return entries.get(0).toString();
final StringBuilder builder = new StringBuilder(entries.get(0).toString());
for (int i = 1; i < len - 1; i++)
builder.append(delimiter + entries.get(i).toString());
builder.append(finalDelimiter + entries.get(len - 1).toString());
builder.append(delimiter).append(entries.get(i).toString());
builder.append(finalDelimiter).append(entries.get(len - 1).toString());
return builder.toString();
}