From ccc15aa048da06ddaf85e6d427dd5f840d63d9fe Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 9 Oct 2020 00:10:36 +0200 Subject: [PATCH] Log exceptions using the plugin/component logger And fix LPInjector loading multiple times / once --- .../buttondevteam/discordplugin/DPUtils.java | 5 ++++- .../discordplugin/DiscordPlugin.java | 8 ++++---- .../discordplugin/DiscordSenderBase.java | 2 +- .../GeneralEventBroadcasterModule.java | 4 ++-- .../broadcaster/PlayerListWatcher.java | 8 ++++---- .../discordplugin/commands/ConnectCommand.java | 3 ++- .../discordplugin/commands/UserinfoCommand.java | 2 +- .../discordplugin/listeners/CommandListener.java | 2 +- .../discordplugin/listeners/CommonListeners.java | 2 +- .../discordplugin/mcchat/MCChatCommand.java | 2 +- .../discordplugin/mcchat/MCChatListener.java | 10 +++++----- .../discordplugin/mcchat/MCChatUtils.java | 2 +- .../mcchat/MinecraftChatModule.java | 16 ++++++++++++---- .../discordplugin/playerfaker/VCMDWrapper.java | 2 +- .../discordplugin/role/RoleCommand.java | 4 ++-- 15 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/main/java/buttondevteam/discordplugin/DPUtils.java b/src/main/java/buttondevteam/discordplugin/DPUtils.java index 7c4181a..4739924 100755 --- a/src/main/java/buttondevteam/discordplugin/DPUtils.java +++ b/src/main/java/buttondevteam/discordplugin/DPUtils.java @@ -151,7 +151,10 @@ public final class DPUtils { Component.setComponentEnabled(component, false); path = config.getPath(); } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to disable component after config error!", e); + if (component != null) + TBMCCoreAPI.SendException("Failed to disable component after config error!", e, component); + else + TBMCCoreAPI.SendException("Failed to disable component after config error!", e, DiscordPlugin.plugin); } getLogger().warning("The config value " + path + " isn't set correctly " + (component == null ? "in global settings!" : "for component " + component.getClass().getSimpleName() + "!")); getLogger().warning("Set the correct ID in the config" + (component == null ? "" : " or disable this component") + " to remove this message."); diff --git a/src/main/java/buttondevteam/discordplugin/DiscordPlugin.java b/src/main/java/buttondevteam/discordplugin/DiscordPlugin.java index f6c6382..22d6102 100755 --- a/src/main/java/buttondevteam/discordplugin/DiscordPlugin.java +++ b/src/main/java/buttondevteam/discordplugin/DiscordPlugin.java @@ -151,7 +151,7 @@ public class DiscordPlugin extends ButtonPlugin { .collectList()).subscribe(this::handleReady); // Take all received GuildCreateEvents and make it a List }); /* All guilds have been received, client is fully connected */ } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to enable the Discord plugin!", e); + TBMCCoreAPI.SendException("Failed to enable the Discord plugin!", e, this); getLogger().severe("You may be able to reset the plugin using /discord reset"); } } @@ -204,7 +204,7 @@ public class DiscordPlugin extends ButtonPlugin { val thr = new Throwable( "The server shut down unexpectedly. See the log of the previous run for more details."); thr.setStackTrace(new StackTraceElement[0]); - TBMCCoreAPI.SendException("The server crashed!", thr); + TBMCCoreAPI.SendException("The server crashed!", thr, this); } else ChromaBot.getInstance().sendMessageCustomAsWell(chan -> chan.flatMap(ch -> ch.createEmbed(ecs -> ecs.setColor(Color.GREEN) .setTitle("Server started - chat connected."))), ChannelconBroadcast.RESTART); @@ -230,7 +230,7 @@ public class DiscordPlugin extends ButtonPlugin { } getLogger().info("Loaded!"); } catch (Exception e) { - TBMCCoreAPI.SendException("An error occurred while enabling DiscordPlugin!", e); + TBMCCoreAPI.SendException("An error occurred while enabling DiscordPlugin!", e, this); } } @@ -284,7 +284,7 @@ public class DiscordPlugin extends ButtonPlugin { mainServer = null; //Allow ReadyEvent again //Configs are emptied so channels and servers are fetched again } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while disabling DiscordPlugin!", e); + TBMCCoreAPI.SendException("An error occured while disabling DiscordPlugin!", e, this); } } diff --git a/src/main/java/buttondevteam/discordplugin/DiscordSenderBase.java b/src/main/java/buttondevteam/discordplugin/DiscordSenderBase.java index 13bdd76..7ada97f 100755 --- a/src/main/java/buttondevteam/discordplugin/DiscordSenderBase.java +++ b/src/main/java/buttondevteam/discordplugin/DiscordSenderBase.java @@ -64,7 +64,7 @@ public abstract class DiscordSenderBase implements CommandSender { }, 4); // Waits a 0.2 second to gather all/most of the different messages } } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while sending message to DiscordSender", e); + TBMCCoreAPI.SendException("An error occured while sending message to DiscordSender", e, DiscordPlugin.plugin); } } diff --git a/src/main/java/buttondevteam/discordplugin/broadcaster/GeneralEventBroadcasterModule.java b/src/main/java/buttondevteam/discordplugin/broadcaster/GeneralEventBroadcasterModule.java index 8dd9b52..6ddf741 100644 --- a/src/main/java/buttondevteam/discordplugin/broadcaster/GeneralEventBroadcasterModule.java +++ b/src/main/java/buttondevteam/discordplugin/broadcaster/GeneralEventBroadcasterModule.java @@ -19,7 +19,7 @@ public class GeneralEventBroadcasterModule extends Component { log("Finished hooking into the player list"); hooked = true; } catch (Exception e) { - TBMCCoreAPI.SendException("Error while hacking the player list! Disable this module if you're on an incompatible version.", e); + TBMCCoreAPI.SendException("Error while hacking the player list! Disable this module if you're on an incompatible version.", e, this); } catch (NoClassDefFoundError e) { logWarn("Error while hacking the player list! Disable this module if you're on an incompatible version."); } @@ -36,7 +36,7 @@ public class GeneralEventBroadcasterModule extends Component { log("Didn't have the player list hooked."); hooked = false; } catch (Exception e) { - TBMCCoreAPI.SendException("Error while hacking the player list!", e); + TBMCCoreAPI.SendException("Error while hacking the player list!", e, this); } catch (NoClassDefFoundError ignored) { } } diff --git a/src/main/java/buttondevteam/discordplugin/broadcaster/PlayerListWatcher.java b/src/main/java/buttondevteam/discordplugin/broadcaster/PlayerListWatcher.java index 8e3cba8..ba218c4 100755 --- a/src/main/java/buttondevteam/discordplugin/broadcaster/PlayerListWatcher.java +++ b/src/main/java/buttondevteam/discordplugin/broadcaster/PlayerListWatcher.java @@ -101,7 +101,7 @@ public class PlayerListWatcher { val params = method.getParameterTypes(); if (params.length == 0) { TBMCCoreAPI.SendException("Found a strange method", - new Exception("Found a sendMessage() method without arguments.")); + new Exception("Found a sendMessage() method without arguments."), module); return null; } if (params[0].getSimpleName().equals("IChatBaseComponent[]")) @@ -114,7 +114,7 @@ public class PlayerListWatcher { sendMessage(args[0], true); else TBMCCoreAPI.SendException("Found a method with interesting params", - new Exception("Found a sendMessage(" + params[0].getSimpleName() + ") method")); + new Exception("Found a sendMessage(" + params[0].getSimpleName() + ") method"), module); return null; } @@ -134,7 +134,7 @@ public class PlayerListWatcher { this.sendAll(packet); // CraftBukkit end } catch (Exception e) { - TBMCCoreAPI.SendException("An error occurred while passing a vanilla message through the player list", e); + TBMCCoreAPI.SendException("An error occurred while passing a vanilla message through the player list", e, module); } } @@ -147,7 +147,7 @@ public class PlayerListWatcher { MCChatUtils.forAllMCChat(MCChatUtils.send((String) toPlainText.invoke(msgf.get(packet)))); } } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to broadcast message sent to all players - hacking failed.", e); + TBMCCoreAPI.SendException("Failed to broadcast message sent to all players - hacking failed.", e, module); } } }).stubOnly()); diff --git a/src/main/java/buttondevteam/discordplugin/commands/ConnectCommand.java b/src/main/java/buttondevteam/discordplugin/commands/ConnectCommand.java index f00c88a..cb537f4 100755 --- a/src/main/java/buttondevteam/discordplugin/commands/ConnectCommand.java +++ b/src/main/java/buttondevteam/discordplugin/commands/ConnectCommand.java @@ -1,6 +1,7 @@ package buttondevteam.discordplugin.commands; import buttondevteam.discordplugin.DiscordPlayer; +import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.CommandClass; @@ -48,7 +49,7 @@ public class ConnectCommand extends ICommand2DC { return true; } } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while connecting a Discord account!", e); + TBMCCoreAPI.SendException("An error occured while connecting a Discord account!", e, DiscordPlugin.plugin); channel.createMessage("An internal error occured!\n" + e).subscribe(); } WaitingToConnect.put(p.getName(), author.getId().asString()); diff --git a/src/main/java/buttondevteam/discordplugin/commands/UserinfoCommand.java b/src/main/java/buttondevteam/discordplugin/commands/UserinfoCommand.java index 7a68b0c..16a9baa 100755 --- a/src/main/java/buttondevteam/discordplugin/commands/UserinfoCommand.java +++ b/src/main/java/buttondevteam/discordplugin/commands/UserinfoCommand.java @@ -73,7 +73,7 @@ public class UserinfoCommand extends ICommand2DC { channel.createMessage(uinfo.toString()).subscribe(); } catch (Exception e) { channel.createMessage("An error occured while getting the user!").subscribe(); - TBMCCoreAPI.SendException("Error while getting info about " + target.getUsername() + "!", e); + TBMCCoreAPI.SendException("Error while getting info about " + target.getUsername() + "!", e, DiscordPlugin.plugin); } return true; } diff --git a/src/main/java/buttondevteam/discordplugin/listeners/CommandListener.java b/src/main/java/buttondevteam/discordplugin/listeners/CommandListener.java index 30dc153..776a711 100644 --- a/src/main/java/buttondevteam/discordplugin/listeners/CommandListener.java +++ b/src/main/java/buttondevteam/discordplugin/listeners/CommandListener.java @@ -65,7 +65,7 @@ public class CommandListener { return DPUtils.reply(message, channel, "unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString) .map(m -> false); } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e); + TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e, DiscordPlugin.plugin); } return Mono.just(false); //If the command succeeded or there was an error, return false }).defaultIfEmpty(true); diff --git a/src/main/java/buttondevteam/discordplugin/listeners/CommonListeners.java b/src/main/java/buttondevteam/discordplugin/listeners/CommonListeners.java index fc94aff..edcf349 100755 --- a/src/main/java/buttondevteam/discordplugin/listeners/CommonListeners.java +++ b/src/main/java/buttondevteam/discordplugin/listeners/CommonListeners.java @@ -63,7 +63,7 @@ public class CommonListeners { timings.printElapsed("Run command 2"); return CommandListener.runCommand(event.getMessage(), ch, false); }); - }).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err)) + }).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err, DiscordPlugin.plugin)) .subscribe(); dispatcher.on(PresenceUpdateEvent.class).subscribe(event -> { if (DiscordPlugin.SafeMode) diff --git a/src/main/java/buttondevteam/discordplugin/mcchat/MCChatCommand.java b/src/main/java/buttondevteam/discordplugin/mcchat/MCChatCommand.java index 476be5f..b090bb3 100755 --- a/src/main/java/buttondevteam/discordplugin/mcchat/MCChatCommand.java +++ b/src/main/java/buttondevteam/discordplugin/mcchat/MCChatCommand.java @@ -43,7 +43,7 @@ public class MCChatCommand extends ICommand2DC { ? "enabled. Use '" + DiscordPlugin.getPrefix() + "mcchat' again to turn it off." // : "disabled.")).subscribe(); } catch (Exception e) { - TBMCCoreAPI.SendException("Error while setting mcchat for user " + author.getUsername() + "#" + author.getDiscriminator(), e); + TBMCCoreAPI.SendException("Error while setting mcchat for user " + author.getUsername() + "#" + author.getDiscriminator(), e, module); } return true; } // TODO: Pin channel switching to indicate the current channel diff --git a/src/main/java/buttondevteam/discordplugin/mcchat/MCChatListener.java b/src/main/java/buttondevteam/discordplugin/mcchat/MCChatListener.java index a75cb7e..ade2fc7 100755 --- a/src/main/java/buttondevteam/discordplugin/mcchat/MCChatListener.java +++ b/src/main/java/buttondevteam/discordplugin/mcchat/MCChatListener.java @@ -151,7 +151,7 @@ public class MCChatListener implements Listener { sendtask.cancel(); sendtask = null; } catch (Exception ex) { - TBMCCoreAPI.SendException("Error while sending message to Discord!", ex); + TBMCCoreAPI.SendException("Error while sending message to Discord!", ex, module); } } @@ -320,13 +320,13 @@ public class MCChatListener implements Listener { lmfd.removeSelfReaction(DiscordPlugin.DELIVERED_REACTION).subscribe(); // Remove it no matter what, we know it's there 99.99% of the time } } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e); + TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e, module); } MCChatUtils.lastmsgfromd.put(event.getMessage().getChannelId().asLong(), event.getMessage()); event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION).subscribe(); } } catch (Exception e) { - TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e); + TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e, module); } } @@ -391,9 +391,9 @@ public class MCChatListener implements Listener { else Bukkit.dispatchCommand(dsender, cmd); } catch (NoClassDefFoundError e) { - TBMCCoreAPI.SendException("A class is not found when trying to run command " + cmd + "!", e); + TBMCCoreAPI.SendException("A class is not found when trying to run command " + cmd + "!", e, module); } catch (Exception e) { - TBMCCoreAPI.SendException("An error occurred when trying to run command " + cmd + "! Vanilla commands are only supported in some MC versions.", e); + TBMCCoreAPI.SendException("An error occurred when trying to run command " + cmd + "! Vanilla commands are only supported in some MC versions.", e, module); } }); return true; diff --git a/src/main/java/buttondevteam/discordplugin/mcchat/MCChatUtils.java b/src/main/java/buttondevteam/discordplugin/mcchat/MCChatUtils.java index 467fa30..c6c026c 100644 --- a/src/main/java/buttondevteam/discordplugin/mcchat/MCChatUtils.java +++ b/src/main/java/buttondevteam/discordplugin/mcchat/MCChatUtils.java @@ -79,7 +79,7 @@ public class MCChatUtils { private static void updatePL(LastMsgData lmd) { if (!(lmd.channel instanceof TextChannel)) { TBMCCoreAPI.SendException("Failed to update player list for channel " + lmd.channel.getId(), - new Exception("The channel isn't a (guild) text channel.")); + new Exception("The channel isn't a (guild) text channel."), getModule()); return; } String topic = ((TextChannel) lmd.channel).getTopic().orElse(""); diff --git a/src/main/java/buttondevteam/discordplugin/mcchat/MinecraftChatModule.java b/src/main/java/buttondevteam/discordplugin/mcchat/MinecraftChatModule.java index c49de62..b51d4b3 100644 --- a/src/main/java/buttondevteam/discordplugin/mcchat/MinecraftChatModule.java +++ b/src/main/java/buttondevteam/discordplugin/mcchat/MinecraftChatModule.java @@ -18,6 +18,7 @@ import discord4j.core.object.entity.channel.MessageChannel; import lombok.Getter; import lombok.val; import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; import reactor.core.publisher.Mono; import java.util.ArrayList; @@ -31,6 +32,7 @@ import java.util.stream.Collectors; public class MinecraftChatModule extends Component { private @Getter MCChatListener listener; private ServerWatcher serverWatcher; + private LPInjector lpInjector; /** * A list of commands that can be used in public chats - Warning: Some plugins will treat players as OPs, always test before allowing a command! @@ -155,9 +157,10 @@ public class MinecraftChatModule extends Component { } try { - new LPInjector(MainPlugin.Instance); + if (lpInjector == null) + lpInjector = new LPInjector(MainPlugin.Instance); } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e); + TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e, this); } catch (NoClassDefFoundError e) { log("No LuckPerms, not injecting"); //e.printStackTrace(); @@ -168,7 +171,7 @@ public class MinecraftChatModule extends Component { serverWatcher = new ServerWatcher(); serverWatcher.enableDisable(true); } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to hack the server (object)!", e); + TBMCCoreAPI.SendException("Failed to hack the server (object)!", e, this); } } } @@ -179,7 +182,7 @@ public class MinecraftChatModule extends Component { if (serverWatcher != null) serverWatcher.enableDisable(false); } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to restore the server object!", e); + TBMCCoreAPI.SendException("Failed to restore the server object!", e, this); } val chcons = MCChatCustom.getCustomChats(); val chconsc = getConfig().getConfig().createSection("chcons"); @@ -196,4 +199,9 @@ public class MinecraftChatModule extends Component { } MCChatListener.stop(true); } + + @Override + protected void unregister(JavaPlugin plugin) { + lpInjector = null; //Plugin restart, events need to be registered again + } } diff --git a/src/main/java/buttondevteam/discordplugin/playerfaker/VCMDWrapper.java b/src/main/java/buttondevteam/discordplugin/playerfaker/VCMDWrapper.java index 16dfcf6..6ff856b 100644 --- a/src/main/java/buttondevteam/discordplugin/playerfaker/VCMDWrapper.java +++ b/src/main/java/buttondevteam/discordplugin/playerfaker/VCMDWrapper.java @@ -50,7 +50,7 @@ public class VCMDWrapper { return ret; } catch (NoClassDefFoundError | Exception e) { compatWarning(module); - TBMCCoreAPI.SendException("Failed to create vanilla command listener", e); + TBMCCoreAPI.SendException("Failed to create vanilla command listener", e, module); return null; } } diff --git a/src/main/java/buttondevteam/discordplugin/role/RoleCommand.java b/src/main/java/buttondevteam/discordplugin/role/RoleCommand.java index d818950..07fd0e2 100755 --- a/src/main/java/buttondevteam/discordplugin/role/RoleCommand.java +++ b/src/main/java/buttondevteam/discordplugin/role/RoleCommand.java @@ -34,7 +34,7 @@ public class RoleCommand extends ICommand2DC { .flatMap(m -> m.addRole(role.getId()).switchIfEmpty(Mono.fromRunnable(() -> sender.sendMessage("added role.")))) .subscribe(); } catch (Exception e) { - TBMCCoreAPI.SendException("Error while adding role!", e); + TBMCCoreAPI.SendException("Error while adding role!", e, grm); sender.sendMessage("an error occured while adding the role."); } return true; @@ -53,7 +53,7 @@ public class RoleCommand extends ICommand2DC { .flatMap(m -> m.removeRole(role.getId()).switchIfEmpty(Mono.fromRunnable(() -> sender.sendMessage("removed role.")))) .subscribe(); } catch (Exception e) { - TBMCCoreAPI.SendException("Error while removing role!", e); + TBMCCoreAPI.SendException("Error while removing role!", e, grm); sender.sendMessage("an error occured while removing the role."); } return true;