Sub-Commands/Prefix/Ect Help

Discussion in 'Plugin Development' started by CarlosArias604, May 25, 2012.

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

    CarlosArias604

    Hello Bukkit Community

    Hey so I am working on a info plugin, I am not sure if I release it publicly but I hope to.
    Lets say I am making a healing plugin, What if I wanted to be fancy with healing others and "/heal <arg>" wasn't to my flavouring and I wanted to do "/heal other <arg>".
    So far I only know how to do "/heal <arg>"
    Which I don't want to write more and make my plugin look unprofessional with combining them, Like "/healother <arg>".
    I am going to leave my code so you can see what I know and hopefully you can tell me what to add or change. (Keep in note this is not my serious plugin that I making, Just a random one so no one steal meh code! And to keep it a secret)

    Show Spoiler

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (cmd.getName().equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")){
                Player player = (Player) sender;
                player.sendMessage(ChatColor.DARK_GREEN + "You Have Been Healed");
                player.setHealth(20);
                player.setFireTicks(0);
                player.setFoodLevel(20);
                player.setExhaustion(0);
                player.sendMessage(ChatColor.GREEN + "You Have Been Healed");
                return true;
            }
            return false;
        }



    Thank you for your time.

    Sincerely
    Carlos

    (I know this may have come up before... But I didn't bother looking as there is soo much stuff and Google/Youtube didn't help!)
     
  2. Offline

    r0306

    CarlosArias604
    Are you sure you've looked everywhere on Youtube, because I just found a few good tutorials on command executors. Anyways, you notice in your constructor that you have String[] args. So lets say someone typed /heal other Bob for each word after your initial command (lets say it was 'heal'). Then args[0] would be "other" and args[1] would be "Bob" and so on. Remember arrays start with an index of 0. To add a check for the length of the array, use args.length.
     
  3. Offline

    CarlosArias604

    Well on Google Yes, But it was just basic commands.
    On Youtube I only looked so far as most things that came up were "How to make a Bukkit server", Or something along those lines.
    But thank you I will try this out and see how well it works! ;)

    Sincerely
    Carlos

    (If you could maybe provide those links in case I need it!)
     
  4. Offline

    TightMrKI113r

    First you need to check the amount of extra arguments provided. We'll take your healing command as an example.

    Notice how in the onCommand method arguments there is a variable named args? For every extra argument provided, this goes up one. Now, you wish to check for one extra argument, a player name. Using this extra argument, you would loop over the online players, comparing each players name to what the extra argument was. That would be like so (Inside your 'if(command.getName().equalsIgnoreCase...')

    Code:
    if(args.length == 1){
        String toHeal = args[0]; //0 is the first argument
        for(Player player : getServer().getOnlinePlayers(){
            if(player.getName().equalsIgnoreCase(toHeal)){
            player.heal(20);
        }
    }
    You could even check for a custom heal amount, by parsing another arg as an integer, by adding the follwing underneath the above:

    Code:
    if(args.length == 2){
        String toHeal = args[0]; //0 is the first argument
        String amount = args[1];
        for(Player player : getServer().getOnlinePlayers(){
            if(player.getName().equalsIgnoreCase(toHeal)){
            player.heal(Integer.parseInt(amount));//I think its 'parseInt()', might be a bit different
        }
    }
    And thats how you check for extra arguments and use them :) Hope I helped :D
     
    CarlosArias604 likes this.
Thread Status:
Not open for further replies.

Share This Page