Addressing a longstanding issue with the chat command aliases/translations.
The help message for /help <command> was using the translated/aliased string,
which meant that you had to have a usage translation for each command alias.
This became unmaintainable in Project X, where we had artists adding emotes
with lots of aliases, and I'm guessing it was booched in the Y!PP translations,
since they seem to lack translations for things like m.usage_escuchar. Let's
instead use a usage message based on the untranslated command, then compose
the actual command used into that (for aliases). This should be mostly
backwards-compatible, but I will be updating Yohoho and Bang in addition to
Project X: this involves replacing the command name in the usage translations
with {0} and returning getUsage(command) instead of "m.usage_blabla".
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6248 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -101,6 +101,14 @@ public class ChatDirector extends BasicDirector
|
|||||||
*/
|
*/
|
||||||
public abstract static class CommandHandler
|
public abstract static class CommandHandler
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Returns the translatable usage message for the specified command.
|
||||||
|
*/
|
||||||
|
public String getUsage (String command)
|
||||||
|
{
|
||||||
|
return MessageBundle.tcompose(_usageKey, command);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the specified chat command.
|
* Handles the specified chat command.
|
||||||
*
|
*
|
||||||
@@ -125,6 +133,9 @@ public class ChatDirector extends BasicDirector
|
|||||||
public boolean checkAccess (BodyObject user) {
|
public boolean checkAccess (BodyObject user) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The command's usage message translation key. */
|
||||||
|
protected String _usageKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -249,6 +260,9 @@ public class ChatDirector extends BasicDirector
|
|||||||
*/
|
*/
|
||||||
public void registerCommandHandler (MessageBundle msg, String command, CommandHandler handler)
|
public void registerCommandHandler (MessageBundle msg, String command, CommandHandler handler)
|
||||||
{
|
{
|
||||||
|
// the usage key is derived from the untranslated command
|
||||||
|
handler._usageKey = "m.usage_" + command;
|
||||||
|
|
||||||
String key = "c." + command;
|
String key = "c." + command;
|
||||||
if (msg.exists(key)) {
|
if (msg.exists(key)) {
|
||||||
StringTokenizer st = new StringTokenizer(msg.get(key));
|
StringTokenizer st = new StringTokenizer(msg.get(key));
|
||||||
@@ -1146,6 +1160,14 @@ public class ChatDirector extends BasicDirector
|
|||||||
/** Implements <code>/help</code>. */
|
/** Implements <code>/help</code>. */
|
||||||
protected class HelpHandler extends CommandHandler
|
protected class HelpHandler extends CommandHandler
|
||||||
{
|
{
|
||||||
|
@Override
|
||||||
|
public String getUsage (String command)
|
||||||
|
{
|
||||||
|
Map<String, CommandHandler> possibleCommands = getCommandHandlers("");
|
||||||
|
possibleCommands.remove(command);
|
||||||
|
return getUsage(command, possibleCommands);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String handleCommand (SpeakService speakSvc, String command,
|
public String handleCommand (SpeakService speakSvc, String command,
|
||||||
String args, String[] history)
|
String args, String[] history)
|
||||||
@@ -1168,30 +1190,35 @@ public class ChatDirector extends BasicDirector
|
|||||||
|
|
||||||
// handle "/help help" and "/help someboguscommand"
|
// handle "/help help" and "/help someboguscommand"
|
||||||
Map<String, CommandHandler> possibleCommands = getCommandHandlers(hcmd);
|
Map<String, CommandHandler> possibleCommands = getCommandHandlers(hcmd);
|
||||||
if (hcmd.equals("help") || possibleCommands.isEmpty()) {
|
if (_handlers.get(hcmd) == this || possibleCommands.isEmpty()) {
|
||||||
possibleCommands = getCommandHandlers("");
|
return getUsage(command);
|
||||||
possibleCommands.remove("help"); // remove help from the list
|
|
||||||
|
} else if (possibleCommands.size() > 1) {
|
||||||
|
return getUsage(command, possibleCommands);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there is only one possible command display its usage
|
// if there is only one possible command display its usage
|
||||||
switch (possibleCommands.size()) {
|
Map.Entry<String, CommandHandler> entry =
|
||||||
case 1:
|
possibleCommands.entrySet().iterator().next();
|
||||||
Iterator<String> itr = possibleCommands.keySet().iterator();
|
// this is a little funny, but we display the feedback message by hand and return
|
||||||
// this is a little funny, but we display the feeback message by hand and return
|
// SUCCESS so that the chat entry field doesn't think that we've failed and
|
||||||
// SUCCESS so that the chat entry field doesn't think that we've failed and
|
// preserve our command text
|
||||||
// preserve our command text
|
displayFeedback(null, entry.getValue().getUsage(entry.getKey()));
|
||||||
displayFeedback(null, "m.usage_" + itr.next());
|
return ChatCodes.SUCCESS;
|
||||||
return ChatCodes.SUCCESS;
|
}
|
||||||
|
|
||||||
default:
|
/**
|
||||||
Object[] commands = possibleCommands.keySet().toArray();
|
* Returns a usage message listing the provided commands.
|
||||||
Arrays.sort(commands);
|
*/
|
||||||
String commandList = "";
|
protected String getUsage (String command, Map<String, CommandHandler> possibleCommands)
|
||||||
for (Object element : commands) {
|
{
|
||||||
commandList += " /" + element;
|
Object[] commands = possibleCommands.keySet().toArray();
|
||||||
}
|
Arrays.sort(commands);
|
||||||
return MessageBundle.tcompose("m.usage_help", commandList);
|
String commandList = "";
|
||||||
|
for (Object element : commands) {
|
||||||
|
commandList += " /" + element;
|
||||||
}
|
}
|
||||||
|
return MessageBundle.tcompose("m.usage_help", commandList, command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1215,7 +1242,7 @@ public class ChatDirector extends BasicDirector
|
|||||||
String args, String[] history)
|
String args, String[] history)
|
||||||
{
|
{
|
||||||
if (StringUtil.isBlank(args)) {
|
if (StringUtil.isBlank(args)) {
|
||||||
return "m.usage_speak";
|
return getUsage(command);
|
||||||
}
|
}
|
||||||
// note the command to be stored in the history
|
// note the command to be stored in the history
|
||||||
history[0] = command + " ";
|
history[0] = command + " ";
|
||||||
@@ -1233,7 +1260,7 @@ public class ChatDirector extends BasicDirector
|
|||||||
String args, String[] history)
|
String args, String[] history)
|
||||||
{
|
{
|
||||||
if (StringUtil.isBlank(args)) {
|
if (StringUtil.isBlank(args)) {
|
||||||
return "m.usage_emote";
|
return getUsage(command);
|
||||||
}
|
}
|
||||||
// note the command to be stored in the history
|
// note the command to be stored in the history
|
||||||
history[0] = command + " ";
|
history[0] = command + " ";
|
||||||
@@ -1249,7 +1276,7 @@ public class ChatDirector extends BasicDirector
|
|||||||
String args, String[] history)
|
String args, String[] history)
|
||||||
{
|
{
|
||||||
if (StringUtil.isBlank(args)) {
|
if (StringUtil.isBlank(args)) {
|
||||||
return "m.usage_think";
|
return getUsage(command);
|
||||||
}
|
}
|
||||||
// note the command to be stored in the history
|
// note the command to be stored in the history
|
||||||
history[0] = command + " ";
|
history[0] = command + " ";
|
||||||
@@ -1265,7 +1292,7 @@ public class ChatDirector extends BasicDirector
|
|||||||
String args, String[] history)
|
String args, String[] history)
|
||||||
{
|
{
|
||||||
if (StringUtil.isBlank(args)) {
|
if (StringUtil.isBlank(args)) {
|
||||||
return "m.usage_tell";
|
return getUsage(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
final boolean useQuotes = args.startsWith("\"");
|
final boolean useQuotes = args.startsWith("\"");
|
||||||
@@ -1275,7 +1302,7 @@ public class ChatDirector extends BasicDirector
|
|||||||
|
|
||||||
// validate that we didn't eat all the tokens making the handle
|
// validate that we didn't eat all the tokens making the handle
|
||||||
if (StringUtil.isBlank(message)) {
|
if (StringUtil.isBlank(message)) {
|
||||||
return "m.usage_tell";
|
return getUsage(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure we're not trying to tell something to ourselves
|
// make sure we're not trying to tell something to ourselves
|
||||||
@@ -1386,7 +1413,7 @@ public class ChatDirector extends BasicDirector
|
|||||||
String args, String[] history)
|
String args, String[] history)
|
||||||
{
|
{
|
||||||
if (StringUtil.isBlank(args)) {
|
if (StringUtil.isBlank(args)) {
|
||||||
return "m.usage_broadcast";
|
return getUsage(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
// mogrify and verify length
|
// mogrify and verify length
|
||||||
|
|||||||
Reference in New Issue
Block a user