File size: 6,171 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.mojang.realmsclient.gui.screens;

import com.mojang.realmsclient.RealmsMainScreen;
import com.mojang.realmsclient.client.RealmsClient;
import com.mojang.realmsclient.dto.RealmsServer;
import com.mojang.realmsclient.exception.RealmsServiceException;
import com.mojang.realmsclient.util.task.RealmCreationTask;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import net.minecraft.Util;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.layouts.CommonLayouts;
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
import net.minecraft.client.gui.layouts.LinearLayout;
import net.minecraft.client.gui.screens.AlertScreen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.realms.RealmsScreen;
import net.minecraft.util.StringUtil;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class RealmsCreateRealmScreen extends RealmsScreen {
    private static final Component CREATE_REALM_TEXT = Component.translatable("mco.selectServer.create");
    private static final Component NAME_LABEL = Component.translatable("mco.configure.world.name");
    private static final Component DESCRIPTION_LABEL = Component.translatable("mco.configure.world.description");
    private static final int BUTTON_SPACING = 10;
    private static final int CONTENT_WIDTH = 210;
    private final RealmsMainScreen lastScreen;
    private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
    private EditBox nameBox;
    private EditBox descriptionBox;
    private final Runnable createWorldRunnable;

    public RealmsCreateRealmScreen(RealmsMainScreen p_88575_, RealmsServer p_366584_, boolean p_369519_) {
        super(CREATE_REALM_TEXT);
        this.lastScreen = p_88575_;
        this.createWorldRunnable = () -> this.createWorld(p_366584_, p_369519_);
    }

    @Override
    public void init() {
        this.layout.addTitleHeader(this.title, this.font);
        LinearLayout linearlayout = this.layout.addToContents(LinearLayout.vertical()).spacing(10);
        Button button = Button.builder(CommonComponents.GUI_CONTINUE, p_308057_ -> this.createWorldRunnable.run()).build();
        button.active = false;
        this.nameBox = new EditBox(this.font, 210, 20, NAME_LABEL);
        this.nameBox.setResponder(p_325123_ -> button.active = !StringUtil.isBlank(p_325123_));
        this.descriptionBox = new EditBox(this.font, 210, 20, DESCRIPTION_LABEL);
        linearlayout.addChild(CommonLayouts.labeledElement(this.font, this.nameBox, NAME_LABEL));
        linearlayout.addChild(CommonLayouts.labeledElement(this.font, this.descriptionBox, DESCRIPTION_LABEL));
        LinearLayout linearlayout1 = this.layout.addToFooter(LinearLayout.horizontal().spacing(10));
        linearlayout1.addChild(button);
        linearlayout1.addChild(Button.builder(CommonComponents.GUI_BACK, p_296056_ -> this.onClose()).build());
        this.layout.visitWidgets(p_325125_ -> {
            AbstractWidget abstractwidget = this.addRenderableWidget(p_325125_);
        });
        this.repositionElements();
    }

    @Override
    protected void setInitialFocus() {
        this.setInitialFocus(this.nameBox);
    }

    @Override
    protected void repositionElements() {
        this.layout.arrangeElements();
    }

    private void createWorld(RealmsServer p_367400_, boolean p_365842_) {
        if (!p_367400_.isSnapshotRealm() && p_365842_) {
            AtomicBoolean atomicboolean = new AtomicBoolean();
            this.minecraft.setScreen(new AlertScreen(() -> {
                atomicboolean.set(true);
                this.lastScreen.resetScreen();
                this.minecraft.setScreen(this.lastScreen);
            }, Component.translatable("mco.upload.preparing"), Component.empty()));
            CompletableFuture.<RealmsServer>supplyAsync(() -> createSnapshotRealm(p_367400_), Util.backgroundExecutor()).thenAcceptAsync(p_357557_ -> {
                if (!atomicboolean.get()) {
                    this.showResetWorldScreen(p_357557_);
                }
            }, this.minecraft).exceptionallyAsync(p_357560_ -> {
                this.lastScreen.resetScreen();
                Component component;
                if (p_357560_.getCause() instanceof RealmsServiceException realmsserviceexception) {
                    component = realmsserviceexception.realmsError.errorMessage();
                } else {
                    component = Component.translatable("mco.errorMessage.initialize.failed");
                }

                this.minecraft.setScreen(new RealmsGenericErrorScreen(component, this.lastScreen));
                return null;
            }, this.minecraft);
        } else {
            this.showResetWorldScreen(p_367400_);
        }
    }

    private static RealmsServer createSnapshotRealm(RealmsServer p_362204_) {
        RealmsClient realmsclient = RealmsClient.create();

        try {
            return realmsclient.createSnapshotRealm(p_362204_.id);
        } catch (RealmsServiceException realmsserviceexception) {
            throw new RuntimeException(realmsserviceexception);
        }
    }

    private void showResetWorldScreen(RealmsServer p_310274_) {
        RealmCreationTask realmcreationtask = new RealmCreationTask(p_310274_.id, this.nameBox.getValue(), this.descriptionBox.getValue());
        RealmsResetWorldScreen realmsresetworldscreen = RealmsResetWorldScreen.forNewRealm(
            this, p_310274_, realmcreationtask, () -> this.minecraft.execute(() -> {
                    RealmsMainScreen.refreshServerList();
                    this.minecraft.setScreen(this.lastScreen);
                })
        );
        this.minecraft.setScreen(realmsresetworldscreen);
    }

    @Override
    public void onClose() {
        this.minecraft.setScreen(this.lastScreen);
    }
}