From a0382bd64cce5a5cb0c447a80e8dd356ce897a12 Mon Sep 17 00:00:00 2001 From: Joshua Castle <26531652+Kas-tle@users.noreply.github.com> Date: Mon, 14 Jul 2025 09:22:11 -0700 Subject: [PATCH] Prefer use of limbo.yml ofr plugin loading Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com> --- .../loohp/limbo/plugins/PluginManager.java | 103 +++++++++++------- 1 file changed, 61 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/loohp/limbo/plugins/PluginManager.java b/src/main/java/com/loohp/limbo/plugins/PluginManager.java index c21e65f..53f9ce3 100644 --- a/src/main/java/com/loohp/limbo/plugins/PluginManager.java +++ b/src/main/java/com/loohp/limbo/plugins/PluginManager.java @@ -52,48 +52,67 @@ public class PluginManager { this.plugins = new LinkedHashMap<>(); } - protected void loadPlugins() { - for (File file : pluginFolder.listFiles()) { - if (file.isFile() && file.getName().endsWith(".jar")) { - boolean found = false; - try (ZipInputStream zip = new ZipInputStream(new FileInputStream(file))) { - while (true) { - ZipEntry entry = zip.getNextEntry(); - if (entry == null) { - break; - } - String name = entry.getName(); - if (name.endsWith("plugin.yml") || name.endsWith("limbo.yml")) { - found = true; - - FileConfiguration pluginYaml = new FileConfiguration(zip); - String main = pluginYaml.get("main", String.class); - String pluginName = pluginYaml.get("name", String.class); - - if (plugins.containsKey(pluginName)) { - System.err.println("Ambiguous plugin name in " + file.getName() + " with the plugin \"" + plugins.get(pluginName).getClass().getName() + "\""); - break; - } - URLClassLoader child = new URLClassLoader(new URL[] {file.toURI().toURL()}, Limbo.getInstance().getClass().getClassLoader()); - Class clazz = Class.forName(main, true, child); - LimboPlugin plugin = (LimboPlugin) clazz.getDeclaredConstructor().newInstance(); - plugin.setInfo(pluginYaml, file); - plugins.put(plugin.getName(), plugin); - plugin.onLoad(); - Limbo.getInstance().getConsole().sendMessage("Loading plugin " + file.getName() + " " + plugin.getInfo().getVersion() + " by " + plugin.getInfo().getAuthor()); - break; - } - } - } catch (Exception e) { - System.err.println("Unable to load plugin \"" + file.getName() + "\""); - e.printStackTrace(); - } - if (!found) { - System.err.println("Jar file " + file.getName() + " has no plugin.yml!"); - } - } - } - } + protected void loadPlugins() { + for (File file : pluginFolder.listFiles()) { + if (file.isFile() && file.getName().endsWith(".jar")) { + try (ZipInputStream zip = new ZipInputStream(new FileInputStream(file))) { + ZipEntry limboYmlEntry = null; + ZipEntry pluginYmlEntry = null; + + while (true) { + ZipEntry entry = zip.getNextEntry(); + if (entry == null) { + break; + } + String name = entry.getName(); + if (name.endsWith("limbo.yml")) { + limboYmlEntry = entry; + } else if (name.endsWith("plugin.yml")) { + pluginYmlEntry = entry; + } + } + + ZipEntry chosenEntry = limboYmlEntry != null ? limboYmlEntry : pluginYmlEntry; + + if (chosenEntry != null) { + try (ZipInputStream processZip = new ZipInputStream(new FileInputStream(file))) { + while (true) { + ZipEntry currentEntry = processZip.getNextEntry(); + if (currentEntry == null) { + break; + } + + if (currentEntry.getName().equals(chosenEntry.getName())) { + FileConfiguration pluginYaml = new FileConfiguration(processZip); + String main = pluginYaml.get("main", String.class); + String pluginName = pluginYaml.get("name", String.class); + + if (plugins.containsKey(pluginName)) { + System.err.println("Ambiguous plugin name in " + file.getName() + " with the plugin \"" + plugins.get(pluginName).getClass().getName() + "\""); + break; + } + + URLClassLoader child = new URLClassLoader(new URL[]{file.toURI().toURL()}, Limbo.getInstance().getClass().getClassLoader()); + Class clazz = Class.forName(main, true, child); + LimboPlugin plugin = (LimboPlugin) clazz.getDeclaredConstructor().newInstance(); + plugin.setInfo(pluginYaml, file); + plugins.put(plugin.getName(), plugin); + plugin.onLoad(); + Limbo.getInstance().getConsole().sendMessage("Loading plugin " + file.getName() + " " + plugin.getInfo().getVersion() + " by " + plugin.getInfo().getAuthor()); + break; + } + } + } + } else { + System.err.println("Jar file " + file.getName() + " has no plugin.yml or limbo.yml!"); + } + } catch (Exception e) { + System.err.println("Unable to load plugin \"" + file.getName() + "\""); + e.printStackTrace(); + } + } + } + } public List getPlugins() { return new ArrayList<>(plugins.values());