Name Changes

Discussion in 'Plugin Development' started by Tythus, Mar 10, 2014.

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

    Tythus

    Probably the wrong forum but here goes I have been updating my plugins to use the theoretical UUID that will be used instead of names in an update soon but what is going to happen with all my current data of older players is there going to be a way of converting these to the new UUID or am I screwed?
     
  2. Offline

    Opacification

    Well, to get the unique UUID of a player you can just use:
    Code:java
    1. player.getUniqueId();
    2.  

    But I guess you can convert your code and use that method to get the players UUID.
     
  3. Offline

    Garris0n

  4. Offline

    xize

    Tythus
    what you could do for now is for example when the player logs on check if the players file matches with their name if it is use f.renameTofile(p.getUniquedId());

    I did it this way through a constructor:
    Code:
    private Player player;
     private File f;
     private FileConfiguration con;
     private Item Potato;
     /**
      * 
      * @author xize
      * @param a constructor which pass our interface, if there is no user file this constructor will create a new one this constructor only allows type Player.
      * @return xEssentialsPlayer
      * 
      */
     public xEssentialsPlayer(Player player) {
      this.player = player;
      if(Bukkit.getOnlineMode()) {
       this.f = new File(xEssentials.getPlugin().getDataFolder() + File.separator + "players"+File.separator+new MojangUUID(player).getUUID()+".yml"); 
      } else {
       this.f = new File(xEssentials.getPlugin().getDataFolder() + File.separator + "players"+File.separator+player.getName().toLowerCase()+".yml");
      }
      if(this.f.exists()){
       this.con = YamlConfiguration.loadConfiguration(this.f);
       if(!this.con.getString("user").equalsIgnoreCase(player.getName())) {
        String oldName = this.con.getString("user");
        this.con.set("user", player.getName());
        this.con.set("ip", player.getAddress().getAddress().getHostAddress());
        try {
         this.con.save(f);
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
        //call the custom event whenever we noticed the name has been changed!
        Bukkit.getPluginManager().callEvent(new PlayerNameChangeEvent(oldName, player.getName(), player, this));
       } else {
        this.con.set("ip", player.getAddress().getAddress().getHostAddress());
        try {
         this.con.save(f);
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }
      } else {
       //check if the player file is just a normal name if it is we rename the file to the UUID spec, then cancelling futher code execution.
       File possiblename = new File(xEssentials.getPlugin().getDataFolder() + File.separator + "players" + File.separator + player.getName().toLowerCase() + ".yml");
       if(possiblename.exists()) {
        possiblename.renameTo(this.f);
        this.con = YamlConfiguration.loadConfiguration(this.f);
        return;
       }
       this.con = YamlConfiguration.loadConfiguration(this.f);
       this.con.set("isDefault", true);
       this.con.set("ip", player.getAddress().getAddress().getHostAddress());
       this.con.set("user", player.getName());
       this.con.set("fly", false);
       this.con.set("torch", false);
       this.con.set("firefly", false);
       try {
        this.con.save(this.f);
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }
      update();
     }
    
    and the way of getting I did for both online/offline players (its not the best way if you run big servers though):
    Code:
     public static File getOfflinePlayerFile(String player) {
      try {
       File dir = new File(xEssentials.getPlugin().getDataFolder() + File.separator + "players");
       if(dir.isDirectory()) {
        File[] list = dir.listFiles();
        for(File f : list) {
         FileConfiguration con = YamlConfiguration.loadConfiguration(f);
         if(con.isSet("user")) {
          if(con.getString("user").equalsIgnoreCase(player)) {
           return f;
          }
         }
        }
       }
      } catch(Exception e) {
       e.printStackTrace();
      }
      return null;
     }
    
    basicly I made my own player and offline player instances, and I do only changes to names when the player joins which is inside the constructor, since mojang already told on twitter that it is highly unlikely players can see their effect of a name change active on the server, they should have to relog first to see their name is changed this means also it updates the constructor once again.

    but theres also a other solution if you plan to migrate all players including offline players all at once to UUID's and that is using the library which Garris0n linked to you, however I recommend to use the first approuch since that library cannot be used in a synchronized area (which minecraft most of the time is) you should create a new thread and probably use the PlayerLoginEvent or something what runs before PlayerJoinEvent so whenever you get a exception because of a connection timeout you can kick the player, I wasn't able myself to implement it correctly I whas able to make the thread running but that will not garantue the player gets kicked directly:p.

    but perhaps command sided like /convert could be a good idea in combination of that lib if you don't need it for authing through uuid checks what my intention whas:p
     
    Garris0n likes this.
  5. Offline

    Tythus

    Opacification
    xize
    The issue is that is useful if a player logs in while username = constant & unique but the problem arrives with things like permissions and etc what if a player doesn't login until after usernames are no longer unique what do I do then? do I just forget them? lots of these players have donated for ranks etc and I don't want to do that really

    Garris0n
    That looks a bit confusing maybe add some more comments? but it grabs the profiles from the players Mojang page if that's correct?
     
  6. Offline

    Garris0n

    There's an implementation here.
     
  7. Offline

    xize

    Tythus
    as I tried to explain is actually this:

    player joins -> gets passed into a custom object where the constructor checks on the file name which is the p.getUniqueId() then as follows it checks if the contents of that file equals with the name of that player if it doesn't equals we change the user name in that file but untouch the uniqueId then throw a custom event.

    the constructor does also a other check to:
    when the file name just seems to be the players name, we rename the file to the getUniqueId and again reset the process and check if the name is changed as above.

    theoretici it should work but there are only two down sides with this, and that is you need to do this a long time before they release the name change feature if there are any players getting out of retirement and changed their name before they join its probably hardly impossible to set their current data unless you predict their ip never changes which is something I doubt.

    the other problem is the big servers, if you would get 100 unique players per day I'm not sure how it affects cpu performance if you do something like this:

    for(File f : files) {
    //create a YamlConfiguration
    //check the username
    //return custom instance
    }

    I actually made for the fun a tab complete for all offline players but I guess if you got more than 1000k players its a very bad practice:p
     
  8. Offline

    Tythus

    So effectively unless a player rejoins within the months leading up to or doesn't change their name after until they join again they will lose all donation benefits, permissions and etc
     
Thread Status:
Not open for further replies.

Share This Page