added syntax-highlighting to code samples

This commit is contained in:
walterhiggins 2014-02-04 21:36:00 +00:00
parent fe62f61883
commit f1925efd87
7 changed files with 614 additions and 483 deletions

View file

@ -156,21 +156,23 @@ loads the module circle.js in the same directory.
The contents of foo.js:
```javascript
var circle = require('./circle.js');
console.log( 'The area of a circle of radius 4 is '
+ circle.area(4));
```
The contents of circle.js:
```javascript
var PI = Math.PI;
exports.area = function (r) {
return PI * r * r;
};
exports.circumference = function (r) {
return 2 * PI * r;
};
```
The module circle.js has exported the functions area() and
circumference(). To add functions and objects to the root of your
@ -210,9 +212,11 @@ module in the `plugins` directory exports becomes a global
variable. For example, if you have a module greeting.js in the plugins
directory....
```javascript
exports.greet = function(player) {
player.sendMessage('Hello ' + player.name);
};
```
... then `greet` becomes a global function and can be used at the
in-game (or server) command prompt like so...
@ -400,10 +404,12 @@ restored using the `scload()` function.
#### Example
```javascript
var myObject = { name: 'John Doe',
aliases: ['John Ray', 'John Mee'],
date_of_birth: '1982/01/31' };
scsave(myObject, 'johndoe.json');
```
##### johndoe.json contents...
@ -490,6 +496,7 @@ If Node.js supports setTimeout() then it's probably good for ScriptCraft to supp
#### Example
```javascript
//
// start a storm in 5 seconds
//
@ -497,6 +504,7 @@ If Node.js supports setTimeout() then it's probably good for ScriptCraft to supp
var world = server.worlds.get(0);
world.setStorm(true);
}, 5000);
```
### clearTimeout() function
@ -566,23 +574,29 @@ For example imagine you have 3 files program.js, inc.js and math.js ...
### math.js
```javascript
exports.add = function(a,b){
return a + b;
}
```
### inc.js
```javascript
var math = require('./math');
exports.increment = function(n){
return math.add(n, 1);
}
```
### program.js
```javascript
var inc = require('./inc').increment;
var a = 7;
a = inc(a);
print(a);
```
You can see from the above sample code that programs can use modules
and modules themeselves can use other modules. Modules have full
@ -857,18 +871,23 @@ present in the CraftBukkit classpath. To use this module, you should
craftbukkit-sc-mqtt.bat and edit it to include the following
command...
```sh
java -classpath sc-mqtt.jar;craftbukit.jar org.bukkit.craftbukkit.Main
```
If you're using Mac OS, create a new craftbukkit-sc-mqtt.command
file and edit it (using TextWrangler or another text editor) ...
```sh
java -classpath sc-mqtt.jar:craftbukkit.jar org.bukit.craftbukkit.Main
```
4. Execute the craftbukkit-sc-mqtt batch file / command file to start
Craftbukkit. You can now begin using this module to send and receive
messages to/from a Net-enabled Arduino or any other device which uses
the [MQTT protocol][mqtt]
```javascript
var mqtt = require('sc-mqtt');
// create a new client
var client = mqtt.client( 'tcp://localhost:1883', 'uniqueClientId' );
@ -883,6 +902,8 @@ present in the CraftBukkit classpath. To use this module, you should
console.log( 'Message arrived: topic=' + topic + ', message=' + message );
});
```
The `sc-mqtt` module provides a very simple minimal wrapper around the
[Eclipse Paho MQTT Version 3 Client][pahodocs] java-based MQTT
library.
@ -1042,6 +1063,7 @@ String, then it tries to find the player with that name.
#### Example
```javascript
var utils = require('utils');
var name = 'walterh';
var player = utils.player(name);
@ -1050,6 +1072,7 @@ String, then it tries to find the player with that name.
} else {
console.log('No player named ' + name);
}
```
[bkpl]: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/Player.html
[bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html
@ -1088,10 +1111,12 @@ keys in a lookup table.
#### Example
```javascript
var utils = require('utils');
...
var key = utils.locationToString(player.location);
lookupTable[key] = player.name;
```
### utils.locationFromJSON() function
@ -1132,12 +1157,14 @@ is the location of the block the player is looking at (targeting).
The following code will strike lightning at the location the player is looking at...
```javascript
var utils = require('utils');
var playerName = 'walterh';
var targetPos = utils.getMousePos(playerName);
if (targetPos){
targetPos.world.strikeLightning(targetPos);
}
```
### utils.foreach() function
@ -1186,24 +1213,29 @@ and put the code there.
The following example illustrates how to use foreach for immediate processing of an array...
```javascript
var utils = require('utils');
var players = ['moe', 'larry', 'curly'];
utils.foreach (players, function(item){
server.getPlayer(item).sendMessage('Hi ' + item);
});
```
... The `utils.foreach()` function can work with Arrays or any Java-style collection. This is important
because many objects in the Bukkit API use Java-style collections...
```javascript
utils.foreach( server.onlinePlayers, function(player){
player.chat('Hello!');
});
```
... the above code sends a 'Hello!' to every online player.
The following example is a more complex use case - The need to build an enormous structure
without hogging CPU usage...
```javascript
// build a structure 200 wide x 200 tall x 200 long
// (That's 8 Million Blocks - enough to tax any machine!)
var utils = require('utils');
@ -1222,6 +1254,7 @@ without hogging CPU usage...
player.sendMessage('Job Done!');
};
utils.foreach (a, processItem, null, 10, onDone);
```
### utils.nicely() function
@ -1262,6 +1295,7 @@ The utils.at() function will perform a given task at a given time every
To warn players when night is approaching...
```javascript
var utils = require('utils');
utils.at( '19:00', function() {
@ -1271,6 +1305,7 @@ To warn players when night is approaching...
});
});
```
### utils.find() function
@ -1286,10 +1321,12 @@ a given directory and recursiving trawling all sub-directories.
#### Example
```javascript
var utils = require('utils');
var jsFiles = utils.find('./', function(dir,name){
return name.match(/\.js$/);
});
```
## Drone Plugin

View file

@ -474,9 +474,11 @@ object can do, let's use that knowledge to create a Minecraft Mod!
Once you've installed Notepad++, Launch it, create a new file and type the following...
```javascript
exports.greet = function( player ) {
player.sendMessage('Hi ' + player.name);
}
```
... then save the file in a new directory
`craftbukkit/plugins/scriptcraft/plugins/{your_name}` (replace
@ -514,12 +516,14 @@ one or more functions, objects or variables. For example...
#### thrower.js
```javascript
exports.egg = function(player){
player.throwEgg();
}
exports.snowball = function(player){
player.throwSnowball();
}
```
... is a plugin which provides 2 javascript functions called `egg()`
and `snowball()` which can be invoked from the in-game prompt like
@ -536,9 +540,11 @@ differently each time it is called.
Change the `greet()` function so that it looks like this...
```javascript
exports.greet = function ( greeting , player) {
player.sendMessage( greeting + player.name );
}
```
... Save your greet.js file and issue the `/js refresh()` command in
minecraft. Now enter the following command in Minecraft...
@ -696,16 +702,20 @@ At the in-game command prompt type the following then hit Enter...
statements on a single line at the in-game command prompt but the
statements could be written like this...
```javascript
var players = server.onlinePlayers;
for (var i = 0; i < players.length; i++) {
var player = players[i];
var player;
var i;
for ( i = 0; i < players.length; i++ ) {
player = players[i];
player.sendMessage( 'Hi!' );
}
```
... On the first line, a new variable `players` is created from the
server object's onlinePlayers property. `players` is more concise and
easier to type than the long-winded `server.onlinePlayers`. On the
second line, the for loop is declared, a counter variable `i` is set
fourth line, the for loop is declared, a counter variable `i` is set
to 0 (zero - arrays in javascript start at 0 not 1) and each time
around the loop is tested to see if it's less than the number of
players online. At the end of each run around the loop the `i`
@ -729,13 +739,17 @@ function. Open the `hi.js` file you created earlier (using NotePad++ ,
TextWrangler or your editor of choice) and add the following code at
the bottom of the file...
```javascript
exports.hiAll = function () {
var players = server.onlinePlayers;
for (var i = 0; i < players.length; i++) {
var player = players[i];
var players = server.onlinePlayers,
player,
i;
for ( i = 0; i < players.length; i++) {
player = players[i];
player.sendMessage( 'Hi!' );
}
}
```
... save the file, at the in-game command prompt type `reload` and
then type `/js hiAll()`. This will send the message `Hi!` to all of
@ -749,11 +763,13 @@ use `for` loops and Arrays to get things done.
Another way to repeat things over and over is to use a `while`
loop. The following `while` loop counts to 100...
```javascript
var i = 1;
while (i <= 100){
console.log( i );
i = i + 1;
}
```
A `while` loop will repeat until its condition is `false` - the
condition in the above example is `i <= 100` so while i is less than
@ -775,12 +791,14 @@ Just like `for` loops, `while` loops can be also be used to loop
through arrays. The following loop prints out all of the players on
the server...
```javascript
var players = server.onlinePlayers;
var i = 0;
while ( i < players.length ) {
console.log( players[i] );
i = i + 1;
}
```
... whether you chose to use a `for` loop or a `while` loop is largely
a matter of personal taste, `for` loops are more commonly used with
@ -814,31 +832,30 @@ above example uses a named function which already exists ( `console.log` ),
you can also create new functions on-the-fly and pass them to the
utils.foreach() function...
```javascript
/*
give every player the ability to fly.
*/
var utils = require('utils');
utils.foreach( server.onlinePlayers,
function (player) {
utils.foreach( server.onlinePlayers, function( player ) {
player.setAllowFlight(true);
}
);
} );
```
... Another example, this time each player will hear a Cat's Meow...
```javascript
/*
Play a Cat's Meow sound for each player.
*/
var utils = require('utils');
utils.foreach( server.onlinePlayers,
function (player) {
utils.foreach( server.onlinePlayers, function( player ) {
player.playSound(player.location,
org.bukkit.Sound.CAT_MEOW,
1,
1);
}
);
} );
```
### Exercise
Try changing the above function so that different sounds are played
@ -873,6 +890,7 @@ loops come in. Open your favorite text editor and create a new file in
your scriptcraft/plugins/{your-name} directory, name the file `myskyscraper.js`, then
type the following...
```javascript
var myskyscraper = function(floors) {
if (typeof floors == 'undefined'){
floors = 10;
@ -887,9 +905,9 @@ type the following...
}
return this.move('myskyscraper'); // return to where we started
};
var Drone = require('../drone/drone.js').Drone;
Drone.extend('myskyscraper',myskyscraper);
```
... so this takes a little explaining. First I create a new function
called myskyscraper that will take a single parameter `floors` so that
@ -969,17 +987,15 @@ flying or not? This is where the `if - else` construct comes in handy.
Open your favorite editor and type the following code into a new file
in your scriptcraft/plugins directory...
function flightStatus(player)
{
if ( player.flying )
{
```javascript
function flightStatus( player ) {
if ( player.flying ) {
player.sendMessage( 'Hey, You are flying!' );
}
else
{
} else {
player.sendMessage( 'You are not flying.' );
}
}
```
... now type `/reload` at the in-game prompt then type `/js
flightStatus(self)` and an appropriate message will appear based on
@ -1009,10 +1025,12 @@ of event occurs, it's probably best to illustrate this by example. The
following code sends a message to any player who breaks a block in the
game...
```javascript
events.on('block.BlockBreakEvent', function ( listener, event ) {
var breaker = event.player;
breaker.sendMessage('You broke a block');
} );
```
The `events.on()` function is how you *register* a function which you
want to be called whenever a particular type of event occurs. In the
@ -1063,11 +1081,13 @@ just specify the fully qualified class name instead. E.g. ...
If you want an event handler to only execute once, you can remove the handler like this...
```javascript
events.on('block.BlockBreakEvent', function( listener, evt ) {
var breaker = evt.player;
breaker.sendMessage('You broke a block');
evt.handlers.unregister( listener );
} );
```
The `evt.handlers.unregister( listener );` statement will remove this
function from the list of listeners for this event.
@ -1121,27 +1141,34 @@ the name column until I find 'jane' then look *across* to get her
score. In Javascript, an object which stored such a table would look
like this...
```javascript
var scoreboard = {
walter: 5,
tom: 6,
jane: 8,
bart: 7
};
```
... and if I wanted to write a function which took a player name as a
parameter and returned their score, I'd do it like this...
```javascript
function getScore(player){
return scoreboard[ player ];
}
```
... I might call such a function like this...
```javascript
var janesScore = getScore('jane'); // returns 8
```
... putting it all together, a hypothetical scoreboard.js mdoule might
look something like this...
```javascript
var utils = require('utils');
var scores = {};
@ -1162,6 +1189,7 @@ look something like this...
exports.getScore = function(name){
return scores[name];
};
```
## Counting block break events for each player
@ -1170,6 +1198,7 @@ keep a count of how many blocks each player has broken ...
#### block-break-counter.js
```javascript
var breaks = {};
// every time a player joins the game reset their block-break-count to 0
events.on('player.PlayerJoinEvent', function(listener, event){
@ -1182,8 +1211,8 @@ keep a count of how many blocks each player has broken ...
breaks[breaker.name] = breakCount;
breaker.sendMessage('You broke ' + breakCount + ' blocks');
});
```
With a little more work, you could turn this into a game where players
compete against each other to break as many blocks as possible within

View file

@ -439,9 +439,11 @@ object can do, let's use that knowledge to create a Minecraft Mod!
Once you've installed Notepad++, Launch it, create a new file and type the following...
```javascript
exports.greet = function( player ) {
player.sendMessage('Hi ' + player.name);
}
```
... then save the file in a new directory
`craftbukkit/plugins/scriptcraft/plugins/{your_name}` (replace
@ -479,12 +481,14 @@ one or more functions, objects or variables. For example...
#### thrower.js
```javascript
exports.egg = function(player){
player.throwEgg();
}
exports.snowball = function(player){
player.throwSnowball();
}
```
... is a plugin which provides 2 javascript functions called `egg()`
and `snowball()` which can be invoked from the in-game prompt like
@ -501,9 +505,11 @@ differently each time it is called.
Change the `greet()` function so that it looks like this...
```javascript
exports.greet = function ( greeting , player) {
player.sendMessage( greeting + player.name );
}
```
... Save your greet.js file and issue the `/js refresh()` command in
minecraft. Now enter the following command in Minecraft...
@ -661,16 +667,20 @@ At the in-game command prompt type the following then hit Enter...
statements on a single line at the in-game command prompt but the
statements could be written like this...
```javascript
var players = server.onlinePlayers;
for (var i = 0; i < players.length; i++) {
var player = players[i];
var player;
var i;
for ( i = 0; i < players.length; i++ ) {
player = players[i];
player.sendMessage( 'Hi!' );
}
```
... On the first line, a new variable `players` is created from the
server object's onlinePlayers property. `players` is more concise and
easier to type than the long-winded `server.onlinePlayers`. On the
second line, the for loop is declared, a counter variable `i` is set
fourth line, the for loop is declared, a counter variable `i` is set
to 0 (zero - arrays in javascript start at 0 not 1) and each time
around the loop is tested to see if it's less than the number of
players online. At the end of each run around the loop the `i`
@ -694,13 +704,17 @@ function. Open the `hi.js` file you created earlier (using NotePad++ ,
TextWrangler or your editor of choice) and add the following code at
the bottom of the file...
```javascript
exports.hiAll = function () {
var players = server.onlinePlayers;
for (var i = 0; i < players.length; i++) {
var player = players[i];
var players = server.onlinePlayers,
player,
i;
for ( i = 0; i < players.length; i++) {
player = players[i];
player.sendMessage( 'Hi!' );
}
}
```
... save the file, at the in-game command prompt type `reload` and
then type `/js hiAll()`. This will send the message `Hi!` to all of
@ -714,11 +728,13 @@ use `for` loops and Arrays to get things done.
Another way to repeat things over and over is to use a `while`
loop. The following `while` loop counts to 100...
```javascript
var i = 1;
while (i <= 100){
console.log( i );
i = i + 1;
}
```
A `while` loop will repeat until its condition is `false` - the
condition in the above example is `i <= 100` so while i is less than
@ -740,12 +756,14 @@ Just like `for` loops, `while` loops can be also be used to loop
through arrays. The following loop prints out all of the players on
the server...
```javascript
var players = server.onlinePlayers;
var i = 0;
while ( i < players.length ) {
console.log( players[i] );
i = i + 1;
}
```
... whether you chose to use a `for` loop or a `while` loop is largely
a matter of personal taste, `for` loops are more commonly used with
@ -779,31 +797,30 @@ above example uses a named function which already exists ( `console.log` ),
you can also create new functions on-the-fly and pass them to the
utils.foreach() function...
```javascript
/*
give every player the ability to fly.
*/
var utils = require('utils');
utils.foreach( server.onlinePlayers,
function (player) {
utils.foreach( server.onlinePlayers, function( player ) {
player.setAllowFlight(true);
}
);
} );
```
... Another example, this time each player will hear a Cat's Meow...
```javascript
/*
Play a Cat's Meow sound for each player.
*/
var utils = require('utils');
utils.foreach( server.onlinePlayers,
function (player) {
utils.foreach( server.onlinePlayers, function( player ) {
player.playSound(player.location,
org.bukkit.Sound.CAT_MEOW,
1,
1);
}
);
} );
```
### Exercise
Try changing the above function so that different sounds are played
@ -838,6 +855,7 @@ loops come in. Open your favorite text editor and create a new file in
your scriptcraft/plugins/{your-name} directory, name the file `myskyscraper.js`, then
type the following...
```javascript
var myskyscraper = function(floors) {
if (typeof floors == 'undefined'){
floors = 10;
@ -852,9 +870,9 @@ type the following...
}
return this.move('myskyscraper'); // return to where we started
};
var Drone = require('../drone/drone.js').Drone;
Drone.extend('myskyscraper',myskyscraper);
```
... so this takes a little explaining. First I create a new function
called myskyscraper that will take a single parameter `floors` so that
@ -934,17 +952,15 @@ flying or not? This is where the `if - else` construct comes in handy.
Open your favorite editor and type the following code into a new file
in your scriptcraft/plugins directory...
function flightStatus(player)
{
if ( player.flying )
{
```javascript
function flightStatus( player ) {
if ( player.flying ) {
player.sendMessage( 'Hey, You are flying!' );
}
else
{
} else {
player.sendMessage( 'You are not flying.' );
}
}
```
... now type `/reload` at the in-game prompt then type `/js
flightStatus(self)` and an appropriate message will appear based on
@ -974,10 +990,12 @@ of event occurs, it's probably best to illustrate this by example. The
following code sends a message to any player who breaks a block in the
game...
```javascript
events.on('block.BlockBreakEvent', function ( listener, event ) {
var breaker = event.player;
breaker.sendMessage('You broke a block');
} );
```
The `events.on()` function is how you *register* a function which you
want to be called whenever a particular type of event occurs. In the
@ -1028,11 +1046,13 @@ just specify the fully qualified class name instead. E.g. ...
If you want an event handler to only execute once, you can remove the handler like this...
```javascript
events.on('block.BlockBreakEvent', function( listener, evt ) {
var breaker = evt.player;
breaker.sendMessage('You broke a block');
evt.handlers.unregister( listener );
} );
```
The `evt.handlers.unregister( listener );` statement will remove this
function from the list of listeners for this event.
@ -1086,27 +1106,34 @@ the name column until I find 'jane' then look *across* to get her
score. In Javascript, an object which stored such a table would look
like this...
```javascript
var scoreboard = {
walter: 5,
tom: 6,
jane: 8,
bart: 7
};
```
... and if I wanted to write a function which took a player name as a
parameter and returned their score, I'd do it like this...
```javascript
function getScore(player){
return scoreboard[ player ];
}
```
... I might call such a function like this...
```javascript
var janesScore = getScore('jane'); // returns 8
```
... putting it all together, a hypothetical scoreboard.js mdoule might
look something like this...
```javascript
var utils = require('utils');
var scores = {};
@ -1127,6 +1154,7 @@ look something like this...
exports.getScore = function(name){
return scores[name];
};
```
## Counting block break events for each player
@ -1135,6 +1163,7 @@ keep a count of how many blocks each player has broken ...
#### block-break-counter.js
```javascript
var breaks = {};
// every time a player joins the game reset their block-break-count to 0
events.on('player.PlayerJoinEvent', function(listener, event){
@ -1147,8 +1176,8 @@ keep a count of how many blocks each player has broken ...
breaks[breaker.name] = breakCount;
breaker.sendMessage('You broke ' + breakCount + ' blocks');
});
```
With a little more work, you could turn this into a game where players
compete against each other to break as many blocks as possible within

View file

@ -18,23 +18,29 @@ For example imagine you have 3 files program.js, inc.js and math.js ...
### math.js
```javascript
exports.add = function(a,b){
return a + b;
}
```
### inc.js
```javascript
var math = require('./math');
exports.increment = function(n){
return math.add(n, 1);
}
```
### program.js
```javascript
var inc = require('./inc').increment;
var a = 7;
a = inc(a);
print(a);
```
You can see from the above sample code that programs can use modules
and modules themeselves can use other modules. Modules have full

View file

@ -12,21 +12,23 @@ loads the module circle.js in the same directory.
The contents of foo.js:
```javascript
var circle = require('./circle.js');
console.log( 'The area of a circle of radius 4 is '
+ circle.area(4));
```
The contents of circle.js:
```javascript
var PI = Math.PI;
exports.area = function (r) {
return PI * r * r;
};
exports.circumference = function (r) {
return 2 * PI * r;
};
```
The module circle.js has exported the functions area() and
circumference(). To add functions and objects to the root of your
@ -66,9 +68,11 @@ module in the `plugins` directory exports becomes a global
variable. For example, if you have a module greeting.js in the plugins
directory....
```javascript
exports.greet = function(player) {
player.sendMessage('Hello ' + player.name);
};
```
... then `greet` becomes a global function and can be used at the
in-game (or server) command prompt like so...
@ -256,10 +260,12 @@ restored using the `scload()` function.
#### Example
```javascript
var myObject = { name: 'John Doe',
aliases: ['John Ray', 'John Mee'],
date_of_birth: '1982/01/31' };
scsave(myObject, 'johndoe.json');
```
##### johndoe.json contents...
@ -346,6 +352,7 @@ If Node.js supports setTimeout() then it's probably good for ScriptCraft to supp
#### Example
```javascript
//
// start a storm in 5 seconds
//
@ -353,6 +360,7 @@ If Node.js supports setTimeout() then it's probably good for ScriptCraft to supp
var world = server.worlds.get(0);
world.setStorm(true);
}, 5000);
```
### clearTimeout() function
@ -477,7 +485,6 @@ function __onEnable ( __engine, __plugin, __script )
var canonizedFilename = _canonize( file );
if ( file.exists() ) {
parent = file.getParentFile();
reader = new FileReader( file );
br = new BufferedReader( reader );
code = '';

View file

@ -17,18 +17,23 @@ present in the CraftBukkit classpath. To use this module, you should
craftbukkit-sc-mqtt.bat and edit it to include the following
command...
```sh
java -classpath sc-mqtt.jar;craftbukit.jar org.bukkit.craftbukkit.Main
```
If you're using Mac OS, create a new craftbukkit-sc-mqtt.command
file and edit it (using TextWrangler or another text editor) ...
```sh
java -classpath sc-mqtt.jar:craftbukkit.jar org.bukit.craftbukkit.Main
```
4. Execute the craftbukkit-sc-mqtt batch file / command file to start
Craftbukkit. You can now begin using this module to send and receive
messages to/from a Net-enabled Arduino or any other device which uses
the [MQTT protocol][mqtt]
```javascript
var mqtt = require('sc-mqtt');
// create a new client
var client = mqtt.client( 'tcp://localhost:1883', 'uniqueClientId' );
@ -43,6 +48,8 @@ present in the CraftBukkit classpath. To use this module, you should
console.log( 'Message arrived: topic=' + topic + ', message=' + message );
});
```
The `sc-mqtt` module provides a very simple minimal wrapper around the
[Eclipse Paho MQTT Version 3 Client][pahodocs] java-based MQTT
library.

View file

@ -22,6 +22,7 @@ String, then it tries to find the player with that name.
#### Example
```javascript
var utils = require('utils');
var name = 'walterh';
var player = utils.player(name);
@ -30,6 +31,7 @@ String, then it tries to find the player with that name.
} else {
console.log('No player named ' + name);
}
```
[bkpl]: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/Player.html
[bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html
@ -96,10 +98,12 @@ keys in a lookup table.
#### Example
```javascript
var utils = require('utils');
...
var key = utils.locationToString(player.location);
lookupTable[key] = player.name;
```
***/
exports.locationToString = function( location ) {
@ -179,12 +183,14 @@ is the location of the block the player is looking at (targeting).
The following code will strike lightning at the location the player is looking at...
```javascript
var utils = require('utils');
var playerName = 'walterh';
var targetPos = utils.getMousePos(playerName);
if (targetPos){
targetPos.world.strikeLightning(targetPos);
}
```
***/
exports.getMousePos = function( player ) {
@ -251,24 +257,29 @@ and put the code there.
The following example illustrates how to use foreach for immediate processing of an array...
```javascript
var utils = require('utils');
var players = ['moe', 'larry', 'curly'];
utils.foreach (players, function(item){
server.getPlayer(item).sendMessage('Hi ' + item);
});
```
... The `utils.foreach()` function can work with Arrays or any Java-style collection. This is important
because many objects in the Bukkit API use Java-style collections...
```javascript
utils.foreach( server.onlinePlayers, function(player){
player.chat('Hello!');
});
```
... the above code sends a 'Hello!' to every online player.
The following example is a more complex use case - The need to build an enormous structure
without hogging CPU usage...
```javascript
// build a structure 200 wide x 200 tall x 200 long
// (That's 8 Million Blocks - enough to tax any machine!)
var utils = require('utils');
@ -287,6 +298,7 @@ without hogging CPU usage...
player.sendMessage('Job Done!');
};
utils.foreach (a, processItem, null, 10, onDone);
```
***/
var _foreach = function( array, callback, context, delay, onCompletion ) {
@ -366,6 +378,7 @@ The utils.at() function will perform a given task at a given time every
To warn players when night is approaching...
```javascript
var utils = require('utils');
utils.at( '19:00', function() {
@ -375,6 +388,7 @@ To warn players when night is approaching...
});
});
```
***/
exports.at = function( time24hr, callback, worlds ) {
@ -415,10 +429,12 @@ a given directory and recursiving trawling all sub-directories.
#### Example
```javascript
var utils = require('utils');
var jsFiles = utils.find('./', function(dir,name){
return name.match(/\.js$/);
});
```
***/
exports.find = function( dir , filter ) {