[TUT/INTERMEDIATE] Grabbing player from an argument

Discussion in 'Resources' started by Jaker232, Nov 23, 2011.

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

    Jaker232

    We all know we can do /whitelist add [player] but what is the real truth behind it, how does it catch a randomized (like 123abc, 132abc) name? I will show you how this will work.

    First, you will need arguments called, so I recommend
    Code:java
    1.  
    2. if(args.length == 1) {
    3. }
    4.  

    We do not do
    Code:java
    1.  
    2. if(args[0].equals("something")) {
    3. }
    4.  

    Instead, in the args.length, we want to find the target (aka player that is being modified) so import server using this line.
    Code:java
    1.  
    2. Server server = Bukkit.getServer();
    3.  

    And the target import I use..
    Code:java
    1.  
    2. Player target = server.getPlayer(args[0]);
    3.  


    Okay, so we ran down to the basic, target is generating a warning that is never used. Don't panic. There is two ways to do this.

    Player Online
    If you want to kick someone, you would do
    Code:java
    1.  
    2. if(target != null) {
    3. } else {
    4. player.sendMessage("Player not found!");
    5. }
    6.  


    First, it checks to see if the target is in-game then executes the game. If player does not exist, "else" will handle this. It is generally recommended to send an error message saying player not found.

    Player Offline
    We want to ban players, whether they are online or not. This is the shortest way. All you have to do in args.length is put
    Code:java
    1.  
    2. target.<action>(parameters);
    3.  

    Replace <action> to the field defined (?) and replace parameters with true or false. Getting information is not required.

    Ending
    I spent 5 minutes working on this tutorial. I want to thank @emericask8ur for teaching me this, and I want to pass this down from generations to generations. You can also do this for integers and strings.

    Post your opinions and feedback to greatly improve this tutorial.
     
    Knight Hawk3 and emericask8ur like this.
  2. Offline

    Knight Hawk3

    Thanks, I've been trying to do this for awhile!
     
  3. Offline

    jogeta_masude

    I'm not sure I understand this, what if I wanted to say get the user from '/pex promote <user>' would this work for that? So that i can then for example, give them $50 for being promoted?
     
  4. Offline

    thehutch

    also you should use .equalsIgnoreCase("") so you don't have to worry about the case :D
     
  5. Offline

    xBlackSheepx

    Hey, this only works in the main class... Have you any ideas making it working in a CommandExecutor?
    sry for my english...i from germany ;)

    EDIT:
    Hey, this only works in the main class... Have you any ideas making it working in a CommandExecutor?
    sry for my english...i'm from germany ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  6. Couple things:
    • This is Beginner, at least in my opinion. If you don't know how to preform something like this your in trouble with any command-based plugin..
    • The second half of the plugin is relatively unclear. (This part)
      • target.<action>(parameters);
      • Replace <action> to the field defined (?) and replace parameters with true or false. Getting information is not required.
    • Your whole "Ban offline players" thing is completly screwed up. If you try to ban the player while he is offline in the present state, it will NPE, because you are getting an "Online" player. Instead, use something like:
      Code:java
      1.  
      2. OfflinePlayer player = Bukkit.getOfflinePlayer(args[0]);
      3. player.setBanned(true);
      4.  
     
    marszczybrew likes this.
  7. Offline

    DrAgonmoray

    How long have you been able to get offline players? :confused:

    so full of win!
     
  8. Pretty long time now :p
     
  9. Offline

    Jaker232

    *facepalm*
    If I had knew that.
     
Thread Status:
Not open for further replies.

Share This Page