RealmsCreateWorldFlow .java

com.mojang.realmsclient.client.worldupload.RealmsCreateWorldFlow (class, 139 lines): members: gameRulesOpt, createWorld, realmsServer, returnScreen, slot.

com.mojang.realmsclient.client.worlduploadclass139 linesclient5.4 KB

API Surface (1)

void createWorld(final Minecraft minecraft, final Screen returnScreen, final Screen lastScreen, final int slot, final RealmsServer realmsServer, final @Nullable RealmCreationTask realmCreationTask)

Fields & Constants (9)

LOGGER
minecraft
returnScreen
lastScreen
slot
realmsServer
registryAccess
worldDataAndGenSettings
gameRulesOpt

String literals (7)

"Failed to create temporary world folder"
"mco.create.world.failed"
"mco.create.world.reset.title"
"Failed to create realms world"
"minecraft_realms_world_upload"
"datapacks"
"level.dat"

Full Source

RealmsCreateWorldFlow.java139 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient.client.worldupload;
2
3
import com.mojang.logging.LogUtils;
4
import com.mojang.realmsclient.RealmsMainScreen;
5
import com.mojang.realmsclient.dto.RealmsServer;
6
import com.mojang.realmsclient.dto.RealmsSetting;
7
import com.mojang.realmsclient.dto.RealmsSlot;
8
import com.mojang.realmsclient.dto.RealmsWorldOptions;
9
import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen;
10
import com.mojang.realmsclient.gui.screens.configuration.RealmsConfigureWorldScreen;
11
import com.mojang.realmsclient.util.task.RealmCreationTask;
12
import java.io.IOException;
13
import java.nio.file.Files;
14
import java.nio.file.Path;
15
import java.util.List;
16
import java.util.Optional;
17
import java.util.concurrent.CompletionException;
18
import net.fabricmc.api.EnvType;
19
import net.fabricmc.api.Environment;
20
import net.minecraft.SharedConstants;
21
import net.minecraft.client.Minecraft;
22
import net.minecraft.client.gui.screens.AlertScreen;
23
import net.minecraft.client.gui.screens.Screen;
24
import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen;
25
import net.minecraft.core.RegistryAccess;
26
import net.minecraft.nbt.CompoundTag;
27
import net.minecraft.nbt.NbtIo;
28
import net.minecraft.network.chat.CommonComponents;
29
import net.minecraft.network.chat.Component;
30
import net.minecraft.world.level.gamerules.GameRules;
31
import net.minecraft.world.level.storage.LevelStorageSource;
32
import net.minecraft.world.level.storage.WorldData;
33
import net.minecraft.world.level.storage.LevelDataAndDimensions.WorldDataAndGenSettings;
34
import org.jspecify.annotations.Nullable;
35
import org.slf4j.Logger;
36
37
@Environment(EnvType.CLIENT)
38
public class RealmsCreateWorldFlow {
39
	private static final Logger LOGGER = LogUtils.getLogger();
40
41
	public static void createWorld(
42
		final Minecraft minecraft,
43
		final Screen returnScreen,
44
		final Screen lastScreen,
45
		final int slot,
46
		final RealmsServer realmsServer,
47
		final @Nullable RealmCreationTask realmCreationTask
48
	) {
49
		CreateWorldScreen.openFresh(
50
			minecraft,
51
			() -> minecraft.gui.setScreen(returnScreen),
52
			(createWorldScreen, finalLayers, worldDataAndGenSettings, gameRules, tempDataPackDir) -> {
53
				Path worldFolder;
54
				try {
55
					worldFolder = createTemporaryWorldFolder(finalLayers.compositeAccess(), worldDataAndGenSettings, gameRules, tempDataPackDir);
56
				} catch (IOException e) {
57
					LOGGER.warn("Failed to create temporary world folder", e);
58
					minecraft.gui.setScreen(new RealmsGenericErrorScreen(Component.translatable("mco.create.world.failed"), lastScreen));
59
					return true;
60
				}
61
62
				RealmsWorldOptions realmsWorldOptions = RealmsWorldOptions.createFromSettings(
63
					worldDataAndGenSettings.data().getLevelSettings(), SharedConstants.getCurrentVersion().name()
64
				);
65
				RealmsSlot realmsSlot = new RealmsSlot(slot, realmsWorldOptions, List.of(RealmsSetting.hardcoreSetting(worldDataAndGenSettings.data().isHardcore())));
66
				RealmsWorldUpload realmsWorldUpload = new RealmsWorldUpload(
67
					worldFolder, realmsSlot, minecraft.getUser(), realmsServer.id, RealmsWorldUploadStatusTracker.noOp()
68
				);
69
				minecraft.setScreenAndShow(
70
					new AlertScreen(realmsWorldUpload::cancel, Component.translatable("mco.create.world.reset.title"), Component.empty(), CommonComponents.GUI_CANCEL, false)
71
				);
72
				if (realmCreationTask != null) {
73
					realmCreationTask.run();
74
				}
75
76
				realmsWorldUpload.packAndUpload().handleAsync((result, exception) -> {
77
					if (exception != null) {
78
						if (exception instanceof CompletionException e) {
79
							exception = e.getCause();
80
						}
81
82
						if (exception instanceof RealmsUploadCanceledException) {
83
							minecraft.setScreenAndShow(lastScreen);
84
						} else {
85
							if (exception instanceof RealmsUploadFailedException realmsUploadFailedException) {
86
								LOGGER.warn("Failed to create realms world {}", realmsUploadFailedException.getStatusMessage());
87
							} else {
88
								LOGGER.warn("Failed to create realms world", exception);
89
							}
90
91
							minecraft.setScreenAndShow(new RealmsGenericErrorScreen(Component.translatable("mco.create.world.failed"), lastScreen));
92
						}
93
					} else {
94
						if (returnScreen instanceof RealmsConfigureWorldScreen configureWorldScreen) {
95
							configureWorldScreen.fetchServerData(realmsServer.id);
96
						}
97
98
						if (realmCreationTask != null) {
99
							RealmsMainScreen.play(realmsServer, returnScreen, true);
100
						} else {
101
							minecraft.setScreenAndShow(returnScreen);
102
						}
103
104
						RealmsMainScreen.refreshServerList();
105
					}
106
107
					return null;
108
				}, minecraft);
109
				return true;
110
			}
111
		);
112
	}
113
114
	private static Path createTemporaryWorldFolder(
115
		final RegistryAccess registryAccess,
116
		final WorldDataAndGenSettings worldDataAndGenSettings,
117
		final Optional<GameRules> gameRulesOpt,
118
		final @Nullable Path tempDataPackDir
119
	) throws IOException {
120
		Path worldFolder = Files.createTempDirectory("minecraft_realms_world_upload");
121
		if (tempDataPackDir != null) {
122
			Files.move(tempDataPackDir, worldFolder.resolve("datapacks"));
123
		}
124
125
		WorldData worldData = worldDataAndGenSettings.data();
126
		CompoundTag dataTag = worldData.createTag(null);
127
		CompoundTag root = new CompoundTag();
128
		root.put("Data", dataTag);
129
		Path levelDat = Files.createFile(worldFolder.resolve("level.dat"));
130
		NbtIo.writeCompressed(root, levelDat);
131
		LevelStorageSource.writeWorldGenSettings(registryAccess, worldFolder, worldDataAndGenSettings.genSettings());
132
		if (gameRulesOpt.isPresent()) {
133
			LevelStorageSource.writeGameRules(worldData, worldFolder, gameRulesOpt.get());
134
		}
135
136
		return worldFolder;
137
	}
138
}
139

Decompiled Minecraft 26.3 source of RealmsCreateWorldFlow (com/mojang/realmsclient/client/worldupload/RealmsCreateWorldFlow.java) with Mojang official mappings, stored verbatim on the site.