Improved Tab completion to work with Java Enums on JRE7 and JRE8.

Added bukkit namespace.
This commit is contained in:
walterhiggins 2014-05-20 00:05:38 +01:00
parent e3078804ab
commit c324adf269
8 changed files with 116 additions and 5 deletions

View file

@ -233,6 +233,7 @@ Walter Higgins
* [utils.serverAddress() function](#utilsserveraddress-function) * [utils.serverAddress() function](#utilsserveraddress-function)
* [utils.watchFile() function](#utilswatchfile-function) * [utils.watchFile() function](#utilswatchfile-function)
* [utils.unwatchFile() function](#utilsunwatchfile-function) * [utils.unwatchFile() function](#utilsunwatchfile-function)
* [utils.array() function](#utilsarray-function)
* [Drone Plugin](#drone-plugin) * [Drone Plugin](#drone-plugin)
* [TLDNR; (Just read this if you're impatient)](#tldnr-just-read-this-if-youre-impatient) * [TLDNR; (Just read this if you're impatient)](#tldnr-just-read-this-if-youre-impatient)
* [Constructing a Drone Object](#constructing-a-drone-object) * [Constructing a Drone Object](#constructing-a-drone-object)
@ -2904,6 +2905,16 @@ var utils = require('utils');
utils.unwatchFile( 'test.txt'); utils.unwatchFile( 'test.txt');
``` ```
### 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);
## Drone Plugin ## Drone Plugin
The Drone is a convenience class for building. It can be used for... The Drone is a convenience class for building. It can be used for...

View file

@ -1,3 +1,6 @@
# 2014 05 19
Improved Tab Completion to work with Java Enums too.
# 2014 05 12 # 2014 05 12
Turn off modality for conversations which are started via the 'input' module. Turn off modality for conversations which are started via the 'input' module.

26
src/main/js/lib/bukkit.js Normal file
View file

@ -0,0 +1,26 @@
var bukkit = {
stat: org.bukkit.Statistic,
stats: org.bukkit.Statistic,
material: org.bukkit.Material,
art: org.bukkit.Art,
mode: org.bukkit.GameMode,
sound: org.bukkit.Sound,
players: function(){
var result = [];
for (var i = 0; i < server.onlinePlayers.length; i++){
result.push(server.onlinePlayers[i]);
}
return result;
},
worlds: function(){
var result = [];
var lWorlds = server.worlds;
for (var i = 0; i < lWorlds.size(); i++){
result.push(lWorlds.get(i));
}
return result;
}
};
module.exports = function( container ){
container.bukkit = bukkit;
};

View file

@ -3,7 +3,27 @@ exports.isJavaObject = function( o ) {
return false; return false;
} }
if (o !== undefined && o !== null){ if (o !== undefined && o !== null){
return o.getClass ? true : false; try {
// this throws error for java objects in jre7
if (typeof o.constructor === 'function'){
return false;
}
} catch (e){
return true;
}
try {
var result = o.getClass ? true : false; // throws error for Enums/Class in jre7
if (result == true){
return result;
}
}catch (e2){
// fail silently and move on to next test
}
// java classes don't have a getClass so just because .getClass isn't present
// doesn't mean it's not a Java Enum or Class (.getClass only works for object instances?)
if (o instanceof java.lang.Object){
return true;
}
} }
return o instanceof java.lang.Object; return o instanceof java.lang.Object;
}; };

View file

@ -630,6 +630,7 @@ function __onEnable ( __engine, __plugin, __script )
org.bukkit.event.HandlerList['unregisterAll(org.bukkit.plugin.Plugin)'](__plugin); org.bukkit.event.HandlerList['unregisterAll(org.bukkit.plugin.Plugin)'](__plugin);
}); });
require('bukkit')( global );
global.__onCommand = function( sender, cmd, label, args) { global.__onCommand = function( sender, cmd, label, args) {
var jsArgs = [], var jsArgs = [],

View file

@ -72,8 +72,18 @@ var _getProperties = function( o ) {
} }
for ( i in o ) { for ( i in o ) {
if ( i.match( /^[^_]/ ) ) { if ( i.match( /^[^_]/ ) ) {
if ( typeof o[i] == 'function' ) { if ( typeof o[i] == 'function'){
result.push( i+'()' ); if ( ! (o[i] instanceof java.lang.Object) ) {
try {
if (o[i].constructor){} // throws error for java objects in jre7
result.push(i + '()');
} catch (e ){
result.push(i);
}
}else {
result.push( i );
}
} else { } else {
result.push( i ); result.push( i );
} }
@ -146,7 +156,13 @@ var onTabCompleteJS = function( result, cmdSender, pluginCmd, cmdAlias, cmdArgs
if ( !name ) { // fix issue #115 if ( !name ) { // fix issue #115
break; break;
} }
symbol = symbol[name]; // this causes problem in jre8 if name is '' try {
// this causes problems in jre if symbol is an enum and name is partial-match
symbol = symbol[name]; // this causes problem in jre8 if name is ''
} catch (e){
symbol = null;
break;
}
if ( typeof symbol == 'undefined' ) { if ( typeof symbol == 'undefined' ) {
break; break;
} }

View file

@ -15,7 +15,7 @@ for (var i = 0;i < materials.length; i++ ){
items[name] = (function(material){ items[name] = (function(material){
return function(amount){ return function(amount){
if (typeof amount == 'undefined'){ if (typeof amount == 'undefined'){
amount = 1; return material;
} }
if (typeof amount == 'number'){ if (typeof amount == 'number'){
return new bkItemStack(material, amount); return new bkItemStack(material, amount);

View file

@ -557,3 +557,37 @@ function fileWatcher() {
setTimeout( fileWatcher, 5000 ); setTimeout( fileWatcher, 5000 );
}; };
setTimeout( fileWatcher, 5000 ); setTimeout( fileWatcher, 5000 );
/**************************************************************************
### 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);
***/
exports.array = function( ){
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;
};