[WORKAROUND] getServer().getPlayer() deprecated in Bukkit 1.7.8

Discussion in 'Plugin Development' started by EnderLance, Apr 14, 2014.

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

    EnderLance

    So we all must have noticed that in the latest developer build of Bukkit, the getServer().getPlayer(String arg0) method has been deprecated.

    This means that the onCommand() methods will not be able to process a player based on args.

    I just made a useful workaround for this, which lets you get an online player by his/her name.

    Here's the method (just add this somewhere in your code)
    Code:java
    1. public Player getPlayer(String name) {
    2. for(Player p : Bukkit.getOnlinePlayers()) {
    3. if(p.getName().equalsIgnoreCase(name))
    4. return p;
    5. }
    6. return null;
    7. }

    (code shortened by amhokies)


    Original Code:
    Show Spoiler

    Code:java
    1. public Player getPlayer(String name)
    2. {
    3. Player[] online = getServer().getOnlinePlayers();
    4. ArrayList<String> names = new ArrayList<String>();
    5.  
    6. Player player = null;
    7.  
    8. for(Player p : online)
    9. {
    10. names.add(p.getName());
    11. }
    12.  
    13. if(names.contains(name))
    14. {
    15. int index = names.indexOf(name);
    16. player = online[index].getPlayer();
    17. }
    18.  
    19. return player;
    20. }




    Then to use it in another method (say you want to set him/her as flying) just do

    Code:java
    1. getPlayer(playersName).setFlying(true);



    Hope this helps!


    - EnderLance
     
  2. Offline

    amhokies

    EnderLance
    While this was nice of you to share your idea with everyone, just because it's deprecated doesn't mean you can't use it. In most cases it's a good idea to keep from accessing deprecated methods, but in this case, the only reason it's deprecated is to raise awareness of UUID migration beginning in 1.8.

    EDIT: also, your code can be condensed quite a bit to:
    Code:java
    1. public Player getPlayer(String name) {
    2. for(Player p : Bukkit.getOnlinePlayers()) {
    3. if(p.getName().equalsIgnoreCase(name))
    4. return p;
    5. }
    6. return null;
    7. }
     
    hintss and EnderLance like this.
  3. Offline

    EnderLance

    Thanks for the quick reply :)

    Ah, I wasn't aware you could use deprecated method... Still a novice here :p

    Anyways, would you mind if I updated my post with the shortened code you provided?

    Thanks,

    - EnderLance
     
  4. Offline

    amhokies

    Sure, go ahead.
     
  5. Offline

    skyrimfan1

  6. Offline

    Garris0n

    1. The method was deprecated to "raise awareness" about the UUID methods, or something like that. You can still use it just fine, in fact the deprecation will be removed with the 1.8 update according to Bukkit. Also, using something that does the same thing as a deprecated method is essentially using the deprecated method, so why not just use the deprecated method?
    2. Your method is actually "replicating" getPlayerExact(), not getPlayer(). If you're curious, here is the getPlayer() method.
     
    EnderLance likes this.
  7. Offline

    RawCode

    Garris0n likes this.
Thread Status:
Not open for further replies.

Share This Page