Search in sources :

Example 1 with ICommand

use of net.minecraft.command.ICommand in project watson by totemo.

the class ClientCommandManager method handleClientCommand.

// --------------------------------------------------------------------------
/**
   * Parse and execute the command line if it is a command that has been
   * registered with this ICommandManager.
   * 
   * @param commandLine the command line.
   * @return true if the command was handled locally; false if it must be passed
   *         on to the server (via chat packets).
   */
public boolean handleClientCommand(String commandLine) {
    if (!commandLine.startsWith("/")) {
        return false;
    }
    String[] tokens = getTokens(commandLine);
    if (tokens.length == 0) {
        return false;
    }
    ICommand command = getCommand(tokens[0]);
    if (command != null) {
        executeCommand(getCommandSender(), commandLine);
    }
    return command != null;
}
Also used : ICommand(net.minecraft.command.ICommand)

Example 2 with ICommand

use of net.minecraft.command.ICommand in project MinecraftForge by MinecraftForge.

the class ClientCommandHandler method executeCommand.

/**
     * @return 1 if successfully executed, -1 if no permission or wrong usage,
     *         0 if it doesn't exist or it was canceled (it's sent to the server)
     */
@Override
public int executeCommand(ICommandSender sender, String message) {
    message = message.trim();
    if (message.startsWith("/")) {
        message = message.substring(1);
    }
    String[] temp = message.split(" ");
    String[] args = new String[temp.length - 1];
    String commandName = temp[0];
    System.arraycopy(temp, 1, args, 0, args.length);
    ICommand icommand = getCommands().get(commandName);
    try {
        if (icommand == null) {
            return 0;
        }
        if (icommand.checkPermission(this.getServer(), sender)) {
            CommandEvent event = new CommandEvent(icommand, sender, args);
            if (MinecraftForge.EVENT_BUS.post(event)) {
                if (event.getException() != null) {
                    throw event.getException();
                }
                return 0;
            }
            this.tryExecute(sender, args, icommand, message);
            return 1;
        } else {
            sender.sendMessage(format(RED, "commands.generic.permission"));
        }
    } catch (WrongUsageException wue) {
        sender.sendMessage(format(RED, "commands.generic.usage", format(RED, wue.getMessage(), wue.getErrorObjects())));
    } catch (CommandException ce) {
        sender.sendMessage(format(RED, ce.getMessage(), ce.getErrorObjects()));
    } catch (Throwable t) {
        sender.sendMessage(format(RED, "commands.generic.exception"));
        t.printStackTrace();
    }
    return -1;
}
Also used : WrongUsageException(net.minecraft.command.WrongUsageException) ICommand(net.minecraft.command.ICommand) CommandEvent(net.minecraftforge.event.CommandEvent) CommandException(net.minecraft.command.CommandException)

Example 3 with ICommand

use of net.minecraft.command.ICommand in project MinecraftForge by MinecraftForge.

the class CommandTreeBase method getTabCompletions.

@Override
@Nonnull
public List<String> getTabCompletions(@Nonnull MinecraftServer server, @Nonnull ICommandSender sender, @Nonnull String[] args, @Nullable BlockPos pos) {
    if (args.length == 1) {
        List<String> keys = new ArrayList<String>();
        for (ICommand c : getCommandMap().values()) {
            if (c.checkPermission(server, sender)) {
                keys.add(c.getName());
            }
        }
        Collections.sort(keys, null);
        return getListOfStringsMatchingLastWord(args, keys);
    }
    ICommand cmd = getCommandMap().get(args[0]);
    if (cmd != null) {
        return cmd.getTabCompletions(server, sender, shiftArgs(args), pos);
    }
    return super.getTabCompletions(server, sender, args, pos);
}
Also used : ICommand(net.minecraft.command.ICommand) ArrayList(java.util.ArrayList) TextComponentString(net.minecraft.util.text.TextComponentString) Nonnull(javax.annotation.Nonnull)

Example 4 with ICommand

use of net.minecraft.command.ICommand in project watson by totemo.

the class ClientCommandManager method executeCommand.

// --------------------------------------------------------------------------
/**
   * @see net.minecraft.src.ICommandManager#executeCommand(net.minecraft.src.ICommandSender,
   *      java.lang.String)
   * 
   *      The JavaDocs for the interface don't currently describe the exact
   *      meaning of the return value. Looking at the code for
   *      {@link net.minecraft.src.CommandHandler} it contains a loop that
   *      applies a command for all players who match a particular name pattern.
   *      The returned value is the number of times that the command was
   *      successfully executed by that loop. Therefore in the case of this
   *      class, it returns 1 on success and 0 on error.
   */
@Override
public int executeCommand(ICommandSender sender, String commandLine) {
    try {
        String[] tokens = getTokens(commandLine);
        String verb = tokens[0];
        ICommand command = getCommand(verb);
        if (command == null) {
            throw new CommandNotFoundException();
        }
        tokens = Arrays.copyOfRange(tokens, 1, tokens.length);
        if (command.canCommandSenderUseCommand(sender)) {
            command.processCommand(sender, tokens);
            return 1;
        } else {
            sendError(sender, new ChatComponentTranslation("commands.generic.permission", new Object[0]));
        }
    } catch (WrongUsageException ex) {
        sendError(sender, new ChatComponentTranslation("commands.generic.usage", new Object[] { new ChatComponentTranslation(ex.getMessage(), ex.getErrorObjects()) }));
    } catch (CommandException ex) {
        sendError(sender, new ChatComponentTranslation(ex.getMessage(), ex.getErrorObjects()));
    } catch (Throwable throwable) {
        sendError(sender, new ChatComponentTranslation("commands.generic.exception", new Object[0]));
        Log.exception(Level.WARNING, "error processing command", throwable);
    }
    return 0;
}
Also used : WrongUsageException(net.minecraft.command.WrongUsageException) ICommand(net.minecraft.command.ICommand) ChatComponentTranslation(net.minecraft.util.ChatComponentTranslation) CommandException(net.minecraft.command.CommandException) CommandNotFoundException(net.minecraft.command.CommandNotFoundException)

Aggregations

ICommand (net.minecraft.command.ICommand)4 CommandException (net.minecraft.command.CommandException)2 WrongUsageException (net.minecraft.command.WrongUsageException)2 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 CommandNotFoundException (net.minecraft.command.CommandNotFoundException)1 ChatComponentTranslation (net.minecraft.util.ChatComponentTranslation)1 TextComponentString (net.minecraft.util.text.TextComponentString)1 CommandEvent (net.minecraftforge.event.CommandEvent)1