Added support for spaces in aliases

This commit is contained in:
Ne0nx3r0 2013-07-28 16:57:48 -05:00
parent 3781d66047
commit a5fb024fc7
5 changed files with 89 additions and 100 deletions

View File

@ -17,7 +17,8 @@ to:
bring:
1: /tp !1p !name
# multiple command example
tpthenkick:
# note spaces in an alias go within quotes
"tp then kick":
1:
- /tp !1p !name
- /kick !1p

View File

@ -1,59 +1,59 @@
package com.ne0nx3r0.betteralias.alias;
import java.util.HashMap;
import java.util.List;
public class Alias
{
private final String command;
private final String permission;
private final HashMap<Integer, List<AliasCommand>> parameters;
public Alias(String commandName)
{
this.command = commandName;
this.permission = null;
this.parameters = new HashMap<Integer,List<AliasCommand>>();
}
public Alias(String commandName,String permissionNode)
{
this.command = commandName;
this.permission = permissionNode;
this.parameters = new HashMap<Integer,List<AliasCommand>>();
}
public boolean hasCommandFor(int length)
{
return this.parameters.containsKey(length) || this.parameters.containsKey(-1);
}
public String getPermissionNode()
{
return this.permission;
}
public boolean hasPermission()
{
return this.permission != null;
}
Iterable<AliasCommand> getCommands(int length)
{
List<AliasCommand> commands = this.parameters.get(length);
if(commands != null)
{
return commands;
}
return this.parameters.get(-1);
}
void setCommandsFor(int length,List<AliasCommand> commandsList)
{
this.parameters.put(length, commandsList);
}
}
package com.ne0nx3r0.betteralias.alias;
import java.util.HashMap;
import java.util.List;
public class Alias
{
public final String command;
private final String permission;
private final HashMap<Integer, List<AliasCommand>> parameters;
public Alias(String commandName)
{
this.command = commandName;
this.permission = null;
this.parameters = new HashMap<Integer,List<AliasCommand>>();
}
public Alias(String commandName,String permissionNode)
{
this.command = commandName;
this.permission = permissionNode;
this.parameters = new HashMap<Integer,List<AliasCommand>>();
}
public boolean hasCommandFor(int length)
{
return this.parameters.containsKey(length) || this.parameters.containsKey(-1);
}
public String getPermissionNode()
{
return this.permission;
}
public boolean hasPermission()
{
return this.permission != null;
}
Iterable<AliasCommand> getCommands(int length)
{
List<AliasCommand> commands = this.parameters.get(length);
if(commands != null)
{
return commands;
}
return this.parameters.get(-1);
}
void setCommandsFor(int length,List<AliasCommand> commandsList)
{
this.parameters.put(length, commandsList);
}
}

View File

@ -171,11 +171,6 @@ public class AliasManager
return true;
}
public Alias getAlias(String sCommand)
{
return this.aliases.get(sCommand);
}
public boolean sendAliasCommands(Alias alias,CommandSender cs, String commandString)
{
Player player = null;
@ -414,6 +409,19 @@ public class AliasManager
return false;
}
public Alias getAliasMatch(String sCommand)
{
for(String sAlias : this.aliases.keySet())
{
if(sCommand.startsWith(sAlias))
{
return this.aliases.get(sAlias);
}
}
return null;
}
// Delayed tasks
private static class waitConsoleCommand implements Runnable
{

View File

@ -23,38 +23,28 @@ public class BetterAliasCommandListener implements Listener
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e)
{
String sCommand = null;
String sArgs = "";
String sCommand = e.getMessage().substring(1);
Alias alias = plugin.aliasManager.getAliasMatch(sCommand);
if(e.getMessage().contains(" "))
{
sCommand = e.getMessage().substring(1,e.getMessage().indexOf(" "));
sArgs = e.getMessage().substring(e.getMessage().indexOf(" ")+1);
}
else
{
sCommand = e.getMessage().substring(1);
}
Alias alias = plugin.aliasManager.getAlias(sCommand);
String sArgs = sCommand.substring(alias.command.length());
if(alias != null)
{
Player player = e.getPlayer();
String sNode = "betteralias."+alias.getPermissionNode();
if(alias.hasPermission()
&& !player.hasPermission("betteralias."+alias.getPermissionNode()))
&& !player.hasPermission(sNode))
{
player.sendMessage(ChatColor.RED+"You do not have permission to use this alias.");
player.sendMessage(ChatColor.GRAY+"Node: "+sNode);
e.setCancelled(true);
}
else
{
if(plugin.aliasManager.sendAliasCommands(
alias,
(CommandSender) e.getPlayer(),
sArgs));
if(plugin.aliasManager.sendAliasCommands(alias,(CommandSender) e.getPlayer(),sArgs))
{
e.setCancelled(true);
}
@ -65,25 +55,15 @@ public class BetterAliasCommandListener implements Listener
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onConsoleCommand(ServerCommandEvent e)
{
Alias alias;
String sCommand = null;
String sCommand = e.getCommand();
Alias alias = plugin.aliasManager.getAliasMatch(sCommand);
if(e.getCommand().contains(" "))
{
sCommand = e.getCommand().substring(e.getCommand().indexOf(" ")+1);
alias = plugin.aliasManager.getAlias(e.getCommand().substring(0,e.getCommand().indexOf(" ")));
}
else
{
alias = plugin.aliasManager.getAlias(e.getCommand());
sCommand = "";
}
String sArgs = sCommand.substring(alias.command.length());
if(alias != null)
{
if(plugin.aliasManager.sendAliasCommands(alias,e.getSender(),sCommand))
if(plugin.aliasManager.sendAliasCommands(alias,e.getSender(),sArgs))
{
e.setCommand("bareload donothing");
}

View File

@ -1,6 +1,6 @@
name: BetterAlias
main: com.ne0nx3r0.betteralias.BetterAlias
version: '1.2'
version: '1.3'
database: false
description: Command alias system
commands: