From b5f273924a1528c16f9af8ae7bd926df75886019 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Tue, 15 Jan 2013 20:55:41 +0000 Subject: [PATCH] cleanup directory names - every plugin should be in its own directory --- js-plugins/arrows/arrows.js | 90 +++++++++++++++++++++++++++++++++++++ js-plugins/events/events.js | 77 +++++++++++++++++++++++++++++++ js-plugins/signs/select.js | 4 +- 3 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 js-plugins/arrows/arrows.js create mode 100644 js-plugins/events/events.js diff --git a/js-plugins/arrows/arrows.js b/js-plugins/arrows/arrows.js new file mode 100644 index 0000000..b30b9b4 --- /dev/null +++ b/js-plugins/arrows/arrows.js @@ -0,0 +1,90 @@ +/************************************************************************* + * + * The arrows mod adds fancy arrows to the game. + * + * Usage: + * /js arrows.sign() turns a targeted sign into a Arrows menu + * /js arrows.normal() sets arrow type to normal. + * /js arrows.explosive() - makes arrows explode. + * /js arrows.teleport() - makes player teleport to where arrow has landed. + * /js arrows.flourish() - makes a tree grow where the arrow lands. + * /js arrows.lightning() - lightning strikes where the arrow lands. + * + * All of the above functions can take an optional player object or name as + * a parameter. E.g. + * + * /js arrows.explosive('player23') makes player23's arrows explosive. + * + ************************************************************************/ +var rootDir = __folder; +load(rootDir + "signs/select.js"); +load(rootDir + "events/events.js"); +var arrows = arrows || {}; +// ------------------------------------------------------------------------ +// Private implementation +// ------------------------------------------------------------------------ +(function(){ + var _players = {}; + // + // setup functions for the arrow types + // + var _types = {normal: 0, explosive: 1, teleport: 2, flourish: 3, lightning: 4}; + for (var i in _types){ + arrows[[i]] = (function(n){ + return function(player){ + if (typeof player == "undefined") + player = __self; + var playerName = null; + if (typeof player == "string") + playerName = player; + else + playerName = player.name; + _players[playerName] = n; + }; + })(_types[i]); + } + if (typeof arrows.sign != "undefined") + return; + + var _arrowSign = + signs.select("Arrow", + ["Normal","Explosive","Teleport","Flourish","Lightning"], + function(player,sign,selectedText,selectedIndex){ + _players[player.name] = selectedIndex; + }); + // + // event handler called when a projectile hits something + // + var _onArrowHit = function(listener,event) + { + var projectile = event.entity; + var world = projectile.world; + var shooter = projectile.shooter; + if (projectile instanceof org.bukkit.entity.Arrow && + shooter instanceof org.bukkit.entity.Player) + { + var arrowType = _players[shooter.name]; + switch (arrowType){ + case 1: + projectile.remove(); + world.createExplosion(projectile.location,2.5); + break; + case 2: + projectile.remove(); + shooter.teleport(projectile.location, + org.bukkit.event.player.PlayerTeleportEvent.TeleportCause.ENDER_PEARL); + break; + case 3: + projectile.remove(); + world.generateTree(projectile.location, org.bukkit.TreeType.BIG_TREE); + break; + case 4: + projectile.remove(); + world.strikeLightning(projectile.location); + break; + } + } + }; + arrows.sign = _arrowSign; + events.on("entity.ProjectileHitEvent",_onArrowHit); +}()); diff --git a/js-plugins/events/events.js b/js-plugins/events/events.js new file mode 100644 index 0000000..310e5d7 --- /dev/null +++ b/js-plugins/events/events.js @@ -0,0 +1,77 @@ +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; + // + // can't have objects that implement multiple interface in javax.scripts.* + // + var theListener = new _event.Listener(){}; + + var _on = function(eventType, handler, priority) + { + if (typeof priority == "undefined"){ + priority = _event.EventPriority.NORMAL; + }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(); + var listener = {}; + var eventExecutor = new _plugin.EventExecutor(){ + execute: function(l,e){ + handler(listener.reg,e); + } + }; + listener.reg = new _plugin.RegisteredListener( + theListener,eventExecutor,priority,__plugin,true + ) + handlerList.register(listener.reg); + return listener.reg; + }; + events.on = _on; + events._eventsLoaded = true; +}()); diff --git a/js-plugins/signs/select.js b/js-plugins/signs/select.js index 720557d..582d7ca 100644 --- a/js-plugins/signs/select.js +++ b/js-plugins/signs/select.js @@ -1,4 +1,4 @@ -load(__folder + "../bukkit/events.js"); +load(__folder + "../events/events.js"); // // signs module declaration // @@ -87,7 +87,7 @@ signs.select = function(/* String */ label, /* Array */ options, /* Function */ // // update it every time player interacts with it. // - bukkit.on("player.PlayerInteractEvent",function(listener, event) { + events.on("player.PlayerInteractEvent",function(listener, event) { if (event.clickedBlock.state.equals(sign)) _updateSign(event.player,sign); });