#Is there a non-depreciated way to get an OfflinePlayer by name?

1 messages ยท Page 1 of 1 (latest)

fallow shale
#

Hey guys! Title says it all :D

mild wyvern
#

command geek hi lmao

carmine radish
#

names are not guaranteed to be unique

#

what exactly is the xy here

fallow shale
#

Yep, I'm not depending on this for storage or anything... it's more for command arguments (for example to ban an offline player)

carmine radish
#

ah

#

Well, generally you can throw it at mojang API

mild wyvern
#

idk u could do smth that stores the name and when he tries to join he cant?

carmine radish
#

ban by name sounds like a pretty not good idea

mild wyvern
carmine radish
#

no

#

even then it is an issue

mild wyvern
#

why?

carmine radish
#

I can just rename

mild wyvern
#

oh yeah

junior iris
fallow shale
#

I just want to be able to grab an offline player by name (through command arguments) and from there I store the uuid to be banned, not the name

#

the name is never stored

carmine radish
#

Just throw the name at the mojang API

fallow shale
#

I'm just using it to get the player

carmine radish
#

and grab the profile from there

fallow shale
junior iris
#

Does getOfflinePlayer(String) get it from Mojangs API?

carmine radish
#

It does fallback iirc

junior iris
#

According to the docs it says it can make a web request

carmine radish
#

but also uses local server cache which is the issue

junior iris
#

Yeah true

fallow shale
#

Using the Mojang API worked! However it has 100-200 milliseconds of delay... is this normal and is there anyway to get around this?

carmine radish
#

well you are doing a network request

fallow shale
#

Not sure what that is ๐Ÿ˜…

carmine radish
#

A network request ?

#

you are asking a mojang server

#

your data has to travel the internet

#

that takes a bit

fallow shale
#

oh

#

yes

#

hmm so there's gonna be a delay no matter what?

carmine radish
#

Pretty much yes

#

I would suggest checking if a player with the name is currently online first

#

e.g. most bans might just be while the target is online

#

that way you can skip the networking

fallow shale
#

Yeah ofc, will implement that right away. Should I be worried about lag on the server due to the delay caused by the request?

carmine radish
#

You want to move that request off the main thread

fallow shale
#

Cool will do that ๐Ÿ‘

carmine radish
#

btw what lib are you using ? or are you doing a plain http request

#

I haven't played with spigot API much

#

so maybe you are using an API I just don't know about yet

fallow shale
#

here's my code:

public static OfflineUser getOfflineUser(String name) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("https://api.mojang.com/users/profiles/minecraft/" + name).openStream()));
            String value = (((JsonObject) JsonParser.parseReader(reader)).get("id")).toString().replaceAll("\"", "");
            reader.close();
            return getOfflineUser(UUID.fromString(value.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5")));
        } catch (Exception e) {
            return null;
        }
    }```