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

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

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 = "";
Alias alias = plugin.aliasManager.getAliasMatch(sCommand);
if(e.getMessage().contains(" ")) String sArgs = sCommand.substring(alias.command.length());
{
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);
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;
Alias alias = plugin.aliasManager.getAliasMatch(sCommand);
if(e.getCommand().contains(" ")) String sArgs = sCommand.substring(alias.command.length());
{
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 = "";
}
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: