API: Difference between revisions

From Citizens Wiki

No edit summary
No edit summary
Line 34: Line 34:
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.
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. An example registration and simple trait is given below.
To register a trait, we use the '''TraitFactory''' class. This controls registration for your custom traits.
 
{{codebox|height=300px|Example registration and simple trait|<syntaxhighlight lang="java5">
  public class MyTrait extends Trait {
  public class MyTrait extends Trait {
     public MyTrait() {
     public MyTrait() {
Line 53: Line 53:
     }
     }
  }
  }
</syntaxhighlight>
}}


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.
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.
Line 69: Line 71:
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.
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.


An example is given below.
{{codebox|height=300px|Example|<syntaxhighlight lang="java5">
 
  public class MyGoal implements Goal {
  public class MyGoal implements Goal {
     private Object state;
     private Object state;
Line 95: Line 96:
     }
     }
  }
  }
</syntaxhighlight>
}}


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 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.

Revision as of 10:27, 6 August 2012

Under Construction: These pages are under construction for the upcoming release of Citizens 2.0, a complete rewrite of the Citizens plugin you know and love with tons of spiffy new features. If you're looking for reference for Citizens 1.x and its character types, please see the index of 1.x pages. Citizens 2.0 has not been released, contrary to what the wiki says. We're just preparing it for the eventual release. You CAN however download the beta at the Citizens Jenkins.


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.net

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.

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
{{{2}}}

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