Convert String (PlayerName) to Player?

Discussion in 'Plugin Development' started by KaiBB, Feb 15, 2012.

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

    KaiBB

    Should be my last question for now, is there anyway to do this? I need to get the message of a certain player and convert the username they type into a Player :)
     
  2. Offline

    mmiillkkaa

    plugin.getServer.getPlayer(playerName) I believe it is.
     
  3. Offline

    TopGear93

    simple way to do it. :D

    String name = player.getName();
     
  4. Offline

    KaiBB

    No, no. I'm turning a string into a Player, not a Player into a string -_-

    What?

    Let me explain.
    I have, in a PlayerChatEvent, a player who types a username.
    I need to take that username and convert the username into a player.
    String = Username.
    Convert string to Player.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
    To175 likes this.
  5. Offline

    TopGear93

    String names = Server.getOnlinePlayers();
    if(msg.contains(names)){
    player.sendMessage("You typed" + names);

    might work. seems like a logical way :D. if it doesnt work then you might need to float around on the http://jd.bukkit.org/doxygen/index.html and look for the right methods and such.
     
  6. Offline

    Wundark

    Here is the code I think you are looking for.

    PHP:
    Player p Bukkit.getServer().getPlayer(ADD USERNAME HERE);
     
    To175, Domi381 and dark navi like this.
  7. Offline

    TopGear93

    ah that might be another great way.

    String name;
    Player p = Bukkit.getServer().getPlayer(name);
    p.sendMessage("Debug" + name);
     
  8. Offline

    KaiBB

    TopGear93 likes this.
  9. Offline

    thehutch

    KaiBB

    Here is what you should use :D

    Code:java
    1. Player p = Bukkit.getPlayer("PlayerName");
    2. if (p != null) {
    3. // Do stuff
    4. } else {
    5. // Player is not online
    6. }
     
    PandazNWafflez and TopGear93 like this.
  10. Offline

    TopGear93

    Yea, all our ideas would work.
     
  11. Offline

    thehutch

    TopGear93 infact none of your methods really work or have any type of backup system incase of user error :(

    Code:
    String names = Server.getOnlinePlayers();
    if(msg.contains(names)){
    player.sendMessage("You typed" + names);
    And this doesn't make any sense you can't check if a String contains a Player array

    and second none of what you said had any relation to what the OP was asking, sorry but you should make sure what you're writing is correct.

    PS: Sorry if I sound rude but you should be aware of this so that you learn aswell.
     
    PandazNWafflez likes this.
  12. Offline

    Wolfy9247

    To prevent you from getting a stack-trace returned if the player doesn't exist, you'd want something like:
    Code:java
    1.  
    2. try {
    3. Player player = Bukkit.getPlayer(args[0]);
    4. }
    5. sender.sendMessage(ChatColor.RED + "Player doesn't exist!");
    6. return true;
    7. }
    8. Player player = Bukkit.getPlayer(args[0]);
    9. sender.sendMessage("The player's name is: "+player.getName());
    10. return true;
    11.  
     
  13. Offline

    thehutch

    Actually that try/catch will never catch anything because a NPE will never be called unless you entered a null string (which args[0] can never be), a NPE is only called when you try to use a null object.
     
    PandazNWafflez likes this.
  14. Offline

    Wolfy9247

    I can actually prove you wrong on that one. If you're to do:
    Code:java
    1.  
    2. Player player = Bukkit.getPlayer(args[0]);
    3.  


    And the player types in /cmd <INVALIDPLAYER> (where the invalid player is a player that doesn't exist)

    Then the server's going to return the player as null, causing an NPE.

    The stack-trace looks like this:
    NPE Stacktrace (open)

    20:44:15 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'wnc'
    in plugin WNChatCleaner v0.2
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    8)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:4
    02)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:784)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:744)

    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:732)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:100)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:537)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:435)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.NullPointerException
    at wolfy9247.WNChatCleaner.Main.onCommand(Main.java:169)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    ... 12 more
     
  15. Offline

    thehutch

    (facepalm) , I'm sorry but you're wrong :(

    Code:
    Player player = Bukkit.getPlayer(args[0]);
    If args[0] is an invalid player then player will be null but it won't cause an exception because you haven't used it for anything. In your example if args[0] was null then the NPe would be at player.getName() because you're trying to get the Name of an object that doesn't have a value (aka it's null), if you moved your catch to under that line then if args[0] was null then it would catch an NPE otherwise your code wouldn't work :( , plus checking if the value is null is better than using a try/catch it see if it errors.

    http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception
     
    PandazNWafflez likes this.
  16. Offline

    Dinnerbone Bukkit Team Member

    thehutch is correct. A NPE is only thrown if you actually try to use null for something like it were a real object, and never any other time. You should test if the method returned null by doing something like this:
    Code:
    Player player = Bukkit.getPlayer[args[0]];
    if (player == null) {
        sender.sendMessage(ChatColor.RED + "Player doesn't exist!");
        return true;
    }
    
    However, it's possible that there is no args[0], so you should check the length of args first and make sure it has at least one element, or that'll throw up another exception.


    Edit: It's entirely possible that an NPE may be thrown if there's a bug in Bukkit itself, in which case that should be reported and you should still nullcheck as normal. However, from the stacktrace you posted, this is not likely to be the case.
     
    Skyost, thehutch and cheese5505 like this.
  17. Offline

    cheese5505

    Thank you.
     
  18. Offline

    thehutch

    Urgh, stop trying to argue please. Just because a variable is null doesn't mean it will throw a NPE. An NPE is caused when you try to use a variable to do something but the variable itself is null. For example:

    Code:
    Player player = Bukkit.getPlayer(args[0]);
    Let's just say args[0] is null, the player doesn't exist ok :D

    If you tried to send this "player" a message using this:

    Code:
    player.sendMessage("I am a null player you can't do this!")
    ^ This line will throw a NPE because the player variable is null.

    To check if a variable is null you would use this code to do so

    Code:
        Player p = Bukkit.getPlayer("PlayerName");
        if (p != null) {
        // Do stuff
        } else {
        // Player is not online
        }
    This will check to see if the player variable is null if not then //Do stuff else your player is null sendMessage, whatever :D

    PS: Not being rude but it is 3:10am and I should be sleeping not explaining NPE's to people after I have told them multiple times :D
     
    PandazNWafflez and cheese5505 like this.
  19. Offline

    cheese5505

    Exactly.
     
  20. Offline

    thehutch

    Dinnerbone Thank you and also tomorrow or today depending when I wake up i'll make a list of all exception which repeatedly occur so that stuff like this will not happen again :D
     
  21. Offline

    flatd

    Find it funny when all of u do long ways to get player name when for the commands u could just do Player player = (Player) sender;
     
  22. Offline

    Wolfy9247

    That's not what the OP was wanting. The point was to get the Player from a String.
     
  23. Offline

    KaiBB

    It's not a command. It's a player typing a username into chat.

    So if I added something to the non-existent player's inventory? Would that throw me an NPE?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  24. Offline

    SirTyler

    just do (non-existent player).getName(); or some none intensive method, will throw an NPE and you wont have run threw more code to test it.
     
  25. Offline

    KaiBB

    K, thanks.
     
  26. Offline

    Njol

    Why you you want a NPE? To spam the server log, frighten the server admins, and getting your plugin's page spammed with error reports?
     
  27. Offline

    KaiBB

    I do not want an NPE...
     
  28. Offline

    SirTyler

    It will throw and NPE to be caught by the try/catch. it will only show in the log if you print it. Mostly its just to check if the player exists or not.
     
  29. Offline

    KaiBB

    Alright.
     
  30. Offline

    Njol

    SirTyler just suggested provoking a NPE such that "you wont have run [through] more code to test it" and you replied "K, thanks". At least read the post you reply to...

    This is the post in which your question was answered:
    Why provoke & catch a NPE if you can simply test for null?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
Thread Status:
Not open for further replies.

Share This Page