I’ve been working on a Bukkit plugin project for a while, and I’ve finally decided to make a formal release. So in my crazy scramble to document everything about my project, I decided to clean up some APIs for people that would like to use them. Now, I’ve hit the wall with a single problem though.
Basically, in this API, you must implement an interface and then register your implementation with a class that manages every implementation and also checks for/registers another variable with the PluginMetrics that are bundled inside the plugin.
Here is my interface, with the “name” and “tracker” fields being fields of concern:
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;
...
}
And, here is the method (and init() method) where all my issues arise:
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();
}
}
}
And an example implementation:
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.. --
Every time Tracker metric = (Tracker) a.getDeclaredField("tracker").get(a); happens, it’s
reported as null. I’ve tried with a.getField() instead of a.getDeclaredField(), but same problem. Both the name and tracker variables receive private, static, and final when they are declared in the implementations, and using a.getField() on the name variable has worked fine ever since I first wrote this.
If someone could point me in the right direction, I would greatly appreciate it 🙂
Also, here is the exception that occurs if it helps with figuring out my problem any better:
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)
I found my answer. Turns out, I was trying to run #addCustomData() on the Metrics instance before there was Metrics instance ready to be used. Just had to move Actions.init() down a few lines in my controller 🙂