0
所以我正在爲一個遊戲,它做什麼,一個插件加載器是從.jar
一個文件夾內加載插件(class
對象),然後將它們添加到ArrayList
(讓用戶可以做他們自己的mods)。但是我的Java經驗並不是最好的:/到目前爲止,它獲得了jar
中的類,但我需要檢查類中的方法(如標記的構造函數)。或者如果有人知道更好的方法來做到這一點。任何幫助,將不勝感激。如何獲得一個類中的方法
Plugin.java
:
package me.rigamortis.faurax.plugin;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;
public class Plugin {
public Minecraft mc = Minecraft.getMinecraft();
final File folder = new File(mc.mcDataDir + File.separator + "Faurax/Plugins");
public String jar;
public static String className;
private String name;
private String desc;
private int color;
private int key;
public Plugin() {
if(!folder.exists())
folder.mkdirs();
loadPlugins(folder);
if(folder != null)
getClassNames(folder);
}
public void setName(String newName) {
name = newName;
}
public void setDesc(String newDesc) {
desc = newDesc;
}
public void setColor(int newColor) {
color = newColor;
}
public void setKey(int newKey) {
key = newKey;
}
public String getName() {
return this.name;
}
public String getDesc() {
return this.desc;
}
public int getKey() {
return this.key;
}
public int getColor() {
return this.color;
}
/**
* A hook for plugins
*/
public void onTick() {}
public void loadPlugins(final File folder) {
try{
for (final File fileEntry : folder.listFiles()) {
if (!fileEntry.isDirectory()) {
if(fileEntry.getName().endsWith(".jar")){
jar = fileEntry.getName();
Core.logNormal("Loading " + fileEntry.getName());
}
}
}
}
catch(Exception e) {
Core.logError("Error? jar isin't a plugin.");
e.printStackTrace();
}
}
public void getClassNames(final File dir) {
try{
ZipInputStream zip = new ZipInputStream(new FileInputStream(dir + File.seperator + jar));
for(ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry())
if(entry.getName().endsWith(".class") && !entry.isDirectory()) {
Core.logNormal("Found "+entry);
className = entry.toString();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
PluginLoader.java
:
package me.rigamortis.faurax.plugin;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;
public class PluginLoader {
public static List<Plugin> plugins = new ArrayList<Plugin>();
public PluginLoader() {
loadPlugins();
}
public void loadPlugins() {}
}
使用__reflection __ !!!〜或者您可以指示程序記住方法,因爲它們不會被更改。順便說一句,你爲什麼要在不使用Bukkit的情況下構建一個Minecraft插件? – Unihedron
製作一個改裝悲傷的客戶端:P雖然檢查出反思。相當新的Java .-。 – 420Blaze
檢查http://stackoverflow.com/questions/22200676/recursively-list-all-properties-exposed-by-a-class/22201894#22201894,upvote如果它有用 – robermann