Solved Getting an argument

Discussion in 'Plugin Development' started by mike2033, May 3, 2013.

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

    mike2033

    Hello devs!

    I'd like to ask if someone here knows how you can read "args". I already looked at dev.bukkit.org, but it seemed like those were for real entities there. Example is the easiest way to explain:

    Player types: /command hello
    Set "hello" as an string
    Server returns "hello" to the sender: sender.sendMessage(string);

    Hopefully its understandable. Thank you in advance
     
  2. Do you mean something like this?
    Code:
      if (args.length == 0) {
     
  3. Offline

    ZeusAllMighty11

    Code:
    public boolean onCommand(...){
        for(int i=0;i<args.length;i++){
            System.out.println(args[i] + " is the " + i + " string in the command." );
        }
        return false
    }
    
    This should give you a good idea on what arrays are for
     
    mike2033 likes this.
  4. Offline

    mike2033

    First - thank you both for your very fast reply... I more want to first read the arg out and THEN working with it.

    Here is an example from the BukkitWiki

    Code:
    Player s = (Player)sender;
    Player target = s.getServer().getPlayer(args[0]); // Gets the player who was typed in the command.
    Here "target" is set as the player typed in. I want to read out what player have been typed in and then for example send him a message.
    The command in this example could be like

    /kill *player name here*

    and the playername, as I said, would be the "target" then.
     
  5. Offline

    ZeusAllMighty11

    Code:
    public boolean onCommand(...){
        if(sender instanceof Player){
           if(args.length >= 1 && Bukkit.getPlayer(args[0]) != null){
               Bukkit.getPlayer(args[0]).setHealth(0);
           }
        }
        return false;
    }
     
  6. Offline

    mike2033

    Thanks TheGreenGamer!
     
  7. TheGreenGamerHD
    Why are you checking if sender is a player and why are you calling getPlayer() twice and without nullchecking ?

    mike2033
    I suggest you do it like this instead:
    Code:
    public boolean onCommand(...) {
        if(args.length > 0) {
            Player target = Bukkit.getPlayer(args[0]);
    
            if(target != null) {
                target.setHealth(0);
                sender.sendMessage("You made " + target.getName() + " die.");
            } else {
                sender.sendMessage("Can't find player: " + args[0]);
            }
    
            return true;
        }
    
        return false;
    }
     
  8. Offline

    Nova20012

    Digi

    I thought you have to check if the sender is a player, otherwise you get error code when try /kill <name> in console
     
  9. Nova20012
    You have to check instanceof Object when you cast to said Object, in this code you are not using anything from Player object regarding the sender, you are not casting him to Player therefore you don't need to check if he's a player.
    And logically you can use that command from the server console since it's not doing anything to you as a player (or console), it's doing something to the target.
     
    Nova20012 likes this.
  10. Offline

    Nova20012

    Digi
    Okay, i'll add that to my knowledge for my plugins! Thanks!
     
Thread Status:
Not open for further replies.

Share This Page