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,90 +1,124 @@
/************************************************************************* /*
*
* 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 = The arrows mod adds fancy arrows to the game.
signs.select("Arrow",
["Normal","Explosive","Teleport","Flourish","Lightning"], Usage:
function(player,sign,selectedText,selectedIndex){
_players[player.name] = selectedIndex; /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.
// event handler called when a projectile hits something /js arrows.teleport() - makes player teleport to where arrow has landed.
// /js arrows.flourish() - makes a tree grow where the arrow lands.
var _onArrowHit = function(listener,event) /js arrows.lightning() - lightning strikes where the arrow lands.
{
var projectile = event.entity; All of the above functions can take an optional player object or name as
var world = projectile.world; a parameter. E.g.
var shooter = projectile.shooter;
if (projectile instanceof org.bukkit.entity.Arrow && /js arrows.explosive('player23') makes player23's arrows explosive.
shooter instanceof org.bukkit.entity.Player)
{ */
var arrowType = _players[shooter.name];
switch (arrowType){ var arrows = arrows || plugin("arrows",{
case 1: /*
projectile.remove(); turn a sign into a menu of arrow choices
world.createExplosion(projectile.location,2.5); */
break; sign: function(sign){},
case 2: /*
projectile.remove(); change player's arrows to normal
shooter.teleport(projectile.location, */
org.bukkit.event.player.PlayerTeleportEvent.TeleportCause.ENDER_PEARL); normal: function(player){},
break; /*
case 3: change player's arrows to explode on impact
projectile.remove(); */
world.generateTree(projectile.location, org.bukkit.TreeType.BIG_TREE); explosive: function(player){},
break; /*
case 4: change player's arrows to teleporting
projectile.remove(); */
world.strikeLightning(projectile.location); teleport: function(player){},
break; /*
} change player's arrows to plant trees where they land
} */
}; flourish: function(player){},
arrows.sign = _arrowSign; /*
events.on("entity.ProjectileHitEvent",_onArrowHit); 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(){
//
// setup functions for the arrow types
//
var _types = {normal: 0, explosive: 1, teleport: 2, flourish: 3, lightning: 4};
for (var type in _types)
{
arrows[type] = (function(n){
return function(player){
if (typeof player == "undefined")
player = __self;
var playerName = null;
if (typeof player == "string")
playerName = player;
else
playerName = player.name;
arrows.store.players[playerName] = n;
};
})(_types[type]);
}
}()); }());
/*
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"],
_onMenuChoice );
/*
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 = arrows.store.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;
}
}
};
events.on("entity.ProjectileHitEvent",_onArrowHit);
});

View file

@ -1,131 +1,131 @@
// //
// 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) // getPlayerPos returns the player's x,y,z and yaw (direction)
// getMousePos returns the x,y,z of the current block being targeted. // getMousePos returns the x,y,z of the current block being targeted.
// putBlock(x,y,z,blockId,metadata) puts a block at a location in current world // putBlock(x,y,z,blockId,metadata) puts a block at a location in current world
// getBlock(x,y,z) gets the block and metadata (returned as a string in form '35:15') // getBlock(x,y,z) gets the block and metadata (returned as a string in form '35:15')
// putSign(texts,x,y,z,blockId,metadata) puts a sign at the given location // putSign(texts,x,y,z,blockId,metadata) puts a sign at the given location
// notifyAdministrators(msg) sends a message to all admins/ops. // notifyAdministrators(msg) sends a message to all admins/ops.
// echo(msg) prints a message on screen to current user. // echo(msg) prints a message on screen to current user.
// //
(function(){ (function(){
// //
// only execute once // only execute once
// //
if (typeof getPlayerPos != "undefined"){ if (typeof getPlayerPos != "undefined"){
return; return;
} }
var _getPlayerPos = function(){ var _getPlayerPos = function(){
if (typeof __self == "undefined") if (typeof __self == "undefined")
return; return;
return __self.location; return __self.location;
}; };
var _getMousePos = function(){ var _getMousePos = function(){
if (typeof __self == "undefined") if (typeof __self == "undefined")
return; return;
// __self might be CONSOLE or a CommandBlock // __self might be CONSOLE or a CommandBlock
if (!__self.getTargetBlock) if (!__self.getTargetBlock)
return; return;
var targetedBlock = __self.getTargetBlock(null,5); var targetedBlock = __self.getTargetBlock(null,5);
if (targetedBlock == null || targetedBlock.isEmpty()){ if (targetedBlock == null || targetedBlock.isEmpty()){
return null; return null;
} }
return targetedBlock.location; return targetedBlock.location;
}; };
var _putBlock = function(x,y,z,blockId,metadata){ var _putBlock = function(x,y,z,blockId,metadata){
if (typeof metadata == "undefined"){ if (typeof metadata == "undefined"){
metadata = 0; metadata = 0;
} }
var world = _getWorld(); var world = _getWorld();
if (!world) if (!world)
return; return;
var block = world.getBlockAt(x,y,z); var block = world.getBlockAt(x,y,z);
if (blockId === 6){ if (blockId === 6){
var treeType = null; var treeType = null;
switch (metadata){ switch (metadata){
case 0: case 0:
treeType = org.bukkit.TreeType.BIG_TREE; treeType = org.bukkit.TreeType.BIG_TREE;
break; break;
case 1: case 1:
treeType = org.bukkit.TreeType.REDWOOD; treeType = org.bukkit.TreeType.REDWOOD;
break; break;
case 2: case 2:
treeType = org.bukkit.TreeType.BIRCH; treeType = org.bukkit.TreeType.BIRCH;
break; break;
case 3: case 3:
treeType = org.bukkit.TreeType.JUNGLE; treeType = org.bukkit.TreeType.JUNGLE;
break; break;
} }
return world.generateTree(block.location,treeType); return world.generateTree(block.location,treeType);
}else{ }else{
block.setTypeId(blockId); block.setTypeId(blockId);
block.setData(metadata); block.setData(metadata);
} }
}; };
var _putSign = function(texts, x, y, z, blockId, meta){ var _putSign = function(texts, x, y, z, blockId, meta){
putBlock(x,y,z,blockId,meta); putBlock(x,y,z,blockId,meta);
var block = _getBlockObject(x,y,z); var block = _getBlockObject(x,y,z);
state = block.state; state = block.state;
if (state instanceof org.bukkit.block.Sign){ if (state instanceof org.bukkit.block.Sign){
for (var i = 0;i < texts.length; i++) for (var i = 0;i < texts.length; i++)
state.setLine(i%4,texts[i]); state.setLine(i%4,texts[i]);
state.update(true); state.update(true);
} }
}; };
var _getBlock = function(x,y,z){ var _getBlock = function(x,y,z){
var block = _getBlockObject(x,y,z); var block = _getBlockObject(x,y,z);
if (block) if (block)
return "" + block.typeId + ":" + block.data; return "" + block.typeId + ":" + block.data;
}; };
var _getBlockObject = function(x,y,z){ var _getBlockObject = function(x,y,z){
var world = _getWorld(); var world = _getWorld();
if (world) if (world)
return world.getBlockAt(x,y,z); return world.getBlockAt(x,y,z);
}; };
var _getWorld = function(){ var _getWorld = function(){
if (typeof __self == "undefined") if (typeof __self == "undefined")
return; return;
if (__self instanceof org.bukkit.command.BlockCommandSender) if (__self instanceof org.bukkit.command.BlockCommandSender)
return __self.block.location.world; return __self.block.location.world;
if (__self instanceof org.bukkit.entity.Player) if (__self instanceof org.bukkit.entity.Player)
return __self.location.world; return __self.location.world;
}; };
var _notifyAdministrators = function(msg){ var _notifyAdministrators = function(msg){
var ops = __plugin.server.operators; var ops = __plugin.server.operators;
for (var i = 0; i < ops.size();i++){ for (var i = 0; i < ops.size();i++){
if (ops[i].isOnline()) if (ops[i].isOnline())
ops[i].player.chat(msg); ops[i].player.chat(msg);
} }
__plugin.logger.info(msg); __plugin.logger.info(msg);
}; };
var _echo = function(msg){ var _echo = function(msg){
__plugin.logger.info(msg); __plugin.logger.info(msg);
if (typeof __self == "undefined"){ if (typeof __self == "undefined"){
java.lang.System.out.println(msg); java.lang.System.out.println(msg);
return; return;
} }
__self.sendMessage(msg); __self.sendMessage(msg);
}; };
getPlayerPos = _getPlayerPos; getPlayerPos = _getPlayerPos;
getMousePos = _getMousePos; getMousePos = _getMousePos;
putBlock = _putBlock; putBlock = _putBlock;
getBlock = _getBlock; getBlock = _getBlock;
putSign = _putSign; putSign = _putSign;
notifyAdministrators = _notifyAdministrators; notifyAdministrators = _notifyAdministrators;
echo = _echo; echo = _echo;
}()); }());

View file

@ -5,12 +5,13 @@
save (object, filename) - saves an object to a file. 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)
the plugin doesn't have to worry about loading and saving - defines a new plugin. If isPersistent is true then
state - that will be done by the framework. Just make sure the plugin doesn't have to worry about loading and saving
that anything you want to save (and restore) is in the 'store' state - that will be done by the framework. Just make sure
property - this will be created automatically if not already defined. that anything you want to save (and restore) is in the 'store'
(its type is object {} ) property - this will be created automatically if not already defined.
(its type is object {} )
ready (function) - specifies code to be executed only when all the plugins have loaded. ready (function) - specifies code to be executed only when all the plugins have loaded.
@ -32,25 +33,23 @@ var verbose = true;//verbose || false;
var _canonize = function(file){ return file.getCanonicalPath().replaceAll("\\\\","/"); }; var _canonize = function(file){ return file.getCanonicalPath().replaceAll("\\\\","/"); };
var _originalScript = __script; var _originalScript = __script;
var parentFileObj = new java.io.File(__script).getParentFile(); var parentFileObj = new java.io.File(__script).getParentFile();
var jsPluginsRootDir = parentFileObj.getParentFile(); var jsPluginsRootDir = parentFileObj.getParentFile();
var jsPluginsRootDirName = _canonize(jsPluginsRootDir); var jsPluginsRootDirName = _canonize(jsPluginsRootDir);
/* /*
Load the contents of the file and evaluate as javascript
Load the contents of the file and evaluate as javascript */
*/
var _load = function(filename) var _load = function(filename)
{ {
var result = null; var result = null;
var file = new java.io.File(filename); var file = new java.io.File(filename);
var canonizedFilename = _canonize(file); var canonizedFilename = _canonize(file);
if (verbose) if (verbose)
print("loading " + canonizedFilename); print("loading " + canonizedFilename);
if (file.exists()){ if (file.exists()){
var parent = file.getParentFile(); var parent = file.getParentFile();
@ -61,13 +60,11 @@ var verbose = true;//verbose || false;
}else{ }else{
print("Error: " + canonizedFilename + " not found"); print("Error: " + canonizedFilename + " not found");
} }
return result; return result;
}; };
/* /*
recursively walk the given directory and return a list of all .js files
recursively walk the given directory and return a list of all .js files */
*/
var _listJsFiles = function(store,dir) var _listJsFiles = function(store,dir)
{ {
if (typeof dir == "undefined"){ if (typeof dir == "undefined"){
@ -75,24 +72,22 @@ var verbose = true;//verbose || false;
} }
var files = dir.listFiles(); var files = dir.listFiles();
for (var i = 0;i < files.length; i++){ for (var i = 0;i < files.length; i++){
var file = files[i]; var file = files[i];
if (file.isDirectory()){ if (file.isDirectory()){
_listJsFiles(store,file); _listJsFiles(store,file);
}else{ }else{
if (file.getCanonicalPath().endsWith(".js") && if (file.getCanonicalPath().endsWith(".js") &&
!(file.getName().startsWith("_")) && !(file.getName().startsWith("_")) &&
file.exists()) file.exists())
{ {
store.push(file); store.push(file);
} }
} }
} }
}; };
/* /*
Reload all of the .js files in the given directory
Reload all of the .js files in the given directory */
*/
var _reload = function(pluginDir) var _reload = function(pluginDir)
{ {
var jsFiles = []; var jsFiles = [];
@ -112,76 +107,73 @@ 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);
var out = new java.io.PrintWriter(new java.io.FileWriter(f));
out.println("__data = " + objectToStr);
out.close();
};
/*
plugin management
*/
var _plugins = {};
var _plugin = function(/* String */ moduleName, /* Object */ moduleObject, isPersistent)
{
//
// don't load plugin more than once
//
if (typeof _plugins[moduleName] != "undefined")
return;
Save a javascript object to a file (saves using JSON notation) var pluginData = {persistent: isPersistent, module: moduleObject};
moduleObject.store = moduleObject.store || {};
_plugins[moduleName] = pluginData;
*/ if (isPersistent)
var _save = function(object, filename){ moduleObject.store = load(jsPluginsRootDirName + "/" + moduleName + "-store.txt") || {};
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();
};
global[moduleName] = moduleObject;
return moduleObject;
};
/*
allow for deferred execution (once all modules have loaded)
*/
var _deferred = [];
var _ready = function( func ){
_deferred.push(func);
};
/*
command management - allow for non-ops to execute approved javascript code.
*/
var _commands = {};
var _command = function(name,func){
if (typeof name == "undefined"){
// it's an invocation from the Java Plugin!
if (__cmdArgs.length === 0)
throw new Error("Usage: jsp command-name command-parameters");
var name = __cmdArgs[0];
func = _commands[name]
if (typeof func === "undefined")
throw new Error("Command '" + name + "' does not exist.");
var params = [];
for (var i =1; i < __cmdArgs.length;i++){
params.push("" + __cmdArgs[i]);
}
return func(params);
}else{
_commands[name] = func;
return func;
}
};
/* /*
plugin mgmt Tab Completion of the /js and /jsp commands
*/ */
var _plugins = {};
var _plugin = function(/* String */ moduleName, /* Object */ moduleObject, isPersistent)
{
//
// don't load plugin more than once
//
if (typeof _plugins[moduleName] != "undefined")
return;
var pluginData = {persistent: isPersistent, module: moduleObject};
moduleObject.store = moduleObject.store || {};
_plugins[moduleName] = pluginData;
if (isPersistent)
moduleObject.store = load(jsPluginsRootDirName + "/" + moduleName + "-store.txt") || {};
global[moduleName] = moduleObject;
};
/*
allow for deferred execution (once all modules have loaded)
*/
var _deferred = [];
var _ready = function( func ){
_deferred.push(func);
};
/*
command management - allow for non-ops to execute approved javascript code.
*/
var _commands = {};
var _command = function(name,func){
if (typeof name == "undefined"){
// it's an invocation from the Java Plugin!
if (__cmdArgs.length === 0)
throw new Error("Usage: jsp command-name command-parameters");
var name = __cmdArgs[0];
func = _commands[name]
if (typeof func === "undefined")
throw new Error("Command '" + name + "' does not exist.");
var params = [];
for (var i =1; i < __cmdArgs.length;i++){
params.push("" + __cmdArgs[i]);
}
return func(params);
}else{
_commands[name] = func;
return func;
}
};
/*
Tab Completion of the /js and /jsp commands
*/
var _isJavaObject = function(o){ var _isJavaObject = function(o){
var result = false; var result = false;
try { try {
@ -198,10 +190,10 @@ var verbose = true;//verbose || false;
{ {
var result = []; var result = [];
if (_isJavaObject(o)) if (_isJavaObject(o))
{ {
propertyLoop: propertyLoop:
for (var i in o) for (var i in o)
{ {
// //
// don't include standard Object methods // don't include standard Object methods
// //
@ -228,32 +220,32 @@ var verbose = true;//verbose || false;
} }
return result.sort(); return result.sort();
}; };
/* /*
Tab completion for the /jsp commmand Tab completion for the /jsp commmand
*/ */
var __onTabCompleteJSP = function() { var __onTabCompleteJSP = function() {
var result = global.__onTC_result; var result = global.__onTC_result;
for (var i in _commands) for (var i in _commands)
result.add(i); result.add(i);
return result; return result;
}; };
/* /*
Tab completion for the /js command Tab completion for the /js command
*/ */
var __onTabCompleteJS = function() var __onTabCompleteJS = function()
{ {
if (__onTC_cmd.name == "jsp") if (__onTC_cmd.name == "jsp")
return __onTabCompleteJSP() return __onTabCompleteJSP()
var _globalSymbols = _getProperties(global) var _globalSymbols = _getProperties(global)
var result = global.__onTC_result; var result = global.__onTC_result;
var args = global.__onTC_args; var args = global.__onTC_args;
var propsOfLastArg = []; var propsOfLastArg = [];
var statement = args.join(" "); var statement = args.join(" ");
statement = statement.replace(/^\s+/,"").replace(/\s+$/,""); statement = statement.replace(/^\s+/,"").replace(/\s+$/,"");
if (statement.length == 0) if (statement.length == 0)
propsOfLastArg = _globalSymbols; propsOfLastArg = _globalSymbols;
else{ else{
var statementSyms = statement.split(/[^a-zA-Z0-9_\.]/); var statementSyms = statement.split(/[^a-zA-Z0-9_\.]/);
var lastSymbol = statementSyms[statementSyms.length-1]; var lastSymbol = statementSyms[statementSyms.length-1];
@ -274,23 +266,23 @@ var verbose = true;//verbose || false;
lastGoodSymbol = symbol; lastGoodSymbol = symbol;
} }
if (typeof symbol == "undefined"){ if (typeof symbol == "undefined"){
// //
// look up partial matches against last good symbol // look up partial matches against last good symbol
// //
var objectProps = _getProperties(lastGoodSymbol); var objectProps = _getProperties(lastGoodSymbol);
if (name == ""){ if (name == ""){
// if the last symbol looks like this.. // if the last symbol looks like this..
// ScriptCraft. // ScriptCraft.
// //
for (var i =0;i < objectProps.length;i++) for (var i =0;i < objectProps.length;i++)
propsOfLastArg.push(statement+objectProps[i]); propsOfLastArg.push(statement+objectProps[i]);
}else{ }else{
// it looks like this.. // it looks like this..
// ScriptCraft.co // ScriptCraft.co
// //
var li = statement.lastIndexOf(name); var li = statement.lastIndexOf(name);
statement = statement.substring(0,li); statement = statement.substring(0,li);
for (var i = 0; i < objectProps.length;i++) for (var i = 0; i < objectProps.length;i++)
if (objectProps[i].indexOf(name) == 0) if (objectProps[i].indexOf(name) == 0)
@ -314,36 +306,43 @@ var verbose = true;//verbose || false;
result.add(propsOfLastArg[i]); result.add(propsOfLastArg[i]);
}; };
global.load = _load; /*
global.save = _save; utility function - convert a Location to a string
global.reload = _reload; */
global.plugin = _plugin; var _locToString = function(location){
global.ready = _ready; return JSON.stringify([""+location.world.name,location.x, location.y, location.z]);
global.command = _command; };
global.load = _load;
global.save = _save;
global.reload = _reload;
global.plugin = _plugin;
global.ready = _ready;
global.command = _command;
global._onTabComplete = __onTabCompleteJS; global._onTabComplete = __onTabCompleteJS;
global.locationToString = _locToString;
// //
// assumes this was loaded from js-plugins/core/ // assumes this was loaded from js-plugins/core/
// load all of the plugins. // load all of the plugins.
// //
reload(jsPluginsRootDir); reload(jsPluginsRootDir);
// //
// all modules have loaded // all modules have loaded
// //
for (var i =0;i < _deferred.length;i++) for (var i =0;i < _deferred.length;i++)
_deferred[i](); _deferred[i]();
events.on("server.PluginDisableEvent",function(l,e){ events.on("server.PluginDisableEvent",function(l,e){
// //
// save all plugins which have persistent data // save all plugins which have persistent data
// //
for (var moduleName in _plugins){ for (var moduleName in _plugins){
var pluginData = _plugins[moduleName]; var pluginData = _plugins[moduleName];
if (pluginData.persistent) if (pluginData.persistent)
save(pluginData.module.store, jsPluginsRootDirName + "/" + moduleName + "-store.txt"); save(pluginData.module.store, jsPluginsRootDirName + "/" + moduleName + "-store.txt");
} }
}); });
}()); }());

View file

@ -19,8 +19,8 @@ var Drone = Drone || {
// /js box(5).right(2).box('35:15',4,9,1) // /js box(5).right(2).box('35:15',4,9,1)
// //
// ... creates a single wooden block and a 2001 black obelisk that is // ... creates a single wooden block and a 2001 black obelisk that is
// 4 wide x 9 tall x 1 long in size. // 4 wide x 9 tall x 1 long in size.
// //
// If you want to see what else ScriptCraft's Drone can do, read on... // If you want to see what else ScriptCraft's Drone can do, read on...
// //
// creating a new drone // creating a new drone
@ -248,11 +248,11 @@ var Drone = Drone || {
// There is no need to read any further unless you want to understand how the Drone object works. // There is no need to read any further unless you want to understand how the Drone object works.
// //
(function(){ (function(){
// //
// don't want to declare object more than once // don't want to declare object more than once
// //
if (Drone.constructor == Function) if (Drone.constructor == Function)
return; return;
// //
// Drone Constructor // Drone Constructor
@ -270,13 +270,13 @@ var Drone = Drone || {
}else{ }else{
// base it on the player's current location // base it on the player's current location
usePlayerCoords = true; usePlayerCoords = true;
// //
// it's possible that drone.js could be loaded by a non-playing op // it's possible that drone.js could be loaded by a non-playing op
// (from the server console) // (from the server console)
// //
if (!playerPos){ if (!playerPos){
return null; return null;
} }
this.x = playerPos.x; this.x = playerPos.x;
this.y = playerPos.y; this.y = playerPos.y;
this.z = playerPos.z; this.z = playerPos.z;
@ -572,10 +572,10 @@ var Drone = Drone || {
var xo = (a-x0); var xo = (a-x0);
var yo = (b-y0); var yo = (b-y0);
if (fill){ if (fill){
// wph 20130114 more efficient esp. for large cylinders/spheres // wph 20130114 more efficient esp. for large cylinders/spheres
if (yo < 0){ if (yo < 0){
drone.fwd(yo).right(xo).box(block,1,height,Math.abs(yo*2)+1).back(yo).left(xo); drone.fwd(yo).right(xo).box(block,1,height,Math.abs(yo*2)+1).back(yo).left(xo);
} }
} }
gotoxy(xo,yo).box(block,1,height,1).move('center'); gotoxy(xo,yo).box(block,1,height,1).move('center');
}; };

View file

@ -40,9 +40,9 @@ var events = events || {
} }
var _event = org.bukkit.event; var _event = org.bukkit.event;
var _plugin = org.bukkit.plugin; var _plugin = org.bukkit.plugin;
// //
// can't have objects that implement multiple interface in javax.scripts.* // can't have objects that implement multiple interface in javax.scripts.*
// //
var theListener = new _event.Listener(){}; var theListener = new _event.Listener(){};
var _on = function(eventType, handler, priority) var _on = function(eventType, handler, priority)
@ -60,17 +60,17 @@ var events = events || {
} }
} }
var handlerList = eventType.getHandlerList(); var handlerList = eventType.getHandlerList();
var listener = {}; var listener = {};
var eventExecutor = new _plugin.EventExecutor(){ var eventExecutor = new _plugin.EventExecutor(){
execute: function(l,e){ execute: function(l,e){
handler(listener.reg,e); handler(listener.reg,e);
} }
}; };
listener.reg = new _plugin.RegisteredListener( listener.reg = new _plugin.RegisteredListener(
theListener,eventExecutor,priority,__plugin,true theListener,eventExecutor,priority,__plugin,true
) )
handlerList.register(listener.reg); handlerList.register(listener.reg);
return listener.reg; return listener.reg;
}; };
events.on = _on; events.on = _on;
events._eventsLoaded = true; events._eventsLoaded = true;

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