Skip to content

Integration guide for normal api methods

hclewk edited this page Jan 28, 2012 · 2 revisions

You need to add JSONAPI.jar to your classpath when compiling. In Eclipse, this means adding JSONAPI.jar to the build path.

Adding/overloading JSONAPI methods is quite easy. You just need to implement 1 interface JSONAPICallHandler, which looks like this:

public interface JSONAPICallHandler {
	// if you return true here, you WILL handle the API call in the handle method.
	// will be called twice for every API call, once to test if the method exists and once on the actuall call
	public boolean willHandle(APIMethodName methodName);
	
	// the result of this method will be treated as the response to the API request, even if null is returned (some methods do return null)
	public Object handle(APIMethodName methodName, Object[] args);
}

Here is a sample implementation:

class MyCoolPlugin extends JavaPlugin implements JSONAPICallHandler {
	public void onEnable() {
		...
		Plugin checkplugin = this.getServer().getPluginManager().getPlugin("JSONAPI");
		if(checkplugin != null) {
			// get the JSONAPI instance
			JSONAPI jsonapi = (JSONAPI)checkplugin;
			
			jsonapi.registerAPICallHandler(this);
		}
		else {
			// JSONAPI isn't loaded. Throw an error or continue on your way.
		}
		...
	}
	
	public boolean willHandle(APIMethodName methodName) {
		if(methodName.matches("ping")) {
			return true;
		}
	}
	
	public Object handle(APIMethodName methodName, Object[] args) {
		if(methodName.matches("ping")) {
			return "pong";
		}
	}
}