2014-01-02 19:46:46 +01:00
|
|
|
'use strict';
|
2013-02-10 14:42:32 +01:00
|
|
|
/************************************************************************
|
2013-12-28 09:44:40 +01:00
|
|
|
## events Module
|
|
|
|
|
2015-01-18 21:27:37 +01:00
|
|
|
The Events module provides a thin wrapper around CanaryMod's or
|
|
|
|
Bukkit's Event-handling API. The Java-based CanaryMod and Bukkit
|
|
|
|
Events APIs make use of Java Annotations which are not available in
|
|
|
|
Javascript, so this module provides a simple way to listen to
|
|
|
|
minecraft events in javascript.
|
2013-02-10 14:42:32 +01:00
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
### events.on() static method
|
|
|
|
|
2015-01-18 21:27:37 +01:00
|
|
|
This method is used to register event listeners. This method is called by all of the Event Helper methods.
|
|
|
|
The `events` object has functions for registering listeners for each type of event. For example, you can register a block-break listener using events.on:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) {
|
|
|
|
echo(evt.player, evt.player.name + ' broke a block!');
|
|
|
|
} );
|
|
|
|
```
|
|
|
|
|
|
|
|
or you can (and probably should) use the more succinct:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
events.blockDestroy( function( evt, cancel ) {
|
|
|
|
echo(evt.player, evt.player.name + ' broke a block!');
|
|
|
|
} );
|
|
|
|
```
|
|
|
|
|
|
|
|
The events.on method can be used to register standard CanaryMod/Bukkit
|
|
|
|
events and can also be used to register non-standard events - that is
|
|
|
|
- events provided by plugins.
|
2013-02-10 14:42:32 +01:00
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Parameters
|
|
|
|
|
2015-01-18 21:27:37 +01:00
|
|
|
* eventType - A Java class. See the [CanaryMod Hook API][cmEvtApi] or [Bukkit Event API][buk] for details of the many event types.
|
2013-02-10 14:42:32 +01:00
|
|
|
|
|
|
|
* callback - A function which will be called whenever the event
|
2015-01-18 21:27:37 +01:00
|
|
|
fires. The callback in turn takes 2 parameters:
|
|
|
|
|
|
|
|
- event : the event fired
|
|
|
|
- cancel : a function which if invoked will cancel the event - not all event types are cancelable; this function only cancels cancelable events).
|
|
|
|
|
|
|
|
* priority (optional - default: "CRITICAL" for CanaryMod or "HIGHEST" for Bukkit) -
|
|
|
|
The priority the listener/callback takes over other listeners to the same event.
|
|
|
|
Possible values for CanaryMod are "CRITICAL", "HIGH", "LOW", "NORMAL" and "PASSIVE".
|
|
|
|
For an explanation of what the different CanaryMod Hook priorities
|
|
|
|
mean, refer to CanaryMod's [Hook Priority class][cmPriority].
|
|
|
|
Possible values for Bukkit are "HIGH", "HIGHEST", "LOW", "LOWEST", "NORMAL", "MONITOR".
|
|
|
|
For an explanation of what the different Bukkit Event priorities
|
|
|
|
mean, refer to bukkit's [Event API Reference][buk2].
|
2013-02-10 14:42:32 +01:00
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Returns
|
|
|
|
|
2014-03-15 19:06:23 +01:00
|
|
|
An object which can be used to unregister the listener.
|
2013-02-10 14:42:32 +01:00
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Example:
|
2013-02-10 14:42:32 +01:00
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
The following code will print a message on screen every time a block is broken in the game
|
2013-12-24 01:16:07 +01:00
|
|
|
|
2014-02-04 22:49:12 +01:00
|
|
|
```javascript
|
2015-01-18 21:27:37 +01:00
|
|
|
events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) {
|
|
|
|
echo(evt.player, evt.player.name + ' broke a block!');
|
2014-02-04 22:49:12 +01:00
|
|
|
} );
|
|
|
|
```
|
2013-02-10 14:42:32 +01:00
|
|
|
|
|
|
|
To handle an event only once and unregister from further events...
|
2014-02-04 22:49:12 +01:00
|
|
|
|
|
|
|
```javascript
|
2015-01-18 21:27:37 +01:00
|
|
|
events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) {
|
|
|
|
echo( evt.player, evt.player.name + ' broke a block!');
|
|
|
|
this.unregister();
|
2014-02-04 22:49:12 +01:00
|
|
|
} );
|
2014-04-26 21:34:45 +02:00
|
|
|
```
|
2013-02-10 14:42:32 +01:00
|
|
|
|
2014-03-15 19:06:23 +01:00
|
|
|
The `this` keyword when used inside the callback function refers to
|
|
|
|
the Listener object created by ScriptCraft. It has a single method
|
|
|
|
`unregister()` which can be used to stop listening. This is the same
|
|
|
|
object which is returned by the `events.on()` function.
|
|
|
|
|
2013-02-10 14:42:32 +01:00
|
|
|
To unregister a listener *outside* of the listener function...
|
|
|
|
|
2014-02-04 22:49:12 +01:00
|
|
|
```javascript
|
2015-01-18 21:27:37 +01:00
|
|
|
var myBlockBreakListener = events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt ) { ... } );
|
2014-02-04 22:49:12 +01:00
|
|
|
...
|
2014-03-15 19:06:23 +01:00
|
|
|
myBlockBreakListener.unregister();
|
2014-02-04 22:49:12 +01:00
|
|
|
```
|
2014-09-30 00:42:41 +02:00
|
|
|
|
2013-02-10 14:42:32 +01:00
|
|
|
[buk2]: http://wiki.bukkit.org/Event_API_Reference
|
|
|
|
[buk]: http://jd.bukkit.org/dev/apidocs/index.html?org/bukkit/event/Event.html
|
2015-01-18 21:27:37 +01:00
|
|
|
[cmEvtApi]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/Hook.html
|
|
|
|
[cmPriority]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/plugin/Priority.html
|
2013-02-10 14:42:32 +01:00
|
|
|
|
|
|
|
***/
|
2014-11-09 12:52:10 +01:00
|
|
|
var helper;
|
2015-01-18 21:27:37 +01:00
|
|
|
/*global __plugin, module, require*/
|
2014-09-30 00:42:41 +02:00
|
|
|
if (__plugin.canary){
|
|
|
|
module.exports = require('events-canary');
|
2014-11-09 12:52:10 +01:00
|
|
|
helper = require('events-helper-canary');
|
2014-09-30 00:42:41 +02:00
|
|
|
} else {
|
|
|
|
module.exports = require('events-bukkit');
|
2014-11-09 12:52:10 +01:00
|
|
|
helper = require('events-helper-bukkit');
|
2014-06-06 20:40:47 +02:00
|
|
|
}
|
2014-09-30 00:42:41 +02:00
|
|
|
for ( var func in helper ) {
|
|
|
|
module.exports[func] = helper[func];
|
2013-12-24 01:16:07 +01:00
|
|
|
};
|