Search in sources :

Example 1 with CommandEvent

use of net.minecraftforge.event.CommandEvent in project Engine by VoltzEngine-Project.

the class PermissionsCommandManager method executeCommand.

@Override
public int executeCommand(ICommandSender sender, String cmd) {
    // Clean up command string
    cmd = cmd.trim();
    if (cmd.startsWith("/")) {
        cmd = cmd.substring(1);
    }
    // Get command name and arguments
    String[] args = cmd.split(" ");
    String command_name = args[0];
    args = dropFirstString(args);
    ICommand icommand = (ICommand) this.getCommands().get(command_name);
    int usernameIndex = this.getUsernameIndex(icommand, args);
    int j = 0;
    ChatComponentTranslation chatcomponenttranslation;
    try {
        if (icommand == null) {
            throw new CommandNotFoundException();
        }
        // Checks if the user can use the command
        if (hasPermissionForCommand(sender, icommand, args)) {
            CommandEvent event = new CommandEvent(icommand, sender, args);
            if (MinecraftForge.EVENT_BUS.post(event)) {
                if (event.exception != null) {
                    throw event.exception;
                }
                return 1;
            }
            if (usernameIndex > -1) {
                // Executes the command on several players if listed
                EntityPlayerMP[] players = PlayerSelector.matchPlayers(sender, args[usernameIndex]);
                String s2 = args[usernameIndex];
                int k = players.length;
                for (int l = 0; l < k; ++l) {
                    EntityPlayerMP entityplayermp = players[l];
                    args[usernameIndex] = entityplayermp.getCommandSenderName();
                    try {
                        icommand.processCommand(sender, args);
                        ++j;
                    } catch (CommandException commandexception1) {
                        ChatComponentTranslation chatcomponenttranslation1 = new ChatComponentTranslation(commandexception1.getMessage(), commandexception1.getErrorOjbects());
                        chatcomponenttranslation1.getChatStyle().setColor(EnumChatFormatting.RED);
                        sender.addChatMessage(chatcomponenttranslation1);
                    }
                }
                args[usernameIndex] = s2;
            } else {
                try {
                    icommand.processCommand(sender, args);
                    ++j;
                } catch (CommandException commandexception) {
                    chatcomponenttranslation = new ChatComponentTranslation(commandexception.getMessage(), commandexception.getErrorOjbects());
                    chatcomponenttranslation.getChatStyle().setColor(EnumChatFormatting.RED);
                    sender.addChatMessage(chatcomponenttranslation);
                }
            }
        } else {
            ChatComponentTranslation chatcomponenttranslation2 = new ChatComponentTranslation("commands.generic.permission");
            chatcomponenttranslation2.getChatStyle().setColor(EnumChatFormatting.RED);
            sender.addChatMessage(chatcomponenttranslation2);
        }
    } catch (WrongUsageException wrongusageexception) {
        chatcomponenttranslation = new ChatComponentTranslation("commands.generic.usage", new ChatComponentTranslation(wrongusageexception.getMessage(), wrongusageexception.getErrorOjbects()));
        chatcomponenttranslation.getChatStyle().setColor(EnumChatFormatting.RED);
        sender.addChatMessage(chatcomponenttranslation);
    } catch (CommandException commandexception2) {
        chatcomponenttranslation = new ChatComponentTranslation(commandexception2.getMessage(), commandexception2.getErrorOjbects());
        chatcomponenttranslation.getChatStyle().setColor(EnumChatFormatting.RED);
        sender.addChatMessage(chatcomponenttranslation);
    } catch (Throwable throwable) {
        chatcomponenttranslation = new ChatComponentTranslation("commands.generic.exception");
        chatcomponenttranslation.getChatStyle().setColor(EnumChatFormatting.RED);
        sender.addChatMessage(chatcomponenttranslation);
        Engine.instance.logger().error("Failed to process command: \'" + cmd + "\'", throwable);
    }
    return j;
}
Also used : ChatComponentTranslation(net.minecraft.util.ChatComponentTranslation) CommandEvent(net.minecraftforge.event.CommandEvent) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Example 2 with CommandEvent

use of net.minecraftforge.event.CommandEvent 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)

Aggregations

CommandEvent (net.minecraftforge.event.CommandEvent)2 CommandException (net.minecraft.command.CommandException)1 ICommand (net.minecraft.command.ICommand)1 WrongUsageException (net.minecraft.command.WrongUsageException)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 ChatComponentTranslation (net.minecraft.util.ChatComponentTranslation)1