Fixed problems with homes plugin and added support for tab completion of jsp commands

This commit is contained in:
walterhiggins 2013-01-19 17:01:59 +00:00
parent 68c3c7dd71
commit ba12e76a09
2 changed files with 108 additions and 47 deletions

View file

@ -41,7 +41,7 @@ var verbose = verbose || false;
/* /*
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,warnOnFileNotFound)
{ {
var result = null; var result = null;
var file = new java.io.File(filename); var file = new java.io.File(filename);
@ -58,7 +58,8 @@ var verbose = verbose || false;
__engine.put("__folder",(parent?_canonize(parent):"")+"/"); __engine.put("__folder",(parent?_canonize(parent):"")+"/");
result = __engine.eval(reader); result = __engine.eval(reader);
}else{ }else{
print("Error: " + canonizedFilename + " not found"); if (warnOnFileNotFound)
__plugin.logger.warning(canonizedFilename + " not found");
} }
return result; return result;
}; };
@ -112,7 +113,13 @@ var verbose = verbose || false;
*/ */
var _save = function(object, filename){ var _save = function(object, filename){
print(filename); print(filename);
var objectToStr = JSON.stringify(object); var objectToStr = null;
try{
objectToStr = JSON.stringify(object);
}catch(e){
print("ERROR: " + e.getMessage() + " while saving " + filename);
return;
}
var f = new java.io.File(filename); var f = new java.io.File(filename);
var out = new java.io.PrintWriter(new java.io.FileWriter(f)); var out = new java.io.PrintWriter(new java.io.FileWriter(f));
out.println("__data = " + objectToStr); out.println("__data = " + objectToStr);
@ -147,30 +154,47 @@ var verbose = verbose || false;
var _ready = function( func ){ var _ready = function( func ){
_deferred.push(func); _deferred.push(func);
}; };
var _cmdInterceptors = [];
/* /*
command management - allow for non-ops to execute approved javascript code. command management - allow for non-ops to execute approved javascript code.
*/ */
var _commands = {}; var _commands = {};
var _command = function(name,func){ var _command = function(name,func,options,intercepts){
if (typeof name == "undefined"){ if (typeof name == "undefined"){
// it's an invocation from the Java Plugin! // it's an invocation from the Java Plugin!
if (__cmdArgs.length === 0) if (__cmdArgs.length === 0)
throw new Error("Usage: jsp command-name command-parameters"); throw new Error("Usage: jsp command-name command-parameters");
var name = __cmdArgs[0]; var name = __cmdArgs[0];
func = _commands[name] var cmd = _commands[name];
if (typeof func === "undefined") if (typeof cmd === "undefined"){
throw new Error("Command '" + name + "' does not exist."); // it's not a global command - pass it on to interceptors
var params = []; var intercepted = false;
for (var i =1; i < __cmdArgs.length;i++){ for (var i = 0;i < _cmdInterceptors.length;i++){
params.push("" + __cmdArgs[i]); if (_cmdInterceptors[i](__cmdArgs))
} intercepted = true;
}
if (!intercepted)
__self.sendMessage("Command '" + name + "' is not recognised");
}else{
func = cmd.callback;
var params = [];
for (var i =1; i < __cmdArgs.length;i++){
params.push("" + __cmdArgs[i]);
}
return func(params); return func(params);
}
}else{ }else{
_commands[name] = func; if (typeof options == "undefined")
options = [];
_commands[name] = {callback: func, options: options};
if (intercepts)
_cmdInterceptors.push(func);
return func; return func;
} }
}; };
var _rmCommand = function(name){
delete _commands[name];
};
/* /*
Tab Completion of the /js and /jsp commands Tab Completion of the /js and /jsp commands
*/ */
@ -225,8 +249,14 @@ var verbose = verbose || false;
*/ */
var __onTabCompleteJSP = function() { var __onTabCompleteJSP = function() {
var result = global.__onTC_result; var result = global.__onTC_result;
for (var i in _commands) var args = global.__onTC_args;
result.add(i); var cmd = _commands[args[0]];
if (cmd)
for (var i = 0;i < cmd.options.length; i++)
result.add(cmd.options[i]);
else
for (var i in _commands)
result.add(i);
return result; return result;
}; };
/* /*

View file

@ -1,23 +1,33 @@
/*
The homes plugin lets players set a location as home and return to the location, invite
other players to their home and also visit other player's homes.
*/
plugin("homes", { plugin("homes", {
/* ========================================================================
basic functions
======================================================================== */
help: function(){ help: function(){
return [ return [
/* basic functions */
"/jsp home : Return to your own home", "/jsp home : Return to your own home",
"/jsp home <player> : Go to player's home", "/jsp home <player> : Go to player's home",
"/jsp home set : Set your current location as home", "/jsp home set : Set your current location as home",
"/jsp home delete : Delete your home location", "/jsp home delete : Delete your home location",
/* social */
"/jsp home list : List homes you can visit", "/jsp home list : List homes you can visit",
"/jsp home ilist : List players who can visit your home", "/jsp home ilist : List players who can visit your home",
"/jsp home invite <player> : Invite <player> to your home", "/jsp home invite <player> : Invite <player> to your home",
"/jsp home uninvite <player> : Uninvite <player> to your home", "/jsp home uninvite <player> : Uninvite <player> to your home",
"/jsp home public : Open your home to all players", "/jsp home public : Open your home to all players",
"/jsp home private : Make your home private", "/jsp home private : Make your home private",
/* administration */
"/jsp home listall : Show all houses (ops only)", "/jsp home listall : Show all houses (ops only)",
"/jsp home clear <player> : Clears player's home location (ops only)" "/jsp home clear <player> : Clears player's home location (ops only)"
]; ];
}, },
/* ========================================================================
basic functions
======================================================================== */
go: function(guest, host){ go: function(guest, host){
if (typeof host == "undefined") if (typeof host == "undefined")
host = guest; host = guest;
@ -107,7 +117,7 @@ plugin("homes", {
invitations.push(guest.name); invitations.push(guest.name);
this.store.invites[host.name] = invitations; this.store.invites[host.name] = invitations;
guest.sendMessage(host.name + " has invited you to their home."); guest.sendMessage(host.name + " has invited you to their home.");
guest.sendMessage("type /jsp home go " + host.name + " to accept"); guest.sendMessage("type '/jsp home " + host.name + "' to accept");
}, },
/* /*
Uninvite someone to the home Uninvite someone to the home
@ -156,14 +166,13 @@ plugin("homes", {
} }
}, true); }, true);
/* /*
initialize the store private implementation
*/ */
homes.store.houses = homes.store.houses || {}; (function(){
homes.store.openHouses = homes.store.openHouses || {}; /*
homes.store.invites = homes.store.invites || {}; define a set of command options that can be used by players
*/
command("home", function(params){
var options = { var options = {
set: function(){homes.set();}, set: function(){homes.set();},
'delete': function(){ homes.remove();}, 'delete': function(){ homes.remove();},
@ -174,7 +183,10 @@ command("home", function(params){
__self.sendMessage("There are no homes to visit"); __self.sendMessage("There are no homes to visit");
return; return;
}else{ }else{
__self.sendMessage(["You can visit any of these " + visitable.length + " homes"].concat(visitable)); __self.sendMessage([
"You can visit any of these " + visitable.length + " homes"
,visitable.join(", ")
]);
} }
}, },
ilist: function(){ ilist: function(){
@ -182,8 +194,9 @@ command("home", function(params){
if (potentialVisitors.length == 0) if (potentialVisitors.length == 0)
__self.sendMessage("No one can visit your home"); __self.sendMessage("No one can visit your home");
else else
__self.sendMessage("These " + potentialVisitors.length + "players can visit your home\n" + __self.sendMessage([
JSON.stringify(potentialVisitors)); "These " + potentialVisitors.length + "players can visit your home",
potentialVisitors.join(", ")]);
}, },
invite: function(){ invite: function(){
if (params.length == 1){ if (params.length == 1){
@ -210,15 +223,18 @@ command("home", function(params){
homes.uninvite(__self,guest); homes.uninvite(__self,guest);
}, },
'public': function(){ 'public': function(){
homes.open(__self,params[1]); homes.open(__self,params.slice(1).join(' '));
__self.sendMessage("Your home is open to the public"); __self.sendMessage("Your home is open to the public");
}, },
'private': function(){ homes.close(); }, 'private': function(){
homes.close();
__self.sendMessage("Your home is closed to the public");
},
listall: function(){ listall: function(){
if (!__self.isOp()) if (!__self.isOp())
__self.sendMessage("Only operators can do this"); __self.sendMessage("Only operators can do this");
else else
__self.sendMessage(homes.listall()); __self.sendMessage(homes.listall().join(", "));
}, },
clear: function(){ clear: function(){
if (!__self.isOp()) if (!__self.isOp())
@ -227,18 +243,33 @@ command("home", function(params){
homes.clear(params[1]); homes.clear(params[1]);
} }
}; };
if (params.length == 0){ var optionList = [];
homes.go(); for (var o in options)
return; optionList.push(o);
} /*
var option = options[params[0]]; Expose a set of commands that players can use at the in-game command prompt
if (option) */
option() command("home", function(params){
else{ if (params.length == 0){
var host = getPlayerObject(params[1]); homes.go();
if (!host) return;
__self.sendMessage(param[1] + " is not here"); }
else var option = options[params[0]];
homes.go(__self,host); if (option)
} option();
}); else{
var host = getPlayerObject(params[0]);
if (!host)
__self.sendMessage(params[0] + " is not here");
else
homes.go(__self,host);
}
},optionList);
/*
initialize the store
*/
homes.store.houses = homes.store.houses || {};
homes.store.openHouses = homes.store.openHouses || {};
homes.store.invites = homes.store.invites || {};
}());