fixed events so that listeners can be removed

This commit is contained in:
walterhiggins 2013-01-08 00:49:05 +00:00
parent 1c041d73da
commit a4ebb4788f

View file

@ -4,10 +4,17 @@ var global = this;
// //
// 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
// //
// bukkit.on('block.BlockBreakEvent',function(evt){ // bukkit.on("block.BlockBreakEvent", function(listener, evt){
// print (evt.player.name + " broke a block!"); // print (evt.player.name + " broke a block!");
// }); // });
// //
// To handle an event only once and unregister from further events...
//
// bukk.on("block.BlockBreakEvent", function(listener, evt){
// print (evt.player.name + " broke a block!");
// evt.handlers.unregister(listener);
// });
//
var bukkit = bukkit || { var bukkit = bukkit || {
// //
// handle events in Minecraft // handle events in Minecraft
@ -33,9 +40,11 @@ var bukkit = bukkit || {
} }
var _event = org.bukkit.event; var _event = org.bukkit.event;
var _plugin = org.bukkit.plugin; var _plugin = org.bukkit.plugin;
//
// can't have objects that implement multiple interface in javax.scripts.*
//
var theListener = new _event.Listener(){}; var theListener = new _event.Listener(){};
//
var _on = function(eventType, handler, priority) var _on = function(eventType, handler, priority)
{ {
if (typeof priority == "undefined"){ if (typeof priority == "undefined"){
@ -51,18 +60,17 @@ var bukkit = bukkit || {
} }
} }
var handlerList = eventType.getHandlerList(); var handlerList = eventType.getHandlerList();
var handlerWrapper = { var listener = {};
var eventExecutor = new _plugin.EventExecutor(){
execute: function(l,e){ execute: function(l,e){
// drop listener handler(listener.reg,e);
handler(e);
} }
}; };
var registeredListener = new _plugin.RegisteredListener(theListener, listener.reg = new _plugin.RegisteredListener(
handlerWrapper, theListener,eventExecutor,priority,__plugin,true
priority, )
plugin, handlerList.register(listener.reg);
true); return listener.reg;
handlerList.register(registeredListener);
}; };
bukkit.on = _on; bukkit.on = _on;
bukkit._eventsLoaded = true; bukkit._eventsLoaded = true;