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

View file

@ -5,13 +5,14 @@
save (object, filename) - saves an object to a file.
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'
property - this will be created automatically if not already defined.
(its type is object {} )
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'
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.
command (name, function) - defines a command that can be used by non-operators.
@ -32,25 +33,23 @@ var verbose = true;//verbose || false;
var _canonize = function(file){ return file.getCanonicalPath().replaceAll("\\\\","/"); };
var _originalScript = __script;
var parentFileObj = new java.io.File(__script).getParentFile();
var jsPluginsRootDir = parentFileObj.getParentFile();
var jsPluginsRootDirName = _canonize(jsPluginsRootDir);
var parentFileObj = new java.io.File(__script).getParentFile();
var jsPluginsRootDir = parentFileObj.getParentFile();
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 result = null;
{
var result = null;
var file = new java.io.File(filename);
var canonizedFilename = _canonize(file);
var canonizedFilename = _canonize(file);
if (verbose)
print("loading " + canonizedFilename);
if (verbose)
print("loading " + canonizedFilename);
if (file.exists()){
var parent = file.getParentFile();
@ -61,13 +60,11 @@ var verbose = true;//verbose || false;
}else{
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)
{
if (typeof dir == "undefined"){
@ -75,24 +72,22 @@ var verbose = true;//verbose || false;
}
var files = dir.listFiles();
for (var i = 0;i < files.length; i++){
var file = files[i];
var file = files[i];
if (file.isDirectory()){
_listJsFiles(store,file);
}else{
if (file.getCanonicalPath().endsWith(".js") &&
!(file.getName().startsWith("_")) &&
file.exists())
file.exists())
{
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 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){
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();
};
/*
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;
var pluginData = {persistent: isPersistent, module: moduleObject};
moduleObject.store = moduleObject.store || {};
_plugins[moduleName] = pluginData;
/*
plugin mgmt
*/
var _plugins = {};
var _plugin = function(/* String */ moduleName, /* Object */ moduleObject, isPersistent)
{
//
// don't load plugin more than once
//
if (typeof _plugins[moduleName] != "undefined")
return;
if (isPersistent)
moduleObject.store = load(jsPluginsRootDirName + "/" + moduleName + "-store.txt") || {};
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;
}
};
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
*/
/*
Tab Completion of the /js and /jsp commands
*/
var _isJavaObject = function(o){
var result = false;
try {
@ -198,10 +190,10 @@ var verbose = true;//verbose || false;
{
var result = [];
if (_isJavaObject(o))
{
{
propertyLoop:
for (var i in o)
{
{
//
// don't include standard Object methods
//
@ -228,32 +220,32 @@ var verbose = true;//verbose || false;
}
return result.sort();
};
/*
Tab completion for the /jsp commmand
*/
var __onTabCompleteJSP = function() {
var result = global.__onTC_result;
for (var i in _commands)
result.add(i);
return result;
};
/*
Tab completion for the /js command
*/
/*
Tab completion for the /jsp commmand
*/
var __onTabCompleteJSP = function() {
var result = global.__onTC_result;
for (var i in _commands)
result.add(i);
return result;
};
/*
Tab completion for the /js command
*/
var __onTabCompleteJS = function()
{
if (__onTC_cmd.name == "jsp")
return __onTabCompleteJSP()
if (__onTC_cmd.name == "jsp")
return __onTabCompleteJSP()
var _globalSymbols = _getProperties(global)
var result = global.__onTC_result;
var args = global.__onTC_args;
var propsOfLastArg = [];
var statement = args.join(" ");
statement = statement.replace(/^\s+/,"").replace(/\s+$/,"");
statement = statement.replace(/^\s+/,"").replace(/\s+$/,"");
if (statement.length == 0)
propsOfLastArg = _globalSymbols;
propsOfLastArg = _globalSymbols;
else{
var statementSyms = statement.split(/[^a-zA-Z0-9_\.]/);
var lastSymbol = statementSyms[statementSyms.length-1];
@ -274,28 +266,28 @@ var verbose = true;//verbose || false;
lastGoodSymbol = symbol;
}
if (typeof symbol == "undefined"){
//
//
// look up partial matches against last good symbol
//
var objectProps = _getProperties(lastGoodSymbol);
if (name == ""){
// if the last symbol looks like this..
// ScriptCraft.
//
// if the last symbol looks like this..
// ScriptCraft.
//
for (var i =0;i < objectProps.length;i++)
propsOfLastArg.push(statement+objectProps[i]);
}else{
// it looks like this..
// ScriptCraft.co
//
var li = statement.lastIndexOf(name);
statement = statement.substring(0,li);
}else{
// it looks like this..
// ScriptCraft.co
//
var li = statement.lastIndexOf(name);
statement = statement.substring(0,li);
for (var i = 0; i < objectProps.length;i++)
if (objectProps[i].indexOf(name) == 0)
propsOfLastArg.push(statement + objectProps[i]);
}
}else{
var objectProps = _getProperties(symbol);
@ -307,44 +299,51 @@ var verbose = true;//verbose || false;
for (var i = 0;i < _globalSymbols.length; i++)
if (_globalSymbols[i].indexOf(lastSymbol) == 0)
propsOfLastArg.push(statement.replace(lastSymbol,_globalSymbols[i]));
}
}
for (var i = 0;i < propsOfLastArg.length; i++)
result.add(propsOfLastArg[i]);
};
global.load = _load;
global.save = _save;
global.reload = _reload;
global.plugin = _plugin;
global.ready = _ready;
global.command = _command;
/*
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;
global.plugin = _plugin;
global.ready = _ready;
global.command = _command;
global._onTabComplete = __onTabCompleteJS;
global.locationToString = _locToString;
//
// assumes this was loaded from js-plugins/core/
// load all of the plugins.
// load all of the plugins.
//
reload(jsPluginsRootDir);
//
// all modules have loaded
//
for (var i =0;i < _deferred.length;i++)
_deferred[i]();
events.on("server.PluginDisableEvent",function(l,e){
//
// save all plugins which have persistent data
//
for (var moduleName in _plugins){
var pluginData = _plugins[moduleName];
if (pluginData.persistent)
save(pluginData.module.store, jsPluginsRootDirName + "/" + moduleName + "-store.txt");
}
});
//
// all modules have loaded
//
for (var i =0;i < _deferred.length;i++)
_deferred[i]();
events.on("server.PluginDisableEvent",function(l,e){
//
// save all plugins which have persistent data
//
for (var moduleName in _plugins){
var pluginData = _plugins[moduleName];
if (pluginData.persistent)
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)
//
// ... 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...
//
// 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.
//
(function(){
//
// don't want to declare object more than once
//
if (Drone.constructor == Function)
return;
//
// don't want to declare object more than once
//
if (Drone.constructor == Function)
return;
//
// Drone Constructor
@ -270,13 +270,13 @@ var Drone = Drone || {
}else{
// base it on the player's current location
usePlayerCoords = true;
//
// it's possible that drone.js could be loaded by a non-playing op
// (from the server console)
//
if (!playerPos){
return null;
}
//
// it's possible that drone.js could be loaded by a non-playing op
// (from the server console)
//
if (!playerPos){
return null;
}
this.x = playerPos.x;
this.y = playerPos.y;
this.z = playerPos.z;
@ -572,10 +572,10 @@ var Drone = Drone || {
var xo = (a-x0);
var yo = (b-y0);
if (fill){
// wph 20130114 more efficient esp. for large cylinders/spheres
if (yo < 0){
drone.fwd(yo).right(xo).box(block,1,height,Math.abs(yo*2)+1).back(yo).left(xo);
}
// wph 20130114 more efficient esp. for large cylinders/spheres
if (yo < 0){
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');
};

View file

@ -40,9 +40,9 @@ var events = events || {
}
var _event = org.bukkit.event;
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 _on = function(eventType, handler, priority)
@ -60,17 +60,17 @@ var events = events || {
}
}
var handlerList = eventType.getHandlerList();
var listener = {};
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
)
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;
return listener.reg;
};
events.on = _on;
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);
});