我在 Bukkit 插件项目上工作了一段时间,终于决定正式发布。因此,在我疯狂地记录有关我项目的所有内容的过程中,我决定为想要使用它们的人清理一些 API。现在,我遇到了一个问题。
基本上,在此 API 中,您必须实现一个接口,然后将您的实现注册到一个管理每个实现的类,并使用插件内捆绑的 PluginMetrics 检查/注册另一个变量。
这是我的界面,“名称”和“跟踪器”字段是关注的字段:
public interface IAction {
/**
* Name of the action.
*/
String name = null;
/**
* Stores args passed to the action by the player.
*/
String[] args = null;
/**
* Holds the Metrics tracker object.
*/
Tracker tracker = null;
...
}
而且,这是我所有问题出现的方法(和 init() 方法):
public static void registerAction(final Class a) {
try {
store.put((String) a.getField("name").get(a), a);
log.exDebug(String.format("Action %s (%s) was registered.", (String) a.getField("name").get(a), a.getCanonicalName()));
try {
Tracker metric = (Tracker) a.getField("tracker").get(a);
try {
OpenAuth.getMetrics().addCustomData(metric);
log.exDebug(String.format("Registered Metrics data tracker [%s] from %s.", metric.getColumnName(), a.getCanonicalName()));
} catch (java.lang.Exception e) {
log.info("Exception occurred while registering Action data tracker.");
}
} catch (java.lang.Exception e) {
log.info("Exception occurred while registering Action data tracker.");
// e.printStackTrace();
}
} catch (java.lang.Exception e) {
log.info("Exception occurred while registering an Action.");
e.printStackTrace();
}
}
public static void init() {
for (Actions a : Actions.values()) {
try {
registerAction(a.getAction());
} catch (java.lang.Exception e) {
log.info("Exception occurred while initialising Actions enumerator.");
e.printStackTrace();
}
}
for (Object ob : classLoader.load().getClasses()) {
Class c = null;
try {
c = (Class) ob;
} catch (java.lang.Exception e) {
log.info("Exception occurred while casting up external Action.");
e.printStackTrace();
}
try {
registerAction(c);
} catch (java.lang.Exception e) {
log.info("Exception occurred while registering external Action.");
e.printStackTrace();
}
}
}
和一个示例实现:
public class FreezeStick implements IAction {
public static final String name = "freeze";
public static final Tracker tracker = new Tracker("FreezeStick");
private String[] args = null;
private Session attached;
private SessionController sc;
private final LogHandler log = new LogHandler();
private final String permissible = "openauth.action.freeze";
private OAServer server;
private boolean used = false;
protected OAPlayer sender;
protected OAPlayer target;
public FreezeStick(OAServer server, Session attached) {
this.server = server;
this.sc = server.getController().getSessionController();
this.attached = attached;
this.setSender(this.attached.getPlayer());
}
-- etc.. --
每次Tracker metric = (Tracker) a.getDeclaredField("tracker").get(a);
发生时,都会报告为null
。我试过用 a.getField() 而不是 a.getDeclaredField(),但同样的问题。name 和 tracker 变量private, static, and final
在实现中声明时都会收到,并且自从我第一次编写此代码以来,在 name 变量上使用 a.getField() 就可以正常工作。
如果有人能指出我正确的方向,我将不胜感激:)
此外,如果它有助于更好地解决我的问题,则会出现以下异常:
2013-01-02 10:48:10 [INFO] [OpenAuth-debug] Action ban (me.maiome.openauth.actions.BanStick) was registered.
2013-01-02 10:48:10 [INFO] [OpenAuth] Exception occurred while adding Action data tracker.
2013-01-02 10:48:10 [SEVERE] java.lang.NullPointerException
2013-01-02 10:48:10 [SEVERE] at me.maiome.openauth.actions.Actions.registerAction(Actions.java:65)
2013-01-02 10:48:10 [SEVERE] at me.maiome.openauth.actions.Actions.init(Actions.java:104)
2013-01-02 10:48:10 [SEVERE] at me.maiome.openauth.bukkit.OpenAuth.onEnable(OpenAuth.java:230)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.loadPlugin(CraftServer.java:278)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.enablePlugins(CraftServer.java:260)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.<init>(CraftServer.java:214)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.PlayerList.<init>(PlayerList.java:52)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedPlayerList.<init>(SourceFile:11)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedServer.init(DedicatedServer.java:104)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:399)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)