How to specify player receiver in onCommand method?

Discussion in 'Plugin Development' started by Lanuk, Apr 26, 2012.

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

    Lanuk

    I know this is probably a really dumb question, but I really can't figure it out. How can I specify a target player in one of my commands? I am guessing just as there is a "sender", there is not a "receiver". Help would be appreciated, thanks.
     
  2. Offline

    r0306

    The target player should be one of the arguments in the command. For example, say your command was /give bob stone. In this case, bob would be the receiver. What you would do to call up the player is to use arg[0] (bob). So you would do.

    Code:
    if (Bukkit.getPlayer(arg[1]).isOnline) {
    //give player stone
    }
     
  3. Offline

    Lanuk

    Ah, ok, thanks! To think it was that simple XD
     
  4. Offline

    G4meM0ment

    Isnt player a string then? How to parse the args to player?
     
  5. Offline

    Postbag

    Code:
    String targetString = args[1].toLowerCase(); //convert string to player
    Player target = Bukkit.getPlayer(targetString);
     
  6. You don't need the first line, it might only cause problems.
    Nope. 'Bukkit.getPlayer(<name>)' will return the player, where <name> is the players name which is correct since arg[1] (btw: it should be arg[0] if you don't have any other arguments or you'll get some indexoutofrange exceptions) is a string containing the name of the player. Check if the returned player is not null to see if the player, the user typed in, could be found.
     
  7. You don't need to make it lowercase but you need to check args.length if it's got the argument... because it will spit errors if you just type /give.
    Then check if the returned player is not null, if it's null then the player doesn't exist.
     
  8. Offline

    G4meM0ment

    I want to overgive the player to another method, but i need to be player then and args[0] is a string.

    I only can send strings to the method but I dont know how to parse it.

    And one more question for the onCommand, I renamed it once to onCommandTest but it didnt worked then.
    Is it possible to rename the onCommand, or do I need to create new classes?
     
  9. G4meM0ment
    It should've been clear by now that getServer().getPlayer() converts player name String to Player object.
     
  10. Offline

    G4meM0ment

    Sry didnt got this, thanks for the help :D

    But whats with onCommand can I rename it to onCommand1?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  11. No you can't just rename it because then it won't trigger.
    You can divide your code into multiple sections by triggering some methods when some conditions are met in the onCommand() OR you can use getCommand("cmd...").setExecutor(class) to use another class as the callback for the command... but I think that'll confuse you more.
    You can always read about these basic stuff in: http://wiki.bukkit.org/Plugin_Tutorial
     
Thread Status:
Not open for further replies.

Share This Page