| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 4 | import com.mojang.realmsclient.client.RealmsClient;
|
| 5 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 6 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 7 | import com.mojang.realmsclient.util.task.RealmCreationTask;
|
| 8 | import java.util.concurrent.CompletableFuture;
|
| 9 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.gui.components.Button;
|
| 13 | import net.minecraft.client.gui.components.EditBox;
|
| 14 | import net.minecraft.client.gui.layouts.CommonLayouts;
|
| 15 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 16 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 17 | import net.minecraft.client.gui.screens.AlertScreen;
|
| 18 | import net.minecraft.network.chat.CommonComponents;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.realms.RealmsScreen;
|
| 21 | import net.minecraft.util.StringUtil;
|
| 22 | import net.minecraft.util.Util;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class RealmsCreateRealmScreen extends RealmsScreen {
|
| 26 | private static final Component CREATE_REALM_TEXT = Component.translatable("mco.selectServer.create");
|
| 27 | private static final Component NAME_LABEL = Component.translatable("mco.configure.world.name");
|
| 28 | private static final Component DESCRIPTION_LABEL = Component.translatable("mco.configure.world.description");
|
| 29 | private static final int BUTTON_SPACING = 10;
|
| 30 | private static final int CONTENT_WIDTH = 210;
|
| 31 | private final RealmsMainScreen lastScreen;
|
| 32 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 33 | private EditBox nameBox;
|
| 34 | private EditBox descriptionBox;
|
| 35 | private final Runnable createWorldRunnable;
|
| 36 |
|
| 37 | public RealmsCreateRealmScreen(final RealmsMainScreen lastScreen, final RealmsServer server, final boolean isSnapshot) {
|
| 38 | super(CREATE_REALM_TEXT);
|
| 39 | this.lastScreen = lastScreen;
|
| 40 | this.createWorldRunnable = () -> this.createWorld(server, isSnapshot);
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void init() {
|
| 45 | this.layout.addTitleHeader(this.title, this.font);
|
| 46 | LinearLayout content = this.layout.addToContents(LinearLayout.vertical()).spacing(10);
|
| 47 | Button createButton = Button.builder(CommonComponents.GUI_CONTINUE, button -> this.createWorldRunnable.run()).build();
|
| 48 | createButton.active = false;
|
| 49 | this.nameBox = new EditBox(this.font, 210, 20, NAME_LABEL);
|
| 50 | this.nameBox.setResponder(value -> createButton.active = !StringUtil.isBlank(value));
|
| 51 | this.descriptionBox = new EditBox(this.font, 210, 20, DESCRIPTION_LABEL);
|
| 52 | content.addChild(CommonLayouts.labeledElement(this.font, this.nameBox, NAME_LABEL));
|
| 53 | content.addChild(CommonLayouts.labeledElement(this.font, this.descriptionBox, DESCRIPTION_LABEL));
|
| 54 | LinearLayout bottomButtons = this.layout.addToFooter(LinearLayout.horizontal().spacing(10));
|
| 55 | bottomButtons.addChild(createButton);
|
| 56 | bottomButtons.addChild(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).build());
|
| 57 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 58 | this.repositionElements();
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected void setInitialFocus() {
|
| 63 | this.setInitialFocus(this.nameBox);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected void repositionElements() {
|
| 68 | this.layout.arrangeElements();
|
| 69 | }
|
| 70 |
|
| 71 | private void createWorld(final RealmsServer server, final boolean initializeSnapshotRealm) {
|
| 72 | if (!server.isSnapshotRealm() && initializeSnapshotRealm) {
|
| 73 | AtomicBoolean canceled = new AtomicBoolean();
|
| 74 | this.minecraft.gui.setScreen(new AlertScreen(() -> {
|
| 75 | canceled.set(true);
|
| 76 | this.lastScreen.resetScreen();
|
| 77 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 78 | }, Component.translatable("mco.upload.preparing"), Component.empty()));
|
| 79 | CompletableFuture.<RealmsServer>supplyAsync(() -> createSnapshotRealm(server), Util.backgroundExecutor()).thenAcceptAsync(snapshotServer -> {
|
| 80 | if (!canceled.get()) {
|
| 81 | this.showResetWorldScreen(snapshotServer);
|
| 82 | }
|
| 83 | }, this.minecraft).exceptionallyAsync(ex -> {
|
| 84 | this.lastScreen.resetScreen();
|
| 85 | Component errorMessage;
|
| 86 | if (ex.getCause() instanceof RealmsServiceException realmsServiceException) {
|
| 87 | errorMessage = realmsServiceException.realmsError.errorMessage();
|
| 88 | } else {
|
| 89 | errorMessage = Component.translatable("mco.errorMessage.initialize.failed");
|
| 90 | }
|
| 91 |
|
| 92 | this.minecraft.gui.setScreen(new RealmsGenericErrorScreen(errorMessage, this.lastScreen));
|
| 93 | return null;
|
| 94 | }, this.minecraft);
|
| 95 | } else {
|
| 96 | this.showResetWorldScreen(server);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | private static RealmsServer createSnapshotRealm(final RealmsServer server) {
|
| 101 | RealmsClient client = RealmsClient.getOrCreate();
|
| 102 |
|
| 103 | try {
|
| 104 | return client.createSnapshotRealm(server.id);
|
| 105 | } catch (RealmsServiceException e) {
|
| 106 | throw new RuntimeException(e);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | private void showResetWorldScreen(final RealmsServer server) {
|
| 111 | RealmCreationTask realmCreationTask = new RealmCreationTask(server.id, this.nameBox.getValue().trim(), this.descriptionBox.getValue().trim());
|
| 112 | RealmsResetWorldScreen resetWorldScreen = RealmsResetWorldScreen.forNewRealm(this, server, realmCreationTask, () -> this.minecraft.execute(() -> {
|
| 113 | RealmsMainScreen.refreshServerList();
|
| 114 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 115 | }));
|
| 116 | this.minecraft.gui.setScreen(resetWorldScreen);
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public void onClose() {
|
| 121 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 122 | }
|
| 123 | }
|
| 124 |
|