Changing a String --> Player ?

Discussion in 'Plugin Development' started by MuisYa, Oct 15, 2011.

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

    MuisYa

    Oke, sooo the AdminPanel plugin is getting a total rewrite and im going to put some nice thingies in.
    Tough i cant get 1 thing fixed:

    Banning, i think this method should work:
    Code:
            if (thePlayer.isBanned()) {
                thePlayer.setBanned(false);
            }
            else {
                thePlayer.setBanned(true);
                thePlayer.kickPlayer("You got banned by: " + theSender.getName());
            }
    At least, i dont see whats wrong with it... But it creates a nullpointer...
    Thanks alot!
     
  2. Offline

    stelar7

    is thePlayer null?
     
  3. Offline

    MuisYa

    Nope, Player thePlayer is the player i just got out of the text field.
    Im sure that it is the isBanned() method, but i think im just using it wrong...
    Same for isOnline() with me...

    Can someone help me?
     
  4. Offline

    MuisYa

    I dont wanna bich, but thats not what i did?
    Anyway, i have read it all. Everything that was there, i knew allmost all already tough...
    Still cant figure it out... The nullpointer is leading to line 114, which is that line i specified above:
    Code:
    if (thePlayer.isBanned()) {
        // do something
    }
     
  5. Offline

    coldandtired

    It means thePlayer is null, and whatever method you're using to grab thePlayer (which you should post here) isn't working properly.
     
  6. Offline

    AbeJ

    Post the error you get. The entire error.
     
  7. I was exaggerating to show what I meant. But all you posted was some code and "it creates a nullpointer".
    Anyway, my post was focused on the word "detailed". Now you at least posted the line where it is thrown, but still no stack-trace. Even though it is now to 90% thePlayer that is null, it could still not be the top-most entry in your stacktrace and the NPE is caused inside of the method (as far as we know from your information).

    When it is the top-most entry of the stack-trace, then you can be 100% sure that thePlayer is null, so then for us to help it is significant to see the declaration of thePlayer.
     
  8. Offline

    austen407

    Try doing something like:
    if (thePlayer.isBanned()) {
    try{ thePlayer.setBanned(false);
    }
    catch(Exception ex) { logger.info(ex.toString()); } } else {
    try{ thePlayer.setBanned(true); thePlayer.kickPlayer("You got banned by: " + theSender.getName());
    }
    catch(Exception ex) { logger.info(ex.toString()); }
    }
     
  9. Offline

    wwsean08

    did you remember to convert the string it takes from a textbox into an actual player?
     
  10. Ignore the post two posts above mine, please.
    You do not need to try-catch-everything, because all Java exceptions are already shown in the console.
     
  11. Offline

    thehutch

    Maybe posting the entire class or atleast the entire section of code relating to this play starting from an if doesnt help much if we cant see where your getting player from. :)
     
  12. Offline

    MuisYa

    I threw some debuggers in:
    Can someone give me a method that changes a String into a player?
    Thanks, because thats the problem.. My changing method doesnt work...

    What i used:
    Code:
    Player thePlayer = plugin.getServer().getPlayer(thePlayerString);
    plugin.getServer(); If you want to get the server ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  13. Bukkit.gerServer() is better, but that method should work, you should just check whether the player is null afterwards (and that is the case when the player could not be found).
     
  14. Offline

    MuisYa

    Can you add me on skype for a second? Because i just cant get it fixed...
    @Pandemoneus
     
  15. Not using Skype. ;)
     
  16. Offline

    MuisYa

    Code:
            Player thePlayer = plugin.getServer().getPlayer(thePlayerString);
    
            if (thePlayer.isOnline()) {
    How do i do this?
    Code:
    isOnline()
    isBanned()
    isOP()
    All gives a nullPointer for me, but im sure it is a player...
     
  17. Code:java
    1. Player thePlayer = plugin.getServer().getPlayer(thePlayerString);
    2.  
    3. if (thePlayer != null) {
    4. // do all your previous stuff
    5. } else {
    6. //send an error message
    7. }
     
  18. Offline

    Abrupt

    the only reason why getPlayer would return null is if a player couldn't be found with that name on the server.
     
  19. Offline

    Technius

    Code:Java
    1.  
    2. Player[] plist = plugin.getServer().getOnlinePlayers();
    3. for (Player p:plist)
    4. {
    5. if(p.getName().equalsIgnoreCase(nameofplayeryouwanttoban));
    6. {
    7. p.kickPlayer("The Ban Hammer has spoken!");
    8. p.setBanned(true);
    9. break;
    10. }
    11. }
    12.  
     
  20. @Technius
    You do lots of unneeded checks when you could get the player directly instead. Also, this won't work to unban players.
     
  21. Offline

    Technius

    But it works for online players. And getPlayer may not work with offline players. Banned players are usually offline.
     
  22. Offline

    MuisYa

    Well, i fixed it myself.. Thanks everyone!
    And i think the method isOnline() is not working...

    Wanna see the result: YouControl
     
  23. Offline

    thehutch

    Thank you very much you just sorted out one of my problems :)
     
  24. I'd rather like to see the source, lol.
     
  25. Offline

    emericask8ur

    if its a command do
    Player playa = getServer.getPlayer(args[0]);
    if(playa !=null){
    sender.sendMessage("Blah");
    } else {
    sender.sendMessage("Derp");
    }
    return true;
    }
    is that what you lookin for?
     
  26. Offline

    MuisYa

    @emericask8ur Thats what i WAS looking for yes :3
    Thanks!
     
    emericask8ur likes this.
  27. And what's so different from mine earlier?
     
Thread Status:
Not open for further replies.

Share This Page