Skip to content

Commit

Permalink
fix: Do not try loading directories and give plugins their own logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
DxsSucuk committed Jan 24, 2025
1 parent 3a35d0c commit a893633
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/main/java/de/presti/ree6/addons/ReePluginContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import lombok.Getter;
import org.pf4j.RuntimeMode;
import org.slf4j.Logger;

@Getter
public class ReePluginContext {

private final RuntimeMode runtimeMode;
private final Logger logger;

public ReePluginContext(RuntimeMode runtimeMode) {
public ReePluginContext(RuntimeMode runtimeMode, Logger logger) {
this.runtimeMode = runtimeMode;
this.logger = logger;
}

}
3 changes: 2 additions & 1 deletion src/main/java/de/presti/ree6/addons/ReePluginFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.pf4j.DefaultPluginFactory;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;

Expand All @@ -12,7 +13,7 @@ public class ReePluginFactory extends DefaultPluginFactory {

@Override
protected Plugin createInstance(Class<?> pluginClass, PluginWrapper pluginWrapper) {
ReePluginContext context = new ReePluginContext(pluginWrapper.getRuntimeMode());
ReePluginContext context = new ReePluginContext(pluginWrapper.getRuntimeMode(), LoggerFactory.getLogger(pluginClass));
try {
Constructor<?> constructor = pluginClass.getConstructor(ReePluginContext.class);
return (Plugin) constructor.newInstance(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public YamlPluginDescriptorFinder(String yamlFileName) {

@Override
public boolean isApplicable(Path pluginPath) {
return Files.exists(pluginPath) && (Files.isDirectory(pluginPath) || FileUtils.isZipOrJarFile(pluginPath));
return Files.exists(pluginPath) && FileUtils.isZipOrJarFile(pluginPath);
}

@Override
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/de/presti/ree6/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.utils.messages.MessageEditBuilder;
import org.pf4j.PluginManager;
import org.pf4j.PluginState;
import org.pf4j.PluginWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -289,7 +291,7 @@ public static void main(String[] args) {
BotWorker.createBot(version, shards);
getInstance().addEvents();
} catch (Exception ex) {
log.error("[Main] Error while init: " + ex.getMessage());
log.error("[Main] Error while init: {}", ex.getMessage());
Sentry.captureException(ex);
System.exit(0);
return;
Expand Down Expand Up @@ -380,15 +382,18 @@ public static void main(String[] args) {
getInstance().getPluginManager().startPlugins();

log.info("Registering all Commands of plugins.");
List<ICommand> commands = getInstance().getPluginManager().getExtensions(ICommand.class);
log.info("Found {} commands in all plugins.", commands.size());
commands.forEach(command -> {
try {
getInstance().getCommandManager().addCommand(command);
} catch (CommandInitializerException e) {
log.warn("Failed to initialize command: {}", command.getClass().getSimpleName(), e);
}
});
for (PluginWrapper plugin : getInstance().getPluginManager().getPlugins()) {
if (plugin.getPluginState() != PluginState.STARTED) continue;
List<ICommand> commands = getInstance().getPluginManager().getExtensions(ICommand.class, plugin.getPluginId());
log.info("Found {} commands in {}.", commands.size(), plugin.getPluginId());
commands.forEach(command -> {
try {
getInstance().getCommandManager().addCommand(command);
} catch (CommandInitializerException e) {
log.warn("Failed to initialize command: {}", command.getClass().getSimpleName(), e);
}
});
}
}

// Create checker Thread.
Expand Down

0 comments on commit a893633

Please sign in to comment.