Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (โ‹ฎ) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions โ†’ Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.