Solved Getting offlinePlayer UUID

Discussion in 'Plugin Development' started by ItsMas_, Feb 20, 2016.

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

    ItsMas_

    In my plugin, every player has their own yml file to store data of them in.
    The file is named <uuid of player>.yml

    For one of the commands, I need to be able to access an offlinePlayer's file.

    However, when I try and use getUniqueId().toString on an OfflinePlayer, I get a NullPointerException, so I'm guessing you can't get the uuid of offline players like that.

    What is the quickest and most efficient way to get the uuid of an offlineplayer?
     
  2. Offline

    teej107

  3. Offline

    ItsMas_

    Code:
    OfflinePlayer oTarget = Bukkit.getServer().getPlayer(args[0]);
    
    if (!isInt(args[1])){
    
        sender.sendMessage(PunishUtil.daysNumber);
        return true;
    }
    
    int days = Integer.parseInt(args[1]);
    
    String reason = "";
    
    for (int i = 2; i < args.length; i++){
        reason += args[i] + " ";
    }
    
    reason = reason.trim();
    
    File file = new File("plugins//MasCore//Punish//Mutes//" + oTarget.getUniqueId().toString() + ".yml");
    NPE given on line 19 of above

    That is all the code starting where the player variable is assisgned, ending with the line that gives the error

    EDIT: I know I shoudln't hardcode the file location, I am getting round to fix this all in one go later, since I have done it many other times in my project. I am 99% sure this isn't the problem.
     
    Last edited: Feb 20, 2016
  4. Offline

    Xerox262

    @ItsMas_ I could've swore that you've had a problem like this before, try replacing all "//" with File.separator on line 19 then test, from what I remember this changed your error.

    Remember "plugins//MasCore//Punish//Mutes//" should mean "plugins/null/MasCore/Punish/null/Mutes/null/"
     
  5. Offline

    teej107

    Or just the getter for retrieving the plugin data folder and the right Constructor for File. I have never needed to use File.separator.
     
  6. Offline

    ItsMas_

    @Xerox262
    Changed it to this:
    Code:
    file = new File(plugin.getDataFolder() + File.separator + "Punish" + file.separator + "Mutes" + file.separator + oTarget.getUniqueId().toString() + ".yml");
    Still get an NPE on that line
     
  7. Offline

    Xerox262

    @ItsMas_ Damn, didn't realize sooner, you're using Bukkit.getPlayer(String); rather than Bukkit.getOfflinePlayer(String); that would only get the offline player for people that're actually online :p
     
    ItsMas_ likes this.
  8. Offline

    ItsMas_

    Thank you so much!
     
Thread Status:
Not open for further replies.

Share This Page