moving bukkit.js from lib to modules (it shouldn't be part of core)

This commit is contained in:
walterhiggins 2014-09-25 20:36:39 +01:00
parent 0906f61575
commit cc4f98474d
4 changed files with 49 additions and 74 deletions

View file

@ -13,16 +13,8 @@ This method is used to register event listeners.
#### Parameters #### Parameters
* eventName - A string or java class. If a string is supplied it must * eventName - A java class. See [Bukkit API][buk] for
be part of the Bukkit event class name. See [Bukkit API][buk] for details of the many bukkit event types. Provide the full class name (without
details of the many bukkit event types. When a string is supplied
there is no need to provide the full class name - you should omit
the 'org.bukkit.event' prefix. e.g. if the string
"block.BlockBreakEvent" is supplied then it's converted to the
org.bukkit.event.block.BlockBreakEvent class .
If a java class is provided (say in the case where you've defined
your own custom event) then provide the full class name (without
enclosing quotes). enclosing quotes).
* callback - A function which will be called whenever the event * callback - A function which will be called whenever the event
@ -43,7 +35,7 @@ An object which can be used to unregister the listener.
The following code will print a message on screen every time a block is broken in the game The following code will print a message on screen every time a block is broken in the game
```javascript ```javascript
events.on( 'block.BlockBreakEvent', function( evt ) { events.on( Packages.org.bukkit.event.block.BlockBreakEvent, function( evt ) {
evt.player.sendMessage( evt.player.name + ' broke a block!'); evt.player.sendMessage( evt.player.name + ' broke a block!');
} ); } );
``` ```
@ -51,7 +43,7 @@ events.on( 'block.BlockBreakEvent', function( evt ) {
To handle an event only once and unregister from further events... To handle an event only once and unregister from further events...
```javascript ```javascript
events.on( 'block.BlockBreakEvent', function( evt ) { events.on( Packages.org.bukkit.event.block.BlockBreakEvent, function( evt ) {
evt.player.sendMessage( evt.player.name + ' broke a block!'); evt.player.sendMessage( evt.player.name + ' broke a block!');
this.unregister(); this.unregister();
} ); } );
@ -65,19 +57,10 @@ object which is returned by the `events.on()` function.
To unregister a listener *outside* of the listener function... To unregister a listener *outside* of the listener function...
```javascript ```javascript
var myBlockBreakListener = events.on( 'block.BlockBreakEvent', function( evt ) { ... } ); var myBlockBreakListener = events.on( Packages.org.bukkit.event.block.BlockBreakEvent, function( evt ) { ... } );
... ...
myBlockBreakListener.unregister(); myBlockBreakListener.unregister();
``` ```
To listen for events using a full class name as the `eventName` parameter...
```javascript
events.on( org.bukkit.event.block.BlockBreakEvent, function( evt ) {
evt.player.sendMessage( evt.player.name + ' broke a block!');
} );
```
[buk2]: http://wiki.bukkit.org/Event_API_Reference [buk2]: http://wiki.bukkit.org/Event_API_Reference
[buk]: http://jd.bukkit.org/dev/apidocs/index.html?org/bukkit/event/Event.html [buk]: http://jd.bukkit.org/dev/apidocs/index.html?org/bukkit/event/Event.html
@ -97,46 +80,35 @@ var nashorn = (typeof Java != 'undefined');
function getHandlerListForEventType( eventType ){ function getHandlerListForEventType( eventType ){
var result = null; var result = null;
var clazz = null; var clazz = null;
if (!(typeof eventType == 'string')){ if (nashorn) {
// it's a fully qualified event class
if (nashorn) { //Nashorn doesn't like when getHandlerList is in a superclass of your event
//so to avoid this problem, call getHandlerList using java.lang.reflect
//Nashorn doesn't like when getHandlerList is in a superclass of your event //methods
//so to avoid this problem, call getHandlerList using java.lang.reflect clazz = eventType['class'];
//methods result = clazz.getMethod("getHandlerList").invoke(null);
clazz = eventType['class'];
result = clazz.getMethod("getHandlerList").invoke(null);
} else {
result = eventType.getHandlerList();
}
} else { } else {
// it's an event class name partial result = eventType.getHandlerList();
if (nashorn) {
clazz = java.lang.Class.forName(bkEventPackage + '' + eventType);
result = clazz.getMethod("getHandlerList").invoke(null);
} else {
var eventType2 = eval( bkEventPackage + eventType );
result = eventType2.getHandlerList();
}
} }
return result; return result;
} }
exports.on = function( exports.on = function(
/* String or java Class */ /* Java Class */
eventType, eventType,
/* function( registeredListener, event) */ /* function( registeredListener, event) */
handler, handler,
/* (optional) String (HIGH, HIGHEST, LOW, LOWEST, NORMAL, MONITOR), */ /* (optional) String (HIGH, HIGHEST, LOW, LOWEST, NORMAL, MONITOR), */
priority ) { priority ) {
var handlerList, var handlerList,
listener = {}, regd,
eventExecutor; eventExecutor;
if ( typeof priority == 'undefined' ) { if ( typeof priority == 'undefined' ) {
priority = bkEventPriority.HIGHEST; priority = bkEventPriority.HIGHEST;
} else { } else {
priority = bkEventPriority[priority.toUpperCase()]; priority = bkEventPriority[priority.toUpperCase().trim()];
} }
handlerList = getHandlerListForEventType (eventType); handlerList = getHandlerListForEventType (eventType);
@ -154,10 +126,10 @@ exports.on = function(
The workaround is to make the ScriptCraftPlugin java class a Listener. The workaround is to make the ScriptCraftPlugin java class a Listener.
Should only unregister() registered plugins in ScriptCraft js code. Should only unregister() registered plugins in ScriptCraft js code.
*/ */
listener.reg = new bkRegisteredListener( __plugin, eventExecutor, priority, __plugin, true ); regd = new bkRegisteredListener( __plugin, eventExecutor, priority, __plugin, true );
handlerList.register( listener.reg ); handlerList.register( regd );
result.unregister = function(){ result.unregister = function(){
handlerList.unregister( listener.reg ); handlerList.unregister( regd );
}; };
return result; return result;
}; };

View file

@ -0,0 +1,27 @@
/*
wph 20140102 - warn if legacy 'craftbukkit/js-plugins' or 'craftbukkit/scriptcraft' directories are present
*/
module.exports = function( jsPluginsRootDir ) {
var cbPluginsDir = jsPluginsRootDir.parentFile,
cbDir = new File(cbPluginsDir.canonicalPath).parentFile,
legacyExists = false,
legacyDirs = [new File( cbDir, 'js-plugins' ),
new File( cbDir, 'scriptcraft' )];
for ( var i = 0; i < legacyDirs.length; i++ ) {
if ( legacyDirs[i].exists()
&& legacyDirs[i].isDirectory() ) {
legacyExists = true;
console.warn('Legacy ScriptCraft directory %s was found. This directory is no longer used.',
legacyDirs[i].canonicalPath);
console.warn('Please put plugins in the plugins/scriptcraft/plugins directory');
}
}
if ( legacyExists ) {
console.info( 'Please note that the working directory for %s is %s',
__plugin, jsPluginsRootDir.canonicalPath );
}
};

View file

@ -714,30 +714,6 @@ function __onEnable ( __engine, __plugin, __script )
}; };
plugins.autoload( global, new File(jsPluginsRootDir,'plugins'), logger ); plugins.autoload( global, new File(jsPluginsRootDir,'plugins'), logger );
/* require('legacy-check')(jsPluginsRootDir);
wph 20140102 - warn if legacy 'craftbukkit/js-plugins' or 'craftbukkit/scriptcraft' directories are present
*/
(function(){
var cbPluginsDir = jsPluginsRootDir.parentFile,
cbDir = new File(cbPluginsDir.canonicalPath).parentFile,
legacyExists = false,
legacyDirs = [new File( cbDir, 'js-plugins' ),
new File( cbDir, 'scriptcraft' )];
for ( var i = 0; i < legacyDirs.length; i++ ) {
if ( legacyDirs[i].exists()
&& legacyDirs[i].isDirectory() ) {
legacyExists = true;
console.warn('Legacy ScriptCraft directory %s was found. This directory is no longer used.',legacyDirs[i].canonicalPath);
console.warn('Please put plugins in the plugins/scriptcraft/plugins directory');
}
}
if ( legacyExists ) {
console.info( 'Please note that the working directory for %s is %s',
__plugin, jsPluginsRootDir.canonicalPath );
}
})();
} }