Subcommand runs main command as well

Discussion in 'Plugin Development' started by mrcal17, Mar 12, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    mrcal17

    Hi, in my plugin the subcommand when ran runs the main command as well. Here is some src I would love some help thank you!
    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
            Player player = (Player) sender;
                if(player.hasPermission("ShutAlert.run")){
                    if(command.getName().equalsIgnoreCase("ShutAlert")){
                        Bukkit.broadcastMessage(ChatColor.RED + "[" + ChatColor.GREEN +  "ShutAlert" + ChatColor.RED + "]" + ChatColor.BLUE + "Sever is about to restart in 20 seconds");
                            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                @Override
                                public void run() {
                                    getServer().dispatchCommand(getServer().getConsoleSender(), "save-all");
                                    getServer().dispatchCommand(getServer().getConsoleSender(), "stop");
                                    player.kickPlayer("Server is restarting!");
                                }
                            }, 400L);
                        if (args.length < 1){
                            return false;
                        }
                        if(args[0].equalsIgnoreCase("help")){
                            sender.sendMessage("Please just run /shutalert to alert and restart server.\n Requires permission: ShutAlert.run");
                            return true;
                        }
    
     
  2. Offline

    Zombie_Striker

    @mrcal17
    You did not post what is your problem, nor did you post what do you need help with.

    Some things that you should fix:
    1. DON'T BLIND CAST! This is a very basic and well documented problems. You have to check if the sender is a player before casting, or it will not always work. Check if sender is an instanceof Player.
    2. Check what the command is before checking the permission.
    3. You may want to add an if statement around the runnable, since currently it will always shutdown the server even if the player adds the "help" subcommand.
    4. Don't use \n for sending messages. Most likely that will break the chat in some versions of bukkit/spigot.
     
  3. Offline

    mrcal17

    I put the instanceof player sender after the permission, thank you for that fix. Around my runnable I need an if statement? What would the statement entail?
     
  4. Offline

    Desle

    do you mean the args[0] when talking about the subcommand? If you don't want something to run when there's an argument, just add an if statement to it.

    Code:
    if (command.getName().equalsIgnoreCase("ShutAlert")) {
    
        if (args.length == 0) {
            //main command
            return false;
        }
    
        if (args[0].equalsIgnoreCase("help")) {
            //sub command help
        }
    
    }
     
  5. Offline

    mrcal17

    Oh I get it! Thanks fam
     
  6. Offline

    Zombie_Striker

    @mrcal17
    Mark as solved if your problem has been solved.
     
Thread Status:
Not open for further replies.

Share This Page