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: bring:
1: /tp !1p !name 1: /tp !1p !name
# multiple command example # multiple command example
tpthenkick: # note spaces in an alias go within quotes
"tp then kick":
1: 1:
- /tp !1p !name - /tp !1p !name
- /kick !1p - /kick !1p

View File

@ -5,7 +5,7 @@ import java.util.List;
public class Alias public class Alias
{ {
private final String command; public final String command;
private final String permission; private final String permission;
private final HashMap<Integer, List<AliasCommand>> parameters; private final HashMap<Integer, List<AliasCommand>> parameters;

View File

@ -171,11 +171,6 @@ public class AliasManager
return true; return true;
} }
public Alias getAlias(String sCommand)
{
return this.aliases.get(sCommand);
}
public boolean sendAliasCommands(Alias alias,CommandSender cs, String commandString) public boolean sendAliasCommands(Alias alias,CommandSender cs, String commandString)
{ {
Player player = null; Player player = null;
@ -414,6 +409,19 @@ public class AliasManager
return false; 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 // Delayed tasks
private static class waitConsoleCommand implements Runnable private static class waitConsoleCommand implements Runnable
{ {

View File

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

View File

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