Managing OfflinePlayers' changing usernames

Discussion in 'Plugin Development' started by skyland1a, Oct 26, 2020.

  1. Offline


    I have a theoretical problem as follows:
    {UUID: 12345, Username: MinecraftLvr}: Joins my server
    {UUID: 12345, Username: MinecraftLvr}: Leaves my server
    {UUID: 12345, Username: MinecraftLvr}: Changes username to "TyrannosaurusREX"
    EDIT: [37 days pass]
    {UUID: 98765, Username: ChuckyChez}: Changes username to "MinecraftLvr"
    {UUID: 98765, Username: MinecraftLvr}: Joins my server
    {UUID: 98765, Username: MinecraftLvr}: Leaves my server

    The server now has 2 OfflinePlayers with the same username but different UUID's cached.

    Now I have no way to access the OfflinePlayer instance of {UUID: 12345} via their username until they join the server again.
    This can be solved by querying the Mojang API and getting {UUID: 12345}'s new username and pointing every command used in my plugin taking a username as a parameter and converting it to the proper UUID.

    However, other plugins running on the server will still be using the broken username to UUID conversion (Bukkit's standard conversion) which

    S0, how can I modify the OfflinePlayer(s) returned with any Bukkit command that returns an OfflinePlayer(s) so that the username update is reflected in other plugins running on the server?

    TLDR: How do you change the result of
    Bukkit.getOfflinePlayer(UUID uuid)
    Bukkit.getOfflinePlayer(String username)
    So that an OfflinePlayer can have a custom username?
    Last edited: Oct 26, 2020
  2. Online

    timtower Moderator Moderator

    @skyland1a Name is locked for a couple days I believe.
    And this can be bypassed by just not storing names.
    Strahan likes this.
  3. Offline


    Yes, but the event still can occur, although rarely, it has happened before on a server I played on regularly (although not to my account specifically.) So I want my plugin to account for this case.

    Bukkit stores the names of every player who has joined the server already, my plugin checks if a player joins with the same username as another player (who is offline) and if they have different UUID.

    Say another plugin had the command /playerinfo [username]
    1. A player types /playerinfo MinecraftLvr
    2. A third party plugin calls Bukkit.getOfflinePlayer(String username) to get the OfflinePlayer Instance of the username (which can be used to get the UUID)
    3. The method Bukkit.getOfflinePlayer(String username) returns:
      {UUID: 12345, Username: MinecraftLvr} OR
      {UUID: 98765, Username: MinecraftLvr}
      (Depending on how OfflinePlayers are stored)
      regardless of who actually owns the username currently.
    Basically the plugin needs to fix that by updating Bukkit's OfflinePlayer storage to the result provided by the Mojang API.

    So in this case, utilizing the stored players is a must as we don't want to force users to spent time typing an entire UUID in their command!
    Last edited: Oct 26, 2020
  4. Offline


    Yes, this is a good example of why I always reference players by UUID. Your plugin should never depend on something variable when you need something constant.
  5. Online

    timtower Moderator Moderator

    @skyland1a You can still have the issue where the user wants info on user1 but due to the rename he gets info about user2.
    How do you want to handle that then?
  6. Offline


    Whenever a player joins the server, if there is a conflict between usernames in the OfflinePlayer cache, I want the old OfflinePlayer's username to be updated to their current one via Mojang's API. This way, only conflicting usernames will be updated. This means that there is always a way to access a UUID via a username.
    If a player changes their name, and it doesn't conflict, then it won't be updated.

    So if a player changes their name to someone else's old username, then the old username will be updated, so their UUID can still be accessed using their up-to-date username.

    Therefore, if your user wants info on user1, it is possible, as long as they use the updated username and not the old one, instead of it being impossible to get the information.
  7. Online

    timtower Moderator Moderator

    @skyland1a When they join it should auto update. Conflict checking can be annoying as you can have a lot of OfflinePlayers on the server.
  8. Offline


    It only updates for the joining player, any other out-of-date usernames that conflict with the joining player will not be updated - at least to my intuitive knowledge, this is hard to test.
    Last edited: Oct 26, 2020
  9. Offline


    To put it more simply, is there a way to edit Bukkit's OfflinePlayer database (accessed via:
    Bukkit.getOfflinePlayer(String username)
    Bukkit.getOfflinePlayer(UUID uuid)

    So that I can modify the username field of an OfflinePlayer?

Share This Page