From 4d3f79d7326c1e870ad65487ead37d53399591a5 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Tue, 30 Jun 2009 22:15:20 +0000 Subject: [PATCH] If we hit an exact match in looking for command handlers, return only it and ignore any other commands that are prefixes. Do we have something like ImmutableMap.of(key, value) for regular Maps? It would've been nice here. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5841 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../crowd/chat/client/ChatDirector.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/java/com/threerings/crowd/chat/client/ChatDirector.java b/src/java/com/threerings/crowd/chat/client/ChatDirector.java index 625e3e005..9ddb9bc43 100644 --- a/src/java/com/threerings/crowd/chat/client/ChatDirector.java +++ b/src/java/com/threerings/crowd/chat/client/ChatDirector.java @@ -396,7 +396,7 @@ public class ChatDirector extends BasicDirector args = text.substring(sidx + 1).trim(); } - HashMap possibleCommands = getCommandHandlers(command); + Map possibleCommands = getCommandHandlers(command); switch (possibleCommands.size()) { case 0: StringTokenizer tok = new StringTokenizer(text); @@ -948,10 +948,11 @@ public class ChatDirector extends BasicDirector } /** - * Returns a hashmap containing all command handlers that match the specified command (i.e. the - * specified command is a prefix of their registered command string). + * Returns a map containing all command handlers that match the specified command (i.e. the + * specified command is a prefix of their registered command string). If there's an exact + * match, only that match is returned, even if the command is a prefix of other handlers. */ - protected HashMap getCommandHandlers (String command) + protected Map getCommandHandlers (String command) { HashMap matches = Maps.newHashMap(); BodyObject user = (BodyObject)_ctx.getClient().getClientObject(); @@ -964,6 +965,11 @@ public class ChatDirector extends BasicDirector if (!handler.checkAccess(user)) { continue; } + if (cmd.equals(command)) {// If we hit an exact match, it wins + matches.clear(); + matches.put(cmd, handler); + return matches; + } matches.put(cmd, handler); } return matches; @@ -1144,7 +1150,7 @@ public class ChatDirector extends BasicDirector } // handle "/help help" and "/help someboguscommand" - HashMap possibleCommands = getCommandHandlers(hcmd); + Map possibleCommands = getCommandHandlers(hcmd); if (hcmd.equals("help") || possibleCommands.isEmpty()) { possibleCommands = getCommandHandlers(""); possibleCommands.remove("help"); // remove help from the list