Retrieving Odd Information from onCommand

Discussion in 'Plugin Development' started by Harieo, Mar 25, 2016.

Thread Status:
Not open for further replies.
  1. Hello,

    I have got my command method setup, that's the easy part. A not so easy part is getting certain things from the String array. So I have a command (taking an example from another plugin) /msg <player> <message> but how do I get the <player> to an actual player (converting it from String to Player so I can use it)?

    I've been trying to do multiple for loops inside each other but it hasn't been successful.

    Does anyone have a solution?

    Thanks,
    Harieo
     
  2. Offline

    Ligachamp

    Search in the bukkit API for "getPlayerByName" methods, this should help you further ;)

    EDIT: Look here :)
     
  3. Thank you, you've revolutionised my plugins. :D

    Hold on, how exactly is it that you identify a specific part of a String array so I can isolate the name? I thought I knew but Java says I do not.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 25, 2016
    Ligachamp likes this.
  4. Offline

    I Al Istannen

    @Harieo
    You can use Bukkit.getPlayer(String) or Bukkit.getPlayerExact(String). They are deprecated, but work nonetheless and are cleaner than looping through every player online.

    I don't quite get what you mean with isolating the name. As you probably know, you can access the parts of the Array with "name[index]". That is already isolated. If you want to find out which part is the player name, either (recommended) specify that in the usage (message) for the command, or loop through every part and check if that is a player name.
     
  5. It's the index I am having a problem with. I thought it was just the Java numberline, (Starts at 0 and works it's way up) but Java is trying to blow me off the face of the earth for trying
     
  6. Offline

    mcdorli

    Maybe there are no arguments in the array show us your code
     
  7. Code:
    public Boolean onCommand(CommandSender sender, Command team, String arg1, String[] args){
    
    Player player = Bukkit.getPlayer(args[0]);
    
    if(player == null){
      sender.sendMessage(ChatColor.RED + "Invalid Argument!");
    }
    else
    {
      player.sendMessage(ChatColor.GOLD + "Someone has requested your team!");
    }
    
    }
     
  8. Offline

    Zombie_Striker

    You cannot just add arguments to the onCommand. You are also getting an arg before you even know the player has sent any args.

    Remove the "arg1" parameter and check if length of "args" is greater than 0 (testing if it even has an args[0]) before trying to get it.
     
  9. Wait, I was always taught that I need String arg1 for some reason or another (a formality of onCommand). This has been proven by the fact if you auto-generate the method from CommandExecutor, it comes up with String arg1.

    So I can remove it?
     
  10. Offline

    timtower Administrator Administrator Moderator

    @Harieo No, it is generally named "label" and not arg1
     
  11. Offline

    Zombie_Striker

    @Harieo
    My mistake. Although it is actually apart of the parameter, it is normally called 'label' (also, recently there have been a bunch people just adding arguments to constructors).

    You may want to change "arg1" to label, and make sure the length of the args are greater than 0 before getting the arg.
     
Thread Status:
Not open for further replies.

Share This Page