Jump to content

Recommended Posts

Posted

Hello everyone, this may be more of a Java question but I couldn't find anything helpful on the internet.

I'm in the following scenario: when a player opens a chest (inventory gui), my mod will make a http get request (which might take a while if the server is slow), and then update the chest contents depending om the response. And, before the request finishes, there will be "Loading..." message displayed.

However, when putting all code in main thread, while the game waits for response, the fps drops to zero as expected. If I make the request in a separate thread, no blocking occurs but I've read in a lot of places that one should not call Minecraft functions from threads other than the main one. If I set a callback function for http request, it gets executed in another thread, too.

So I'm wondering how can I do the non-blocking request and await it's completion. I've looked into java.util.concurrent classes but every approach I tried resulted in either blocking or calling minecraft code from another thread.

Any help is appreciated, and apologies if this question is not well suited for this forum, I haven't found a better place to ask.

  • 2 weeks later...
Posted

Hey, i have figured it out. Here is an example class i made

public class SomeService {

    private final PlayerManager playerManager;

    public SomeService(PlayerManager playerManager) {
        this.playerManager = playerManager;
    }

    public void kickPlayer(UUID playerId) {
        CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(10_000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            return new Random().nextBoolean();
        }).thenAcceptAsync((kick) -> {
            // player might have left the server in the meantime, so playermanager allows us to get the player safely
            ServerPlayerEntity player = playerManager.getPlayer(playerId);

            if (player == null) {
                return;
            }

            if (kick) {
                player.networkHandler.disconnect(Text.of("Unlucky!"));
            } else {
                player.sendMessage(Text.of("lucky!"));
            }
        }, playerManager.getServer());
    }

}

It's fabric and server side, but i think it will work very similarly in forge.

 

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.