diff --git a/paper-api/src/org/bukkit/plugin/PluginLoader.java b/paper-api/src/org/bukkit/plugin/PluginLoader.java index 6a11406ce2..f7913d20dd 100644 --- a/paper-api/src/org/bukkit/plugin/PluginLoader.java +++ b/paper-api/src/org/bukkit/plugin/PluginLoader.java @@ -2,6 +2,7 @@ package org.bukkit.plugin; import java.io.File; +import java.util.regex.Pattern; /** * Represents a plugin loader, which provides access and management for all plugins @@ -38,11 +39,14 @@ public interface PluginLoader { /** * Loads the plugin contained in the specified file * - * File must be a .jar and contain a valid plugin.yaml file - * * @param file File to attempt to load * @return Plugin that was contained in the specified file, or null if * unsuccessful */ public Plugin loadPlugin(File file); + + /** + * Returns a list of all filename filters expected by this PluginLoader + */ + public Pattern[] getPluginFileFilters(); } diff --git a/paper-api/src/org/bukkit/plugin/PluginManager.java b/paper-api/src/org/bukkit/plugin/PluginManager.java new file mode 100644 index 0000000000..1de78b9a25 --- /dev/null +++ b/paper-api/src/org/bukkit/plugin/PluginManager.java @@ -0,0 +1,82 @@ + +package org.bukkit.plugin; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import org.bukkit.Server; +import java.util.regex.Pattern; + +/** + * Handles all plugin management from the Server + */ +public final class PluginManager { + private final Server server; + private final HashMap fileAssociations = new HashMap(); + + public PluginManager(Server instance) { + server = instance; + } + + /** + * Registers the specified plugin loader + * + * @param loader PluginLoader to register + */ + public void RegisterInterface(PluginLoader loader) { + Pattern[] patterns = loader.getPluginFileFilters(); + + for (Pattern pattern : patterns) { + fileAssociations.put(pattern, loader); + } + } + + /** + * Loads the plugins contained within the specified directory + * + * @param directory Directory to check for plugins + * @return A list of all plugins loaded + */ + public Plugin[] loadPlugins(File directory) { + List result = new ArrayList(); + File[] files = directory.listFiles(); + + for (File file : files) { + Plugin plugin = loadPlugin(file); + + if (plugin != null) { + result.add(plugin); + } + } + + return (Plugin[])result.toArray(); + } + + /** + * Loads the plugin in the specified file + * + * File must be valid according to the current enabled Plugin interfaces + * + * @param file File containing the plugin to load + * @return The Plugin loaded, or null if it was invalid + */ + public Plugin loadPlugin(File file) { + Set filters = fileAssociations.keySet(); + Plugin result = null; + + for (Pattern filter : filters) { + String name = file.getName(); + Matcher match = filter.matcher(name); + + if (match.find()) { + PluginLoader loader = fileAssociations.get(filter); + result = loader.loadPlugin(file); + } + } + + return result; + } +} diff --git a/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java b/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java new file mode 100644 index 0000000000..abc16deb9a --- /dev/null +++ b/paper-api/src/org/bukkit/plugin/java/JavaPluginLoader.java @@ -0,0 +1,37 @@ + +package org.bukkit.plugin.java; + +import java.io.File; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginLoader; +import java.util.regex.Pattern; + +/** + * Represents a Java plugin loader, allowing plugins in the form of .jars + */ +public final class JavaPluginLoader implements PluginLoader { + private final Pattern[] fileFilters = new Pattern[] { + Pattern.compile("\\.jar$"), + }; + + public Plugin getPlugin(String name) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean isPluginEnabled(String name) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean isPluginEnabled(Plugin plugin) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Plugin loadPlugin(File file) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Pattern[] getPluginFileFilters() { + return fileFilters; + } + +}