better tab completion on /jsp issue #35

This commit is contained in:
walterhiggins 2013-01-26 17:49:11 +00:00
parent 446ba9ad7b
commit 1732bf15dd
2 changed files with 101 additions and 75 deletions

View file

@ -1,73 +1,75 @@
plugin("alias", { plugin("alias", {
help: function(){ help: function(){
return [ return [
"/jsp alias set <alias> <commands> : Set a shortcut/alias for one or more commands (separated by ';')\n" + "/jsp alias set <alias> <commands> : Set a shortcut/alias for one or more commands (separated by ';')\n" +
"For example: '/jsp alias set sunny time set 4000; weather clear'\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 sunny (is the same as..\n/time set 4000\n/weather clear",
"/jsp alias delete <alias> : Removes a shortcut/alias", "/jsp alias delete <alias> : Removes a shortcut/alias",
"/jsp alias list : shows a list of the player's command aliases", "/jsp alias list : shows a list of the player's command aliases",
"/jsp alias help : Shows this message" "/jsp alias help : Shows this message"
]; ];
}, },
set: function(player, alias, commands){ set: function(player, alias, commands){
var aliases = this.store.players; var aliases = this.store.players;
var name = player.name; var name = player.name;
aliases[name] = aliases[name] || {}; aliases[name] = aliases[name] || {};
aliases[name][alias] = commands; aliases[name][alias] = commands;
}, },
remove: function(player, alias){ remove: function(player, alias){
var aliases = this.store.players; var aliases = this.store.players;
if (aliases[player.name]) if (aliases[player.name])
delete aliases[player.name][alias]; delete aliases[player.name][alias];
}, },
list: function(player){ list: function(player){
var result = []; var result = [];
var aliases = this.store.players[player.name]; var aliases = this.store.players[player.name];
for (var a in aliases) for (var a in aliases)
result.push(a + " = " + aliases[a].join(";")); result.push(a + " = " + aliases[a].join(";"));
return result; return result;
} }
},true); },true);
alias.store.players = alias.store.players || {}; alias.store.players = alias.store.players || {};
command("alias",function(params){ command("alias",function(params){
/* /*
this function also intercepts command options for /jsp this function also intercepts command options for /jsp
*/ */
if (params[0] === "help"){ if (params[0] === "help"){
self.sendMessage(alias.help()); self.sendMessage(alias.help());
return; return;
} }
if (params[0] === "set"){ if (params[0] === "set"){
var aliasCmd = params[1]; var aliasCmd = params[1];
var cmdStr = params.slice(2).join(' '); var cmdStr = params.slice(2).join(' ');
var cmds = cmdStr.split(';'); var cmds = cmdStr.split(';');
alias.set(self,aliasCmd,cmds); alias.set(self,aliasCmd,cmds);
return; return;
} }
if (params[0] === "delete"){ if (params[0] === "delete"){
alias.remove(self,params[1]); alias.remove(self,params[1]);
return ; return ;
} }
if (params[0] === "list"){ if (params[0] === "list"){
self.sendMessage(alias.list(self)); self.sendMessage(alias.list(self));
return; return;
} }
if (params.length == 0)
var playerHasAliases = alias.store.players[self.name]; return self.sendMessage(alias.help());
if (!playerHasAliases)
return false; var playerHasAliases = alias.store.players[self.name];
// is it an alias? if (!playerHasAliases)
var commands = playerHasAliases[params[0]]; return false;
if (!commands) // is it an alias?
return false; var commands = playerHasAliases[params[0]];
for (var i = 0;i < commands.length; i++){ if (!commands)
// fill in template return false;
var cmd = commands[i]; for (var i = 0;i < commands.length; i++){
cmd = cmd.replace(/{([0-9]*)}/g,function(dummy,index){ return params[index];}) // fill in template
self.performCommand(cmd); var cmd = commands[i];
} cmd = cmd.replace(/{([0-9]*)}/g,function(dummy,index){ return params[index] || "";})
return true; self.performCommand(cmd);
}
return true;
},["help","set","delete","list"],true); },["help","set","delete","list"],true);

View file

@ -21,7 +21,7 @@
var global = this; var global = this;
var verbose = verbose || false; var verbose = verbose || false;
/* /*
wph 20130124 - make self, plugin and bukkit public - these are far more useful now that tab-complete works. wph 20130124 - make self, plugin and server public - these are far more useful now that tab-complete works.
*/ */
var server = org.bukkit.Bukkit.server; var server = org.bukkit.Bukkit.server;
// //
@ -128,7 +128,6 @@ var server = org.bukkit.Bukkit.server;
Save a javascript object to a file (saves using JSON notation) Save a javascript object to a file (saves using JSON notation)
*/ */
var _save = function(object, filename){ var _save = function(object, filename){
print(filename);
var objectToStr = null; var objectToStr = null;
try{ try{
objectToStr = JSON.stringify(object); objectToStr = JSON.stringify(object);
@ -175,7 +174,8 @@ var server = org.bukkit.Bukkit.server;
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,options,intercepts){ 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)
@ -266,13 +266,37 @@ var server = org.bukkit.Bukkit.server;
var __onTabCompleteJSP = function() { var __onTabCompleteJSP = function() {
var result = global.__onTC_result; var result = global.__onTC_result;
var args = global.__onTC_args; var args = global.__onTC_args;
var cmd = _commands[args[0]]; var cmdInput = args[0];
if (cmd) var cmd = _commands[cmdInput];
for (var i = 0;i < cmd.options.length; i++) if (cmd){
result.add(cmd.options[i]); var opts = cmd.options;
else var len = opts.length;
for (var i in _commands) if (args.length == 1){
result.add(i); for (var i = 0;i < len; i++)
result.add(opts[i]);
}else{
// partial e.g. /jsp chat_color dar
for (var i = 0;i < len; i++){
if (opts[i].indexOf(args[1]) == 0){
result.add(opts[i]);
}
}
}
}else{
if (args.length == 0){
for (var i in _commands)
result.add(i);
}else{
// partial e.g. /jsp al
// should tabcomplete to alias
//
for (var c in _commands){
if (c.indexOf(cmdInput) == 0){
result.add(c);
}
}
}
}
return result; return result;
}; };
/* /*