playername from UUID when player is not online

Discussion in 'Plugin Development' started by BimKurzAfk, Oct 23, 2020.

  1. Offline

    BimKurzAfk

    Hey,
    I want to get the playername from the UUID when the player is not online, but the method:
    Code:
    Bukkit.getOfflinePlayer(UUID);
    does not work. I think I have to get the name from a website but I dont know how. Can someone help me?
     
  2. Offline

    timtower Moderator Moderator

  3. Offline

    BimKurzAfk

    it returns null except the player is online
     
  4. Offline

    timtower Moderator Moderator

  5. Offline

    spuddy

    Never done this myself, but a quick look on Mojang's API docs says that making a request to
    https://api.mojang.com/user/profiles/<uuid>/names
    Will get a list of all names that UUID has used, I'd assume the first one would be the current. This is not the simplest thing to do, you need to mess around with URIs, ASync tasks and what not as you do not want to be making http, especially https requests in the main thread. It might be a handy Util function for my own library however so I'll see what I can come up with.

    Another option assuming you only want names of players who've actually connected to your server before would be to store their names against their UUIDs in a config file when they log in. It wouldn't detect a name change until they login with the new name however.

    Out of interest, the getOfflinePlayer function you say doesn't work. Have you tried calling it multiple times? If it's running an async to retrieve the offline player, it may need to be polled.
     
    Last edited: Oct 23, 2020
  6. Offline

    timtower Moderator Moderator

    Have you considered saving the name in the config?
     
  7. Offline

    BimKurzAfk

  8. Offline

    timtower Moderator Moderator

    @BimKurzAfk getOfflinePlayer should still not return null though.
    Why do you need the name? Could you post your code?
     
  9. Offline

    spuddy

    The main problem with doing things like that in the main thread is that, relatively speaking, network IO takes a long time. The calling thread blocks until it gets a reply. These days, a request from a reliable website is answered in a few milliseconds, which doesn't sound like a lot to us, but to your CPU, that's forever. Practically, that means you server is likely going to be skipping a game tick or two every time you do this. Players are probably not going to notice that if it's happening infrequently so it will function. Your issue will occur when you get network problems. If for some reason the API is running slow, is down for maint or what not. Your request will sit there until it finally gets a response or times out. During that time, your server will be frozen, players will notice that. Ultimately, however you want to implement stuff is your decision, if you feel Mojang's API is reliable enough and you don't mind skipping a game tick or two occasionally, then that's fine. It's just best to be informed on the possible drawbacks of any solution. Running the request async has its own HashSet<DrawBack> too.
     

Share This Page