2015-01-17 09:45:32 +01:00
|
|
|
/*global __plugin, org, exports, server, setTimeout, Packages*/
|
2014-01-02 19:46:46 +01:00
|
|
|
'use strict';
|
2014-09-30 00:42:41 +02:00
|
|
|
var File = java.io.File;
|
|
|
|
|
|
|
|
if (__plugin.bukkit){
|
|
|
|
var bkBukkit = org.bukkit.Bukkit,
|
|
|
|
bkLocation = org.bukkit.Location,
|
|
|
|
bkBlockCommandSender = org.bukkit.command.BlockCommandSender;
|
|
|
|
}
|
|
|
|
if (__plugin.canary){
|
|
|
|
var Canary = Packages.net.canarymod.Canary;
|
|
|
|
}
|
2013-02-10 18:53:08 +01:00
|
|
|
/************************************************************************
|
2013-12-28 09:44:40 +01:00
|
|
|
## Utilities Module
|
|
|
|
|
2013-12-31 21:12:57 +01:00
|
|
|
The `utils` module is a storehouse for various useful utility
|
|
|
|
functions which can be used by plugin and module authors. It contains
|
|
|
|
miscellaneous utility functions and classes to help with programming.
|
2013-02-10 18:53:08 +01:00
|
|
|
|
2013-12-31 21:12:57 +01:00
|
|
|
### utils.player() function
|
2013-12-31 19:21:40 +01:00
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
The utils.player() function will return a [Player][cmpl] object
|
2013-12-31 19:21:40 +01:00
|
|
|
with the given name. This function takes a single parameter
|
2015-01-17 09:45:32 +01:00
|
|
|
`playerName` which can be either a String or a [Player][cmpl] object -
|
2013-12-31 19:21:40 +01:00
|
|
|
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
|
|
|
|
|
2013-12-31 21:12:57 +01:00
|
|
|
* 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.
|
2013-12-31 19:21:40 +01:00
|
|
|
|
|
|
|
#### Example
|
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
|
|
|
var name = 'walterh';
|
|
|
|
var player = utils.player(name);
|
|
|
|
if ( player ) {
|
2015-01-17 09:45:32 +01:00
|
|
|
echo(player, 'Got ' + name);
|
2014-02-04 22:36:00 +01:00
|
|
|
} else {
|
2015-01-17 09:45:32 +01:00
|
|
|
console.log('No player named ' + name);
|
2014-02-04 22:36:00 +01:00
|
|
|
}
|
|
|
|
```
|
2013-12-31 19:21:40 +01:00
|
|
|
|
|
|
|
[bkpl]: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/Player.html
|
2015-01-17 09:45:32 +01:00
|
|
|
[cmpl]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/entity/living/humanoid/Player.html
|
|
|
|
[cmloc]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/world/position/Location.html
|
2013-12-31 21:12:57 +01:00
|
|
|
[bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html
|
2013-12-31 19:21:40 +01:00
|
|
|
|
|
|
|
***/
|
2015-01-11 01:09:56 +01:00
|
|
|
function _player( playerName ) {
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( typeof playerName == 'undefined' ) {
|
|
|
|
if ( typeof self == 'undefined' ) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return self;
|
2013-12-24 01:17:33 +01:00
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
} else {
|
|
|
|
if ( typeof playerName == 'string' )
|
2014-09-30 00:42:41 +02:00
|
|
|
if (__plugin.canary) {
|
|
|
|
return Canary.server.getPlayer( playerName );
|
|
|
|
} else {
|
|
|
|
return bkBukkit.getPlayer( playerName );
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
else
|
|
|
|
return playerName; // assumes it's a player object
|
|
|
|
}
|
2013-12-24 01:17:33 +01:00
|
|
|
};
|
2013-12-31 21:12:57 +01:00
|
|
|
/*************************************************************************
|
2015-01-11 01:09:56 +01:00
|
|
|
### utils.world( worldName ) function
|
|
|
|
|
|
|
|
Returns a World object matching the given name
|
|
|
|
|
|
|
|
***/
|
|
|
|
function _world( worldName ){
|
|
|
|
if (__plugin.canary){
|
|
|
|
try {
|
|
|
|
return Canary.server.worldManager.getWorld( worldName, true );
|
|
|
|
} catch (error) {
|
|
|
|
console.error( 'utils.world() failed to load ' + worldName + ',Error:' + error );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (__plugin.bukkit){
|
|
|
|
return bkBukkit.getWorld( worldName );
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
exports.world = _world;
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
### utils.blockAt( Location ) function
|
|
|
|
|
|
|
|
Returns the Block at the given location.
|
|
|
|
|
|
|
|
***/
|
|
|
|
function _blockAt( location ){
|
|
|
|
if (__plugin.canary){
|
|
|
|
return location.world.getBlockAt(location);
|
|
|
|
}
|
|
|
|
if (__plugin.bukkit){
|
|
|
|
return location.block;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
exports.blockAt = _blockAt;
|
|
|
|
/*************************************************************************
|
2013-12-31 21:12:57 +01:00
|
|
|
### utils.locationToJSON() function
|
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
utils.locationToJSON() returns a [Location][cmloc] object in JSON form...
|
2013-12-31 21:12:57 +01:00
|
|
|
|
|
|
|
{ world: 'world5',
|
|
|
|
x: 56.9324,
|
|
|
|
y: 103.9954,
|
|
|
|
z: 43.1323,
|
|
|
|
yaw: 0.0,
|
|
|
|
pitch: 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
This can be useful if you write a plugin that needs to store location data since bukkit's Location object is a Java object which cannot be serialized to JSON by default.
|
2013-12-24 01:17:33 +01:00
|
|
|
|
2013-12-31 21:12:57 +01:00
|
|
|
#### Parameters
|
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
* location: An object of type [Location][cmloc]
|
2013-12-31 21:12:57 +01:00
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
|
|
|
A JSON object in the above form.
|
|
|
|
|
|
|
|
***/
|
2014-01-29 20:49:15 +01:00
|
|
|
var _locationToJSON = function( location ) {
|
2015-01-17 10:30:53 +01:00
|
|
|
var yaw = __plugin.bukkit ? location.yaw : (__plugin.canary ? location.rotation : 0);
|
2014-01-29 20:49:15 +01:00
|
|
|
return {
|
|
|
|
world: ''+location.world.name,
|
|
|
|
x: location.x,
|
|
|
|
y: location.y,
|
|
|
|
z: location.z,
|
2015-01-17 10:30:53 +01:00
|
|
|
yaw: yaw,
|
2014-01-29 20:49:15 +01:00
|
|
|
pitch: location.pitch
|
|
|
|
};
|
2013-12-31 19:21:40 +01:00
|
|
|
};
|
2013-12-31 21:12:57 +01:00
|
|
|
/*************************************************************************
|
|
|
|
### utils.locationToString() function
|
|
|
|
|
|
|
|
The utils.locationToString() function returns a
|
2015-01-17 09:45:32 +01:00
|
|
|
[Location][cmloc] object in string form...
|
2013-12-31 21:12:57 +01:00
|
|
|
|
|
|
|
'{"world":"world5",x:56.9324,y:103.9954,z:43.1323,yaw:0.0,pitch:0.0}'
|
|
|
|
|
|
|
|
... which can be useful if you write a plugin which uses Locations as
|
|
|
|
keys in a lookup table.
|
|
|
|
|
|
|
|
#### Example
|
2014-02-04 22:36:00 +01:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
|
|
|
...
|
|
|
|
var key = utils.locationToString(player.location);
|
|
|
|
lookupTable[key] = player.name;
|
|
|
|
```
|
2013-12-31 21:12:57 +01:00
|
|
|
|
|
|
|
***/
|
2014-01-29 20:49:15 +01:00
|
|
|
exports.locationToString = function( location ) {
|
|
|
|
return JSON.stringify( _locationToJSON( location ) );
|
2013-12-31 19:21:40 +01:00
|
|
|
};
|
|
|
|
exports.locationToJSON = _locationToJSON;
|
|
|
|
|
2013-12-31 21:12:57 +01:00
|
|
|
/*************************************************************************
|
|
|
|
### utils.locationFromJSON() function
|
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
This function reconstructs an [Location][cmloc] object from
|
2013-12-31 21:12:57 +01:00
|
|
|
a JSON representation. This is the counterpart to the
|
|
|
|
`locationToJSON()` function. It takes a JSON object of the form
|
|
|
|
returned by locationToJSON() and reconstructs and returns a bukkit
|
|
|
|
Location object.
|
|
|
|
|
|
|
|
***/
|
2014-01-29 20:49:15 +01:00
|
|
|
exports.locationFromJSON = function( json ) {
|
|
|
|
var world;
|
|
|
|
if ( json.constuctor == Array ) {
|
|
|
|
// for support of legacy format
|
2015-01-11 01:09:56 +01:00
|
|
|
world = _world( json[0] );
|
2014-02-19 23:17:19 +01:00
|
|
|
return new bkLocation( world, json[1], json[2] , json[3] );
|
2014-01-29 20:49:15 +01:00
|
|
|
} else {
|
2014-09-30 00:42:41 +02:00
|
|
|
if (__plugin.canary){
|
2015-01-11 01:09:56 +01:00
|
|
|
world = _world( json.world );
|
2014-09-30 00:42:41 +02:00
|
|
|
var cmLocation = Packages.net.canarymod.api.world.position.Location;
|
2015-01-17 10:30:53 +01:00
|
|
|
return new cmLocation(world, json.x, json.y, json.z, json.pitch?json.pitch:0, json.yaw?json.yaw:0);
|
2014-09-30 00:42:41 +02:00
|
|
|
} else {
|
2015-01-11 01:09:56 +01:00
|
|
|
world = _world( json.world );
|
2015-01-17 10:30:53 +01:00
|
|
|
return new bkLocation( world, json.x, json.y , json.z, json.yaw?json.yaw:0, json.pitch?json.pitch:0 );
|
2014-09-30 00:42:41 +02:00
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2013-12-24 01:17:33 +01:00
|
|
|
};
|
|
|
|
|
2013-12-31 19:21:40 +01:00
|
|
|
exports.player = _player;
|
2014-01-29 20:49:15 +01:00
|
|
|
|
|
|
|
exports.getPlayerObject = function( player ) {
|
|
|
|
console.warn( 'utils.getPlayerObject() is deprecated. Use utils.player() instead.' );
|
|
|
|
return _player(player);
|
2013-12-31 19:21:40 +01:00
|
|
|
};
|
2013-12-31 21:12:57 +01:00
|
|
|
/*************************************************************************
|
|
|
|
### utils.getPlayerPos() function
|
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
This function returns the player's [Location][cmloc] (x, y, z, pitch
|
2013-12-31 21:12:57 +01:00
|
|
|
and yaw) for a named player. If the "player" is in fact a
|
2015-01-18 22:16:38 +01:00
|
|
|
[BlockCommand][bkbcs] then the attached Block's location is returned.
|
2013-12-24 01:17:33 +01:00
|
|
|
|
2013-12-31 21:12:57 +01:00
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
* player : A [org.bukkit.command.CommandSender][bkbcs] (Player or BlockCommandSender) or player name (String).
|
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
A [Location][cmloc] object.
|
2013-12-31 21:12:57 +01:00
|
|
|
|
|
|
|
[bkbcs]: http://jd.bukkit.org/dev/apidocs/org/bukkit/command/BlockCommandSender.html
|
|
|
|
[bksndr]: http://jd.bukkit.org/dev/apidocs/index.html?org/bukkit/command/CommandSender.html
|
|
|
|
***/
|
2015-01-18 22:16:38 +01:00
|
|
|
function getPlayerPos( player ){
|
2014-01-29 20:49:15 +01:00
|
|
|
player = _player( player );
|
|
|
|
if ( player ) {
|
2015-01-18 22:16:38 +01:00
|
|
|
if (__plugin.bukkit){
|
|
|
|
if ( player instanceof bkBlockCommandSender )
|
|
|
|
return player.block.location;
|
|
|
|
else
|
|
|
|
return player.location;
|
|
|
|
}
|
|
|
|
if (__plugin.canary){
|
|
|
|
if ( player instanceof Packages.net.canarymod.api.world.blocks.CommandBlock)
|
|
|
|
return player.block.location;
|
|
|
|
else
|
|
|
|
return player.location;
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
|
|
|
return null;
|
2015-01-18 22:16:38 +01:00
|
|
|
}
|
|
|
|
exports.getPlayerPos = getPlayerPos;
|
2013-12-31 21:12:57 +01:00
|
|
|
/************************************************************************
|
|
|
|
### utils.getMousePos() function
|
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
This function returns a [Location][cmloc] object (the
|
2013-12-31 21:12:57 +01:00
|
|
|
x,y,z) of the current block being targeted by the named player. This
|
|
|
|
is the location of the block the player is looking at (targeting).
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
* player : The player whose targeted location you wish to get.
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
|
|
|
|
The following code will strike lightning at the location the player is looking at...
|
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
|
|
|
var playerName = 'walterh';
|
|
|
|
var targetPos = utils.getMousePos(playerName);
|
|
|
|
if (targetPos){
|
2015-01-17 09:45:32 +01:00
|
|
|
if (__plugin.canary){
|
|
|
|
targetPos.world.makeLightningBolt(targetPos);
|
|
|
|
}
|
|
|
|
if (__plugin.bukkit){
|
|
|
|
targetPos.world.strikeLightning(targetPos);
|
|
|
|
}
|
2014-02-04 22:36:00 +01:00
|
|
|
}
|
|
|
|
```
|
2013-12-24 01:17:33 +01:00
|
|
|
|
2013-12-31 21:12:57 +01:00
|
|
|
***/
|
2014-01-29 20:49:15 +01:00
|
|
|
exports.getMousePos = function( player ) {
|
|
|
|
|
|
|
|
player = _player(player);
|
|
|
|
if ( !player ) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-09-30 00:42:41 +02:00
|
|
|
var targetedBlock ;
|
|
|
|
if ( __plugin.canary ) {
|
|
|
|
var cmLineTracer = Packages.net.canarymod.LineTracer;
|
|
|
|
var lineTracer = new cmLineTracer(player);
|
|
|
|
targetedBlock = lineTracer.getTargetBlock();
|
|
|
|
if (targetedBlock == null){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// player might be CONSOLE or a CommandBlock
|
|
|
|
if ( !player.getTargetBlock ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
targetedBlock = player.getTargetBlock( null, 5 );
|
|
|
|
if ( targetedBlock == null || targetedBlock.isEmpty() ) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
|
|
|
return targetedBlock.location;
|
2013-12-24 01:17:33 +01:00
|
|
|
};
|
2013-02-17 23:29:41 +01:00
|
|
|
/************************************************************************
|
2013-12-31 21:12:57 +01:00
|
|
|
### utils.foreach() function
|
2013-12-28 09:44:40 +01:00
|
|
|
|
2013-02-17 23:29:41 +01:00
|
|
|
The utils.foreach() function is a utility function for iterating over
|
2015-01-17 09:45:32 +01:00
|
|
|
an array of objects (or a java.util.Collection of objects) and
|
|
|
|
processing each object in turn. Where 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.
|
2013-02-17 23:29:41 +01:00
|
|
|
|
|
|
|
[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.
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
* context (optional) : An object which may be used by the callback.
|
2015-01-17 09:45:32 +01:00
|
|
|
* delayInMilliseconds (optional, numeric) : If a delay is specified then the processing will be scheduled so that
|
2013-02-17 23:29:41 +01:00
|
|
|
each item will be processed in turn with a delay between the completion of each
|
2015-01-17 09:45:32 +01:00
|
|
|
item and the start of the next. This is recommended for any CPU-intensive process.
|
2013-02-17 23:29:41 +01:00
|
|
|
* 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...
|
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
2015-01-17 09:45:32 +01:00
|
|
|
var players = utils.players();
|
|
|
|
utils.foreach (players, function( player ) {
|
|
|
|
echo( player , 'Hi ' + player);
|
2014-02-04 22:36:00 +01:00
|
|
|
});
|
|
|
|
```
|
2013-08-03 14:50:06 +02:00
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
... The `utils.foreach()` function can work with Arrays or any
|
|
|
|
Java-style collection. This is important because many objects in the
|
|
|
|
CanaryMod and Bukkit APIs use Java-style collections...
|
2013-08-03 14:50:06 +02:00
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
```javascript
|
2015-01-17 09:45:32 +01:00
|
|
|
// in bukkit, server.onlinePlayers returns a java.util.Collection object
|
2014-02-04 22:36:00 +01:00
|
|
|
utils.foreach( server.onlinePlayers, function(player){
|
2015-01-17 09:45:32 +01:00
|
|
|
player.chat('Hello!');
|
2014-02-04 22:36:00 +01: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
|
|
|
***/
|
2014-01-29 20:49:15 +01:00
|
|
|
var _foreach = function( array, callback, context, 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, context, array);
|
|
|
|
i++;
|
|
|
|
};
|
|
|
|
var hasNext = function( ) {
|
|
|
|
return i < len;
|
|
|
|
};
|
|
|
|
_nicely( next, hasNext, onCompletion, delay );
|
|
|
|
} else {
|
|
|
|
for ( ;i < len; i++ ) {
|
|
|
|
callback( array[i], i, context, array );
|
2013-12-24 01:17:33 +01:00
|
|
|
}
|
2014-01-29 20:49:15 +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
|
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
The utils.nicely() function is for performing background processing. utils.nicely() lets you
|
2013-02-17 23:29:41 +01:00
|
|
|
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).
|
2015-01-17 09:45:32 +01:00
|
|
|
* delayInMilliseconds : The delay between each call.
|
2013-02-17 23:29:41 +01:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
***/
|
2014-01-29 20:49:15 +01:00
|
|
|
var _nicely = function( next, hasNext, onDone, delay ) {
|
|
|
|
if ( hasNext() ){
|
|
|
|
next();
|
2015-01-17 09:45:32 +01:00
|
|
|
setTimeout( function() {
|
2014-01-29 20:49:15 +01:00
|
|
|
_nicely( next, hasNext, onDone, delay );
|
|
|
|
}, delay );
|
|
|
|
}else{
|
|
|
|
if ( onDone ) {
|
|
|
|
onDone();
|
2013-12-24 01:17:33 +01:00
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2013-12-24 01:17:33 +01:00
|
|
|
};
|
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...
|
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
2013-12-24 01:17:33 +01:00
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
utils.at( '19:00', function() {
|
2013-08-08 09:02:27 +02:00
|
|
|
|
2015-01-17 09:45:32 +01:00
|
|
|
utils.players(function( player ) {
|
|
|
|
echo( player, 'The night is dark and full of terrors!' );
|
|
|
|
});
|
2014-02-04 22:36:00 +01:00
|
|
|
|
|
|
|
});
|
|
|
|
```
|
2013-08-03 14:14:09 +02:00
|
|
|
|
|
|
|
***/
|
2015-01-24 01:07:58 +01:00
|
|
|
exports.at = function( time24hr, callback, pWorlds ) {
|
2014-01-29 20:49:15 +01:00
|
|
|
var forever = function(){ return true; };
|
|
|
|
var timeParts = time24hr.split( ':' );
|
2015-01-24 01:07:58 +01:00
|
|
|
var timeMins = (timeParts[0] * 60) + (timeParts[1] * 1);
|
|
|
|
if ( typeof pWorlds == 'undefined' ) {
|
|
|
|
pWorlds = worlds();
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2015-01-24 01:07:58 +01:00
|
|
|
var calledToday = false;
|
2014-01-29 20:49:15 +01:00
|
|
|
_nicely( function() {
|
2015-01-24 01:07:58 +01:00
|
|
|
_foreach( pWorlds, function ( world ) {
|
|
|
|
var time = getTime24(world);
|
|
|
|
var diff = time - timeMins;
|
|
|
|
if (diff > 0 && diff < 5 && !calledToday){
|
2014-01-29 20:49:15 +01:00
|
|
|
callback();
|
2015-01-24 01:07:58 +01:00
|
|
|
calledToday = true;
|
|
|
|
}
|
|
|
|
if (diff < 0){
|
|
|
|
calledToday = false;
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
|
|
|
});
|
2015-01-24 01:07:58 +01:00
|
|
|
}, forever, null, 1000 );
|
2013-02-10 18:53:08 +01:00
|
|
|
};
|
2015-01-11 13:27:47 +01:00
|
|
|
/*************************************************************************
|
|
|
|
### utils.time( world ) function
|
|
|
|
|
|
|
|
Returns the timeofday (in minecraft ticks) for the given world. This function is necessary because
|
|
|
|
canarymod and bukkit differ in how the timeofday is calculated.
|
|
|
|
|
|
|
|
See http://minecraft.gamepedia.com/Day-night_cycle#Conversions
|
|
|
|
|
|
|
|
***/
|
|
|
|
function getTime(world){
|
|
|
|
if (__plugin.bukkit){
|
|
|
|
return world.time;
|
|
|
|
}
|
|
|
|
if (__plugin.canary){
|
|
|
|
// there's a bug in canary where if you call world.setTime() the world.totalTime
|
|
|
|
// becomes huge.
|
|
|
|
if (world.totalTime < world.rawTime){
|
|
|
|
return world.totalTime;
|
|
|
|
} else {
|
|
|
|
return ((world.totalTime % world.rawTime) + world.relativeTime) % 24000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
exports.time = getTime;
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
### utils.time24( world ) function
|
|
|
|
|
|
|
|
Returns the timeofday for the given world using 24 hour notation. (number of minutes)
|
|
|
|
|
|
|
|
See http://minecraft.gamepedia.com/Day-night_cycle#Conversions
|
|
|
|
|
|
|
|
***/
|
|
|
|
function getTime24(world){
|
|
|
|
var mcTime = getTime(world);
|
|
|
|
var mins = Math.floor( ( (mcTime + 6000) % 24000) / 16.6667 );
|
|
|
|
return mins;
|
|
|
|
}
|
|
|
|
exports.time24 = getTime24;
|
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
|
|
|
|
|
2014-02-04 22:36:00 +01:00
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
|
|
|
var jsFiles = utils.find('./', function(dir,name){
|
|
|
|
return name.match(/\.js$/);
|
|
|
|
});
|
|
|
|
```
|
2013-12-28 09:44:40 +01:00
|
|
|
|
|
|
|
***/
|
2014-01-29 20:49:15 +01:00
|
|
|
exports.find = function( dir , filter ) {
|
|
|
|
var result = [];
|
|
|
|
var recurse = function( dir, store ) {
|
2014-02-11 22:10:56 +01:00
|
|
|
var files, dirfile = new File( dir );
|
2014-01-29 20:49:15 +01:00
|
|
|
|
|
|
|
if ( typeof filter == 'undefined' ) {
|
|
|
|
files = dirfile.list();
|
|
|
|
} else {
|
|
|
|
files = dirfile.list(filter);
|
2013-12-24 01:17:33 +01:00
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
_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;
|
|
|
|
};
|
2014-02-10 21:55:32 +01:00
|
|
|
/************************************************************************
|
|
|
|
### utils.serverAddress() function
|
|
|
|
|
|
|
|
The utils.serverAddress() function returns the IP(v4) address of the server.
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
|
|
|
var serverAddress = utils.serverAddress();
|
|
|
|
console.log(serverAddress);
|
|
|
|
```
|
|
|
|
***/
|
|
|
|
exports.serverAddress = function() {
|
|
|
|
var interfaces = java.net.NetworkInterface.getNetworkInterfaces();
|
|
|
|
var current,
|
|
|
|
addresses,
|
|
|
|
current_addr;
|
|
|
|
while ( interfaces.hasMoreElements() ) {
|
|
|
|
current = interfaces.nextElement();
|
|
|
|
if ( ! current.isUp() || current.isLoopback() || current.isVirtual() ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
addresses = current.getInetAddresses();
|
|
|
|
while (addresses.hasMoreElements()) {
|
|
|
|
current_addr = addresses.nextElement();
|
|
|
|
if ( current_addr.isLoopbackAddress() )
|
|
|
|
continue;
|
|
|
|
if ( current_addr instanceof java.net.Inet4Address)
|
|
|
|
return current_addr.getHostAddress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
/************************************************************************
|
|
|
|
### utils.watchFile() function
|
|
|
|
|
|
|
|
Watches for changes to the given file or directory and calls the function provided
|
|
|
|
when the file changes.
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
* File - the file to watch (can be a file or directory)
|
|
|
|
* Callback - The callback to invoke when the file has changed. The callback takes the
|
|
|
|
changed file as a parameter.
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
|
|
|
utils.watchFile( 'test.txt', function( file ) {
|
|
|
|
console.log( file + ' has changed');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
***/
|
|
|
|
var filesWatched = {};
|
2014-06-28 14:32:55 +02:00
|
|
|
var dirsWatched = {};
|
|
|
|
|
2014-02-10 21:55:32 +01:00
|
|
|
exports.watchFile = function( file, callback ) {
|
|
|
|
if ( typeof file == 'string' ) {
|
|
|
|
file = new File(file);
|
|
|
|
}
|
2014-06-28 14:32:55 +02:00
|
|
|
//console.log("Watching file " + file);
|
2014-02-10 21:55:32 +01:00
|
|
|
filesWatched[file.canonicalPath] = {
|
|
|
|
callback: callback,
|
|
|
|
lastModified: file.lastModified()
|
|
|
|
};
|
|
|
|
};
|
2014-06-28 14:32:55 +02:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
### utils.watchDir() function
|
|
|
|
|
|
|
|
Watches for changes to the given directory and calls the function provided
|
|
|
|
when the directory changes. It works by calling watchFile/watchDir for each
|
|
|
|
file/subdirectory.
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
* Dir - the file to watch (can be a file or directory)
|
|
|
|
* Callback - The callback to invoke when the directory has changed.
|
|
|
|
The callback takes the changed file as a parameter.
|
|
|
|
For each change inside the directory the callback will also
|
|
|
|
be called.
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
|
|
|
utils.watchDir( 'players/_ial', function( dir ) {
|
|
|
|
console.log( dir + ' has changed');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
***/
|
|
|
|
|
|
|
|
exports.watchDir = function( dir, callback ) {
|
|
|
|
if ( typeof dir == 'string' ) {
|
|
|
|
dir = new File(dir);
|
|
|
|
}
|
|
|
|
//console.log("Watching dir " + dir);
|
|
|
|
dirsWatched[dir.canonicalPath] = {
|
|
|
|
callback: callback,
|
|
|
|
lastModified: dir.lastModified()
|
|
|
|
};
|
|
|
|
|
|
|
|
var files = dir.listFiles(),file;
|
|
|
|
if ( !files ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for ( var i = 0; i < files.length; i++ ) {
|
|
|
|
file = files[i];
|
|
|
|
if (file.isDirectory( )) {
|
|
|
|
exports.watchDir(file,callback);
|
|
|
|
}else{
|
|
|
|
exports.watchFile(file,callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-02-11 22:10:56 +01:00
|
|
|
/************************************************************************
|
|
|
|
### utils.unwatchFile() function
|
|
|
|
|
|
|
|
Removes a file from the watch list.
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
|
|
|
utils.unwatchFile( 'test.txt');
|
|
|
|
```
|
|
|
|
|
|
|
|
***/
|
|
|
|
exports.unwatchFile = function( file, callback ) {
|
|
|
|
if ( typeof file == 'string' ) {
|
|
|
|
file = new File(file);
|
|
|
|
}
|
2014-06-28 14:32:55 +02:00
|
|
|
//console.log("Unwatching file " + file);
|
2014-02-11 22:10:56 +01:00
|
|
|
delete filesWatched[file.canonicalPath];
|
|
|
|
};
|
|
|
|
|
2014-06-28 14:32:55 +02:00
|
|
|
/************************************************************************
|
|
|
|
### utils.unwatchDir() function
|
|
|
|
|
|
|
|
Removes a directory from the watch list and all files inside the directory
|
|
|
|
are also "unwatched"
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
```javascript
|
|
|
|
var utils = require('utils');
|
|
|
|
utils.unwatchDir ('players/_ial');
|
|
|
|
```
|
|
|
|
Would cause also
|
|
|
|
```javascript
|
|
|
|
utils.unwatchFile (file);
|
|
|
|
```
|
|
|
|
for each file inside directory (and unwatchDir for each directory inside it)
|
|
|
|
|
|
|
|
***/
|
|
|
|
exports.unwatchDir = function( dir, callback ) {
|
|
|
|
if ( typeof dir == 'string' ) {
|
|
|
|
dir = new File(dir);
|
|
|
|
}
|
|
|
|
//console.log("Unwatching dir " + dir);
|
|
|
|
delete dirsWatched[dir.canonicalPath];
|
|
|
|
|
|
|
|
var files = dir.listFiles(),file;
|
|
|
|
if ( !files ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for ( var i = 0; i < files.length; i++ ) {
|
|
|
|
file = files[i];
|
|
|
|
if (file.isDirectory( )) {
|
|
|
|
exports.unwatchDir(file,callback);
|
|
|
|
}else{
|
|
|
|
exports.unwatchFile(file,callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function fileWatcher(calledCallbacks) {
|
2014-02-10 21:55:32 +01:00
|
|
|
for (var file in filesWatched) {
|
|
|
|
var fileObject = new File(file);
|
|
|
|
var lm = fileObject.lastModified();
|
|
|
|
if ( lm != filesWatched[file].lastModified ) {
|
2014-06-28 14:32:55 +02:00
|
|
|
//console.log("Change found in " + file);
|
2014-02-10 21:55:32 +01:00
|
|
|
filesWatched[file].lastModified = lm;
|
|
|
|
filesWatched[file].callback(fileObject);
|
2014-06-28 14:32:55 +02:00
|
|
|
if (!fileObject.exists()) {
|
|
|
|
//console.log("File " + file + " was removed.");
|
|
|
|
exports.unwatchFile(file,filesWatched[file].callback);
|
|
|
|
}
|
2014-02-10 21:55:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-06-28 14:32:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
//monitors directories for time change
|
|
|
|
//when a change is detected watchFiles are invoked for each of the files in directory
|
|
|
|
//and callback is called
|
|
|
|
function dirWatcher(calledCallbacks) {
|
|
|
|
for (var dir in dirsWatched) {
|
|
|
|
var dirObject = new File(dir);
|
|
|
|
var lm = dirObject.lastModified();
|
|
|
|
var dw = dirsWatched[dir];
|
|
|
|
if ( lm != dirsWatched[dir].lastModified ) {
|
|
|
|
//console.log("Change found in " + dir);
|
|
|
|
dirsWatched[dir].lastModified = lm;
|
|
|
|
dirsWatched[dir].callback(dirObject);
|
|
|
|
|
|
|
|
exports.unwatchDir(dir, dw.callback);
|
|
|
|
//causes all files to be rewatched
|
|
|
|
if (dirObject.exists()) {
|
|
|
|
exports.watchDir(dir, dw.callback);
|
|
|
|
} else {
|
|
|
|
//console.log("Directory " + dir + " was removed.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//guarantees that a callback is only called once for each change
|
|
|
|
function monitorDirAndFiles() {
|
|
|
|
fileWatcher ();
|
|
|
|
dirWatcher ();
|
|
|
|
setTimeout( monitorDirAndFiles, 3000 );
|
|
|
|
};
|
|
|
|
|
|
|
|
setTimeout( monitorDirAndFiles, 3000 );
|
2014-05-20 01:05:38 +02:00
|
|
|
/**************************************************************************
|
|
|
|
### utils.array() function
|
|
|
|
|
|
|
|
Converts Java collection objects to type Javascript array so they can avail of
|
|
|
|
all of Javascript's Array goodness.
|
|
|
|
|
|
|
|
#### Example
|
|
|
|
|
|
|
|
var utils = require('utils');
|
|
|
|
var worlds = utils.array(server.worlds);
|
|
|
|
|
|
|
|
***/
|
2014-10-18 12:05:30 +02:00
|
|
|
function toArray( ){
|
2014-05-20 01:05:38 +02:00
|
|
|
var result = [],
|
|
|
|
javaArray = null,
|
|
|
|
i = 0;
|
|
|
|
if (arguments[0] instanceof java.util.Collection){
|
|
|
|
// it's a java collection
|
|
|
|
javaArray = arguments[0].toArray();
|
|
|
|
for ( ;i < javaArray.length; i++) {
|
|
|
|
result.push(javaArray[i]);
|
|
|
|
}
|
|
|
|
} else if (arguments[0].constructor === Array){
|
|
|
|
// it's a javascript array
|
|
|
|
return arguments[0];
|
|
|
|
} else if (arguments[0].length) {
|
|
|
|
// it's a java array
|
|
|
|
javaArray = arguments[0];
|
|
|
|
for ( ;i < javaArray.length; i++) {
|
|
|
|
result.push(javaArray[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2014-10-18 12:05:30 +02:00
|
|
|
}
|
|
|
|
exports.array = toArray;
|
|
|
|
|
2015-01-24 01:07:58 +01:00
|
|
|
function worlds(){
|
|
|
|
if (__plugin.canary){
|
|
|
|
return toArray(server.worldManager.allWorlds);
|
|
|
|
}
|
|
|
|
if (__plugin.bukkit){
|
|
|
|
return toArray(server.worlds);
|
|
|
|
}
|
2014-10-18 12:05:30 +02:00
|
|
|
}
|
2015-01-24 01:07:58 +01:00
|
|
|
exports.worlds = worlds;
|
2014-10-18 12:05:30 +02:00
|
|
|
|
2014-10-05 18:29:04 +02:00
|
|
|
/*************************************************************************
|
|
|
|
### utils.players() function
|
|
|
|
|
2015-01-11 13:27:47 +01:00
|
|
|
This function returns a javascript array of all online players on the
|
|
|
|
server. You can optionally provide a function which will be invoked
|
|
|
|
with each player as a parameter. For example, to give each player the
|
|
|
|
ability to shoot arrows which launch fireworks:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
require('utils').players( arrows.firework )
|
|
|
|
```
|
|
|
|
|
|
|
|
Any players with a bow will be able to launch fireworks by shooting.
|
2014-10-05 18:29:04 +02:00
|
|
|
|
|
|
|
### utils.playerNames() function
|
|
|
|
|
|
|
|
This function returns a javascript array of player names (as javascript strings)
|
|
|
|
|
|
|
|
***/
|
2014-09-30 00:42:41 +02:00
|
|
|
function getPlayersBukkit(){
|
|
|
|
var result = [];
|
|
|
|
for (var i = 0; i < server.onlinePlayers.length; i++){
|
|
|
|
result.push(server.onlinePlayers[i]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
function getPlayersCanary(){
|
|
|
|
var result = [];
|
|
|
|
var players = server.playerList;
|
|
|
|
for (var i = 0; i < players.size(); i++){
|
|
|
|
result.push(players.get(i));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-10-05 18:29:04 +02:00
|
|
|
var getPlayers = null;
|
|
|
|
if (__plugin.canary) {
|
|
|
|
getPlayers = getPlayersCanary;
|
|
|
|
} else {
|
|
|
|
getPlayers = getPlayersBukkit;
|
|
|
|
}
|
|
|
|
|
2014-10-18 13:08:00 +02:00
|
|
|
function getStatBukkit(){
|
|
|
|
if (arguments.length == 1){
|
|
|
|
var stat = arguments[1];
|
|
|
|
return org.bukkit.Statistic[stat.toUpperCase()];
|
|
|
|
} else {
|
|
|
|
var player = arguments[0];
|
|
|
|
var stat = arguments[1];
|
|
|
|
return player.getStatistic(org.bukkit.Statistic[stat.toUpperCase()]);
|
|
|
|
}
|
|
|
|
|
2014-09-30 00:42:41 +02:00
|
|
|
}
|
2014-10-18 13:08:00 +02:00
|
|
|
function getStatCanary(){
|
2014-09-30 00:42:41 +02:00
|
|
|
var cmStatistics = Packages.net.canarymod.api.statistics.Statistics;
|
2014-10-18 13:08:00 +02:00
|
|
|
if (arguments.length == 1){
|
|
|
|
var stat = arguments[0];
|
|
|
|
return cmStatistics[stat.toUpperCase()].instance;
|
|
|
|
} else {
|
|
|
|
var player = arguments[0];
|
|
|
|
var stat = arguments[1];
|
|
|
|
return player.getStat(cmStatistics[stat.toUpperCase()].instance);
|
|
|
|
}
|
2014-09-30 00:42:41 +02:00
|
|
|
}
|
2014-10-18 13:08:00 +02:00
|
|
|
if (__plugin.canary){
|
|
|
|
var cmStatistics = Packages.net.canarymod.api.statistics.Statistics;
|
|
|
|
var values = cmStatistics.values();
|
|
|
|
for (var i = 0;i < values.length; i++){
|
|
|
|
var value = values[i];
|
|
|
|
try {
|
|
|
|
var stat = value.instance;
|
|
|
|
getStatCanary[value.name()] = stat;
|
|
|
|
}catch (e){
|
|
|
|
// as of 20141018 some calls to getInstance() will generate an NPE
|
|
|
|
// see https://github.com/CanaryModTeam/CanaryMod/issues/84
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 18:29:04 +02:00
|
|
|
function getPlayerNames(){
|
|
|
|
return getPlayers().map(function(p){ return p.name; });
|
|
|
|
}
|
2015-01-11 13:27:47 +01:00
|
|
|
exports.players = function(fn){
|
|
|
|
var result = getPlayers();
|
|
|
|
if (fn){
|
|
|
|
result.forEach(fn);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2014-10-05 18:29:04 +02:00
|
|
|
exports.playerNames = getPlayerNames;
|
|
|
|
|
2014-09-30 00:42:41 +02:00
|
|
|
/*************************************************************************
|
|
|
|
### utils.stat() function
|
|
|
|
|
|
|
|
This function returns a numeric value for a given player statistic.
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
2014-10-18 13:08:00 +02:00
|
|
|
* Player - The player object (optional - if only the statistic name parameter is provided then the statistic object is returned)
|
2014-09-30 00:42:41 +02:00
|
|
|
* Statistic - A string whose value should be one of the following (CanaryMod)
|
|
|
|
* ANIMALSBRED
|
|
|
|
* BOATONECM
|
|
|
|
* CLIMBONECM
|
|
|
|
* CROUCHONECM
|
|
|
|
* DAMAGEDEALT
|
|
|
|
* DAMAGETAKEN
|
|
|
|
* DEATHS
|
|
|
|
* DRIVEONECM
|
|
|
|
* DROP
|
|
|
|
* FALLONECM
|
|
|
|
* FISHCAUGHT
|
|
|
|
* FLYONECM
|
|
|
|
* HORSEONECM
|
|
|
|
* JUMP
|
|
|
|
* JUNKFISHED
|
|
|
|
* LEAVEGAME
|
|
|
|
* MINECARTONECM
|
|
|
|
* MOBKILLS
|
|
|
|
* PIGONECM
|
|
|
|
* PLAYERKILLS
|
|
|
|
* PLAYONEMINUTE
|
|
|
|
* SPRINTONECM
|
|
|
|
* SWIMONECM
|
|
|
|
* TALKEDTOVILLAGER
|
|
|
|
* TIMESINCEDEATH
|
|
|
|
* TRADEDWITHVILLAGER
|
|
|
|
* TREASUREFISHED
|
|
|
|
* WALKONECM
|
|
|
|
|
2014-10-18 13:08:00 +02:00
|
|
|
See [CanaryMod's Statistic][cmstat] class for an up-to-date list of possible stat values
|
2014-09-30 00:42:41 +02:00
|
|
|
|
|
|
|
[cmstat]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/statistics/Statistics.html
|
|
|
|
|
2014-10-18 13:08:00 +02:00
|
|
|
#### Example 1 Getting stats for a player
|
|
|
|
|
|
|
|
var utils = require('utils');
|
|
|
|
var jumpCount = utils.stat( player, 'jump');
|
|
|
|
|
|
|
|
#### Example 2 Getting the JUMP statistic object (which can be used elsewhere)
|
|
|
|
|
|
|
|
var utils = require('utils');
|
|
|
|
var JUMPSTAT = utils.stat('jump');
|
|
|
|
var jumpCount = player.getStat( JUMPSTAT ); // canary-specific code
|
|
|
|
|
|
|
|
This function also contains values for each possible stat so you can get at stats like this...
|
|
|
|
|
|
|
|
var utils = require('utils');
|
|
|
|
var JUMPSTAT = utils.stat.JUMP; // Accessing the value
|
|
|
|
var jumpCount = player.getStat ( JUMPSTAT ); // canary-specific code
|
2014-09-30 00:42:41 +02:00
|
|
|
***/
|
|
|
|
exports.stat = __plugin.canary ? getStatCanary: getStatBukkit;
|