Persistence - Yay

This commit is contained in:
walterhiggins 2013-01-17 23:28:12 +00:00
parent eb42cf3c11
commit e46451b13b
6 changed files with 423 additions and 508 deletions

View file

@ -1,36 +1,61 @@
/*************************************************************************
*
* 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
// ------------------------------------------------------------------------
/*
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 arrows = arrows || plugin("arrows",{
/*
turn a sign into a menu of arrow choices
*/
sign: function(sign){},
/*
change player's arrows to normal
*/
normal: function(player){},
/*
change player's arrows to explode on impact
*/
explosive: function(player){},
/*
change player's arrows to teleporting
*/
teleport: function(player){},
/*
change player's arrows to plant trees where they land
*/
flourish: function(player){},
/*
change player's arrows to strike lightning where they land
*/
lightning: function(player){}
},true);
/*
private implementation of normal, explosive, teleport, flourish and lightning functions
*/
(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){
for (var type in _types)
{
arrows[type] = (function(n){
return function(player){
if (typeof player == "undefined")
player = __self;
@ -39,22 +64,32 @@ var arrows = arrows || {};
playerName = player;
else
playerName = player.name;
_players[playerName] = n;
arrows.store.players[playerName] = n;
};
})(_types[i]);
})(_types[type]);
}
if (typeof arrows.sign != "undefined")
return;
var _arrowSign =
signs.select("Arrow",
}());
/*
Arrows depends on 2 other modules: 'signs' and 'events' so the following code
can't execute until all modules have loaded (ready).
*/
ready(function()
{
/*
called when the player chooses an arrow option from a menu sign
*/
var _onMenuChoice = function(event){
if (typeof arrows.store.players == "undefined")
arrows.store.players = {};
arrows.store.players[event.player.name] = event.number;
};
arrows.sign = signs.menu("Arrow",
["Normal","Explosive","Teleport","Flourish","Lightning"],
function(player,sign,selectedText,selectedIndex){
_players[player.name] = selectedIndex;
});
//
// event handler called when a projectile hits something
//
_onMenuChoice );
/*
event handler called when a projectile hits something
*/
var _onArrowHit = function(listener,event)
{
var projectile = event.entity;
@ -63,7 +98,7 @@ var arrows = arrows || {};
if (projectile instanceof org.bukkit.entity.Arrow &&
shooter instanceof org.bukkit.entity.Player)
{
var arrowType = _players[shooter.name];
var arrowType = arrows.store.players[shooter.name];
switch (arrowType){
case 1:
projectile.remove();
@ -85,6 +120,5 @@ var arrows = arrows || {};
}
}
};
arrows.sign = _arrowSign;
events.on("entity.ProjectileHitEvent",_onArrowHit);
}());
});

View file

@ -1,5 +1,5 @@
//
// define these primitive methods used by drone.js (and potentiall others)
// Define these primitive methods used by drone.js (and potentiall others)
//
// getPlayerPos returns the player's x,y,z and yaw (direction)
// getMousePos returns the x,y,z of the current block being targeted.

View file

@ -5,7 +5,8 @@
save (object, filename) - saves an object to a file.
plugin (name, interface, isPersistent) - defines a new plugin. If isPersistent is true then
plugin (name, interface, isPersistent)
- defines a new plugin. If isPersistent is true then
the plugin doesn't have to worry about loading and saving
state - that will be done by the framework. Just make sure
that anything you want to save (and restore) is in the 'store'
@ -38,9 +39,7 @@ var verbose = true;//verbose || false;
/*
Load the contents of the file and evaluate as javascript
*/
var _load = function(filename)
{
@ -64,9 +63,7 @@ var verbose = true;//verbose || false;
return result;
};
/*
recursively walk the given directory and return a list of all .js files
*/
var _listJsFiles = function(store,dir)
{
@ -89,9 +86,7 @@ var verbose = true;//verbose || false;
}
};
/*
Reload all of the .js files in the given directory
*/
var _reload = function(pluginDir)
{
@ -113,22 +108,18 @@ var verbose = true;//verbose || false;
};
/*
Save a javascript object to a file (saves using JSON notation)
*/
var _save = function(object, filename){
print(filename);
var objectToStr = JSON.stringify(object);
var f = new java.io.File(filename);
print(filename);
var out = new java.io.PrintWriter(new java.io.FileWriter(f));
out.println("__data = " + objectToStr);
out.close();
};
/*
plugin mgmt
plugin management
*/
var _plugins = {};
var _plugin = function(/* String */ moduleName, /* Object */ moduleObject, isPersistent)
@ -147,6 +138,7 @@ var verbose = true;//verbose || false;
moduleObject.store = load(jsPluginsRootDirName + "/" + moduleName + "-store.txt") || {};
global[moduleName] = moduleObject;
return moduleObject;
};
/*
allow for deferred execution (once all modules have loaded)
@ -314,6 +306,12 @@ var verbose = true;//verbose || false;
result.add(propsOfLastArg[i]);
};
/*
utility function - convert a Location to a string
*/
var _locToString = function(location){
return JSON.stringify([""+location.world.name,location.x, location.y, location.z]);
};
global.load = _load;
global.save = _save;
global.reload = _reload;
@ -321,6 +319,7 @@ var verbose = true;//verbose || false;
global.ready = _ready;
global.command = _command;
global._onTabComplete = __onTabCompleteJS;
global.locationToString = _locToString;
//
// assumes this was loaded from js-plugins/core/

View file

@ -1,118 +0,0 @@
load(__folder + "../events/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.
//
events.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);
});