API: Difference between revisions

From Citizens Wiki

Line 42: Line 42:
{{codebox|height=300px|width=100%|Example registration and simple trait|<syntaxhighlight lang="java5">
{{codebox|height=300px|width=100%|Example registration and simple trait|<syntaxhighlight lang="java5">
//This is your trait that will be applied to a npc using the /trait mytraitname command. Each NPC gets its own instance of this class.
//This is your trait that will be applied to a npc using the /trait mytraitname command. Each NPC gets its own instance of this class.
//You can add your functionality here, or create new classes to implement listeners, run logic, etc.
//the Trait class has a reference to the attached NPC class through 'npc' or getNPC().
//the Trait class has a reference to the attached NPC class through 'npc' or getNPC().
//The Trait class also implements Listener so you can add EventHandlers directly to your trait.
public class MyTrait extends Trait {
public class MyTrait extends Trait {


Line 63: Line 63:
public void save(DataKey key) {
public void save(DataKey key) {
key.setBoolean("SomeSetting",SomeSetting);
key.setBoolean("SomeSetting",SomeSetting);
}
@EventHandler
public void click(net.citizensnpcs.api.event.NPCClickEvent event){
//Handle a click on a NPC. The event has a getNPC() method.
//Be sure to check event.getNPC() == this.getNPC() so you only handle clicks on this NPC!
}
}


Line 89: Line 96:


}
}


//This is your bukkit plugin class. Use it to hook your trait into Citizens and handle any commands.
//This is your bukkit plugin class. Use it to hook your trait into Citizens and handle any commands.

Revision as of 18:18, 17 September 2012

Current Release Download: Jenkins

Developmental Builds: Jenkins

Documentation: JavaDocs

Source: Github

Citizens has an extensive API that can be used for making your plugins work with NPCs or even for adding a brand new character that can be attached to an NPC. Make sure you always are using an up-to-date build of the CitizensAPI to ensure that your plugin works with the latest release of Citizens.

Javadocs can be found at http://jd.citizensnpcs.com

Hooking Into Citizens

Hooking into Citizens is as simple as creating a basic plugin and adding the line depend: [Citizens] into your plugin.yml. From here, a common basic entry point is the CitizensAPI class. This gives you access to the NPCRegistry for NPC lookup, as well as the TraitFactory which allows trait registration.

Checking if an entity is a Citizens NPC

Citizens NPCs will have the "NPC" metadata set to true. Eg.

boolean isCitizensNPC = entity.hasMetadata("NPC");

PLEASE NOTE: Players sharing names with NPCs will have the same metadata as the NPC as per https://bukkit.atlassian.net/browse/BUKKIT-2501. Working on a fix for this.


Creating a Trait

Traits are persistent, attachable objects that are linked to an NPC and provide specific functionality. This can be anything from a full-blown dynamic villager AI to a simple talking trait.

To register a trait, we use the TraitFactory class. This controls registration for your custom traits.

Code: Example registration and simple trait

This sets up a simple trait. The user can already attach a trait to the NPC using the /trait [name] command, but you may want to create your own command that is more user-friendly.

There are various methods that Trait provides to make life easier for developers. For example, developers can override the run() method which is called every tick. @EventHandler can be used to listen for events without having to register them manually.

The onSpawn() method is also important -- this tells you when the npc field provided in Trait (accessible using this.npc or this.getNPC()) is safe to use.

The DataKey parameters provided in save and load allow saving of simple data types such as Strings and primitive values for persistence. These will be saved under the individual NPC's data storage.

Using the AI API

The AI API of Citizens can be broken down into two parts - GoalController and Navigator.

A Goal is a repeatable, abstract unit of work that can be performed by an NPC. It can be registered with a GoalController with a priority (higher is more important). The highest priority goal which can be executed will be prioritised. NPC contains getDefaultGoalController() for this purpose.

The GoalSelector allows a great deal of flexibility within goal implementations. It allows firstly the dynamic selection of sub-goals and the concurrent execution of many sub-goals, and can stop execution at any time.


Code: Example
{{{2}}}

The second concept is the Navigator. This controls the pathfinding aspects of the NPC. The Navigator can have one target at a time, and will call events to notify of completion/cancellation.

The pathfinding range of the Navigator is the maximum range it will search when attempting to find a path to the target. This is usually set by the server admin. The speed of the Navigator is the movement speed of the NPC while moving to the target. The default speed is around 0.3.

See Also

Characters