1.x/API: Difference between revisions

From Citizens Wiki

< 1.x

(Created page with "== JavaDocs == Our API documentation can be found [http://jd.citizensnpcs.net here]. For a basic guide on hooking into Citizens, see below. == Using the Citizens API == Hooking ...")
 
No edit summary
Line 6: Line 6:


<pre>
<pre>
public boolean citizensEnabled = false;


public void onEnable() {
public void onEnable() {
     Plugin test = server.getPluginManager().getPlugin("Citizens");
     PluginManager pm = getServer().getPluginManager();
    Plugin test = pm.getPlugin("Citizens");
     if (test != null) {
     if (test != null) {
        citizensEnabled = true;
         System.out.println("Successfully hooked into Citizens!");
         System.out.println("Successfully hooked into Citizens!");
     } else {
     } else {
         System.out.println("Citizens isn't loaded.");
         System.out.println("Citizens isn't loaded.");
        // disable your plugin because Citizens was not loaded
        pm.disablePlugin(this);
     }
     }
</pre>
</pre>


To utilize this class, add the following import to your code:
Now, to actually use the methods that Citizens has to offer, add the following import to your code:


<pre>import net.citizensnpcs.api.CitizensManager</pre>
<pre>import net.citizensnpcs.api.CitizensManager</pre>
For specifics on the methods CitizensManager has, see our JavaDocs. For now, here is a basic example of how to use the class.
<pre>
//checks if the passed Bukkit entity is an NPC
public void checkNPC(Entity entity) {
    if(CitizensManager.isNPC(entity)) {
        Bukkit.getServer().broadcastMessage(CitizensManager.get(entity).getStrippedName() + " is an NPC!");
    }
}
</pre>
The last step is to make sure Citizens loads before your plugin. If it doesn't your plugin will throw some nasty console errors. To do this, add this line to your plugin.yml:
<pre>soft-depend: [Citizens]</pre>

Revision as of 11:51, 2 September 2011

JavaDocs

Our API documentation can be found here. For a basic guide on hooking into Citizens, see below.

Using the Citizens API

Hooking into Citizens is the same as hooking into any other Bukkit plugin like PermissionsBukkit or iConomy. Add the latest version of Citizens.jar to your build path. Then, add this code to your onEnable in the main class:


public void onEnable() {
    PluginManager pm = getServer().getPluginManager();
    Plugin test = pm.getPlugin("Citizens");
    if (test != null) {
        System.out.println("Successfully hooked into Citizens!");
    } else {
        System.out.println("Citizens isn't loaded.");
        // disable your plugin because Citizens was not loaded
        pm.disablePlugin(this);
    }

Now, to actually use the methods that Citizens has to offer, add the following import to your code:

import net.citizensnpcs.api.CitizensManager

For specifics on the methods CitizensManager has, see our JavaDocs. For now, here is a basic example of how to use the class.

//checks if the passed Bukkit entity is an NPC
public void checkNPC(Entity entity) {
    if(CitizensManager.isNPC(entity)) {
        Bukkit.getServer().broadcastMessage(CitizensManager.get(entity).getStrippedName() + " is an NPC!");
    }
}

The last step is to make sure Citizens loads before your plugin. If it doesn't your plugin will throw some nasty console errors. To do this, add this line to your plugin.yml:

soft-depend: [Citizens]