Solved onCommand won't recognize input

Discussion in 'Spigot Plugin Help' started by abstractionAlpha, Sep 3, 2020.

  1. I'm really just not sure why this is happening. Have tried forum results to no avail. Here's my code:

    Code (Text):
    package com.abstractionalpha.minecraft.plugins.chestrandomizer;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import java.util.ArrayList;

    /**
    * Main class containing executables. Handles command processing and user interface.
    *
    * @version 1.3
    * @author abstractionAlpha
    */
    public final class ChestRandomizer extends JavaPlugin {

        IdTools id = new IdTools();
        FillChests fillChests = new FillChests();
        EmptyChests emptyChests = new EmptyChests();

        private static Plugin plugin;

        /**
         * Plugin start-up logic.
         */
        @Override
        public void onEnable() {
            plugin = this;
            this.saveDefaultConfig();
            getLogger().info("[ChestRandomizer] ChestRandomizer has successfully started up.");
        }

        /**
         * Method allowing plugin to be accessed by other classes.
         *
         * @return plugin
         */
        public static Plugin getPlugin() {
            return plugin;
        }

        /**
         * Plugin shut-down logic.
         */
        @Override
        public void onDisable() {
            getLogger().info("[ChestRandomizer] ChestRandomizer has successfully shut down.");
        }

        /**
         * Handles command inputs from user.
         *
         * @param cmd Command issued (looks for '/cr')
         * @param sender Player or console who issued command
         * @param args Command-line arguments
         * @return Boolean representing successful or unsuccessful execution of command
         */
        public boolean onCommand(Command cmd, CommandSender sender, String[] args) {
            if (cmd.getName().equalsIgnoreCase("cr")) {
                Bukkit.getLogger().info("Check");
                if (args.length > 2) {
                    sender.sendMessage(ChatColor.RED + "Too many arguments. Type '/cr' for help.");
                    return true;
                } else if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "/cr refill <region|radius|all>");
                    sender.sendMessage(ChatColor.RED + "/cr empty <region|radius|all>");
                    return true;
                } else if (args.length == 1) {
                    if (!args[0].equalsIgnoreCase("all")) {
                        sender.sendMessage(ChatColor.RED + "/cr refill <region|radius|all>");
                        return true;
                    }
                }

                String methodType;
                if (args[0].equalsIgnoreCase("fill")) {
                    methodType = "fillType";
                } else if (args[0].equalsIgnoreCase("empty")) {
                    methodType = "emptyType";
                } else {
                    sender.sendMessage(ChatColor.RED + "Invalid case!");
                    return true;
                }

                String methodArgument;
                if (args[1].equalsIgnoreCase("all")) {
                    methodArgument = "allArg";
                } else if (id.isNumeric(args[1])) {
                    methodArgument = "radiusArg";
                } else {
                    methodArgument = "regionArg";
                }

                ArrayList<String> chestList = (ArrayList<String>) getConfig().getStringList("chests.chest-list");
                String chestToFill;
                String chestToEmpty;
                switch (methodArgument) {

                    case "allArg":
                        if (methodType.equalsIgnoreCase("fillType")) {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToFill = chestList.get(i);
                                fillChests.fill(chestToFill);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests filled!");
                        } else {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToEmpty = chestList.get(i);
                                emptyChests.empty(chestToEmpty);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests emptied!");
                        }
                        break;

                    case "radiusArg":
                        if (methodType.equalsIgnoreCase("fillType")) {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToFill = chestList.get(i);
                                fillChests.fill(chestToFill, args[1]);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests in radius " + args[1] + " filled!");
                        } else {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToEmpty = chestList.get(i);
                                emptyChests.empty(chestToEmpty, args[1]);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests in radius " + args[1] + " emptied!");
                        }
                        break;

                    case "regionArg":
                        if (methodType.equalsIgnoreCase("fillType")) {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToFill = chestList.get(i);
                                fillChests.fill(chestToFill, args[1]);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests in region " + args[1] + " filled!");
                        } else {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToEmpty = chestList.get(i);
                                emptyChests.empty(chestToEmpty, args[1]);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests in region " + args[1] + " emptied!");
                        }
                        break;
                }


            } return true;
        }
    }
     
    And here's my plugin.yml:

    Code (YAML):
    name: ChestRandomizer
    version
    : 2.0
    main
    : com.abstractionalpha.minecraft.plugins.chestrandomizer.ChestRandomizer
    api-version
    : 1.16
    authors
    : [abstractionAlpha]
    description
    : Minecraft plugin. Randomizes items in chests.
    website
    : https://www.abstractionalpha.com
    commands
    :
      cr
    :
        description
    : Root command. Lists all command options.
        permission
    : chestrandomizer.cr
        permission-message
    : You do not have <permission>.
    Anyone catching something I'm not? I don't get the "Check" on line 62 in my console, so it isn't making it past the equalsIgnoreCase at all.
     
    #1 abstractionAlpha, Sep 3, 2020
    Last edited: Sep 3, 2020
  2. Tried this already and it didn't work, but I put it in and left it in this time. No return still besides command usage if I put that in. Any other suggestions?
     
  3. You forgot the @Override annotation above onCommand. If you had remembered to use @Override your compiler would have warned you that the parameters in the method are incorrect. The correct parameters are onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3). Also you do not need to register a command handler in a class that extends JavaPlugin
     
  4. Putting @Override throws an error when I try and compile using maven, but implementing your suggested changes didn't fix my problem. I've repasted my code below. Will the onCommand work without the @Override annotation? If so, what else should I change? Also, why can't I implement @Override in the first place? Trying returns:
    Code (Text):
    Error:(60,5) java: method does not override or implement a method from a supertype
    Current ChestRandomizer.java:
    Code (Text):
    package com.abstractionalpha.minecraft.plugins.chestrandomizer;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import java.util.ArrayList;

    /**
    * Main class containing executables. Handles command processing and user interface.
    *
    * @version 1.3
    * @author abstractionAlpha
    */
    public final class ChestRandomizer extends JavaPlugin {

        IdTools id = new IdTools();

        FillChests fillChests = new FillChests();

        EmptyChests emptyChests = new EmptyChests();

        private static Plugin plugin;

        /**
         * Plugin start-up logic.
         */
        @Override
        public void onEnable() {
            plugin = this;
            saveDefaultConfig();
        }

        /**
         * Method allowing plugin to be accessed by other classes.
         *
         * @return plugin
         */
        public static Plugin getPlugin() {
            return plugin;
        }

        /**
         * Plugin shut-down logic.
         */
        @Override
        public void onDisable() {}

        /**
         * Handles command inputs from user.
         *
         * @param cmd Command issued (looks for '/cr')
         * @param sender Player or console who issued command
         * @param args Command-line arguments
         * @return Boolean representing successful or unsuccessful execution of command
         */
        public boolean onCommand(Command cmd, CommandSender sender, String method, String[] args) {
            if (cmd.getName().equalsIgnoreCase("cr")) {
                Bukkit.getLogger().info("Check");
                if (args.length > 1) {
                    sender.sendMessage(ChatColor.RED + "Too many arguments. Type '/cr' for help.");
                    return true;
                }
                } else if (args.length == 0) {
                    if (!method.equalsIgnoreCase("all")) {
                        sender.sendMessage(ChatColor.RED + "/cr refill <region|radius|all>");
                        return true;
                    } else {
                        sender.sendMessage(ChatColor.RED + "/cr refill <region|radius|all>");
                        sender.sendMessage(ChatColor.RED + "/cr empty <region|radius|all>");
                        return true;
                    }
                }

                String methodType;
                if (method.equalsIgnoreCase("fill")) {
                    methodType = "fillType";
                } else if (method.equalsIgnoreCase("empty")) {
                    methodType = "emptyType";
                } else {
                    sender.sendMessage(ChatColor.RED + "Invalid case!");
                    return true;
                }

                String methodArgument;
                if (args[0].equalsIgnoreCase("all")) {
                    methodArgument = "allArg";
                } else if (id.isNumeric(args[0])) {
                    methodArgument = "radiusArg";
                } else {
                    methodArgument = "regionArg";
                }

                ArrayList<String> chestList = (ArrayList<String>) getConfig().getStringList("chests.chest-list");
                String chestToFill;
                String chestToEmpty;
                switch (methodArgument) {

                    case "allArg":
                        if (methodType.equalsIgnoreCase("fillType")) {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToFill = chestList.get(i);
                                fillChests.fill(chestToFill);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests filled!");
                        } else {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToEmpty = chestList.get(i);
                                emptyChests.empty(chestToEmpty);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests emptied!");
                        }
                        break;

                    case "radiusArg":
                        if (methodType.equalsIgnoreCase("fillType")) {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToFill = chestList.get(i);
                                fillChests.fill(chestToFill, args[0]);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests in radius " + args[0] + " filled!");
                        } else {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToEmpty = chestList.get(i);
                                emptyChests.empty(chestToEmpty, args[0]);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests in radius " + args[0] + " emptied!");
                        }
                        break;

                    case "regionArg":
                        if (methodType.equalsIgnoreCase("fillType")) {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToFill = chestList.get(i);
                                fillChests.fill(chestToFill, args[0]);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests in region " + args[0] + " filled!");
                        } else {
                            for (int i=0; i < chestList.size(); i++) {
                                chestToEmpty = chestList.get(i);
                                emptyChests.empty(chestToEmpty, args[0]);
                            }
                            sender.sendMessage(ChatColor.GREEN + "[ChestRandomizer] Chests in region " + args[0] + " emptied!");
                        }
                        break;

                    default:
                        sender.sendMessage(ChatColor.RED + "null");
                        return true;
                }


            return true;
        }
    }
    EDIT 1: Also updated any args.length() usage that I forgot to before pasting code. Will briefly check again and see if that helps.
    EDIT 2: No change.
     
  5. It is supposed to throw an error, that is the whole point of @Override. The error is telling you the method onCommand does not override a method from a superType (It is supposed to). There is no method public boolean onCommand(Command cmd, CommandSender sender, String method, String[] args). The order of the parameters matters. Like i said. The correct parameters are onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3). And yes it will work without @Override but ignoring the annotation and subsequent error is what caused this in the first place. Errors happen for a reason.
     
    #6 ChiefMoneyBags, Sep 4, 2020
    Last edited: Sep 4, 2020
    • Agree Agree x 1
  6. Worked like a charm. Sorry for misunderstanding.