package net.sourceforge.pain.logic.event.console; import net.sourceforge.pain.*; import net.sourceforge.pain.network.console.*; import net.sourceforge.pain.logic.*; import net.sourceforge.pain.logic.fn.*; import net.sourceforge.pain.plugin.command.*; import net.sourceforge.pain.util.*; import java.util.regex.*; /** * PAiN MUD CODEBASE. * User: fmike * Date: Jan 13, 2003 * Time: 6:27:33 AM */ public class ConsoleInputEvent extends AbstractEvent { public static final String COMMAND_PACKAGE = "net.sourceforge.pain.logic.event.console.command."; private static final String[] parsedCommand = new String[2]; private static final Pattern commandPattern = Pattern.compile("\\S{1}\\s+\\S{1}"); public Object execute(Object param) throws Exception { final Console console = (Console) param; CommandHandler handler; if (console.isRawMode()) { handler = (CommandHandler) console.getRawCommand(); } else { final String line = console.popInputLine().trim(); if (line.length() == 0) { return null; } handler = getCommandHandler(line, console); if (handler == null) { processFailedCommand(console); } else if (!handler.isAccessible()) { handler.processNotAccessible(); handler = null; } } if (handler != null) { handler.processCommand(); } return null; } public static CommandHandler getCommandHandler(String inputLine, Console console) throws Exception { CommandHandler handler; parseCommand(inputLine, parsedCommand); String commandName = parsedCommand[0]; String commandParams = parsedCommand[1]; CommandMapper commandMapper = getCommandMapper(); TextCommand textCommand = commandMapper.findCommand(commandName); if (textCommand == null) { return null; } handler = instantiateCommandHandler(textCommand.commandClassName); handler.commandParams = commandParams; handler.command = textCommand; Log.debug("ConsoleInputEvent.getCommandHandler: command:'" + commandName + "' tag:'" + textCommand.tag + "' args:'" + commandParams + "'"); handler.console = console; handler.player = console.getPlayer(); return handler; } public static CommandMapper getCommandMapper() { return (CommandMapper) Core.getPluginManager().getPlugin("command.CommandMapper"); } public static CommandHandler instantiateCommandHandler(String commandClassName) throws ClassNotFoundException, InstantiationException, IllegalAccessException { return (CommandHandler) Core.getLogicLoader().provideClass(COMMAND_PACKAGE + commandClassName).newInstance(); } public static void parseCommand(String line, String result[]) { Matcher matcher = commandPattern.matcher(line); if (matcher.find()) { // found spaces after command int commandEnd = matcher.start() + 1; result[0] = line.substring(0, commandEnd).trim(); //remove leading spaces int argStart = matcher.end() - 1; result[1] = line.substring(argStart); } else { result[0] = line.trim(); result[1] = null; } } public static void processFailedCommand(Console console) { MessageOutFn.out(console, "what?\n"); } }