Scripting: Difference between revisions

From Citizens Wiki

Line 22: Line 22:
importPackage(Packages.org.bukkit.entity);
importPackage(Packages.org.bukkit.entity);
importPackage(Packages.org.bukkit.inventory);
importPackage(Packages.org.bukkit.inventory);
importPackage(Packages.net.citizensnpcs.api.ai.event);
importPackage(java.util);
importPackage(java.util);
function addGoals (goals, npc) {
function addGoals (goals, npc) {
Line 51: Line 52:
                 }
                 }
                 this.player.sendMessage(npc.getName() + " Ha! Stole an item!");
                 this.player.sendMessage(npc.getName() + " Ha! Stole an item!");
                allowed = false; // wait to get home.
                 this.npc.getNavigator().setTarget(this.home);
                 this.npc.getNavigator().setTarget(this.home);
                 selector.finish();
                 selector.finish();
Line 56: Line 58:
         },
         },
shouldExecute : function(selector) {
shouldExecute : function(selector) {
             if (!this.npc.isSpawned())
             if (!this.npc.isSpawned() || !allowed)
                 return false;
                 return false;
             var nearby = this.npc.getBukkitEntity().getNearbyEntities(5,5,5);
             var nearby = this.npc.getBukkitEntity().getNearbyEntities(5,5,5);
Line 69: Line 71:
             }
             }
    return false;
    return false;
      },
      allowed : true,
      onNavigationComplete : function(event) {
          if (event.getNPC() != this.npc)
              return;
          this.allowed = true;
       },
       },
       equals : function(other) {
       equals : function(other) {
           return this == other;
           return this == other;
       }
       }
         };
      };
goals.addGoal(1, new Goal(thiefGoal));
        var goal = new Goal(thiefGoal);
         events.register(goal, "onNavigationComplete", NavigationCompleteEvent.class);  
goals.addGoal(1, goal);
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 15:30, 31 October 2012

Scripting Repository

http://scripts.citizensnpcs.com - submit user scripts (including Denizen scripts) here.

Scripting Support in Citizens2

Citizens2 includes support for various scripting languages including Lua, Javascript and Python.

Javascript support is built into Java; additional languages can be added in through installing things such as JRuby (http://jruby.org/), Jython (http://www.jython.org/), Kahlua (http://www.ohloh.net/p/kahlua) and more. These use the Java Scripting API to give common scripting support to Citizens.

The scripting API contains a few built in objects in the global namespace which aid in some tasks. Currently, the 'events' object allows registering event listeners for Bukkit events and a reference to the Citizens plugin object is available under 'plugin'.

Behaviours

Let's see an example of how to use scripts. Citizens builds in by default the 'Behaviour' trait - this lets you specify AI tasks via scripts. Behaviours can be added to the NPC via the /npc behaviour [behaviours] command, where behaviours is a list of files to use.

Scripts for behaviours should be placed in the plugins/Citizens/scripts/behaviours folder. Scripts are differentiated by their extension -- make sure it is correct!

When the script file is loaded by Citizens, it will call the method 'addGoals(Goals, NPC)'. Goals can be added via calling goals.addGoal(priority, goal), where goal implements the interface specified here -http://jd.citizensnpcs.com/net/citizensnpcs/api/ai/Goal.html.

An example of a behaviour file is given below - this implements a simple thief goal.


Code: Example done in Javascript

Scripting Gotchas and Tips

Javascript

  • Some code will expect the methods equals and hashcode to be implemented - a simple equals function should be used often.
  • for.. in loops do not work with Java collections.
  • The default Javascript-Java implementation cannot use abstract classes.
  • You can implement Java interfaces by using new [interfacename](object), where object contains the methods implementing the interface.
  • Use importPackage(Packages.name.space) or importClass(Packages.name.space.YourClass) to import other classes/packages. The Packages. prefix is not required if the class is in the java.util package.