2013-12-31 21:33:00 +01:00
|
|
|
/*************************************************************************
|
2014-01-04 19:39:49 +01:00
|
|
|
## Example Plugin #7 - Listening for events, Greet players when they join the game.
|
2013-12-31 19:21:40 +01:00
|
|
|
|
2013-12-31 21:33:00 +01:00
|
|
|
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...
|
2013-12-31 19:21:40 +01:00
|
|
|
|
|
|
|
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
|
2014-03-15 19:06:23 +01:00
|
|
|
'callback'). In ScriptCraft, this function takes a single
|
|
|
|
parameter, an event object. All of the information about the event
|
|
|
|
is in the event object.
|
2013-12-31 19:21:40 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2014-12-22 16:23:06 +01:00
|
|
|
What's also notable about this example is how it uses the `isOp()` function. The code...
|
2013-12-31 19:21:40 +01:00
|
|
|
|
2014-12-22 16:23:06 +01:00
|
|
|
if ( isOp(event.player) )
|
2013-12-31 19:21:40 +01:00
|
|
|
|
2014-12-22 16:23:06 +01:00
|
|
|
ScriptCraft uses a special version of JavaScript which comes
|
2013-12-31 19:21:40 +01:00
|
|
|
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/
|
2013-12-31 21:33:00 +01:00
|
|
|
|
2014-03-15 19:06:23 +01:00
|
|
|
events.on( 'player.PlayerJoinEvent', function( event ) {
|
2014-12-22 16:23:06 +01:00
|
|
|
if ( isOp(event.player) ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( event.player, 'Welcome to ' + __plugin);
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2013-12-31 21:33:00 +01:00
|
|
|
});
|
|
|
|
|
2014-04-26 21:20:10 +02:00
|
|
|
Update: Since version 2.0.8 the above code can be replaced by the more succinct:
|
|
|
|
|
|
|
|
events.playerJoin( function( event ) {
|
|
|
|
if ( event.player.op ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( event.player, 'Welcome to ' + __plugin);
|
2014-04-26 21:20:10 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-12-31 21:33:00 +01:00
|
|
|
***/
|
2014-11-08 07:17:57 +01:00
|
|
|
// wph 20140927 - event handler registration differs depending on framework.
|
2014-09-30 00:42:41 +02:00
|
|
|
|
2014-11-08 07:17:57 +01:00
|
|
|
function onJoin( event ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
if ( isOp(event.player) ) {
|
|
|
|
echo( event.player, 'Welcome to ' + __plugin );
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2014-11-08 07:17:57 +01:00
|
|
|
}
|
|
|
|
if (__plugin.canary){
|
|
|
|
// canarymod
|
|
|
|
events.connection( onJoin );
|
|
|
|
} else {
|
|
|
|
// bukkit
|
|
|
|
events.playerJoin( onJoin );
|
|
|
|
}
|