From 2d6d3f1a453dcf5f9e1fa3385a94ad347a5a6fda Mon Sep 17 00:00:00 2001 From: kaenganxt Date: Tue, 19 May 2015 17:29:00 +0200 Subject: [PATCH] Some bug fixes --- src/de/anura/core/API/Core.java | 2 +- src/de/anura/core/API/Errors.java | 56 +++++----- src/de/anura/core/Setup.java | 2 +- src/de/anura/core/Signs.java | 102 +++++++++---------- src/de/anura/core/commands/TeamCommands.java | 7 +- 5 files changed, 91 insertions(+), 78 deletions(-) diff --git a/src/de/anura/core/API/Core.java b/src/de/anura/core/API/Core.java index 77e3ee9..3556acb 100644 --- a/src/de/anura/core/API/Core.java +++ b/src/de/anura/core/API/Core.java @@ -126,7 +126,7 @@ public class Core { Player p = (Player) P; if (!Core.cachedPlayerLanguage.containsKey(p)) { ResultSet rs = Core.getMySql().querySelect("SELECT lang FROM players WHERE uuid = '" + p.getUniqueId().toString() + "'"); - if (!rs.next()) { + if (rs == null || !rs.next()) { //This is a weird bug Core.cachedPlayerLanguage.put(p, "de"); return "de"; diff --git a/src/de/anura/core/API/Errors.java b/src/de/anura/core/API/Errors.java index 7fb99bf..57c061d 100644 --- a/src/de/anura/core/API/Errors.java +++ b/src/de/anura/core/API/Errors.java @@ -4,12 +4,14 @@ import java.sql.ResultSet; import java.util.AbstractMap; import java.util.Map.Entry; import java.util.UUID; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; public class Errors { /** * Reports an exception to the database + * * @param exc A Throwable to get the error data from * @param info Some additional info */ @@ -19,34 +21,37 @@ public class Errors { exc.printStackTrace(); return; } - try { - String message = exc.getLocalizedMessage(); - String name = exc.getClass().toString(); - String server = Core.getMainClass() == null ? "~not available~" : Core.getMainClass().getConfig().getString("server-name"); - String errorSql = "INSERT INTO coreErrors (server, exception, msg, info, timestamp) VALUES('" + server + "', '" + name + "', '" + Core.getMySql().escapeString(message) + "', '" + info + "', '" + System.currentTimeMillis() / 1000 + "')"; - Core.getMySql().queryUpdate(errorSql); - ResultSet rs = Core.getMySql().querySelect("SELECT id FROM coreErrors ORDER BY id DESC LIMIT 1"); - rs.first(); - int id = rs.getInt("id"); - String elemSql = "INSERT INTO coreErrorStack(errorId, className, fileName, methodName, lineNumber, nativeMethod) VALUES"; - boolean first = true; - for(StackTraceElement elem : exc.getStackTrace()) { - if (!first) { - elemSql += ","; - } else { - first = false; + Bukkit.getScheduler().runTaskAsynchronously(Core.getMainClass(), () -> { + try { + String message = exc.getLocalizedMessage(); + String name = exc.getClass().toString(); + String server = Core.getMainClass() == null ? "~not available~" : Core.getMainClass().getConfig().getString("server-name"); + String errorSql = "INSERT INTO coreErrors (server, exception, msg, info, timestamp) VALUES('" + server + "', '" + name + "', '" + Core.getMySql().escapeString(message) + "', '" + info + "', '" + System.currentTimeMillis() / 1000 + "')"; + Core.getMySql().queryUpdate(errorSql); + ResultSet rs = Core.getMySql().querySelect("SELECT id FROM coreErrors ORDER BY id DESC LIMIT 1"); + rs.first(); + int id = rs.getInt("id"); + String elemSql = "INSERT INTO coreErrorStack(errorId, className, fileName, methodName, lineNumber, nativeMethod) VALUES"; + boolean first = true; + for (StackTraceElement elem : exc.getStackTrace()) { + if (!first) { + elemSql += ","; + } else { + first = false; + } + elemSql += "('" + id + "', '" + elem.getClassName() + "', '" + elem.getFileName() + "', '" + elem.getMethodName() + "', '" + elem.getLineNumber() + "', " + elem.isNativeMethod() + ")"; } - elemSql += "('" + id + "', '" + elem.getClassName() + "', '" + elem.getFileName() + "', '" + elem.getMethodName() + "', '" + elem.getLineNumber() + "', " + elem.isNativeMethod() + ")"; + Core.getMySql().queryUpdate(elemSql); + } catch (Throwable ex) { + System.err.println("FATAL ERROR: Error handler generated an exception!! (" + ex.getLocalizedMessage() + ")"); + ex.printStackTrace(); } - Core.getMySql().queryUpdate(elemSql); - } catch (Throwable ex) { - System.err.println("FATAL ERROR: Error handler generated an exception!! (" + ex.getLocalizedMessage() + ")"); - ex.printStackTrace(); - } + }); } /** * Reports an exception to the database + * * @param exc A Throwable to get the error from */ public static void reportException(Throwable exc) { @@ -55,14 +60,16 @@ public class Errors { /** * Method to report a bug + * * @param P The reporter * @param msg A message from the reporter - * @param data An array of error entries generated by {@link make(String, Object)} + * @param data An array of error entries generated by + * {@link make(String, Object)} */ public static void bugReport(Player P, String msg, Entry... data) { UUID uuid = P.getUniqueId(); String dataText = ""; - for(Entry date : data) { + for (Entry date : data) { dataText += date.getKey() + ":" + date.getValue().toString() + ";"; } Core.getMySql().queryUpdate("INSERT INTO serverBugs(player, msg, data, timestamp) VALUES ((SELECT id FROM players WHERE uuid = '" + uuid.toString() + "'), '" + Core.getMySql().escapeString(msg) + "', '" + dataText + "', '" + System.currentTimeMillis() / 1000 + "')"); @@ -70,6 +77,7 @@ public class Errors { /** * Generates an error entry + * * @param key The key for this object * @param value Any object to use as value * @return The error entry to give to diff --git a/src/de/anura/core/Setup.java b/src/de/anura/core/Setup.java index cf38d73..415504f 100644 --- a/src/de/anura/core/Setup.java +++ b/src/de/anura/core/Setup.java @@ -132,7 +132,7 @@ public class Setup { Data.sittingPlayer.put(p, a); } }, 600, 600); - Bukkit.getScheduler().runTaskTimerAsynchronously(Core.getMainClass(), AnuraCore.signs::updateServerSigns, 20 * 5, 20 * 5); + Bukkit.getScheduler().scheduleSyncRepeatingTask(Core.getMainClass(), AnuraCore.signs::updateServerSigns, 20 * 5, 20 * 5); Bukkit.getScheduler().scheduleSyncRepeatingTask(Core.getMainClass(), new VillagerTask(), 20, 20); } catch (Throwable e) { Errors.reportException(e); diff --git a/src/de/anura/core/Signs.java b/src/de/anura/core/Signs.java index 051e265..667965b 100644 --- a/src/de/anura/core/Signs.java +++ b/src/de/anura/core/Signs.java @@ -24,8 +24,7 @@ public class Signs implements PluginMessageListener { return; } ResultSet rs = Core.getMySql().querySelect("SELECT DISTINCT value FROM coreWarpSigns WHERE type = 'server' AND server = '" + Core.getMainClass().getConfig().getString("server-name") + "'"); - rs.last(); - if (rs.getRow() == 0) { + if (rs == null || !rs.first()) { return; } rs.beforeFirst(); @@ -105,62 +104,63 @@ public class Signs implements PluginMessageListener { DataInputStream in = new DataInputStream(new ByteArrayInputStream(message)); String subchannel = in.readUTF(); - if (subchannel.equals("PlayerCount")) { - String server = in.readUTF(); - int playercount = in.readInt(); - String sqlQ = "SELECT X,Y,Z,world FROM coreWarpSigns WHERE server = '" + Core.getMainClass().getConfig().getString("server-name") + "' AND type = 'server' AND value = '" + server + "'"; - ResultSet rs = Core.getMySql().querySelect(sqlQ); - rs.last(); - if (rs.getRow() == 0) { - return; - } - rs.beforeFirst(); - while (rs.next()) { - Boolean remove = false; - World w = Bukkit.getWorld(rs.getString("world")); - BlockState bs = null; - if (w == null) { - remove = true; - } else { - Block b = w.getBlockAt(rs.getInt("X"), rs.getInt("Y"), rs.getInt("Z")); - if (b == null || !(b.getState() instanceof Sign)) { + switch (subchannel) { + case "PlayerCount": + String server = in.readUTF(); + int playercount = in.readInt(); + String sqlQ = "SELECT X,Y,Z,world FROM coreWarpSigns WHERE server = '" + Core.getMainClass().getConfig().getString("server-name") + "' AND type = 'server' AND value = '" + server + "'"; + ResultSet rs = Core.getMySql().querySelect(sqlQ); + rs.last(); + if (rs.getRow() == 0) { + return; + } rs.beforeFirst(); + while (rs.next()) { + Boolean remove = false; + World w = Bukkit.getWorld(rs.getString("world")); + BlockState bs = null; + if (w == null) { remove = true; } else { - bs = b.getState(); + Block b = w.getBlockAt(rs.getInt("X"), rs.getInt("Y"), rs.getInt("Z")); + if (b == null || !(b.getState() instanceof Sign)) { + remove = true; + } else { + bs = b.getState(); + } } - } - if (remove) { - String anotherSQL = "DELETE FROM coreWarpSigns WHERE server = '" + Core.getMainClass().getConfig().getString("server-name") + "' AND type = 'server' AND value = '" + server + "'"; - Core.getMySql().queryUpdate(anotherSQL); - continue; - } - if (bs == null) { - continue; - } - if (bs instanceof Sign) { - Sign s = (Sign) bs; - if (s.getLine(3).equalsIgnoreCase(ChatColor.RED + "Offline")) { - s.setLine(1, ChatColor.RED + "--" + ChatColor.DARK_GRAY + "/" + ChatColor.RED + "--"); + if (remove) { + String anotherSQL = "DELETE FROM coreWarpSigns WHERE server = '" + Core.getMainClass().getConfig().getString("server-name") + "' AND type = 'server' AND value = '" + server + "'"; + Core.getMySql().queryUpdate(anotherSQL); continue; } - String maxplayers = s.getLine(1).substring(s.getLine(1).length() - 2); - Boolean hasPlace = true; - if (maxplayers.equals("--")) { - hasPlace = true; - } else if (Integer.parseInt(maxplayers) == playercount) { - hasPlace = false; + if (bs == null) { + continue; } - if (hasPlace) { - s.setLine(1, ChatColor.GREEN + String.valueOf(playercount) + ChatColor.DARK_GRAY + "/" + ChatColor.GREEN + maxplayers); - } else { - s.setLine(1, ChatColor.RED + String.valueOf(playercount) + ChatColor.DARK_GRAY + "/" + ChatColor.GREEN + maxplayers); + if (bs instanceof Sign) { + Sign s = (Sign) bs; + if (s.getLine(3).equalsIgnoreCase(ChatColor.RED + "Offline")) { + s.setLine(1, ChatColor.RED + "--" + ChatColor.DARK_GRAY + "/" + ChatColor.RED + "--"); + continue; + } + String maxplayers = s.getLine(1).substring(s.getLine(1).length() - 2); + Boolean hasPlace = true; + if (maxplayers.equals("--")) { + hasPlace = true; + } else if (Integer.parseInt(maxplayers) == playercount) { + hasPlace = false; + } + if (hasPlace) { + s.setLine(1, ChatColor.GREEN + String.valueOf(playercount) + ChatColor.DARK_GRAY + "/" + ChatColor.GREEN + maxplayers); + } else { + s.setLine(1, ChatColor.RED + String.valueOf(playercount) + ChatColor.DARK_GRAY + "/" + ChatColor.GREEN + maxplayers); + } + s.update(); } - s.update(); - } - } - } else if (subchannel.equals("updatePermissions")) { - AnuraCore.perms.reload(); - AnuraCore.perms.updateAllPlayers(); + } break; + case "updatePermissions": + AnuraCore.perms.reload(); + AnuraCore.perms.updateAllPlayers(); + break; } } catch (Throwable e) { Errors.reportException(e); diff --git a/src/de/anura/core/commands/TeamCommands.java b/src/de/anura/core/commands/TeamCommands.java index 13f4bb0..f571dff 100644 --- a/src/de/anura/core/commands/TeamCommands.java +++ b/src/de/anura/core/commands/TeamCommands.java @@ -180,7 +180,12 @@ public class TeamCommands implements CommandExecutor { if (args.length != 1) { return false; } - if (Float.parseFloat(args[0]) >= 10 || Float.parseFloat(args[0]) < 0) { + try { + if (Float.parseFloat(args[0]) >= 10 || Float.parseFloat(args[0]) < 0) { + Core.statusMsg(P, "flyspeed_wrong_number", false); + return true; + } + } catch (NumberFormatException ex) { Core.statusMsg(P, "flyspeed_wrong_number", false); return true; }