From e4f09c7b37fcc837690aea14a7e8eb7beeaf6116 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Sat, 19 Jan 2013 17:02:38 +0000 Subject: [PATCH] New 'alias' plugin which allows each player define their own command aliases --- js-plugins/alias/alias.js | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 js-plugins/alias/alias.js diff --git a/js-plugins/alias/alias.js b/js-plugins/alias/alias.js new file mode 100644 index 0000000..761b854 --- /dev/null +++ b/js-plugins/alias/alias.js @@ -0,0 +1,73 @@ +plugin("alias", { + help: function(){ + return [ + "/jsp alias set : Set a shortcut/alias for one or more commands (separated by ';')\n" + + "For example: '/jsp alias set sunny time set 4000; weather clear'\n" + + "/jsp sunny (is the same as..\n/time set 4000\n/weather clear", + "/jsp alias delete : Removes a shortcut/alias", + "/jsp alias list : shows a list of the player's command aliases", + "/jsp alias help : Shows this message" + ]; + }, + set: function(player, alias, commands){ + var aliases = this.store.players; + var name = player.name; + aliases[name] = aliases[name] || {}; + aliases[name][alias] = commands; + }, + remove: function(player, alias){ + var aliases = this.store.players; + if (aliases[player.name]) + delete aliases[player.name][alias]; + }, + list: function(player){ + var result = []; + var aliases = this.store.players[player.name]; + for (var a in aliases) + result.push(a + " = " + aliases[a].join(";")); + return result; + } +},true); + +alias.store.players = alias.store.players || {}; + +command("alias",function(params){ + /* + this function also intercepts command options for /jsp + */ + if (params[0] === "help"){ + __self.sendMessage(alias.help()); + return; + } + if (params[0] === "set"){ + var aliasCmd = params[1]; + var cmdStr = params.slice(2).join(' '); + var cmds = cmdStr.split(';'); + alias.set(__self,aliasCmd,cmds); + return; + } + if (params[0] === "delete"){ + alias.remove(__self,params[1]); + return ; + } + if (params[0] === "list"){ + __self.sendMessage(alias.list(__self)); + return; + } + + var playerHasAliases = alias.store.players[__self.name]; + if (!playerHasAliases) + return false; + // is it an alias? + var commands = playerHasAliases[params[0]]; + if (!commands) + return false; + for (var i = 0;i < commands.length; i++){ + // fill in template + var cmd = commands[i]; + cmd = cmd.replace(/{([0-9]*)}/g,function(dummy,index){ return params[index];}) + __self.performCommand(cmd); + } + return true; + +},["help","set","delete","list"],true);