Jump to content

Recommended Posts

Posted (edited)

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
Posted

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

  Reveal hidden contents

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)

Posted

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:

  Reveal hidden contents

 

Posted
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.

Posted (edited)

“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:

  Reveal hidden contents

 

Posted
  On 2/21/2019 at 7:29 AM, 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.

Expand  

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.

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.