From 8c083702206a3e1745b3ebe0e79cb9d6263df2f9 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 3 Dec 2016 19:53:58 +0100 Subject: [PATCH] Refreshed error handling, removed getlastereror --- .../buttondevteam/chat/ChatProcessing.java | 80 ++++++------------- .../java/buttondevteam/chat/PluginMain.java | 60 +++++--------- .../chat/commands/ucmds/AcceptCommand.java | 4 +- .../ucmds/admin/GetLastErrorCommand.java | 33 -------- 4 files changed, 50 insertions(+), 127 deletions(-) delete mode 100644 src/main/java/buttondevteam/chat/commands/ucmds/admin/GetLastErrorCommand.java diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 885306b..4701dd3 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -25,6 +25,7 @@ import buttondevteam.chat.formatting.ChatFormatterBuilder; import buttondevteam.chat.formatting.TellrawEvent; import buttondevteam.chat.formatting.TellrawPart; import buttondevteam.chat.formatting.TellrawSerializer; +import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCPlayer; import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.TellrawSerializableEnum; @@ -255,18 +256,22 @@ public class ChatProcessing { for (Player p : PluginMain.GetPlayers()) { try { Resident resident = PluginMain.Instance.TU.getResidentMap().get(p.getName().toLowerCase()); - if (!resident.getName().equals(player.getName()) && resident.getModes().contains("spy")) + if (resident != null && !resident.getName().equals(player.getName()) + && resident.getModes().contains("spy")) Bukkit.getPlayer(resident.getName()).sendMessage(String.format("[SPY-%s] - %s: %s", currentchannel.DisplayName, player.getDisplayName(), message)); } catch (Exception e) { } } } - if (currentchannel.equals(Channel.TownChat)) { - try { + try { + if (currentchannel.equals(Channel.TownChat)) { Town town = null; try { - town = PluginMain.Instance.TU.getResidentMap().get(player.getName().toLowerCase()).getTown(); + final Resident resident = PluginMain.Instance.TU.getResidentMap() + .get(player.getName().toLowerCase()); + if (resident != null && resident.hasTown()) + town = resident.getTown(); } catch (NotRegisteredException e) { } if (town == null) { @@ -281,8 +286,7 @@ public class ChatProcessing { Objective obj = PluginMain.SB.getObjective("town"); for (Player p : PluginMain.GetPlayers()) { try { - if (PluginMain.Instance.TU.getResidentMap().get(p.getName().toLowerCase()).getTown().getName() - .equals(town.getName())) + if (town.getResidents().stream().anyMatch(r -> r.getName().equalsIgnoreCase(p.getName()))) obj.getScore(p.getName()).setScore(index); else obj.getScore(p.getName()).setScore(-1); @@ -292,20 +296,13 @@ public class ChatProcessing { } PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, String.format("tellraw @a[score_town=%d,score_town_min=%d] %s", index, index, jsonstr)); - } catch (IllegalStateException e) { - e.printStackTrace(); - player.sendMessage("§cAn error occured while sending the message. (IllegalStateException)"); - return true; - } catch (IllegalArgumentException e) { - e.printStackTrace(); - player.sendMessage("§cAn error occured while sending the message. (IllegalArgumentException)"); - return true; - } - } else if (currentchannel.equals(Channel.NationChat)) { - try { + } else if (currentchannel.equals(Channel.NationChat)) { Town town = null; try { - town = PluginMain.Instance.TU.getResidentMap().get(player.getName().toLowerCase()).getTown(); + final Resident resident = PluginMain.Instance.TU.getResidentMap() + .get(player.getName().toLowerCase()); + if (resident != null && resident.hasTown()) + town = resident.getTown(); } catch (NotRegisteredException e) { } if (town == null) { @@ -329,8 +326,7 @@ public class ChatProcessing { Objective obj = PluginMain.SB.getObjective("nation"); for (Player p : PluginMain.GetPlayers()) { try { - if (PluginMain.Instance.TU.getResidentMap().get(p.getName().toLowerCase()).getTown().getNation() - .getName().equals(nation.getName())) + if (nation.getResidents().stream().anyMatch(r -> r.getName().equalsIgnoreCase(p.getName()))) obj.getScore(p.getName()).setScore(index); else obj.getScore(p.getName()).setScore(-1); @@ -339,17 +335,7 @@ public class ChatProcessing { } PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, String.format("tellraw @a[score_nation=%d,score_nation_min=%d] %s", index, index, jsonstr)); - } catch (IllegalStateException e) { - e.printStackTrace(); - player.sendMessage("§cAn error occured while sending the message. (IllegalStateException)"); - return true; - } catch (IllegalArgumentException e) { - e.printStackTrace(); - player.sendMessage("§cAn error occured while sending the message. (IllegalArgumentException)"); - return true; - } - } else if (currentchannel.equals(Channel.AdminChat)) { - try { + } else if (currentchannel.equals(Channel.AdminChat)) { if (player != null && !player.isOp()) { player.sendMessage("§cYou need to be an OP to use this channel."); return true; @@ -363,17 +349,7 @@ public class ChatProcessing { } PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, String.format("tellraw @a[score_admin=%d,score_admin_min=%d] %s", 1, 1, jsonstr)); - } catch (IllegalStateException e) { - e.printStackTrace(); - player.sendMessage("§cAn error occured while sending the message. (IllegalStateException)"); - return true; - } catch (IllegalArgumentException e) { - e.printStackTrace(); - player.sendMessage("§cAn error occured while sending the message. (IllegalArgumentException)"); - return true; - } - } else if (currentchannel.equals(Channel.ModChat)) { - try { + } else if (currentchannel.equals(Channel.ModChat)) { if (player != null && !PluginMain.permission.playerInGroup(player, "mod")) { player.sendMessage("§cYou need to be a mod to use this channel."); return true; @@ -387,18 +363,14 @@ public class ChatProcessing { } PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, String.format("tellraw @a[score_mod=%d,score_mod_min=%d] %s", 1, 1, jsonstr)); - } catch (IllegalStateException e) { - e.printStackTrace(); - player.sendMessage("§cAn error occured while sending the message. (IllegalStateException)"); - return true; - } catch (IllegalArgumentException e) { - e.printStackTrace(); - player.sendMessage("§cAn error occured while sending the message. (IllegalArgumentException)"); - return true; - } - } else - PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - String.format("tellraw @a %s", jsonstr)); + } else + PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, + String.format("tellraw @a %s", jsonstr)); + } catch (Exception e) { + TBMCCoreAPI.SendException("An error occured while sending a chat message!", e); + player.sendMessage("§cAn error occured while sending the message."); + return true; + } PluginMain.Instance.getServer().getConsoleSender() .sendMessage(String.format("[%s] <%s%s> %s", currentchannel.DisplayName, (player != null ? player.getDisplayName() : sender.getName()), diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index a066f38..afc7d11 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -6,7 +6,6 @@ import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.command.ConsoleCommandSender; -import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; @@ -120,6 +119,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. } private void FlairGetterThreadMethod() { + int errorcount = 0; while (!stop) { try { String body = TBMCCoreAPI.DownloadString(FlairThreadURL + ".json?limit=1000"); @@ -168,7 +168,11 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. Thread.currentThread().interrupt(); } } catch (Exception e) { - LastException = e; + errorcount++; + if (errorcount >= 10) { + errorcount = 0; + TBMCCoreAPI.SendException("Error while getting flairs from Reddit!", e); + } } } } @@ -190,8 +194,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. SetFlair(mp, flair, flairclass, mp.getUserName()); } - public static Exception LastException; - private void SetFlair(ChatPlayer p, String text, String flairclass, String username) { p.setUserName(username); p.setFlairState(FlairStates.Recognised); @@ -200,34 +202,19 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. p.SetFlair(Short.parseShort(text), true); return; case "unknown": - if (text.equals("-1")) // If true, only non-presser/can't press; if - // false, any flair (but we can still detect - // can't press) - { - try { - if (CheckForJoinDate(p)) { + try { + if (CheckForJoinDate(p)) { + if (text.equals("-1")) // If true, only non-presser/can't press; if false, any flair (but we can still detect can't press) p.SetFlair(ChatPlayer.FlairTimeNonPresser); - } else { - p.SetFlair(ChatPlayer.FlairTimeCantPress); - } - } catch (Exception e) { - p.setFlairState(FlairStates.Commented); // Flair unknown - p.SetFlair(ChatPlayer.FlairTimeNone); - e.printStackTrace(); - } - } else { - try { - if (CheckForJoinDate(p)) { - p.setFlairState(FlairStates.Commented); // Flair unknown - p.SetFlair(ChatPlayer.FlairTimeNone); - } else { - p.SetFlair(ChatPlayer.FlairTimeCantPress); - } - } catch (Exception e) { - p.setFlairState(FlairStates.Commented); // Flair unknown - p.SetFlair(ChatPlayer.FlairTimeNone); - e.printStackTrace(); + else + p.SetFlair(ChatPlayer.FlairTimeNone); // Flair unknown + } else { + p.SetFlair(ChatPlayer.FlairTimeCantPress); } + } catch (Exception e) { + p.setFlairState(FlairStates.Commented); // Flair unknown + p.SetFlair(ChatPlayer.FlairTimeNone); + TBMCCoreAPI.SendException("Error while checking join date for player " + p.getPlayerName() + "!", e); } return; default: @@ -289,12 +276,8 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. PlayerListener.AlphaDeaths = yc.getInt("alphadeaths"); } PluginMain.Instance.getLogger().info("Loaded files!"); - } catch (IOException e) { - PluginMain.Instance.getLogger().warning("Error!\n" + e); - LastException = e; - } catch (InvalidConfigurationException e) { - PluginMain.Instance.getLogger().warning("Error!\n" + e); - LastException = e; + } catch (Exception e) { + TBMCCoreAPI.SendException("Error while loading chat files!", e); } } @@ -310,9 +293,8 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. yc.set("alphadeaths", PlayerListener.AlphaDeaths); yc.save(file); PluginMain.Instance.getLogger().info("Saved files!"); - } catch (IOException e) { - PluginMain.Instance.getLogger().warning("Error!\n" + e); - LastException = e; + } catch (Exception e) { + TBMCCoreAPI.SendException("Error while loading chat files!", e); } } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/AcceptCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/AcceptCommand.java index 7156db0..4eb8bad 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/AcceptCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/AcceptCommand.java @@ -9,6 +9,7 @@ import buttondevteam.chat.ChatPlayer; import buttondevteam.chat.FlairStates; import buttondevteam.chat.PlayerJoinTimerTask; import buttondevteam.chat.PluginMain; +import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCPlayer; public class AcceptCommand extends UCommandBase { @@ -65,7 +66,8 @@ public class AcceptCommand extends UCommandBase { try { PluginMain.Instance.DownloadFlair(mp); } catch (Exception e) { - e.printStackTrace(); + TBMCCoreAPI.SendException( + "An error occured while downloading flair for " + player.getCustomName() + "!", e); player.sendMessage( "Sorry, but an error occured while trying to get your flair. Please contact a mod."); mp.Working = false; diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/GetLastErrorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/GetLastErrorCommand.java deleted file mode 100644 index 545e1de..0000000 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/GetLastErrorCommand.java +++ /dev/null @@ -1,33 +0,0 @@ -package buttondevteam.chat.commands.ucmds.admin; - -import org.bukkit.command.CommandSender; - -import buttondevteam.chat.PluginMain; - -public class GetLastErrorCommand extends AdminCommandBase { - - @Override - public String[] GetHelpText(String alias) { - return new String[] { "§6---- Get last error ----", - "This command returns the last exception", - "Note that not all exceptions are recorded" }; - } - - @Override - public boolean OnCommand(CommandSender sender, String alias, - String[] args) { - if (PluginMain.LastException != null) { - sender.sendMessage("Last error:"); - sender.sendMessage(PluginMain.LastException.toString()); - PluginMain.LastException = null; - } else - sender.sendMessage("There were no exceptions."); - return true; - } - - @Override - public String GetAdminCommandPath() { - return "getlasterror"; - } - -}