2013-02-10 18:53:08 +01:00
|
|
|
/************************************************************************
|
2013-12-28 09:44:40 +01:00
|
|
|
## Utilities Module
|
|
|
|
|
2013-02-10 18:53:08 +01:00
|
|
|
Miscellaneous utility functions and classes to help with programming.
|
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
* locationToString(Location) - returns a [bukkit Location][bkloc] object in string form.
|
2013-02-10 18:53:08 +01:00
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
* player(playerName) - returns the Player object for a named player or `self` if no name is provided.
|
2013-02-10 18:53:08 +01:00
|
|
|
|
2013-12-24 01:17:33 +01:00
|
|
|
* getPlayerPos(playerName) - returns the player's x,y,z and yaw (direction) for a named player
|
|
|
|
or player or `self` if no parameter is provided.
|
|
|
|
|
|
|
|
* getMousePos(playerName) - returns the x,y,z of the current block being targeted by the named player
|
|
|
|
or player or `self` if no paramter is provided.
|
2013-02-10 18:53:08 +01:00
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
[bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html
|
|
|
|
|
2013-12-24 01:17:33 +01:00
|
|
|
***/
|
2013-12-31 19:21:40 +01:00
|
|
|
/************************************************************************
|
|
|
|
### player() function
|
|
|
|
|
|
|
|
The utils.player() function will return a [bukkit Player][bkpl] object
|
|
|
|
with the given name. This function takes a single parameter
|
|
|
|
`playerName` which can be either a String or a [Player][bkpl] object -
|
|
|
|
if it's a Player object, then the same object is returned. If it's a
|
|
|
|
String, then it tries to find the player with that name.
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
* playerName : A String or Player object. If no parameter is provided then player() will try to return the `self` variable . It is strongly recommended to provide a parameter.
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
|
|
|
|
var utils = require('utils');
|
|
|
|
var player = utils.player('walterh');
|
|
|
|
player.sendMessage('Got you!');
|
|
|
|
|
|
|
|
[bkpl]: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/Player.html
|
|
|
|
|
|
|
|
***/
|
|
|
|
var _player = function ( playerName ) {
|
|
|
|
if (typeof playerName == 'undefined'){
|
|
|
|
if (typeof self == 'undefined'){
|
2013-12-24 01:17:33 +01:00
|
|
|
return null;
|
|
|
|
} else {
|
2013-02-10 18:53:08 +01:00
|
|
|
return self;
|
2013-12-24 01:17:33 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-12-31 19:21:40 +01:00
|
|
|
if (typeof playerName == 'string')
|
2013-02-10 18:53:08 +01:00
|
|
|
return org.bukkit.Bukkit.getPlayer(playerName);
|
2013-12-24 01:17:33 +01:00
|
|
|
else
|
|
|
|
return playerName; // assumes it's a player object
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
var _locationToJSON = function(location){
|
|
|
|
return {
|
|
|
|
world: ''+location.world.name,
|
|
|
|
x: location.x,
|
|
|
|
y: location.y,
|
|
|
|
z: location.z,
|
|
|
|
yaw: location.yaw,
|
|
|
|
pitch: location.pitch
|
|
|
|
};
|
|
|
|
};
|
2013-12-24 01:17:33 +01:00
|
|
|
exports.locationToString = function(location){
|
2013-12-31 19:21:40 +01:00
|
|
|
return JSON.stringify(_locationToJSON(location));
|
|
|
|
};
|
|
|
|
exports.locationToJSON = _locationToJSON;
|
|
|
|
|
|
|
|
exports.locationFromJSON = function(json){
|
|
|
|
var world = org.bukkit.Bukkit.getWorld(json.world);
|
|
|
|
return new org.bukkit.Location(world, json.x, json.y , json.z, json.yaw, json.pitch);
|
2013-12-24 01:17:33 +01:00
|
|
|
};
|
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
exports.player = _player;
|
|
|
|
exports.getPlayerObject = function(player){
|
|
|
|
console.warn('utils.getPlayerObject() is deprecated. Use utils.player() instead.');
|
|
|
|
return _player(player);
|
|
|
|
};
|
2013-12-24 01:17:33 +01:00
|
|
|
|
|
|
|
exports.getPlayerPos = function( player ) {
|
2013-12-31 19:21:40 +01:00
|
|
|
player = _player(player);
|
|
|
|
if (player){
|
|
|
|
if (player instanceof org.bukkit.command.BlockCommandSender)
|
|
|
|
return player.block.location;
|
|
|
|
else
|
|
|
|
return player.location;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return null;
|
2013-12-24 01:17:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getMousePos = function (player) {
|
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
player = _player(player);
|
2013-12-24 01:17:33 +01:00
|
|
|
if (!player)
|
|
|
|
return null;
|
|
|
|
// player might be CONSOLE or a CommandBlock
|
|
|
|
if (!player.getTargetBlock)
|
|
|
|
return null;
|
|
|
|
var targetedBlock = player.getTargetBlock(null,5);
|
|
|
|
if (targetedBlock == null || targetedBlock.isEmpty()){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return targetedBlock.location;
|
|
|
|
};
|
2013-02-17 23:29:41 +01:00
|
|
|
/************************************************************************
|
2013-12-28 09:44:40 +01:00
|
|
|
### foreach() function
|
|
|
|
|
2013-02-17 23:29:41 +01:00
|
|
|
The utils.foreach() function is a utility function for iterating over
|
2013-03-17 12:57:48 +01:00
|
|
|
an array of objects (or a java.util.Collection of objects) and processing each object in turn. Where
|
2013-02-17 23:29:41 +01:00
|
|
|
utils.foreach() differs from other similar functions found in
|
|
|
|
javascript libraries, is that utils.foreach can process the array
|
|
|
|
immediately or can process it *nicely* by processing one item at a
|
|
|
|
time then delaying processing of the next item for a given number of
|
|
|
|
server ticks (there are 20 ticks per second on the minecraft main
|
|
|
|
thread). This method relies on Bukkit's [org.bukkit.scheduler][sched]
|
|
|
|
package for scheduling processing of arrays.
|
|
|
|
|
|
|
|
[sched]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/package-summary.html
|
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Parameters
|
2013-02-17 23:29:41 +01:00
|
|
|
|
2013-03-17 12:57:48 +01:00
|
|
|
* array : The array to be processed - It can be a javascript array, a java array or java.util.Collection
|
2013-02-17 23:29:41 +01:00
|
|
|
* callback : The function to be called to process each item in the
|
|
|
|
array. The callback function should have the following signature
|
|
|
|
`callback(item, index, object, array)`. That is the callback will
|
|
|
|
be called with the following parameters....
|
|
|
|
|
|
|
|
- item : The item in the array
|
|
|
|
- index : The index at which the item can be found in the array.
|
|
|
|
- object : Additional (optional) information passed into the foreach method.
|
|
|
|
- array : The entire array.
|
|
|
|
|
|
|
|
* object (optional) : An object which may be used by the callback.
|
|
|
|
* delay (optional, numeric) : If a delay is specified (in ticks - 20
|
|
|
|
ticks = 1 second), then the processing will be scheduled so that
|
|
|
|
each item will be processed in turn with a delay between the completion of each
|
|
|
|
item and the start of the next. This is recommended for big builds (say 200 x 200 x 200
|
|
|
|
blocks) or any CPU-intensive process.
|
|
|
|
* onDone (optional, function) : A function to be executed when all processing
|
|
|
|
is complete. This parameter is only used when the processing is delayed. (It's optional even if a
|
|
|
|
delay parameter is supplied).
|
|
|
|
|
|
|
|
If called with a delay parameter then foreach() will return
|
|
|
|
immediately after processing just the first item in the array (all
|
|
|
|
subsequent items are processed later). If your code relies on the
|
|
|
|
completion of the array processing, then provide an `onDone` parameter
|
|
|
|
and put the code there.
|
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Example
|
|
|
|
|
2013-02-17 23:29:41 +01:00
|
|
|
The following example illustrates how to use foreach for immediate processing of an array...
|
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
var utils = require('utils');
|
2013-12-31 19:21:40 +01:00
|
|
|
var players = ['moe', 'larry', 'curly'];
|
2013-08-03 14:50:06 +02:00
|
|
|
utils.foreach (players, function(item){
|
2013-12-31 19:21:40 +01:00
|
|
|
server.getPlayer(item).sendMessage('Hi ' + item);
|
2013-02-17 23:29:41 +01:00
|
|
|
});
|
2013-08-03 14:50:06 +02:00
|
|
|
|
|
|
|
... 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...
|
|
|
|
|
|
|
|
utils.foreach( server.onlinePlayers, function(player){
|
2013-12-31 19:21:40 +01:00
|
|
|
player.chat('Hello!');
|
2013-08-03 14:50:06 +02:00
|
|
|
});
|
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
... the above code sends a 'Hello!' to every online player.
|
2013-08-03 14:50:06 +02:00
|
|
|
|
2013-02-17 23:29:41 +01:00
|
|
|
The following example is a more complex use case - The need to build an enormous structure
|
|
|
|
without hogging CPU usage...
|
|
|
|
|
|
|
|
// build a structure 200 wide x 200 tall x 200 long
|
|
|
|
// (That's 8 Million Blocks - enough to tax any machine!)
|
2013-12-28 09:44:40 +01:00
|
|
|
var utils = require('utils');
|
2013-02-17 23:29:41 +01:00
|
|
|
|
|
|
|
var a = [];
|
|
|
|
a.length = 200;
|
|
|
|
var drone = new Drone();
|
|
|
|
var processItem = function(item, index, object, array){
|
|
|
|
// build a box 200 wide by 200 long then move up
|
|
|
|
drone.box(blocks.wood, 200, 1, 200).up();
|
|
|
|
};
|
|
|
|
// by the time the job's done 'self' might be someone else
|
|
|
|
// assume this code is within a function/closure
|
|
|
|
var player = self;
|
|
|
|
var onDone = function(){
|
2013-12-31 19:21:40 +01:00
|
|
|
player.sendMessage('Job Done!');
|
2013-02-17 23:29:41 +01:00
|
|
|
};
|
2013-08-03 14:50:06 +02:00
|
|
|
utils.foreach (a, processItem, null, 10, onDone);
|
2013-02-17 23:29:41 +01:00
|
|
|
|
|
|
|
***/
|
2013-12-24 01:17:33 +01:00
|
|
|
var _foreach = function(array, callback, object, delay, onCompletion) {
|
|
|
|
if (array instanceof java.util.Collection)
|
|
|
|
array = array.toArray();
|
|
|
|
var i = 0;
|
|
|
|
var len = array.length;
|
|
|
|
if (delay){
|
|
|
|
var next = function(){ callback(array[i],i,object,array); i++;};
|
|
|
|
var hasNext = function(){return i < len;};
|
2013-12-30 02:07:41 +01:00
|
|
|
_nicely(next,hasNext,onCompletion,delay);
|
2013-12-24 01:17:33 +01:00
|
|
|
}else{
|
|
|
|
for (;i < len; i++){
|
|
|
|
callback(array[i],i,object,array);
|
2013-02-17 23:29:41 +01:00
|
|
|
}
|
2013-12-24 01:17:33 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
exports.foreach = _foreach;
|
2013-02-17 23:29:41 +01:00
|
|
|
/************************************************************************
|
2013-12-28 09:44:40 +01:00
|
|
|
### utils.nicely() function
|
|
|
|
|
2013-02-17 23:29:41 +01:00
|
|
|
The utils.nicely() function is for performing processing using the
|
|
|
|
[org.bukkit.scheduler][sched] package/API. utils.nicely() lets you
|
|
|
|
process with a specified delay between the completion of each `next()`
|
|
|
|
function and the start of the next `next()` function.
|
|
|
|
`utils.nicely()` is a recursive function - that is - it calls itself
|
|
|
|
(schedules itself actually) repeatedly until `hasNext` returns false.
|
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Parameters
|
2013-02-17 23:29:41 +01:00
|
|
|
|
|
|
|
* next : A function which will be called if processing is to be done.
|
|
|
|
* hasNext : A function which is called to determine if the `next`
|
|
|
|
callback should be invoked. This should return a boolean value -
|
|
|
|
true if the `next` function should be called (processing is not
|
|
|
|
complete), false otherwise.
|
|
|
|
* onDone : A function which is to be called when all processing is complete (hasNext returned false).
|
|
|
|
* delay : The delay (in server ticks - 20 per second) between each call.
|
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Example
|
|
|
|
|
2013-02-17 23:29:41 +01:00
|
|
|
See the source code to utils.foreach for an example of how utils.nicely is used.
|
|
|
|
|
|
|
|
***/
|
2013-12-30 02:07:41 +01:00
|
|
|
var _nicely = function(next, hasNext, onDone, delay){
|
2013-12-24 01:17:33 +01:00
|
|
|
if (hasNext()){
|
|
|
|
next();
|
|
|
|
server.scheduler.runTaskLater(__plugin,function(){
|
2013-12-30 02:07:41 +01:00
|
|
|
_nicely(next,hasNext,onDone,delay);
|
2013-12-24 01:17:33 +01:00
|
|
|
},delay);
|
|
|
|
}else{
|
|
|
|
if (onDone)
|
|
|
|
onDone();
|
|
|
|
}
|
|
|
|
};
|
2013-12-30 02:07:41 +01:00
|
|
|
exports.nicely = _nicely;
|
2013-08-03 14:14:09 +02:00
|
|
|
/************************************************************************
|
2013-12-28 09:44:40 +01:00
|
|
|
### utils.at() function
|
|
|
|
|
2013-08-03 14:14:09 +02:00
|
|
|
The utils.at() function will perform a given task at a given time every
|
|
|
|
(minecraft) day.
|
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Parameters
|
2013-08-03 14:14:09 +02:00
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
* time24hr : The time in 24hr form - e.g. 9:30 in the morning is '09:30' while
|
|
|
|
9:30 pm is '21:30', midnight is '00:00' and midday is '12:00'
|
2013-08-03 14:14:09 +02:00
|
|
|
* callback : A javascript function which will be invoked at the given time.
|
2013-12-30 02:07:41 +01:00
|
|
|
* worlds : (optional) An array of worlds. Each world has its own clock. If no array of worlds is specified, all the server's worlds are used.
|
2013-08-03 14:14:09 +02:00
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
#### Example
|
2013-08-03 14:14:09 +02:00
|
|
|
|
|
|
|
To warn players when night is approaching...
|
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
var utils = require('utils');
|
2013-12-24 01:17:33 +01:00
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
utils.at( '19:00', function() {
|
2013-08-08 09:02:27 +02:00
|
|
|
|
2013-08-03 14:14:09 +02:00
|
|
|
utils.foreach( server.onlinePlayers, function(player){
|
2013-12-31 19:21:40 +01:00
|
|
|
player.chat('The night is dark and full of terrors!');
|
2013-08-03 14:14:09 +02:00
|
|
|
});
|
2013-08-08 09:02:27 +02:00
|
|
|
|
2013-12-30 02:07:41 +01:00
|
|
|
});
|
2013-08-03 14:14:09 +02:00
|
|
|
|
|
|
|
***/
|
2013-12-30 02:07:41 +01:00
|
|
|
exports.at = function(time24hr, callback, worlds) {
|
2013-12-24 01:17:33 +01:00
|
|
|
var forever = function(){ return true;};
|
2013-12-31 19:21:40 +01:00
|
|
|
var timeParts = time24hr.split(':');
|
2013-12-24 01:17:33 +01:00
|
|
|
var hrs = ((timeParts[0] * 1000) + 18000) % 24000;
|
|
|
|
var mins;
|
|
|
|
if (timeParts.length > 1)
|
|
|
|
mins = (timeParts[1] / 60) * 1000;
|
|
|
|
|
|
|
|
var timeMc = hrs + mins;
|
2013-12-31 19:21:40 +01:00
|
|
|
if (typeof worlds == 'undefined'){
|
2013-12-30 02:07:41 +01:00
|
|
|
worlds = server.worlds;
|
2013-12-24 01:17:33 +01:00
|
|
|
}
|
2013-12-30 02:07:41 +01:00
|
|
|
_nicely(function(){
|
|
|
|
_foreach (worlds, function (world){
|
|
|
|
var time = world.getTime();
|
|
|
|
var diff = timeMc - time;
|
|
|
|
if (diff > 0 && diff < 100){
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
2013-12-24 01:17:33 +01:00
|
|
|
},forever, null, 100);
|
2013-02-10 18:53:08 +01:00
|
|
|
};
|
2013-12-24 01:17:33 +01:00
|
|
|
|
2013-12-28 09:44:40 +01:00
|
|
|
/************************************************************************
|
|
|
|
### utils.find() function
|
|
|
|
|
|
|
|
The utils.find() function will return a list of all files starting at
|
|
|
|
a given directory and recursiving trawling all sub-directories.
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
* dir : The starting path. Must be a string.
|
|
|
|
* filter : (optional) A [FilenameFilter][fnfltr] object to return only files matching a given pattern.
|
|
|
|
|
|
|
|
[fnfltr]: http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
|
|
|
|
var utils = require('utils');
|
|
|
|
var jsFiles = utils.find('./', function(dir,name){
|
|
|
|
return name.match(/\.js$/);
|
|
|
|
});
|
|
|
|
|
|
|
|
***/
|
2013-12-24 01:17:33 +01:00
|
|
|
exports.find = function( dir , filter){
|
|
|
|
var result = [];
|
|
|
|
var recurse = function(dir, store){
|
|
|
|
var files, dirfile = new java.io.File(dir);
|
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
if (typeof filter == 'undefined')
|
2013-12-24 01:17:33 +01:00
|
|
|
files = dirfile.list();
|
|
|
|
else
|
|
|
|
files = dirfile.list(filter);
|
|
|
|
|
|
|
|
_foreach(files, function (file){
|
|
|
|
file = new java.io.File(dir + '/' + file);
|
|
|
|
if (file.isDirectory()){
|
|
|
|
recurse(file.canonicalPath, store);
|
|
|
|
}else{
|
|
|
|
store.push(file.canonicalPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
recurse(dir,result);
|
|
|
|
return result;
|
|
|
|
}
|