Solved Set content in config : issues (getPlayer)

Discussion in 'Plugin Development' started by To175, Nov 18, 2014.

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

    pookeythekid

    Whether it gives you an error or not, just don't use player.getPlayer() (which doesn't exist... I'm just as confused as you are). I can almost guarantee you that you won't be getting an NPE on this:
    Code:java
    1. Player player = (Player) sender;
    2. String playername = player.getName();
     
  2. Offline

    To175

    Skionz it was an example ...
    pookeythekid I didn't get it

    But no matter, I used a code for onCommand which works (My plugin is version B 0.5) :
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. //.... Declaration of all messages from config here ....
    3. if(cmd.getName().equalsIgnoreCase("tk")){
    4. if(sender instanceof Player){
    5. Player player = (Player) sender;
    6. FileConfiguration config = getConfig();
    7. if(args.length == 0){
    8. player.sendMessage(ChatColor.RED + Usage + " : /tk " + HelpCommand);
    9. }
    10. else if(args.length == 1){
    11. //......

    OMF god you are all right, I used getPlayer in an other code...

    But So how can I modify this please ? =>
    Code:java
    1. Bukkit.getPlayer(player);
    2. //and
    3. Bukkit.getServer().getPlayer(player)
     
  3. Offline

    mrCookieSlime Retired Staff

    To175
    What exactly do you mean with "modify". Please explain what you want to accomplish with this bit of code.
     
  4. Offline

    To175

    mrCookieSlime
    In this code I have getPlayer() ...
     
  5. Offline

    mrCookieSlime Retired Staff

    To175
    Yes. player.getPlayer() does not exist but Bukkit.getPlayer(name) is fine.
     
    pookeythekid likes this.
  6. Offline

    pookeythekid

    mrCookieSlime Whoa, you're a Moderator! Congrats! I didn't see any homepage post on this...

    To175 Why do you get the player from the server when you already have an instance of the player?
     
  7. Offline

    mrCookieSlime Retired Staff

    pookeythekid
    Yep, I am. Thanks. And there was no post about this. Anyway, please get back ontopic now.
     
  8. Offline

    pookeythekid

    mrCookieSlime Yeah, it was a bit off topic, but did I get ninja'd before I edited in my reply to To?
     
  9. Offline

    To175

    pookeythekid mrCookieSlime The problem is lines 6 (193) and 21 (137) :
    http://pastebin.com/fnVkyTca
     
  10. Offline

    pookeythekid

    To175 The stack trace says your problem lies in your own method, TeamsMessage(arg, arg, arg). It would say "player.getName()" if this were a problem with player.getName(). The NPE is somewhere in your own method.

    Edit: Also, if you have org.bukkit.entity.Player imported, I don't believe you should use a capital P in Player for a String name.

    Re-Edit: By the way you're using the player's name in your method, it's both possible and recommended that you use UUIDs instead. For example:
    Code:java
    1. public void TeamsMessage(String team, String message, UUID id) {
    2. Player player = Bukkit.getServer().getPlayer(id);
    3. // dot dot dot, other code
    4. // Skip forward a few lines
    5. UUID playerID = event.getPlayer().getUniqueID;
    6. // ...
    7. TeamsMessage(team, brokenHeart, playerID);
     
  11. Offline

    To175

    pookeythekid thanks !!
    but
    I didn't understand

    And why is it giving me a nullPointer if in my DEBUGs I have fixed all var ? (there is no "null" !)
     
  12. Offline

    mazentheamazin

    Yes, it actually does exist. Player extends OfflinePlayer which has the method getPlayer.
     
  13. Offline

    pookeythekid

    This may be a problem.
    Code:java
    1. String Team = "Red":

    May want to not press so hard on the shift key when you're putting in semicolons. ;)
     
  14. Offline

    To175

    pookeythekid
    I use Eclise.
    I have no syntaxe error on Eclipse.
    Consider that syntax errors are just here because I didn't wrote corectly.
    So forget synthaxe.
    So I'm really lost :(
     
  15. Offline

    pookeythekid

    To175 Syntax error or not, you don't want a colon in your code where there should be a semicolon.

    Edit: In your previous message, what DEBUGs? I didn't see any debug printouts in your code.
     
  16. Offline

    To175

    pookeythekid .....................
    There is not any null var !
    (I also do it before the call of TeamMessage() and same thing, every var are set...
     
  17. Offline

    pookeythekid

    To175 Are you on your server when this method is called? the getPlayer() method gets an online player. It throws an NPE if the player isn't online and if you didn't make a check to see if the player is offline. Otherwise, just go through your code line by line, and even check other classes that are involved in it. You'll eventually find something.
     
    To175 likes this.
  18. Offline

    Skionz

    To175 What is "Player?" I see it in your String but I am not sure what it is.
    Code:java
    1. ChatColor.GREEN +Player+ChatColor.GOLD

    Copied ^
     
  19. Offline

    To175

    pookeythekid (+ Skionz ) Hum what is the problem if I am not online ? I don't understand.
    For example, this is working :
    Code:java
    1. event.getPlayer().sendMessage();

    However in the code below playerM is a list with OFFline players for the moment (when the plugin will be finished, it they will be all online because I delete them on the onPlayerQuit event)
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void TeamsMessage(String TeamM, String Message, String Player){
    3. Set<String> SetPlayers = getConfig().getConfigurationSection("GameDatas.Players").getKeys(false);
    4. String PrefixMessage = getConfig().getString("PrefixMessage");
    5. if(!Player.equals("0")){
    6. Message = Message.replace("$player$", Player);
    7. }
    8. if(!TeamM.equals("0")){
    9. for(String playerM: SetPlayers){//on regarde tous les joueurs en jeu
    10. if(getConfig().getString("GameDatas.Players." + playerM + ".Team").equals(TeamM)){//si il est dans la team alors...
    11. Bukkit.getServer().getPlayer(playerM).sendMessage(ChatColor.GOLD + PrefixMessage + ChatColor.BLUE +" "+ Message);
    12. }else{
    13. Bukkit.getServer().getPlayer("to175").sendMessage(ChatColor.BLUE + " DEBUGER " + ChatColor.GOLD + " player : "+ChatColor.GREEN +playerM+ChatColor.GOLD+" Message : "+ChatColor.GREEN +Message+ChatColor.GOLD+" Player : "+ChatColor.GREEN +Player+ChatColor.GOLD+". TeamM : "+ChatColor.GREEN +TeamM+ChatColor.GOLD+" TeamM : "+ChatColor.GREEN +TeamM+ChatColor.GOLD+" END ");
    14. Bukkit.getServer().getPlayer(playerM).sendMessage(ChatColor.GOLD + PrefixMessage + " " + ChatColor.GREEN +"L'équipe "+TeamM+" perd un coeur.");
    15. }
    16. }
    17. }
    18. else{
    19. for(String player: SetPlayers){
    20. Bukkit.getServer().getPlayer(player).sendMessage(ChatColor.GOLD + PrefixMessage + " " + ChatColor.GREEN + Message);
    21. }
    22. }
    23. }
     
  20. Offline

    Skionz

    To175 Your attempting to use Bukkit.getPlayer("to175"); This will throw a NPE if you are not online.
     
  21. Offline

    To175

    I have turn getPlayer into comments ! It works ! thx you very much pookeythekid Skionz
    However, value doesn't set :( it is supposed to be "false"
    Code:
    Bukkit.getServer().getPlayer("to175").sendMessage(ChatColor.BLUE + " DEBUGER " + ChatColor.GOLD + " GameDatas.Teams."+ChatColor.GREEN +Team+ChatColor.GOLD+".Hearts."+ChatColor.GREEN +Heart);
    getConfig().set("GameDatas.Teams."+Team+".Hearts."+Heart, "false");
    I can see my debugger :
    My config :
    Code:
    GameDatas:
      Days: 2
      Teams:
        Blue:
          Hearts:
            one: 'true'
            two: 'true'
            three: 'true'
            four: 'true'
     
  22. Offline

    Skionz

    To175 Did you save it?
     
    To175 likes this.
  23. Offline

    To175

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

Share This Page