From 51584a3d04e3c64603c4789e7f260a5b856ab4ce Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 10 Aug 2016 13:42:02 +0200 Subject: [PATCH] Removed almost every stack trace that might me printed Stack traces will now be printed in the debug log (if enabled) and in the console, there will only be displayed an error message without the stack trace --- .../java/de/epiceric/shopchest/ShopChest.java | 4 ++- .../de/epiceric/shopchest/config/Config.java | 6 ++--- .../de/epiceric/shopchest/nms/Hologram.java | 8 +++--- .../epiceric/shopchest/nms/JsonBuilder.java | 4 +-- .../epiceric/shopchest/nms/SpawnEggMeta.java | 4 +-- .../java/de/epiceric/shopchest/shop/Shop.java | 26 ++++++------------ .../de/epiceric/shopchest/shop/ShopItem.java | 4 +-- .../de/epiceric/shopchest/sql/Database.java | 27 ++++++++++++------- .../java/de/epiceric/shopchest/sql/MySQL.java | 4 ++- .../de/epiceric/shopchest/sql/SQLite.java | 11 +++++--- .../de/epiceric/shopchest/utils/Utils.java | 8 ++++-- 11 files changed, 58 insertions(+), 48 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 1a09ef7..4710358 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -95,6 +95,7 @@ public class ShopChest extends JavaPlugin { fw = new FileWriter(debugLogFile, true); } catch (IOException e) { + getLogger().info("Failed to instantiate FileWriter"); e.printStackTrace(); } } @@ -272,9 +273,9 @@ public class ShopChest extends JavaPlugin { debug("Trying to register command \"/" + config.main_command_name + "\""); ShopCommand.registerCommand(new ShopCommand(this, config.main_command_name, "Manage Shops.", "", new ArrayList()), this); } catch (Exception e) { + getLogger().info("Failed to register command"); debug("Failed to register command"); debug(e); - e.printStackTrace(); } initializeShops(); @@ -312,6 +313,7 @@ public class ShopChest extends JavaPlugin { try { fw.close(); } catch (IOException e) { + getLogger().severe("Failed to close FileWriter"); e.printStackTrace(); } } diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index 9b043a2..3012676 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -342,7 +342,7 @@ public class Config { langConfig.loadFromString(sb.toString()); } catch (IOException | InvalidConfigurationException ex) { - ex.printStackTrace(); + plugin.debug(ex); plugin.getLogger().warning("Using default language values"); } } else { @@ -350,7 +350,7 @@ public class Config { langConfig.load(langDefaultFile); plugin.getLogger().info("Using locale \"en_US\""); } catch (IOException | InvalidConfigurationException e) { - e.printStackTrace(); + plugin.debug(e); plugin.getLogger().warning("Using default language values"); } } @@ -359,7 +359,7 @@ public class Config { plugin.getLogger().info("Using locale \"" + langConfigFile.getName().substring(0, langConfigFile.getName().length() - 5) + "\""); langConfig.load(langConfigFile); } catch (IOException | InvalidConfigurationException ex) { - ex.printStackTrace(); + plugin.debug(ex); plugin.getLogger().warning("Using default language values"); } } diff --git a/src/main/java/de/epiceric/shopchest/nms/Hologram.java b/src/main/java/de/epiceric/shopchest/nms/Hologram.java index b3eec44..131ea33 100644 --- a/src/main/java/de/epiceric/shopchest/nms/Hologram.java +++ b/src/main/java/de/epiceric/shopchest/nms/Hologram.java @@ -73,9 +73,9 @@ public class Hologram { this.location.subtract(0, 0.25, 0); count++; } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { + plugin.getLogger().severe("Could not create Hologram with reflection"); plugin.debug("Could not create Hologram with reflection"); plugin.debug(e); - e.printStackTrace(); } @@ -107,9 +107,9 @@ public class Hologram { Utils.sendPacket(plugin, packet, p); } catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) { + plugin.getLogger().severe("Could not show Hologram to player with reflection"); plugin.debug("Could not show Hologram to player with reflection"); plugin.debug(e); - e.printStackTrace(); } } visible.add(p); @@ -127,9 +127,9 @@ public class Hologram { Utils.sendPacket(plugin, packet, p); } catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) { + plugin.getLogger().severe("Could not hide Hologram from player with reflection"); plugin.debug("Could not hide Hologram from player with reflection"); plugin.debug(e); - e.printStackTrace(); } } visible.remove(p); @@ -159,9 +159,9 @@ public class Hologram { try { o.getClass().getMethod("die").invoke(o); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + plugin.getLogger().severe("Could not remove Hologram with reflection"); plugin.debug("Could not remove Hologram with reflection"); plugin.debug(e); - e.printStackTrace(); } } exists = false; diff --git a/src/main/java/de/epiceric/shopchest/nms/JsonBuilder.java b/src/main/java/de/epiceric/shopchest/nms/JsonBuilder.java index cb8a5c2..872fbb7 100644 --- a/src/main/java/de/epiceric/shopchest/nms/JsonBuilder.java +++ b/src/main/java/de/epiceric/shopchest/nms/JsonBuilder.java @@ -117,9 +117,9 @@ public class JsonBuilder { Utils.sendPacket(plugin, packetPlayOutChat, p); } catch (InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { - plugin.debug("Failed to send packet with reflection"); + plugin.getLogger().severe("Failed to send JSON with reflection"); + plugin.debug("Failed to send JSON with reflection"); plugin.debug(e); - e.printStackTrace(); } } diff --git a/src/main/java/de/epiceric/shopchest/nms/SpawnEggMeta.java b/src/main/java/de/epiceric/shopchest/nms/SpawnEggMeta.java index 8e3a649..59997e5 100644 --- a/src/main/java/de/epiceric/shopchest/nms/SpawnEggMeta.java +++ b/src/main/java/de/epiceric/shopchest/nms/SpawnEggMeta.java @@ -30,9 +30,9 @@ public class SpawnEggMeta { if (id instanceof String) return (String) id; } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - plugin.debug("Could not get NBTEntityID with reflection"); + plugin.getLogger().severe("Failed to get NBTEntityID with reflection"); + plugin.debug("Failed to get NBTEntityID with reflection"); plugin.debug(e); - e.printStackTrace(); } return null; diff --git a/src/main/java/de/epiceric/shopchest/shop/Shop.java b/src/main/java/de/epiceric/shopchest/shop/Shop.java index 0b33fb4..1cf2703 100644 --- a/src/main/java/de/epiceric/shopchest/shop/Shop.java +++ b/src/main/java/de/epiceric/shopchest/shop/Shop.java @@ -42,25 +42,15 @@ public class Shop { Block b = location.getBlock(); if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) { - try { - plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error); - Exception e = new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); - plugin.debug(e); - throw e; - } catch (Exception ex) { - ex.printStackTrace(); - return; - } + plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error); + plugin.getLogger().severe("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); + plugin.debug("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); + return; } else if ((b.getRelative(BlockFace.UP).getType() != Material.AIR) && plugin.getShopChestConfig().show_shop_items) { - try { - plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error); - Exception e = new Exception("No space above chest at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); - plugin.debug(e); - throw e; - } catch (Exception ex) { - ex.printStackTrace(); - return; - } + plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error); + plugin.getLogger().severe("No space above chest at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); + plugin.debug("No space above chest at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); + return; } if (hologram == null || !hologram.exists()) createHologram(); diff --git a/src/main/java/de/epiceric/shopchest/shop/ShopItem.java b/src/main/java/de/epiceric/shopchest/shop/ShopItem.java index 195976b..145b17e 100644 --- a/src/main/java/de/epiceric/shopchest/shop/ShopItem.java +++ b/src/main/java/de/epiceric/shopchest/shop/ShopItem.java @@ -80,9 +80,9 @@ public class ShopItem { creationPackets[1] = packetPlayOutEntityMetadataClass.getConstructor(int.class, dataWatcherClass, boolean.class).newInstance(entityId, dataWatcher, true); creationPackets[2] = packetPlayOutEntityVelocityClass.getConstructor(int.class, double.class, double.class, double.class).newInstance(entityId, 0D, 0D, 0D); } catch (NoSuchMethodException | NoSuchFieldException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + plugin.getLogger().severe("Failed to create shop item"); plugin.debug("Failed to create shop item with reflection"); plugin.debug(e); - e.printStackTrace(); } } @@ -120,9 +120,9 @@ public class ShopItem { Utils.sendPacket(plugin, packetPlayOutEntityDestroy, p); } } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { + plugin.getLogger().severe("Failed to destroy shop item"); plugin.debug("Failed to destroy shop item with reflection"); plugin.debug(e); - e.printStackTrace(); } } diff --git a/src/main/java/de/epiceric/shopchest/sql/Database.java b/src/main/java/de/epiceric/shopchest/sql/Database.java index f661895..45db1b1 100644 --- a/src/main/java/de/epiceric/shopchest/sql/Database.java +++ b/src/main/java/de/epiceric/shopchest/sql/Database.java @@ -68,9 +68,9 @@ public abstract class Database { close(ps, rs); } catch (SQLException ex) { + plugin.getLogger().severe("Failed to connect to database"); plugin.debug("Failed to connect to database"); plugin.debug(ex); - ex.printStackTrace(); } } @@ -112,7 +112,8 @@ public abstract class Database { return highestID; } catch (SQLException ex) { - ex.printStackTrace(); + plugin.debug("Failed to get highest used ID"); + plugin.getLogger().severe("Failed to access database"); } finally { close(ps, rs); } @@ -133,7 +134,9 @@ public abstract class Database { plugin.debug("Removing shop from database (#" + shop.getID() + ")"); ps.executeUpdate(); } catch (SQLException ex) { - ex.printStackTrace(); + plugin.getLogger().severe("Failed to access database"); + plugin.debug("Failed to remove shop from database (#" + shop.getID() + ")"); + plugin.debug(ex); } finally { close(ps, null); } @@ -210,9 +213,8 @@ public abstract class Database { plugin.debug("Getting sellprice: " + sellprice + " (#" + id + ")"); return sellprice; case SHOPTYPE: - ShopType shopType = ShopType.valueOf(rs.getString("shoptype")); - plugin.debug("Getting shoptype: " + rs.getString("shoptype") + " (#" + id + ")"); String shoptype = rs.getString("shoptype"); + plugin.debug("Getting shoptype: " + shoptype + " (#" + id + ")"); if (shoptype.equals("INFINITE")) { @@ -236,7 +238,9 @@ public abstract class Database { plugin.debug("Shop with ID not found, returning null. (#" + id + ")"); } catch (SQLException ex) { - ex.printStackTrace(); + plugin.getLogger().severe("Failed to access database"); + plugin.debug("Failed to get shop info " + shopInfo.toString() + "(#" + id + ")"); + plugin.debug(ex); } finally { close(ps, rs); } @@ -269,7 +273,9 @@ public abstract class Database { ps.executeUpdate(); } catch (SQLException ex) { - ex.printStackTrace(); + plugin.getLogger().severe("Failed to access database"); + plugin.debug("Failed to add shop to database (#" + shop.getID() + ")"); + plugin.debug(ex); } finally { close(ps, null); } @@ -287,7 +293,8 @@ public abstract class Database { if (rs != null) rs.close(); } catch (SQLException ex) { - ex.printStackTrace(); + plugin.debug("Failed to close PreparedStatement/ResultSet"); + plugin.debug(ex); } } @@ -296,14 +303,14 @@ public abstract class Database { */ public void disconnect() { try { - if (connection != null) { + if (connection != null && !connection.isClosed()) { plugin.debug("Disconnecting from database..."); connection.close(); } } catch (SQLException e) { + plugin.getLogger().severe("Failed to disconnect from database"); plugin.debug("Failed to disconnect from database"); plugin.debug(e); - e.printStackTrace(); } } diff --git a/src/main/java/de/epiceric/shopchest/sql/MySQL.java b/src/main/java/de/epiceric/shopchest/sql/MySQL.java index 50b7127..6b71b95 100644 --- a/src/main/java/de/epiceric/shopchest/sql/MySQL.java +++ b/src/main/java/de/epiceric/shopchest/sql/MySQL.java @@ -27,7 +27,9 @@ public class MySQL extends Database { return connection; } catch (Exception ex) { - ex.printStackTrace(); + plugin.getLogger().severe("Failed to get database connection"); + plugin.debug("Failed to get database connection"); + plugin.debug(ex); } return null; diff --git a/src/main/java/de/epiceric/shopchest/sql/SQLite.java b/src/main/java/de/epiceric/shopchest/sql/SQLite.java index 68bf022..52be243 100644 --- a/src/main/java/de/epiceric/shopchest/sql/SQLite.java +++ b/src/main/java/de/epiceric/shopchest/sql/SQLite.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.SQLException; public class SQLite extends Database { @@ -22,7 +23,9 @@ public class SQLite extends Database { try { dbFile.createNewFile(); } catch (IOException ex) { - ex.printStackTrace(); + plugin.getLogger().severe("Failed to create database file"); + plugin.debug("Failed to create database file"); + plugin.debug(ex); } } @@ -35,8 +38,10 @@ public class SQLite extends Database { connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile); return connection; - } catch (Exception ex) { - ex.printStackTrace(); + } catch (ClassNotFoundException | SQLException ex) { + plugin.getLogger().severe("Failed to get database connection"); + plugin.debug("Failed to get database connection"); + plugin.debug(ex); } return null; diff --git a/src/main/java/de/epiceric/shopchest/utils/Utils.java b/src/main/java/de/epiceric/shopchest/utils/Utils.java index 534a65a..b5abe67 100644 --- a/src/main/java/de/epiceric/shopchest/utils/Utils.java +++ b/src/main/java/de/epiceric/shopchest/utils/Utils.java @@ -3,6 +3,7 @@ package de.epiceric.shopchest.utils; import de.epiceric.shopchest.ShopChest; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -135,6 +136,9 @@ public class Utils { */ public static boolean sendPacket(ShopChest plugin, Object packet, Player player) { try { + if (packet == null) + return false; + Class packetClass = Class.forName("net.minecraft.server." + getServerVersion() + ".Packet"); Object nmsPlayer = player.getClass().getMethod("getHandle").invoke(player); Object playerConnection = nmsPlayer.getClass().getField("playerConnection").get(nmsPlayer); @@ -143,9 +147,9 @@ public class Utils { return true; } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | IllegalAccessException | InvocationTargetException e) { + plugin.getLogger().severe("Failed to send packet " + packet.getClass().getName()); plugin.debug("Failed to send packet " + packet.getClass().getName()); plugin.debug(e); - e.printStackTrace(); return false; } } @@ -195,7 +199,7 @@ public class Utils { YamlConfiguration config = new YamlConfiguration(); try { config.loadFromString(new String(DatatypeConverter.parseBase64Binary(string), StandardCharsets.UTF_8)); - } catch (Exception e) { + } catch (IllegalArgumentException | InvalidConfigurationException e) { e.printStackTrace(); return null; }