API

From Citizens Wiki

Revision as of 13:27, 4 August 2012 by Fullwall (talk | contribs)

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. An example registration and simple trait is given below.

public class MyTrait extends Trait {
    public MyTrait() {
         super("mytraitname")
    }
    public void load(DataKey key) {
    }
  
    public void save(DataKey key) {
    }
}
public class MyPlugin extends JavaPlugin {
    public void onEnable() {
        TraitFactory factory = CitizensAPI.getTraitFactory();
        TraitInfo info = TraitInfo.create(MyTrait.class).withName("mytraitname");
        factory.registerTrait(info);
    }
}

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.

An example is given below.

public class MyGoal implements Goal {
    private Object state;
    private GoalSelector selector; // the current selector
    public void reset() {
        state = null;
        // this method can be called at any time - tear down any state
    }
    public void run() {
        if(!npcIsCool()) {
            selector.finish(); // stops execution
        } else if (npcIsAwesome()){
            selector.select(new AwesomeGoal()); // this switches execution to AwesomeGoal and stops execution of this goal.
        } else if (npcNeedsCool()) {
            selector.selectAdditional(new AccumulateCoolGoal()); // AccumulateCoolGoal executes concurrently to this goal.
        }
    }
    public boolean shouldExecute(GoalSelector selector) {
        if (npcIsCool()) {
            this.selector = selector;
            return true;
        }
        return false;
    }
}

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