Sub-Commands/Prefix/Ect Help

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

Thread Status:
Not open for further replies.
  1. 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 (Text):
    1.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2.         if (cmd.getName().equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")){
    3.             Player player = (Player) sender;
    4.             player.sendMessage(ChatColor.DARK_GREEN + "You Have Been Healed");
    5.             player.setHealth(20);
    6.             player.setFireTicks(0);
    7.             player.setFoodLevel(20);
    8.             player.setExhaustion(0);
    9.             player.sendMessage(ChatColor.GREEN + "You Have Been Healed");
    10.             return true;
    11.         }
    12.         return false;
    13.     }

    Thank you for your time.


    (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. 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. 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! ;)


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


    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 (Text):
    1. if(args.length == 1){
    2.     String toHeal = args[0]; //0 is the first argument
    3.     for(Player player : getServer().getOnlinePlayers(){
    4.         if(player.getName().equalsIgnoreCase(toHeal)){
    5.         player.heal(20);
    6.     }
    7. }
    You could even check for a custom heal amount, by parsing another arg as an integer, by adding the follwing underneath the above:

    Code (Text):
    1. if(args.length == 2){
    2.     String toHeal = args[0]; //0 is the first argument
    3.     String amount = args[1];
    4.     for(Player player : getServer().getOnlinePlayers(){
    5.         if(player.getName().equalsIgnoreCase(toHeal)){
    6.         player.heal(Integer.parseInt(amount));//I think its 'parseInt()', might be a bit different
    7.     }
    8. }
    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