This repository has been archived on 2021-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
scriptcraft/src/main/js/plugins/examples/example-7-hello-events.js
2014-12-22 15:23:06 +00:00

104 lines
3.8 KiB
JavaScript

/*************************************************************************
## Example Plugin #7 - Listening for events, Greet players when they join the game.
A simple event-driven minecraft plugin. How to handle Events.
This example demonstrates event-driven programming. The code below
will display the version of ScriptCraft every time an operator joins
the game. This module is notable from previous modules for the
following reasons...
1. It does not export any functions or variables. That's fine. Not
all modules need export stuff. Code in this module will be
executed when the module is first loaded. Because it is in the
`/scriptcraft/plugins` directory, it will be loaded automatically
when the server starts up.
2. It uses ScriptCraft's `events.on()` function to add a new *Event
Handler*. An *Event Handler* is a just a function which gets
called whenever a particular *event* happens in the game. The
function defined below will only be executed whenever a player
joins the game. This style of program is sometimes refered to as
*Event-Driven Programming*.
Adding new *Event Handlers* in ScriptCraft is relatively easy. Use the
`events.on()` function to add a new event handler. It takes 2
parameters...
1. The Event Name, in this case `'player.PlayerJoinEvent'`. You can
browse [all possible Bukkit events][bkevts] (click the 'Next
Package' and 'Previous Package' links to browse).
2. The event handling function (also sometimes refered to as a
'callback'). In ScriptCraft, this function takes a single
parameter, an event object. All of the information about the event
is in the event object.
In the example below, if a player joins the server and is an operator,
then the ScriptCraft plugin information will be displayed to that
player.
What's also notable about this example is how it uses the `isOp()` function. The code...
if ( isOp(event.player) )
ScriptCraft uses a special version of JavaScript which comes
bundled with Java (Minecraft is written in Java) and JavaScript in
Java can access properties of Java objects more succinctly than in
Java itself. What this means in practice is that when you're perusing
the [Bukkit API Reference][bkapi] and come across a method like
[Player.getAllowFlight()][bkgaf], you can write code like this...
var allowFlight = player.getAllowFlight(); // java style
... or the more succinct ...
var allowFlight = player.allowFlight; // javascript style
... Which style you choose is up to you but `player.allowFlight` is
cleaner and more readable. Similarly where you see a method like
[Player.setAllowFlight()][bksaf], you can write ...
player.setAllowFlight(true); // java style
... or the more readable...
player.allowFlight = true; // javascript style
... Which style you choose is up to you.
[bkevts]: http://jd.bukkit.org/dev/apidocs/org/bukkit/event/package-summary.html
[bkgaf]: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/Player.html#getAllowFlight()
[bksaf]: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/Player.html#setAllowFlight()
[bkapi]: http://jd.bukkit.org/dev/apidocs/
events.on( 'player.PlayerJoinEvent', function( event ) {
if ( isOp(event.player) ) {
echo( event.player, 'Welcome to ' + __plugin);
}
});
Update: Since version 2.0.8 the above code can be replaced by the more succinct:
events.playerJoin( function( event ) {
if ( event.player.op ) {
echo( event.player, 'Welcome to ' + __plugin);
}
});
***/
// wph 20140927 - event handler registration differs depending on framework.
function onJoin( event ) {
if ( isOp(event.player) ) {
echo( event.player, 'Welcome to ' + __plugin );
}
}
if (__plugin.canary){
// canarymod
events.connection( onJoin );
} else {
// bukkit
events.playerJoin( onJoin );
}