Jump to content

[1.12] Better way to get UUID of a player?


WaningMatrix

Recommended Posts

So I wrote this method to find the UUID of a player..

public static String getUniqueID(String username) {
        String url = "https://api.mojang.com/users/profiles/minecraft/" + username;

        try {
            String json = IOUtils.toString(new URL(url));
            JsonElement element = new JsonParser().parse(json);
            JsonObject object = element.getAsJsonObject();

            return object.get("id").getAsString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

However whenever the method is run, the game freezes for like 1 or 2 seconds and sometimes even more. Is there any other way to remove this freeze thing from happening or another method to get the UUID of a player? I thought about the  getProfile.getId() but I don't exactly know how do I do that or whether it'll fix the problem or not. As of I know how to use getProfile.getId() it works only if the player in their game server. Let me know if I am wrong along with the corrected way. For Client side.

Thanks.

Edited by WaningMatrix
Link to comment
Share on other sites

If you want something to not pause the main thread when it runs, you need to put it on another thread. https://www.javaworld.com/article/2077138/java-concurrency/introduction-to-java-threads.html

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

3 hours ago, Cadiboo said:

If you want something to not pause the main thread when it runs, you need to put it on another thread. https://www.javaworld.com/article/2077138/java-concurrency/introduction-to-java-threads.html

I don't really get how or what do you mean. Can you like specify it with a example block of code or something which works for Forge?

Link to comment
Share on other sites

You are getting data from an URL, which takes time.

Since you are currently in the main game thread, the entire game is going to freeze until your code has finished running, or in this case, until the server respond with your data.

Creating another thread enables your "requesting from server" code to be ran parallel with the main game thread, which will not freeze the game.

 

In short, you need to create another thread. If you don't know how to do it, you need to learn multithreading in Java.

Cadiboo has provided you a link to a tutorial of multithreading in java; there are more on google or youtube.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

private static final ExecutorService SERVICE = Executors.newFixedThreadPool(50, Thread::new);

    public static void runAsync(Runnable task) {

        ThreadedCommmand.SERVICE.execute(task);
    }

Ok so I wrote this and now whenever I need the call the thing I do 

ThreadedCommmand.runAsync(() -> { ... });

and it works but I have a small question. All this will have effect on the Client side only right? Cause I don't want it to trigger some cheat response or something on the server side. Just want to make it sure. Thanks.

Link to comment
Share on other sites

“Have effect on client” is very vague.

It depends on how you are using it. Where are you using this?

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

51 minutes ago, WaningMatrix said:

All this will have effect on the Client side only right? Cause I don't want it to trigger some cheat response or something on the server side.

If you are running this code on the client only the server has no clue that you are even doing it since you are not interacting with it in any way.

If you are running it on a server I have no idea why accessing a random URL on the internet would trigger a "cheat response". Forge mods do it all the time with their update checkers, whether they are forge provided or custom.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.