From fd11c9ab9756a01241d258977c0295e1be0a0a72 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Fri, 11 Jan 2013 22:32:44 +0000 Subject: [PATCH] 1.4.6 Added new arrows and signs modules --- .../experimental/exploding-arrows.js | 9 -- js-plugins/arrows.js | 89 +++++++++++++ js-plugins/signs/select.js | 118 ++++++++++++++++++ 3 files changed, 207 insertions(+), 9 deletions(-) delete mode 100644 bukkit-plugin/experimental/exploding-arrows.js create mode 100644 js-plugins/arrows.js create mode 100644 js-plugins/signs/select.js diff --git a/bukkit-plugin/experimental/exploding-arrows.js b/bukkit-plugin/experimental/exploding-arrows.js deleted file mode 100644 index 8be0610..0000000 --- a/bukkit-plugin/experimental/exploding-arrows.js +++ /dev/null @@ -1,9 +0,0 @@ -importPackage(org.bukkit.entity); -bukkit.on("entity.ProjectileHitEvent", function(listener, event){ - var projectile = event.entity; - var world = projectile.world; - if (projectile instanceof Arrow && projectile.shooter instanceof Player){ - projectile.remove(); - world.createExplosion(projectile.location,2.5); - } -}); diff --git a/js-plugins/arrows.js b/js-plugins/arrows.js new file mode 100644 index 0000000..2741617 --- /dev/null +++ b/js-plugins/arrows.js @@ -0,0 +1,89 @@ +/************************************************************************* + * + * 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. + * + ************************************************************************/ + +load(__folder + "signs/select.js"); +load(__folder + "bukkit/events.js"); +var arrows = arrows || {}; +// ------------------------------------------------------------------------ +// Private implementation +// ------------------------------------------------------------------------ +(function(){ + // + // 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 _players = {}; + + 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 Arrow && shooter instanceof 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; + bukkit.on("entity.ProjectileHitEvent",_onArrowHit); +}()); diff --git a/js-plugins/signs/select.js b/js-plugins/signs/select.js new file mode 100644 index 0000000..720557d --- /dev/null +++ b/js-plugins/signs/select.js @@ -0,0 +1,118 @@ +load(__folder + "../bukkit/events.js"); +// +// signs module declaration +// +var signs = signs || {}; +/** + * signs.select returns a function which when passed an org.bukkit.block.Sign object, will + * turn that sign into an interactive menu with a list of options which can be changed by + * right-clicking the sign. + * Usage: + * + * var dinnerMenu = signs.select("Dinner",["Lamb","Pork","Chicken","Duck","Beef"], + * function(player,sign,selectedText,selectedIndex){ + * player.sendMessage("You chose " + selectedText); + * }); + * ... get an org.bukkit.block.Sign object... + * var aSign = aBlock.state; + * ... turn the sign into an interactive menu. + * var dinnerMenuSign = dinnerMenu(aSign); + * + */ +signs.select = function(/* String */ label, /* Array */ options, /* Function */ callback, /* Number */ selectedIndex){}; + +(function(){ + if (typeof signs._refresh != "undefined") + return; + var _refresh = function(p_sign,p_selectedIndex,p_displayOptions) + { + var optLen = p_displayOptions.length; + // the offset is where the menu window begins + var offset = Math.max(0, Math.min(optLen-3, Math.floor(p_selectedIndex/3) * 3)); + for (var i = 0;i < 3; i++){ + var text = ""; + if (offset+i < optLen) + text = p_displayOptions[offset+i]; + if (offset+i == p_selectedIndex) + text = ("" + text).replace(/^ /,">"); + p_sign.setLine(i+1,text); + } + p_sign.update(true); + }; + var _select = function( + /* String */ label, + /* Array */ options, + /* Function */ callback, + /* Number */ selectedIndex) + { + importPackage(org.bukkit.block); + + if (typeof selectedIndex == "undefined") + selectedIndex = 0; + + // + // variables common to all instances of this menu can go here + // + var labelPadding = "---------------"; + var optionPadding = " "; + + var paddedLabel = (labelPadding+label+labelPadding).substr(((label.length+30)/2)-7,15); + var optLen = options.length; + var displayOptions = []; + for (var i =0;i < options.length;i++){ + displayOptions[i] = (" " + options[i] + optionPadding).substring(0,15); + } + + return function(/* Sign */ sign){ + if (typeof sign == "undefined"){ + var mouseLoc = getMousePos(); + if (mouseLoc){ + sign = mouseLoc.block.state; + }else{ + throw new Exception("You must provide a sign!"); + } + } + // + // per-sign variables go here + // + var cSelectedIndex = selectedIndex; + sign.setLine(0,paddedLabel); + var _updateSign = function(p_player,p_sign) { + cSelectedIndex = (cSelectedIndex+1) % optLen; + _refresh(p_sign,cSelectedIndex,displayOptions); + callback(p_player,p_sign,options[cSelectedIndex],cSelectedIndex); + }; + // initialize the sign + _refresh(sign,cSelectedIndex,displayOptions); + // + // update it every time player interacts with it. + // + bukkit.on("player.PlayerInteractEvent",function(listener, event) { + if (event.clickedBlock.state.equals(sign)) + _updateSign(event.player,sign); + }); + }; + }; + signs.select = _select; +}()); + +// +// Usage: +// In game, create a sign , target it and type /js signs.testSelect() +// +signs.testSelect = signs.select("Dinner", + ["Lamb","Pork","Chicken","Duck","Beef"], + function(player,sign,selectedText,selectedIndex){ + player.sendMessage("You chose " + selectedText); + }); +// +// This is an example sign that displays a menu of times of day +// interacting with the sign will change the time of day accordingly. +// +// In game, create a sign , target it and type /js signs.timeOfDay() +// +signs.timeOfDay = signs.select("Time", + ["Dawn","Midday","Dusk","Midnight"], + function(player,sign,selectedText,selectedIndex){ + player.location.world.setTime(selectedIndex*6000); + });