Commands help

Discussion in 'Plugin Development' started by AcePilot10, Mar 2, 2015.

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

    AcePilot10

    So the one thing im TERRIBLE in bukkit is commands :(. I really need help i'm getting error with this and I really would like somebody to explain my problems to me so I understand how the commands work. Thanks so much! Heres my code
    Code:
        public boolean onCommand(CommandSender sender, Command cmd,
                String label, String[] args) {
            Player player = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("bb")) {
                //TODO send help MSG
                }
           
                if(args.length == 2 || args[1].equalsIgnoreCase("create")) {
                ArenaManager.getManager().createArena(player.getLocation(), args[2]);
            }
               
            if(args.length == 2 || args[1].equalsIgnoreCase("join")) {
                ArenaManager.getManager().arenaJoin(player, args[2]);
            }
            return false;
        }
    }
     
  2. Offline

    Skionz

    @AcePilot10 The problems are that you are blindly casting a CommandSender to a Player and that you are using args[1] without checking if args[1] exists.
     
  3. Offline

    SuchSwegMuchWow

    @AcePilot10
    Code:
    public boolean onCommand(CommandSender sender, Command cmd,
                String label, String[] args) {
            Player player = (Player) sender;
          
            if(cmd.getName().equalsIgnoreCase("bb")) {
                //TODO send help MSG
    
                //Removed curly bracket here
          
                if(args.length == 2 || args[1].equalsIgnoreCase("create")) {
                ArenaManager.getManager().createArena(player.getLocation(), args[2]);
            }    
           else  if(args.length == 2 || args[1].equalsIgnoreCase("join")) {
                ArenaManager.getManager().arenaJoin(player, args[2]);
            }
            return false;
        }
    }//Added curly bracket here
    }
    Now you can actually check the arguments
     
  4. Offline

    AcePilot10

    @SuchSwegMuchWow @Skionz thanks SOOOO much for the help :)

    Ok, so I decided to change up the plugin and this is my final code (i hope)
    Code:
       
        public boolean onCommand(CommandSender sender, Command cmd,
                String label, String[] args) {
            Player player = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("bb")) {
               
                if(args.length == 2 || args[1].equalsIgnoreCase("create")) {
                    sender.sendMessage(ChatColor.BLUE + "Attempting to create arena " + args[2]);   
                    Arena.createArena(player.getLocation(), args[2]);
                    sender.sendMessage(ChatColor.GREEN + "Success!");
                }
               
                if(args.length != 2) {
                    sender.sendMessage(ChatColor.RED + "Too little args");
                    return true;
                }
               
                else if(args.length == 2 || args[1].equalsIgnoreCase("join")) {
                    Arena.teleportToSpawn(args[2]);   
                }
               
                if(args.length != 2) {
                    sender.sendMessage(ChatColor.RED + "Too little args");
                    return true;
                }
            }
            return false;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page