Log exceptions using the plugin/component logger

And fix LPInjector loading multiple times / once
This commit is contained in:
Norbi Peti 2020-10-09 00:10:36 +02:00
parent 891be91d69
commit ccc15aa048
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
15 changed files with 42 additions and 30 deletions

View file

@ -151,7 +151,10 @@ public final class DPUtils {
Component.setComponentEnabled(component, false); Component.setComponentEnabled(component, false);
path = config.getPath(); path = config.getPath();
} catch (Exception e) { } 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("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."); getLogger().warning("Set the correct ID in the config" + (component == null ? "" : " or disable this component") + " to remove this message.");

View file

@ -151,7 +151,7 @@ public class DiscordPlugin extends ButtonPlugin {
.collectList()).subscribe(this::handleReady); // Take all received GuildCreateEvents and make it a List .collectList()).subscribe(this::handleReady); // Take all received GuildCreateEvents and make it a List
}); /* All guilds have been received, client is fully connected */ }); /* All guilds have been received, client is fully connected */
} catch (Exception e) { } 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"); 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( val thr = new Throwable(
"The server shut down unexpectedly. See the log of the previous run for more details."); "The server shut down unexpectedly. See the log of the previous run for more details.");
thr.setStackTrace(new StackTraceElement[0]); thr.setStackTrace(new StackTraceElement[0]);
TBMCCoreAPI.SendException("The server crashed!", thr); TBMCCoreAPI.SendException("The server crashed!", thr, this);
} else } else
ChromaBot.getInstance().sendMessageCustomAsWell(chan -> chan.flatMap(ch -> ch.createEmbed(ecs -> ecs.setColor(Color.GREEN) ChromaBot.getInstance().sendMessageCustomAsWell(chan -> chan.flatMap(ch -> ch.createEmbed(ecs -> ecs.setColor(Color.GREEN)
.setTitle("Server started - chat connected."))), ChannelconBroadcast.RESTART); .setTitle("Server started - chat connected."))), ChannelconBroadcast.RESTART);
@ -230,7 +230,7 @@ public class DiscordPlugin extends ButtonPlugin {
} }
getLogger().info("Loaded!"); getLogger().info("Loaded!");
} catch (Exception e) { } 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 mainServer = null; //Allow ReadyEvent again
//Configs are emptied so channels and servers are fetched again //Configs are emptied so channels and servers are fetched again
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while disabling DiscordPlugin!", e); TBMCCoreAPI.SendException("An error occured while disabling DiscordPlugin!", e, this);
} }
} }

View file

@ -64,7 +64,7 @@ public abstract class DiscordSenderBase implements CommandSender {
}, 4); // Waits a 0.2 second to gather all/most of the different messages }, 4); // Waits a 0.2 second to gather all/most of the different messages
} }
} catch (Exception e) { } 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);
} }
} }

View file

@ -19,7 +19,7 @@ public class GeneralEventBroadcasterModule extends Component<DiscordPlugin> {
log("Finished hooking into the player list"); log("Finished hooking into the player list");
hooked = true; hooked = true;
} catch (Exception e) { } 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) { } catch (NoClassDefFoundError e) {
logWarn("Error while hacking the player list! Disable this module if you're on an incompatible version."); 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<DiscordPlugin> {
log("Didn't have the player list hooked."); log("Didn't have the player list hooked.");
hooked = false; hooked = false;
} catch (Exception e) { } 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) { } catch (NoClassDefFoundError ignored) {
} }
} }

View file

@ -101,7 +101,7 @@ public class PlayerListWatcher {
val params = method.getParameterTypes(); val params = method.getParameterTypes();
if (params.length == 0) { if (params.length == 0) {
TBMCCoreAPI.SendException("Found a strange method", 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; return null;
} }
if (params[0].getSimpleName().equals("IChatBaseComponent[]")) if (params[0].getSimpleName().equals("IChatBaseComponent[]"))
@ -114,7 +114,7 @@ public class PlayerListWatcher {
sendMessage(args[0], true); sendMessage(args[0], true);
else else
TBMCCoreAPI.SendException("Found a method with interesting params", 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; return null;
} }
@ -134,7 +134,7 @@ public class PlayerListWatcher {
this.sendAll(packet); this.sendAll(packet);
// CraftBukkit end // CraftBukkit end
} catch (Exception e) { } 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)))); MCChatUtils.forAllMCChat(MCChatUtils.send((String) toPlainText.invoke(msgf.get(packet))));
} }
} catch (Exception e) { } 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()); }).stubOnly());

View file

@ -1,6 +1,7 @@
package buttondevteam.discordplugin.commands; package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
@ -48,7 +49,7 @@ public class ConnectCommand extends ICommand2DC {
return true; return true;
} }
} catch (Exception e) { } 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(); channel.createMessage("An internal error occured!\n" + e).subscribe();
} }
WaitingToConnect.put(p.getName(), author.getId().asString()); WaitingToConnect.put(p.getName(), author.getId().asString());

View file

@ -73,7 +73,7 @@ public class UserinfoCommand extends ICommand2DC {
channel.createMessage(uinfo.toString()).subscribe(); channel.createMessage(uinfo.toString()).subscribe();
} catch (Exception e) { } catch (Exception e) {
channel.createMessage("An error occured while getting the user!").subscribe(); 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; return true;
} }

View file

@ -65,7 +65,7 @@ public class CommandListener {
return DPUtils.reply(message, channel, "unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString) return DPUtils.reply(message, channel, "unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString)
.map(m -> false); .map(m -> false);
} catch (Exception e) { } 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 return Mono.just(false); //If the command succeeded or there was an error, return false
}).defaultIfEmpty(true); }).defaultIfEmpty(true);

View file

@ -63,7 +63,7 @@ public class CommonListeners {
timings.printElapsed("Run command 2"); timings.printElapsed("Run command 2");
return CommandListener.runCommand(event.getMessage(), ch, false); 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(); .subscribe();
dispatcher.on(PresenceUpdateEvent.class).subscribe(event -> { dispatcher.on(PresenceUpdateEvent.class).subscribe(event -> {
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)

View file

@ -43,7 +43,7 @@ public class MCChatCommand extends ICommand2DC {
? "enabled. Use '" + DiscordPlugin.getPrefix() + "mcchat' again to turn it off." // ? "enabled. Use '" + DiscordPlugin.getPrefix() + "mcchat' again to turn it off." //
: "disabled.")).subscribe(); : "disabled.")).subscribe();
} catch (Exception e) { } 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; return true;
} // TODO: Pin channel switching to indicate the current channel } // TODO: Pin channel switching to indicate the current channel

View file

@ -151,7 +151,7 @@ public class MCChatListener implements Listener {
sendtask.cancel(); sendtask.cancel();
sendtask = null; sendtask = null;
} catch (Exception ex) { } 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 lmfd.removeSelfReaction(DiscordPlugin.DELIVERED_REACTION).subscribe(); // Remove it no matter what, we know it's there 99.99% of the time
} }
} catch (Exception e) { } 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()); MCChatUtils.lastmsgfromd.put(event.getMessage().getChannelId().asLong(), event.getMessage());
event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION).subscribe(); event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION).subscribe();
} }
} catch (Exception e) { } 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 else
Bukkit.dispatchCommand(dsender, cmd); Bukkit.dispatchCommand(dsender, cmd);
} catch (NoClassDefFoundError e) { } 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) { } 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; return true;

View file

@ -79,7 +79,7 @@ public class MCChatUtils {
private static void updatePL(LastMsgData lmd) { private static void updatePL(LastMsgData lmd) {
if (!(lmd.channel instanceof TextChannel)) { if (!(lmd.channel instanceof TextChannel)) {
TBMCCoreAPI.SendException("Failed to update player list for channel " + lmd.channel.getId(), 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; return;
} }
String topic = ((TextChannel) lmd.channel).getTopic().orElse(""); String topic = ((TextChannel) lmd.channel).getTopic().orElse("");

View file

@ -18,6 +18,7 @@ import discord4j.core.object.entity.channel.MessageChannel;
import lombok.Getter; import lombok.Getter;
import lombok.val; import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,6 +32,7 @@ import java.util.stream.Collectors;
public class MinecraftChatModule extends Component<DiscordPlugin> { public class MinecraftChatModule extends Component<DiscordPlugin> {
private @Getter MCChatListener listener; private @Getter MCChatListener listener;
private ServerWatcher serverWatcher; 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! * 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<DiscordPlugin> {
} }
try { try {
new LPInjector(MainPlugin.Instance); if (lpInjector == null)
lpInjector = new LPInjector(MainPlugin.Instance);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e); TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e, this);
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
log("No LuckPerms, not injecting"); log("No LuckPerms, not injecting");
//e.printStackTrace(); //e.printStackTrace();
@ -168,7 +171,7 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
serverWatcher = new ServerWatcher(); serverWatcher = new ServerWatcher();
serverWatcher.enableDisable(true); serverWatcher.enableDisable(true);
} catch (Exception e) { } 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<DiscordPlugin> {
if (serverWatcher != null) if (serverWatcher != null)
serverWatcher.enableDisable(false); serverWatcher.enableDisable(false);
} catch (Exception e) { } 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 chcons = MCChatCustom.getCustomChats();
val chconsc = getConfig().getConfig().createSection("chcons"); val chconsc = getConfig().getConfig().createSection("chcons");
@ -196,4 +199,9 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
} }
MCChatListener.stop(true); MCChatListener.stop(true);
} }
@Override
protected void unregister(JavaPlugin plugin) {
lpInjector = null; //Plugin restart, events need to be registered again
}
} }

View file

@ -50,7 +50,7 @@ public class VCMDWrapper {
return ret; return ret;
} catch (NoClassDefFoundError | Exception e) { } catch (NoClassDefFoundError | Exception e) {
compatWarning(module); compatWarning(module);
TBMCCoreAPI.SendException("Failed to create vanilla command listener", e); TBMCCoreAPI.SendException("Failed to create vanilla command listener", e, module);
return null; return null;
} }
} }

View file

@ -34,7 +34,7 @@ public class RoleCommand extends ICommand2DC {
.flatMap(m -> m.addRole(role.getId()).switchIfEmpty(Mono.fromRunnable(() -> sender.sendMessage("added role.")))) .flatMap(m -> m.addRole(role.getId()).switchIfEmpty(Mono.fromRunnable(() -> sender.sendMessage("added role."))))
.subscribe(); .subscribe();
} catch (Exception e) { } 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."); sender.sendMessage("an error occured while adding the role.");
} }
return true; return true;
@ -53,7 +53,7 @@ public class RoleCommand extends ICommand2DC {
.flatMap(m -> m.removeRole(role.getId()).switchIfEmpty(Mono.fromRunnable(() -> sender.sendMessage("removed role.")))) .flatMap(m -> m.removeRole(role.getId()).switchIfEmpty(Mono.fromRunnable(() -> sender.sendMessage("removed role."))))
.subscribe(); .subscribe();
} catch (Exception e) { } 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."); sender.sendMessage("an error occured while removing the role.");
} }
return true; return true;