2013-01-15 21:55:41 +01:00
|
|
|
var global = this;
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
//
|
|
|
|
// The following code will print a message on screen every time a block is broken in the game
|
|
|
|
//
|
|
|
|
// events.on("block.BlockBreakEvent", function(listener, evt){
|
|
|
|
// print (evt.player.name + " broke a block!");
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// To handle an event only once and unregister from further events...
|
|
|
|
//
|
|
|
|
// events.on("block.BlockBreakEvent", function(listener, evt){
|
|
|
|
// print (evt.player.name + " broke a block!");
|
|
|
|
// evt.handlers.unregister(listener);
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
var events = events || {
|
|
|
|
//
|
|
|
|
// handle events in Minecraft
|
|
|
|
// --------------------------
|
|
|
|
// eventType can be a string (assumed to be a sub package of org.bukkit.event - e.g.
|
|
|
|
// if the string "block.BlockBreakEvent" is supplied then it's converted to the
|
|
|
|
// org.bukkit.event.block.BlockBreakEvent class . For custom event classes, just
|
|
|
|
// supply the custom event class e.g.
|
|
|
|
// events.on(net.yourdomain.events.YourCustomEvent,function(l,e){ ... });
|
|
|
|
//
|
|
|
|
on: function(
|
|
|
|
/* String or java Class */ eventType,
|
|
|
|
/* function( registeredListener, event) */ handler,
|
|
|
|
/* (optional) String (HIGH, HIGHEST, LOW, LOWEST, NORMAL, MONITOR), */ priority
|
|
|
|
){}
|
|
|
|
};
|
|
|
|
//
|
|
|
|
// private implementation from here on in...
|
|
|
|
//
|
|
|
|
(function(){
|
|
|
|
if (events._eventsLoaded){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var _event = org.bukkit.event;
|
|
|
|
var _plugin = org.bukkit.plugin;
|
2013-01-18 00:28:12 +01:00
|
|
|
//
|
|
|
|
// can't have objects that implement multiple interface in javax.scripts.*
|
|
|
|
//
|
2013-01-15 21:55:41 +01:00
|
|
|
var theListener = new _event.Listener(){};
|
|
|
|
|
|
|
|
var _on = function(eventType, handler, priority)
|
|
|
|
{
|
|
|
|
if (typeof priority == "undefined"){
|
2013-01-16 00:50:52 +01:00
|
|
|
priority = _event.EventPriority.HIGHEST;
|
2013-01-15 21:55:41 +01:00
|
|
|
}else{
|
|
|
|
priority = _event.EventPriority[priority];
|
|
|
|
}
|
|
|
|
if (typeof eventType == "string"){
|
|
|
|
var subPkgs = eventType.split('.');
|
|
|
|
eventType = _event[subPkgs[0]];
|
|
|
|
for (var i = 1;i < subPkgs.length; i++){
|
|
|
|
eventType = eventType[subPkgs[i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var handlerList = eventType.getHandlerList();
|
2013-01-18 00:28:12 +01:00
|
|
|
var listener = {};
|
2013-01-15 21:55:41 +01:00
|
|
|
var eventExecutor = new _plugin.EventExecutor(){
|
2013-01-18 00:28:12 +01:00
|
|
|
execute: function(l,e){
|
|
|
|
handler(listener.reg,e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
listener.reg = new _plugin.RegisteredListener(
|
|
|
|
theListener,eventExecutor,priority,__plugin,true
|
|
|
|
)
|
2013-01-15 21:55:41 +01:00
|
|
|
handlerList.register(listener.reg);
|
2013-01-18 00:28:12 +01:00
|
|
|
return listener.reg;
|
2013-01-15 21:55:41 +01:00
|
|
|
};
|
|
|
|
events.on = _on;
|
|
|
|
events._eventsLoaded = true;
|
|
|
|
}());
|