我可以从像 onEnable() 这样的插件运行方法吗,我的脚本不起作用。:(
扩展类:
package de.R3N3PDE.X.API;
import de.R3N3PDE;
public class JavaPlugin{
Server server;
public ServerManager getServer(){
return new ServerManager(server);
}
public ServerManager setServer(Server s){
server = s;
}
public PluginLogger getLogger(){
return new PluginLogger(server);
}
}
插件:
package de.R3N3PDE.PluginExample;
import de.R3N3PDE.X.API.JavaPlugin;
public class PluginExample extends JavaPlugin{
public void onEnable(){
getLogger().info("Plugin Enabled!");
}
}
插件加载器:
package de.R3N3PDE.X.Plugin;
imports de.R3N3PDE.X.API.JavaPlugin;
public void initPlugins(){
try {
File loc = new File("plugins");
extendClasspath(loc);
ServiceLoader<JavaPlugin> sl = ServiceLoader.load(JavaPlugin.class);
Iterator<JavaPlugin> apit = sl.iterator();
while (apit.hasNext()){;
JavaPlugin met = apit.next();
//Set the Server
med.setServer(s);
//run void onEnable(); in the Plugin????
}
} catch (IOException e) {
e.printStackTrace();
}
}
=====================
编辑:谢谢。我有一个新问题... URLClassLoader 找到插件,但 ServiceLoader 无法加载插件。我认为 ServiceLoader 无法找到扩展类。
插件:
package de.R3N3PDE.PluginExampleName;
import de.CodingDev.X.Server.API.JavaPlugin;
public class PluginExampleName extends JavaPlugin{
@Override
public void onEnable() {
System.out.println("Test");
getLogger().info("Plugin Enabled!");
}
}
我制作了一个新的插件加载器,旧版本不起作用:
public void onLoad(){
File loc = new File("plugins");
File[] flist = loc.listFiles(new FileFilter() {
public boolean accept(File file) {return file.getPath().toLowerCase().endsWith(".jar");}
});
URL[] urls = new URL[flist.length];
for (int i = 0; i < flist.length; i++)
urls[i] = flist[i].toURI().toURL();
URLClassLoader ucl = new URLClassLoader(urls);
for(URL url : ucl.getURLs()){
s.getLogger().info("PluginLoader", "Found Plugin: " + url.getFile());
}
ServiceLoader<JavaPlugin> sl = ServiceLoader.load(JavaPlugin.class, ucl);
Iterator<JavaPlugin> apit = sl.iterator();
while (apit.hasNext()){
s.getLogger().info("PluginLoader", "Loading Plugin");
apit.next().onEnable();
}
}
日志:
[日志] [02.11.2013 15:26:09] [PluginLoader] [INFO] 加载插件...
[日志] [02.11.2013 15:26:09] [PluginLoader] [INFO] 找到插件:/C:/Users/René/Desktop/Server/plugins/CommandLogger.jar