API: Difference between revisions

From Citizens Wiki

No edit summary
Line 144: Line 144:
This is a link to a an example trait. It is similar to the code above, with some additional code for better handling commands, default configuration, and a plugin.yml
This is a link to a an example trait. It is similar to the code above, with some additional code for better handling commands, default configuration, and a plugin.yml
*[[https://github.com/downloads/jrbudda/Citizens2/Citizens%20MyTrait%20Example.zip Download it here]]
*[[https://github.com/downloads/jrbudda/Citizens2/Citizens%20MyTrait%20Example.zip Download it here]]
You will also need to build against [[http://ci.citizensnpcs.com/job/CitizensAPI/ CitizensAPI.jar]] and [[http://dl.bukkit.org/downloads/bukkit/ Bukkit.jar]].
You will also need to build against [[http://ci.citizensnpcs.com/job/CitizensAPI/ CitizensAPI.jar]],[[http://ci.citizensnpcs.com/job/Citizens2/ Citizens.jar]],  and [[http://dl.bukkit.org/downloads/bukkit/ Bukkit.jar]].
 
 


==NPC Events==
==NPC Events==

Revision as of 22:52, 18 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

Do's an Don't

DO

  • Check npc.isSpawned() before using npc.getBukkitEntity()
  • Check npc.isSpawned() before using npc.getNavigator()
  • Create a separate singleton Listener class if you expect there to be many instances of this trait running. This will help performance.
  • Honor npc.data().get(NPC.DEFAULT_PROTECTED_METADATA) If this is true the NPC should be 'invulnerable' to normal damaging effects.
  • use CitizensAPI.getNPCRegistry().isNPC() to check if an entity is a NPC. Real players and player-type NPCs will both return true for instanceof Player.

DON'T

  • Change event data other than to cancel/uncancel it.
  • Attempt to access npc.GetBukkitEntity() until onSpawn() has been called.
  • Change anything in npc.getNavigator.getDefaultParams(). Use the localParams() instead after setting a navigation target.
  • Assume a NPC is a player-type. Mob types have some important differences (like no inventory).

Download an example

This is a link to a an example trait. It is similar to the code above, with some additional code for better handling commands, default configuration, and a plugin.yml

You will also need to build against [CitizensAPI.jar],[Citizens.jar], and [Bukkit.jar].

NPC Events

Citizens implements its own Listeners and will call new NPC-specific versions of many common events. This saves Trait developers the trouble of finding their npcs from the normal event entities. The event object these events provide are just like their Bukkit counterparts with the addition of the getNPC() method. Citizens currently provides the following:

  • EntityTargetNPCEvent
  • NPCClickEvent
  • NPCCollisionEvent
  • NPCCombustByBlockEvent
  • NPCCombustByEntityEvent
  • NPCCombustEvent
  • NPCDamageByBlockEvent
  • NPCDamageByEntityEvent
  • NPCDamageEvent
  • NPCDespawnEvent
  • NPCEvent
  • NPCLeftClickEvent
  • NPCPushEvent
  • NPCRemoveEvent
  • NPCRightClickEvent
  • NPCSelectEvent
  • NPCSpawnEvent

See the [Javadocs] for details.

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