Merge pull request #88 from Kas-tle/feature/prefer-limbo-yml

Prefer use of limbo.yml for plugin loading
This commit is contained in:
LOOHP 2025-07-17 20:34:46 +01:00 committed by GitHub
commit 94ad6d8460
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 61 additions and 42 deletions

View File

@ -52,48 +52,67 @@ public class PluginManager {
this.plugins = new LinkedHashMap<>(); this.plugins = new LinkedHashMap<>();
} }
protected void loadPlugins() { protected void loadPlugins() {
for (File file : pluginFolder.listFiles()) { for (File file : pluginFolder.listFiles()) {
if (file.isFile() && file.getName().endsWith(".jar")) { if (file.isFile() && file.getName().endsWith(".jar")) {
boolean found = false; try (ZipInputStream zip = new ZipInputStream(new FileInputStream(file))) {
try (ZipInputStream zip = new ZipInputStream(new FileInputStream(file))) { ZipEntry limboYmlEntry = null;
while (true) { ZipEntry pluginYmlEntry = null;
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); while (true) {
String main = pluginYaml.get("main", String.class); ZipEntry entry = zip.getNextEntry();
String pluginName = pluginYaml.get("name", String.class); if (entry == null) {
break;
}
String name = entry.getName();
if (name.endsWith("limbo.yml")) {
limboYmlEntry = entry;
} else if (name.endsWith("plugin.yml")) {
pluginYmlEntry = entry;
}
}
if (plugins.containsKey(pluginName)) { ZipEntry chosenEntry = limboYmlEntry != null ? limboYmlEntry : pluginYmlEntry;
System.err.println("Ambiguous plugin name in " + file.getName() + " with the plugin \"" + plugins.get(pluginName).getClass().getName() + "\"");
break; if (chosenEntry != null) {
} try (ZipInputStream processZip = new ZipInputStream(new FileInputStream(file))) {
URLClassLoader child = new URLClassLoader(new URL[] {file.toURI().toURL()}, Limbo.getInstance().getClass().getClassLoader()); while (true) {
Class<?> clazz = Class.forName(main, true, child); ZipEntry currentEntry = processZip.getNextEntry();
LimboPlugin plugin = (LimboPlugin) clazz.getDeclaredConstructor().newInstance(); if (currentEntry == null) {
plugin.setInfo(pluginYaml, file); break;
plugins.put(plugin.getName(), plugin); }
plugin.onLoad();
Limbo.getInstance().getConsole().sendMessage("Loading plugin " + file.getName() + " " + plugin.getInfo().getVersion() + " by " + plugin.getInfo().getAuthor()); if (currentEntry.getName().equals(chosenEntry.getName())) {
break; FileConfiguration pluginYaml = new FileConfiguration(processZip);
} String main = pluginYaml.get("main", String.class);
} String pluginName = pluginYaml.get("name", String.class);
} catch (Exception e) {
System.err.println("Unable to load plugin \"" + file.getName() + "\""); if (plugins.containsKey(pluginName)) {
e.printStackTrace(); System.err.println("Ambiguous plugin name in " + file.getName() + " with the plugin \"" + plugins.get(pluginName).getClass().getName() + "\"");
} break;
if (!found) { }
System.err.println("Jar file " + file.getName() + " has no plugin.yml!");
} 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<LimboPlugin> getPlugins() { public List<LimboPlugin> getPlugins() {
return new ArrayList<>(plugins.values()); return new ArrayList<>(plugins.values());