1.4.6 Added new arrows and signs modules

This commit is contained in:
walterhiggins 2013-01-11 22:32:44 +00:00
parent e5f505dcd5
commit fd11c9ab97
3 changed files with 207 additions and 9 deletions

View file

@ -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);
}
});

89
js-plugins/arrows.js Normal file
View file

@ -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);
}());

118
js-plugins/signs/select.js Normal file
View file

@ -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);
});