Getting command arguments

Discussion in 'Plugin Development' started by wumpyc, Jun 29, 2013.

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

    wumpyc

    Hello, today I decided to script a simple trade chat channel for my server. It is really simple and I'm also very bad at scripting so I got stuck pretty much quick :D
    This is my script:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("trade")){
                if(args.length == 0){
                    player.sendMessage("§cTo use trade channel use: §e/trade <message>");
                    return true;
                }
                else
                   
                   
                }
            }
           
        return true;   
        }
    What I need to do is if there are any arguments to the command I have to get them into a string or something so I can then broadcast it to everyone.

    Simple explanation:
    Using /trade i want to sell diamonds
    would end up sending a message to whole server for example:
    [TRADE: playername] i want to sell diamonds
     
  2. Offline

    Techy4198

    args[0] for the 1st arg, args[1] for 2nd, etc.
     
  3. Offline

    wumpyc

    I want to get the arguments that player wrote. If he did /trade WTS diamond 5k
    Then I want to get the "WTS diamond 5k" out of it so I can broadcast it then.
     
  4. I would solve this thing this way:
    Code:java
    1. String broadcast = "";
    2.  
    3. for(int i=0;i<args.lenght;i++){
    4.  
    5. broadcast = broadcast + args[i];
    6.  
    7. }
    8.  
    9. Bukkit.getServer().broadcastmessage(broadcast);[/i]


    I think that should work
     
  5. Offline

    wumpyc

    Found out already. Thanks anyways
     
  6. Offline

    MP5K

    Yes but not how it should:
    you forgot to add an space to each Argument.
    Code:java
    1.  
    2. public static final String toString(String[] args){
    3. if(args == null) return null;
    4. String raw = "";
    5. for(int i = 0 ; i < args.length; i++){
    6. raw += args[i];
    7. if(i != args.length -1){
    8. raw += " ";
    9. };
    10. };
    11. return raw;
    12. };[/i]
     
  7. Of course :rolleyes:
     
Thread Status:
Not open for further replies.

Share This Page