| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.exception.RealmsDefaultUncaughtExceptionHandler;
|
| 5 | import com.mojang.realmsclient.util.task.LongRunningTask;
|
| 6 | import java.time.Duration;
|
| 7 | import java.util.List;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.GameNarrator;
|
| 11 | import net.minecraft.client.gui.components.Button;
|
| 12 | import net.minecraft.client.gui.components.LoadingDotsWidget;
|
| 13 | import net.minecraft.client.gui.layouts.FrameLayout;
|
| 14 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 15 | import net.minecraft.client.gui.screens.Screen;
|
| 16 | import net.minecraft.client.input.KeyEvent;
|
| 17 | import net.minecraft.network.chat.CommonComponents;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.realms.RealmsScreen;
|
| 20 | import net.minecraft.realms.RepeatedNarrator;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 | import org.slf4j.Logger;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class RealmsLongRunningMcoTaskScreen extends RealmsScreen {
|
| 26 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 27 | private static final RepeatedNarrator REPEATED_NARRATOR = new RepeatedNarrator(Duration.ofSeconds(5L));
|
| 28 | private final List<LongRunningTask> queuedTasks;
|
| 29 | private final Screen lastScreen;
|
| 30 | protected final LinearLayout layout = LinearLayout.vertical();
|
| 31 | private volatile Component title;
|
| 32 | private @Nullable LoadingDotsWidget loadingDotsWidget;
|
| 33 |
|
| 34 | public RealmsLongRunningMcoTaskScreen(final Screen lastScreen, final LongRunningTask... tasks) {
|
| 35 | super(GameNarrator.NO_TITLE);
|
| 36 | this.lastScreen = lastScreen;
|
| 37 | this.queuedTasks = List.of(tasks);
|
| 38 | if (this.queuedTasks.isEmpty()) {
|
| 39 | throw new IllegalArgumentException("No tasks added");
|
| 40 | }
|
| 41 |
|
| 42 | this.title = this.queuedTasks.get(0).getTitle();
|
| 43 | Runnable runnable = () -> {
|
| 44 | for (LongRunningTask task : tasks) {
|
| 45 | this.setTitle(task.getTitle());
|
| 46 | if (task.aborted()) {
|
| 47 | break;
|
| 48 | }
|
| 49 |
|
| 50 | task.run();
|
| 51 | if (task.aborted()) {
|
| 52 | return;
|
| 53 | }
|
| 54 | }
|
| 55 | };
|
| 56 | Thread thread = new Thread(runnable, "Realms-long-running-task");
|
| 57 | thread.setUncaughtExceptionHandler(new RealmsDefaultUncaughtExceptionHandler(LOGGER));
|
| 58 | thread.start();
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public boolean canInterruptWithAnotherScreen() {
|
| 63 | return false;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public void tick() {
|
| 68 | super.tick();
|
| 69 | if (this.loadingDotsWidget != null) {
|
| 70 | REPEATED_NARRATOR.narrate(this.minecraft.getNarrator(), this.loadingDotsWidget.getMessage());
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public boolean keyPressed(final KeyEvent event) {
|
| 76 | if (event.isEscape()) {
|
| 77 | this.cancel();
|
| 78 | return true;
|
| 79 | } else {
|
| 80 | return super.keyPressed(event);
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void init() {
|
| 86 | this.layout.defaultCellSetting().alignHorizontallyCenter();
|
| 87 | this.layout.addChild(realmsLogo());
|
| 88 | this.loadingDotsWidget = new LoadingDotsWidget(this.font, this.title);
|
| 89 | this.layout.addChild(this.loadingDotsWidget, layoutSettings -> layoutSettings.paddingTop(10).paddingBottom(30));
|
| 90 | this.layout.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> this.cancel()).build());
|
| 91 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 92 | this.repositionElements();
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected void repositionElements() {
|
| 97 | this.layout.arrangeElements();
|
| 98 | FrameLayout.centerInRectangle(this.layout, this.getRectangle());
|
| 99 | }
|
| 100 |
|
| 101 | protected void cancel() {
|
| 102 | for (LongRunningTask queuedTask : this.queuedTasks) {
|
| 103 | queuedTask.abortTask();
|
| 104 | }
|
| 105 |
|
| 106 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 107 | }
|
| 108 |
|
| 109 | public void setTitle(final Component title) {
|
| 110 | if (this.loadingDotsWidget != null) {
|
| 111 | this.loadingDotsWidget.setMessage(title);
|
| 112 | }
|
| 113 |
|
| 114 | this.title = title;
|
| 115 | }
|
| 116 | }
|
| 117 |
|