Skip to content

Adding an API method through an annotation

alecgorge edited this page May 15, 2012 · 3 revisions

Starting with JSONAPI 3.6.0, you can define new API methods at runtime using the API_Method annotation.

The source code of the annotation should be pretty self-explanatory to experienced Java developers, but I am going to put an example below for the sake of completeness.

Requirements

You will need to put JSONAPI.jar as a library while compiling your plugin. This will make JSONAPI a hard dependency of your plugin. If you do not want this, you will probably want to use this method instead: Integration-guide-for-normal-api-methods

Example

import com.alecgorge.minecraft.jsonapi.dynamic.API_Method;
import com.alecgorge.minecraft.jsonapi.dynamic.JSONAPIMethodProvider;
...
class XYZ implements JSONAPIMethodProvider {
...
	// somewhere in this class (or even in another class, just change the `this` below to be the object instance
	((JSONAPI)getServer().getPluginManager().getPlugin("JSONAPI")).getCaller().registerMethods(this);
...
	@API_Method(
		namespace = "test",
		argumentDescriptions = {
			"This is a description for the first argument"
		}
	)
	public boolean methodName (String arg) {
		// do stuff
		return true;
	}

In this example, a new API method would be available at "test.methodName" with 1 String argument.

In addition to setting the namespace, and argumentDescriptions, several other things can be set:

  • namespace: This is the namespace used by the plugin. Defaults to ""--or no namespace. String, optional
  • name: By default, the name of the actual method will be used. However, you can override that by setting name. String, optional
  • description: You can attach a description to the method if you want to. However, there really is no point except for your own reference because this value is just thrown out later. String, optional
  • returnDescription: A description of what this value returns. The type of the return value is automatically determined. String, optional
  • argumentDescriptions: An array of descriptions, one String per argument. String[], optional

Please note that your methods will not work without calling the registerMethods method on the Caller class!

Clone this wiki locally