Solved Command Tab Completion not working

Discussion in 'Spigot Plugin Development' started by EvilDarkMatter, May 11, 2020.

  1. Hi everyone,
    Im trying to a plugin that has multiple features so i learn about spigot/bukkit, i've now got to the point of having a custom config and i now wanna make users edit that config from ingame commands.
    To do so i wanted do add a /settings command that auto completed the args after it so for example:
    "/settings commandName settingRule true/false."
    to do so i create a settingsTab class where i define the auto completition:

    Code (Java):

    public class settingsTab implements TabCompleter {
        @Override
        public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {

            if (args.length == 1) {
                List<String> settingsList = new ArrayList<>();

                settingsList.add("StopBlockBreak");
                settingsList.add("StopBlockPlace");
                settingsList.add("DisableDamageTaken");
                settingsList.add("DisableDamageDealt");
                settingsList.add("Blindness");

                return settingsList;
            } else {
                if (args.length == 2) {

                    List<String> settingsList = new ArrayList<>();

                    settingsList.add("true");
                    settingsList.add("false");

                    return settingsList;
                }
            }

            return null;
        }
    }
    I then created the class where i would have my actual command:

    Code (Java):
       
    public class settings implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

            if (cmd.getName().equalsIgnoreCase("settings blindness true")) {
                if (sender instanceof Player) {
                    Player p = (Player) sender;
                    if (p.hasPermission("edmpl.admin")) {
                        p.sendMessage("Test msg");
                    } else {
                        p.sendMessage("You don't have permission!");
                    }
                }
               
            }
            return true;
        }
    }
     

    I then registered the class of the command and the commandTabCompleter in my Main as following:

    Code (Java):
     
    getCommand("settings").setExecutor(new settings());
    getCommand("settings").setTabCompleter(new settingsTab());
     
    But when testing the actual command i dont receive a single feedback, no erros in the console nor chat message saying either "Test msg" or "You don't have permission!".
    Can someone help me?
     
  2. cmd.getName() will only return "settings", so "blindness" and "true" would be args[0] and args[1].
     
  3. Thanks for the reply, this is what i added:

    Code (Text):

       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("settings") && args[0].equalsIgnoreCase("blindness") && args[1].equalsIgnoreCase("true")) {
                    if (sender instanceof Player) {
                        Player p = (Player) sender;
                        if (p.hasPermission("edmpl.admin")) {
                            p.sendMessage("Test Msg");
                        } else {
                            p.sendMessage("You don't have permission!");
                        }
                    }
                }
            return true;
            }
    }
     
    But i get a NullPointerException when using the command.
     
  4. Care to post the stacktrace?
    Edit: also, check if args.length is at least 2 before trying to access args[1]
     
  5. Ok, thanks for the tip, this is the error i get:

    upload_2020-5-11_19-20-32.png
     
  6. Solved:
    upload_2020-5-12_11-31-19.png
     

    Attached Files: