From afe543345e977948444c912c0e2fa3a3f269a1a7 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sat, 13 Apr 2024 17:01:48 +1000 Subject: [PATCH] #991: Add PluginManager#loadPlugins(File[]) By: MikChan --- .../java/org/bukkit/plugin/PluginManager.java | 9 +++ .../bukkit/plugin/SimplePluginManager.java | 57 +++++++++++-------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java b/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java index 41e26451fe..ae3e68562c 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java +++ b/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java @@ -89,6 +89,15 @@ public interface PluginManager { @NotNull public Plugin[] loadPlugins(@NotNull File directory); + /** + * Loads the plugins in the list of the files + * + * @param files List of files containing plugins to load + * @return A list of all plugins loaded + */ + @NotNull + public Plugin[] loadPlugins(@NotNull File[] files); + /** * Disables all the loaded plugins */ diff --git a/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java index 1fe7249d3d..6fac84b3a6 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java +++ b/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java @@ -114,13 +114,26 @@ public final class SimplePluginManager implements PluginManager { Preconditions.checkArgument(directory != null, "Directory cannot be null"); Preconditions.checkArgument(directory.isDirectory(), "Directory must be a directory"); - List result = new ArrayList(); - Set filters = fileAssociations.keySet(); - if (!(server.getUpdateFolder().equals(""))) { updateDirectory = new File(directory, server.getUpdateFolder()); } + return loadPlugins(directory.listFiles()); + } + + /** + * Loads the plugins in the list of the files + * + * @param files List of files containing plugins to load + * @return A list of all plugins loaded + */ + @NotNull + public Plugin[] loadPlugins(@NotNull File[] files) { + Preconditions.checkArgument(files != null, "File list cannot be null"); + + List result = new ArrayList(); + Set filters = fileAssociations.keySet(); + Map plugins = new HashMap(); Set loadedPlugins = new HashSet(); Map pluginsProvided = new HashMap<>(); @@ -128,7 +141,7 @@ public final class SimplePluginManager implements PluginManager { Map> softDependencies = new HashMap>(); // This is where it figures out all possible plugins - for (File file : directory.listFiles()) { + for (File file : files) { PluginLoader loader = null; for (Pattern filter : filters) { Matcher match = filter.matcher(file.getName()); @@ -144,25 +157,24 @@ public final class SimplePluginManager implements PluginManager { description = loader.getPluginDescription(file); String name = description.getName(); if (name.equalsIgnoreCase("bukkit") || name.equalsIgnoreCase("minecraft") || name.equalsIgnoreCase("mojang")) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': Restricted Name"); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "': Restricted Name"); continue; } else if (description.rawName.indexOf(' ') != -1) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': uses the space-character (0x20) in its name"); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "': uses the space-character (0x20) in its name"); continue; } } catch (InvalidDescriptionException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "'", ex); continue; } File replacedFile = plugins.put(description.getName(), file); if (replacedFile != null) { server.getLogger().severe(String.format( - "Ambiguous plugin name `%s' for files `%s' and `%s' in `%s'", - description.getName(), - file.getPath(), - replacedFile.getPath(), - directory.getPath() + "Ambiguous plugin name `%s' for files `%s' and `%s'", + description.getName(), + file.getPath(), + replacedFile.getPath() )); } @@ -179,11 +191,10 @@ public final class SimplePluginManager implements PluginManager { File pluginFile = plugins.get(provided); if (pluginFile != null) { server.getLogger().warning(String.format( - "`%s provides `%s' while this is also the name of `%s' in `%s'", + "`%s provides `%s' while this is also the name of `%s'", file.getPath(), provided, - pluginFile.getPath(), - directory.getPath() + pluginFile.getPath() )); } else { String replacedPlugin = pluginsProvided.put(provided, description.getName()); @@ -264,9 +275,9 @@ public final class SimplePluginManager implements PluginManager { dependencies.remove(plugin); server.getLogger().log( - Level.SEVERE, - "Could not load '" + entry.getValue().getPath() + "' in folder '" + directory.getPath() + "'", - new UnknownDependencyException("Unknown dependency " + dependency + ". Please download and install " + dependency + " to run this plugin.")); + Level.SEVERE, + "Could not load '" + entry.getValue().getPath() + "'", + new UnknownDependencyException("Unknown dependency " + dependency + ". Please download and install " + dependency + " to run this plugin.")); break; } } @@ -304,11 +315,11 @@ public final class SimplePluginManager implements PluginManager { loadedPlugins.add(loadedPlugin.getName()); loadedPlugins.addAll(loadedPlugin.getDescription().getProvides()); } else { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'"); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "'"); } continue; } catch (InvalidPluginException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "'", ex); } } } @@ -335,11 +346,11 @@ public final class SimplePluginManager implements PluginManager { loadedPlugins.add(loadedPlugin.getName()); loadedPlugins.addAll(loadedPlugin.getDescription().getProvides()); } else { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'"); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "'"); } break; } catch (InvalidPluginException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "'", ex); } } } @@ -352,7 +363,7 @@ public final class SimplePluginManager implements PluginManager { while (failedPluginIterator.hasNext()) { File file = failedPluginIterator.next(); failedPluginIterator.remove(); - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': circular dependency detected"); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "': circular dependency detected"); } } }