Using command arguments???

Discussion in 'Plugin Development' started by dsmyth1915, Mar 30, 2012.

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

    dsmyth1915

    I've been searching for a while now, and it seems that noone has an effective way of using the command syntax args[#]. Can anyone explain to me how to use the arguments and define a command with more than 1 argument.
    What I want to do, is create a command that enables and disables on the typing of the command. much like essentials /god on /god off .

    The command;
    public void chesty(CommandSender sender, Command cmd, String CommandLabel, String[] args)

    I've seen it a few times, but the actual concept I haven't gotten down.

    What I have so far(doesn't work at all)
    Code:java
    1. public boolean chesty(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. if(cmd.getName().equalsIgnoreCase("chesty")){
    3. if (!(sender instanceof Player)){
    4. sender.sendMessage("only players may use this command");
    5. if(chesty = false){sender.sendMessage("Please turn on chesty before using this command.");}
    6. if(chesty = true){sender.sendMessage("Command is enabled!");
    7. }
    8. }

    What I want to use; /chesty on && /chesty off

    the code I need to implement for chesty
    Code:java
    1. @EventHandler
    2. public void playerInteract(PlayerInteractEvent event){
    3. Block block = event.getPlayer().getTargetBlock(null,100);
    4. if(block.getState() instanceof InventoryHolder){
    5. InventoryHolder ih = (InventoryHolder)block.getState();
    6. Inventory i = ih.getInventory();
    7. event.getPlayer().openInventory(i);


    Any help will be much appreciated, thanks for you time!
     
  2. Offline

    Drakonix

    You need to rename it.
    Code:JAVA
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){}
    3.  


    Arguments are stored in a variable args[].
    The args[0] is the command, so the args[1] would be the first argument.
    So your command should look like this.

    Code:JAVA
    1.  
    2. public boolean chesty(CommandSender sender, Command cmd, String commandLabel, String[] args){
    3. if(cmd.getName().equalsIgnoreCase("chesty")){
    4. if (!(sender instanceof Player)){
    5. if(args[1].equalsIgnoreCase("on")){
    6. chesty=true;
    7. sender.sendMessage("You have enabled something");
    8. return true;
    9. }else if(args[1].equalsIgnoreCase("off")){
    10. chesty=false;
    11. sender.sendMessage("You have disabled something");
    12. return true;
    13. }else{
    14. sender.sendMessage("Wrong command usage!");
    15. return true;
    16.  
    17. }
    18. }
    19. }
    20.  
    21.  
    22.  
     
  3. Offline

    dsmyth1915

    thanks :) I'll be sure to try it ASAP. implementing the other source, shuold I make another class and call chesty. if it's true allow the person to use it?
     
  4. Offline

    Giant

    Actually args[0] contains the first argument when using onCommand and cmd will hold the executed command. (cmd.getName() will return the name)
     
  5. Offline

    dsmyth1915

    Code:java
    1. if(args[0].equalsIgnoreCase("chesty"){
    so that?

    Edit: for some reason my eclipse doesn't have else if... Whenever I use it it tells me to delete the else. Any suggestions? Jdk 1.6.0.31 eclipse Java EE and classic. Neither eclipse have else if but I know most jdk's have else if.
     
  6. Offline

    Giant

    This would mean that it would pick "/cmd chesty".
     
  7. look out that "args[0]" throws an error, when the size of it (args.lenght == 0) is 0, at the case when the user dont add anny arguments
     
Thread Status:
Not open for further replies.

Share This Page