Problems with custom/dynamic commands.

Discussion in 'Spigot Plugin Development' started by BadBoy6767, Feb 7, 2015.

  1. Hey guys, im using a library i made called Midnight Library and i'm creating a plugin using it that creates custom commands that can be set in the config.
    This is my main code:
    PHP:
    package org.midnightas.midlib.plugin.customcommands;

    import java.io.IOException;

    import org.badboy6767.midnightlibrary.Metrics;
    import org.badboy6767.midnightlibrary.MidLib;
    import org.badboy6767.midnightlibrary.MidnightCommand;
    import org.bukkit.Bukkit;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class CustomCommandsPlugin extends JavaPlugin {

        public CustomCommandsPlugin instance = this;

        @Override
        public void onEnable() {
            MidLib library = (MidLib) Bukkit.getPluginManager().getPlugin(
                    "MidnightLibrary");
            try {
                Metrics metrics = new Metrics(this);
                metrics.start();
            } catch (IOException e) {
                System.out.println("Failed to update and send statistics about MidsCustomCommands.");
            }
            getConfig().options().copyDefaults(true);
            saveConfig();
            for (final String s : getConfig().getStringList("commands")) {
                library.registerCommand(new MidnightCommand() {
                    @Override
                    public Plugin getUser() {
                        return instance;
                    }

                    @Override
                    public String getName() {
                        return s;
                    }

                    @Override
                    public boolean onCall(CommandSender sender, String label,
                            String[] args) {
                        for (String cmd : getConfig().getStringList(
                                "command." + s + ".execute")) {
                            String command = cmd.replace("%name", sender.getName());
                            boolean isConsoleCommand = command.split(" ")[0]
                                    .equalsIgnoreCase("sendmessage")
                                    || command.split(" ")[0]
                                            .equalsIgnoreCase("log");
                            if (args.length == getConfig().getInt(
                                    "command." + s + ".requiredArguments")
                                    || isConsoleCommand) {
                                for (int i = 1; i < 101; i++) {
                                    command = command.replace("%" + i, args[i - 1]);
                                }
                                if (command.split(" ")[0]
                                        .equalsIgnoreCase("sendmessage")) {
                                    sender.sendMessage(command.substring(12));
                                } else if (command.split(" ")[0]
                                        .equalsIgnoreCase("log")) {
                                    Bukkit.getConsoleSender().sendMessage(
                                            command.substring(4));
                                }
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                                        command);
                            } else if (args.length > getConfig().getInt(
                                    "command." + s + ".requiredArguments")) {
                                sendMessage(
                                        sender,
                                        getConfig().getString(
                                                "message.invalidarguments.toomuch"));
                            } else if (args.length < getConfig().getInt(
                                    "command." + s + ".requiredArguments")) {
                                sendMessage(
                                        sender,
                                        getConfig()
                                                .getString(
                                                        "message.invalidarguments.notenough"));
                            }
                        }
                        return true;
                    }
                });
            }
        }

        public void sendMessage(CommandSender sender, String string) {
            sender.sendMessage(string.replace("&", "ยง"));
        }

    }
     
    And this is the configuration that would create my custom command:
    PHP:
    message:
      invalidarguments:
        notenough: '&4Not enough arguments!'
        toomuch: '&4Too many arguments!'
    commands:
    - midcctest
    # Built in commands that allow further customization:
    # NOTE: ONLY THE PLUGIN CAN USE THESE COMMANDS, THEY ARE NOT BUILT ONTO THE SERVER.
    # - sendmessage: Sends a message to the player, use & for color codes.
    # - log: Logs to the server console.
    command:
      # Simple test command, and as a tutorial.
     # Supposed to teleport the player to the block they are standing on already :/
     # Except that the first argument is the Y coordinate they want to teleport to.
     midcctest:
        requiredArguments: 1
        execute:
        - tp %name %x %1 %z
    When i do my custom command (midcctest) and i place my custom argument then it outputs this stacktrace into my console:
    Code (Text):
    org.bukkit.command.CommandException: Unhandled exception executing 'midcctest 255' in org.badboy6767.midnightlibrary.MidLib$1(midcctest)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:188) ~[spigot.jar:git-Spigot-a6b08a3]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:643) ~[spigot.jar:git-Spigot-a6b08a3]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1105) [spigot.jar:git-Spigot-a6b08a3]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:940) [spigot.jar:git-Spigot-a6b08a3]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-a6b08a3]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-a6b08a3]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-a6b08a3]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_71]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_71]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:684) [spigot.jar:git-Spigot-a6b08a3]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-a6b08a3]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:625) [spigot.jar:git-Spigot-a6b08a3]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:528) [spigot.jar:git-Spigot-a6b08a3]
        at java.lang.Thread.run(Unknown Source) [?:1.7.0_71]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at org.midnightas.midlib.plugin.customcommands.CustomCommandsPlugin$1.onCall(CustomCommandsPlugin.java:55) ~[?:?]
        at org.badboy6767.midnightlibrary.MidLib$1.execute(MidLib.java:70) ~[?:?]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot.jar:git-Spigot-a6b08a3]
        ... 13 more
    Like 55 in my Custom Commands plugin is:
    PHP:
    command = command.replace("%" + i, args[i - 1]);
    I even tried doing:
    PHP:
    command = command.replace("%" + i, args[i]);
    But to no avail.
    Whats the problem?
     
  2. Im assuming the problem is that you dont check that there are enough arguments given. The command.replace ("%"+i, args [i-1]) was correct but only in the case that they give i arguments.

    EDIT: You do check for the right amount of arguments and then you dissregard that check and loop 100 times anyways...
    Code (Text):
    for (int i = 1; i < 101; i++) {
       command = command.replace("%" + i, args[i - 1]);
        }
     
     
  3. Oh wow that made me feel stupid, i forgot about how arrays work XD
    Thanks!
     
    • Like Like x 1