| 1 | package net.minecraft.client.gui.screens.worldselection;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.Dynamic;
|
| 7 | import com.mojang.serialization.Lifecycle;
|
| 8 | import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
| 9 | import java.io.IOException;
|
| 10 | import java.nio.file.FileAlreadyExistsException;
|
| 11 | import java.nio.file.Files;
|
| 12 | import java.nio.file.Path;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.UUID;
|
| 15 | import java.util.concurrent.CompletableFuture;
|
| 16 | import java.util.function.Function;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import net.minecraft.ChatFormatting;
|
| 20 | import net.minecraft.CrashReport;
|
| 21 | import net.minecraft.CrashReportCategory;
|
| 22 | import net.minecraft.ReportedException;
|
| 23 | import net.minecraft.SharedConstants;
|
| 24 | import net.minecraft.client.Minecraft;
|
| 25 | import net.minecraft.client.gui.components.toasts.SystemToast;
|
| 26 | import net.minecraft.client.gui.screens.AlertScreen;
|
| 27 | import net.minecraft.client.gui.screens.BackupConfirmScreen;
|
| 28 | import net.minecraft.client.gui.screens.ConfirmScreen;
|
| 29 | import net.minecraft.client.gui.screens.DatapackLoadFailureScreen;
|
| 30 | import net.minecraft.client.gui.screens.FileFixerAbortedScreen;
|
| 31 | import net.minecraft.client.gui.screens.GenericMessageScreen;
|
| 32 | import net.minecraft.client.gui.screens.NoticeWithLinkScreen;
|
| 33 | import net.minecraft.client.gui.screens.RecoverWorldDataScreen;
|
| 34 | import net.minecraft.client.gui.screens.Screen;
|
| 35 | import net.minecraft.client.resources.server.DownloadedPackSource;
|
| 36 | import net.minecraft.commands.Commands.CommandSelection;
|
| 37 | import net.minecraft.core.LayeredRegistryAccess;
|
| 38 | import net.minecraft.core.MappedRegistry;
|
| 39 | import net.minecraft.core.Registry;
|
| 40 | import net.minecraft.core.HolderLookup.Provider;
|
| 41 | import net.minecraft.core.registries.Registries;
|
| 42 | import net.minecraft.nbt.NbtException;
|
| 43 | import net.minecraft.nbt.NbtUtils;
|
| 44 | import net.minecraft.nbt.ReportedNbtException;
|
| 45 | import net.minecraft.network.chat.CommonComponents;
|
| 46 | import net.minecraft.network.chat.Component;
|
| 47 | import net.minecraft.network.chat.MutableComponent;
|
| 48 | import net.minecraft.server.RegistryLayer;
|
| 49 | import net.minecraft.server.ReloadableServerResources;
|
| 50 | import net.minecraft.server.WorldLoader;
|
| 51 | import net.minecraft.server.WorldStem;
|
| 52 | import net.minecraft.server.WorldLoader.DataLoadOutput;
|
| 53 | import net.minecraft.server.WorldLoader.InitConfig;
|
| 54 | import net.minecraft.server.WorldLoader.PackConfig;
|
| 55 | import net.minecraft.server.WorldLoader.ResultFactory;
|
| 56 | import net.minecraft.server.WorldLoader.WorldDataSupplier;
|
| 57 | import net.minecraft.server.packs.repository.PackRepository;
|
| 58 | import net.minecraft.server.packs.repository.ServerPacksSource;
|
| 59 | import net.minecraft.server.packs.resources.CloseableResourceManager;
|
| 60 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 61 | import net.minecraft.util.MemoryReserve;
|
| 62 | import net.minecraft.util.Util;
|
| 63 | import net.minecraft.util.datafix.DataFixTypes;
|
| 64 | import net.minecraft.util.datafix.DataFixers;
|
| 65 | import net.minecraft.util.filefix.AbortedFileFixException;
|
| 66 | import net.minecraft.util.filefix.CanceledFileFixException;
|
| 67 | import net.minecraft.util.filefix.FailedCleanupFileFixException;
|
| 68 | import net.minecraft.util.filefix.FileFixException;
|
| 69 | import net.minecraft.util.filefix.virtualfilesystem.exception.CowFSSymlinkException;
|
| 70 | import net.minecraft.util.worldupdate.UpgradeProgress;
|
| 71 | import net.minecraft.world.level.LevelSettings;
|
| 72 | import net.minecraft.world.level.WorldDataConfiguration;
|
| 73 | import net.minecraft.world.level.dimension.LevelStem;
|
| 74 | import net.minecraft.world.level.gamerules.GameRuleMap;
|
| 75 | import net.minecraft.world.level.gamerules.GameRules;
|
| 76 | import net.minecraft.world.level.levelgen.WorldDimensions;
|
| 77 | import net.minecraft.world.level.levelgen.WorldGenSettings;
|
| 78 | import net.minecraft.world.level.levelgen.WorldOptions;
|
| 79 | import net.minecraft.world.level.levelgen.WorldDimensions.Complete;
|
| 80 | import net.minecraft.world.level.storage.LevelDataAndDimensions;
|
| 81 | import net.minecraft.world.level.storage.LevelResource;
|
| 82 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 83 | import net.minecraft.world.level.storage.LevelSummary;
|
| 84 | import net.minecraft.world.level.storage.PrimaryLevelData;
|
| 85 | import net.minecraft.world.level.storage.WorldData;
|
| 86 | import net.minecraft.world.level.storage.LevelDataAndDimensions.WorldDataAndGenSettings;
|
| 87 | import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
|
| 88 | import net.minecraft.world.level.storage.LevelSummary.BackupStatus;
|
| 89 | import net.minecraft.world.level.validation.ContentValidationException;
|
| 90 | import org.jspecify.annotations.Nullable;
|
| 91 | import org.slf4j.Logger;
|
| 92 |
|
| 93 | @Environment(EnvType.CLIENT)
|
| 94 | public class WorldOpenFlows {
|
| 95 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 96 | private static final UUID WORLD_PACK_ID = UUID.fromString("640a6a92-b6cb-48a0-b391-831586500359");
|
| 97 | private final Minecraft minecraft;
|
| 98 | private final LevelStorageSource levelSource;
|
| 99 |
|
| 100 | public WorldOpenFlows(final Minecraft minecraft, final LevelStorageSource levelSource) {
|
| 101 | this.minecraft = minecraft;
|
| 102 | this.levelSource = levelSource;
|
| 103 | }
|
| 104 |
|
| 105 | public void createFreshLevel(
|
| 106 | final String levelId,
|
| 107 | final LevelSettings levelSettings,
|
| 108 | final WorldOptions options,
|
| 109 | final Function<Provider, WorldDimensions> dimensionsProvider,
|
| 110 | final Screen parentScreen
|
| 111 | ) {
|
| 112 | this.minecraft.setScreenAndShow(new GenericMessageScreen(Component.translatable("selectWorld.data_read")));
|
| 113 | LevelStorageAccess levelSourceAccess = this.createWorldAccess(levelId);
|
| 114 | if (levelSourceAccess != null) {
|
| 115 | PackRepository packRepository = ServerPacksSource.createPackRepository(levelSourceAccess);
|
| 116 | WorldDataConfiguration dataConfiguration = levelSettings.dataConfiguration();
|
| 117 |
|
| 118 | try {
|
| 119 | PackConfig packConfig = new PackConfig(packRepository, dataConfiguration, false, false);
|
| 120 | WorldStem worldStem = this.loadWorldDataBlocking(
|
| 121 | packConfig,
|
| 122 | context -> {
|
| 123 | WorldDimensions dimensions = dimensionsProvider.apply(context.datapackWorldRegistries());
|
| 124 | Complete completeDimensions = dimensions.bake(context.datapackDimensions().lookupOrThrow(Registries.LEVEL_STEM));
|
| 125 | return new DataLoadOutput<>(
|
| 126 | new WorldDataAndGenSettings(
|
| 127 | new PrimaryLevelData(levelSettings, completeDimensions.specialWorldProperty(), completeDimensions.lifecycle()),
|
| 128 | new WorldGenSettings(options, dimensions)
|
| 129 | ),
|
| 130 | completeDimensions.dimensionsRegistryAccess()
|
| 131 | );
|
| 132 | },
|
| 133 | WorldStem::new
|
| 134 | );
|
| 135 | this.minecraft.doWorldLoad(levelSourceAccess, packRepository, worldStem, Optional.empty(), true);
|
| 136 | } catch (Exception e) {
|
| 137 | LOGGER.warn("Failed to load datapacks, can't proceed with server load", e);
|
| 138 | levelSourceAccess.safeClose();
|
| 139 | this.minecraft.gui.setScreen(parentScreen);
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | private LevelStorageAccess/* @Nullable */ createWorldAccess(final String levelId) {
|
| 145 | try {
|
| 146 | return this.levelSource.validateAndCreateAccess(levelId);
|
| 147 | } catch (IOException e) {
|
| 148 | LOGGER.warn("Failed to read level {} data", levelId, e);
|
| 149 | SystemToast.onWorldAccessFailure(this.minecraft, levelId);
|
| 150 | this.minecraft.gui.setScreen(null);
|
| 151 | return null;
|
| 152 | } catch (ContentValidationException e) {
|
| 153 | LOGGER.warn("{}", e.getMessage());
|
| 154 | this.minecraft.gui.setScreen(NoticeWithLinkScreen.createWorldSymlinkWarningScreen(() -> this.minecraft.gui.setScreen(null)));
|
| 155 | return null;
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | public void createLevelFromExistingSettings(
|
| 160 | final LevelStorageAccess levelSourceAccess,
|
| 161 | final ReloadableServerResources serverResources,
|
| 162 | final LayeredRegistryAccess<RegistryLayer> registryAccess,
|
| 163 | final WorldDataAndGenSettings worldDataAndGenSettings,
|
| 164 | final Optional<GameRules> gameRules
|
| 165 | ) {
|
| 166 | PackRepository packRepository = ServerPacksSource.createPackRepository(levelSourceAccess);
|
| 167 | CloseableResourceManager resourceManager = new PackConfig(packRepository, worldDataAndGenSettings.data().getDataConfiguration(), false, false)
|
| 168 | .createResourceManager()
|
| 169 | .getSecond();
|
| 170 | this.minecraft
|
| 171 | .doWorldLoad(levelSourceAccess, packRepository, new WorldStem(resourceManager, serverResources, registryAccess, worldDataAndGenSettings), gameRules, true);
|
| 172 | }
|
| 173 |
|
| 174 | public WorldStem loadWorldStem(
|
| 175 | final LevelStorageAccess worldAccess, final Dynamic<?> levelDataTag, final boolean safeMode, final PackRepository packRepository
|
| 176 | ) throws Exception {
|
| 177 | PackConfig packConfig = LevelStorageSource.getPackConfig(levelDataTag, packRepository, safeMode);
|
| 178 | return this.loadWorldDataBlocking(
|
| 179 | packConfig,
|
| 180 | context -> {
|
| 181 | Registry<LevelStem> datapackDimensions = context.datapackDimensions().lookupOrThrow(Registries.LEVEL_STEM);
|
| 182 | LevelDataAndDimensions data = LevelStorageSource.getLevelDataAndDimensions(
|
| 183 | worldAccess, levelDataTag, context.dataConfiguration(), datapackDimensions, context.datapackWorldRegistries()
|
| 184 | );
|
| 185 | return new DataLoadOutput<>(data.worldDataAndGenSettings(), data.dimensions().dimensionsRegistryAccess());
|
| 186 | },
|
| 187 | WorldStem::new
|
| 188 | );
|
| 189 | }
|
| 190 |
|
| 191 | public Pair<LevelSettings, WorldCreationContext> recreateWorldData(final LevelStorageAccess levelSourceAccess) throws Exception {
|
| 192 | PackRepository packRepository = ServerPacksSource.createPackRepository(levelSourceAccess);
|
| 193 | Dynamic<?> unfixedDataTag = levelSourceAccess.getUnfixedDataTag(false);
|
| 194 | int dataVersion = NbtUtils.getDataVersion(unfixedDataTag);
|
| 195 | if (DataFixers.getFileFixer().requiresFileFixing(dataVersion)) {
|
| 196 | throw new IllegalStateException("Can't recreate world before file fixing; shouldn't be able to get here");
|
| 197 | }
|
| 198 |
|
| 199 | Dynamic<?> levelDataTag = DataFixTypes.LEVEL.updateToCurrentVersion(DataFixers.getDataFixer(), unfixedDataTag, dataVersion);
|
| 200 | PackConfig packConfig = LevelStorageSource.getPackConfig(levelDataTag, packRepository, false);
|
| 201 |
|
| 202 | @Environment(EnvType.CLIENT)
|
| 203 | record Data(LevelSettings levelSettings, WorldOptions options, Registry<LevelStem> existingDimensions) {
|
| 204 | }
|
| 205 |
|
| 206 | return this.loadWorldDataBlocking(
|
| 207 | packConfig,
|
| 208 | context -> {
|
| 209 | Registry<LevelStem> noDatapackDimensions = new MappedRegistry<>(Registries.LEVEL_STEM, Lifecycle.stable()).freeze();
|
| 210 | LevelDataAndDimensions existingData = LevelStorageSource.getLevelDataAndDimensions(
|
| 211 | levelSourceAccess, levelDataTag, context.dataConfiguration(), noDatapackDimensions, context.datapackWorldRegistries()
|
| 212 | );
|
| 213 | return new DataLoadOutput<>(
|
| 214 | new Data(
|
| 215 | existingData.worldDataAndGenSettings().data().getLevelSettings(),
|
| 216 | existingData.worldDataAndGenSettings().genSettings().options(),
|
| 217 | existingData.dimensions().dimensions()
|
| 218 | ),
|
| 219 | context.datapackDimensions()
|
| 220 | );
|
| 221 | },
|
| 222 | (resources, managers, registries, loadedData) -> {
|
| 223 | resources.close();
|
| 224 | DataResult<GameRuleMap> existingGameRules = LevelStorageSource.readExistingSavedData(levelSourceAccess, registries.compositeAccess(), GameRuleMap.TYPE);
|
| 225 | existingGameRules.ifError(e -> LOGGER.error("Failed to parse existing game rules: {}", e.message()));
|
| 226 | InitialWorldCreationOptions initialWorldCreationOptions = new InitialWorldCreationOptions(
|
| 227 | WorldCreationUiState.SelectedGameMode.SURVIVAL, existingGameRules.result().orElse(GameRuleMap.of()), null
|
| 228 | );
|
| 229 | return Pair.of(
|
| 230 | loadedData.levelSettings,
|
| 231 | new WorldCreationContext(
|
| 232 | loadedData.options,
|
| 233 | new WorldDimensions(loadedData.existingDimensions),
|
| 234 | registries,
|
| 235 | managers,
|
| 236 | loadedData.levelSettings.dataConfiguration(),
|
| 237 | initialWorldCreationOptions
|
| 238 | )
|
| 239 | );
|
| 240 | }
|
| 241 | );
|
| 242 | }
|
| 243 |
|
| 244 | private <D, R> R loadWorldDataBlocking(final PackConfig packConfig, final WorldDataSupplier<D> worldDataGetter, final ResultFactory<D, R> worldDataSupplier) throws Exception {
|
| 245 | long start = Util.getMillis();
|
| 246 | InitConfig config = new InitConfig(packConfig, CommandSelection.INTEGRATED, LevelBasedPermissionSet.GAMEMASTER);
|
| 247 | CompletableFuture<R> resourceLoad = WorldLoader.load(config, worldDataGetter, worldDataSupplier, Util.backgroundExecutor(), this.minecraft);
|
| 248 | this.minecraft.managedBlock(resourceLoad::isDone);
|
| 249 | long end = Util.getMillis();
|
| 250 | LOGGER.debug("World resource load blocked for {} ms", end - start);
|
| 251 | return resourceLoad.get();
|
| 252 | }
|
| 253 |
|
| 254 | private void askForBackup(final LevelStorageAccess levelAccess, final boolean oldCustomized, final Runnable proceedCallback, final Runnable cancelCallback) {
|
| 255 | Component backupQuestion;
|
| 256 | Component backupWarning;
|
| 257 | if (oldCustomized) {
|
| 258 | backupQuestion = Component.translatable("selectWorld.backupQuestion.customized");
|
| 259 | backupWarning = Component.translatable("selectWorld.backupWarning.customized");
|
| 260 | } else {
|
| 261 | backupQuestion = Component.translatable("selectWorld.backupQuestion.experimental");
|
| 262 | backupWarning = Component.translatable("selectWorld.backupWarning.experimental");
|
| 263 | }
|
| 264 |
|
| 265 | this.minecraft
|
| 266 | .gui
|
| 267 | .setScreen(
|
| 268 | new BackupConfirmScreen(
|
| 269 | cancelCallback,
|
| 270 | (backup, eraseCache) -> EditWorldScreen.conditionallyMakeBackupAndShowToast(backup, levelAccess)
|
| 271 | .thenAcceptAsync(var1x -> proceedCallback.run(), this.minecraft),
|
| 272 | backupQuestion,
|
| 273 | backupWarning,
|
| 274 | false
|
| 275 | )
|
| 276 | );
|
| 277 | }
|
| 278 |
|
| 279 | public static void confirmWorldCreation(
|
| 280 | final Minecraft minecraft, final CreateWorldScreen parent, final Lifecycle lifecycle, final Runnable task, final boolean skipWarning
|
| 281 | ) {
|
| 282 | BooleanConsumer callback = confirmed -> {
|
| 283 | if (confirmed) {
|
| 284 | task.run();
|
| 285 | } else {
|
| 286 | minecraft.gui.setScreen(parent);
|
| 287 | }
|
| 288 | };
|
| 289 | if (skipWarning || lifecycle == Lifecycle.stable()) {
|
| 290 | task.run();
|
| 291 | } else if (lifecycle == Lifecycle.experimental()) {
|
| 292 | minecraft.gui
|
| 293 | .setScreen(
|
| 294 | new ConfirmScreen(
|
| 295 | callback, Component.translatable("selectWorld.warning.experimental.title"), Component.translatable("selectWorld.warning.experimental.question")
|
| 296 | )
|
| 297 | );
|
| 298 | } else {
|
| 299 | minecraft.gui
|
| 300 | .setScreen(
|
| 301 | new ConfirmScreen(
|
| 302 | callback, Component.translatable("selectWorld.warning.deprecated.title"), Component.translatable("selectWorld.warning.deprecated.question")
|
| 303 | )
|
| 304 | );
|
| 305 | }
|
| 306 | }
|
| 307 |
|
| 308 | public void openWorld(final String levelId, final Runnable onCancel) {
|
| 309 | this.minecraft.setScreenAndShow(new GenericMessageScreen(Component.translatable("selectWorld.data_read")));
|
| 310 | LevelStorageAccess worldAccess = this.createWorldAccess(levelId);
|
| 311 | if (worldAccess != null) {
|
| 312 | this.openWorldLoadLevelData(worldAccess, onCancel);
|
| 313 | }
|
| 314 | }
|
| 315 |
|
| 316 | private void openWorldLoadLevelData(final LevelStorageAccess worldAccess, final Runnable onCancel) {
|
| 317 | this.minecraft.setScreenAndShow(new GenericMessageScreen(Component.translatable("selectWorld.data_read")));
|
| 318 |
|
| 319 | Dynamic<?> levelDataTag;
|
| 320 | LevelSummary summary;
|
| 321 | try {
|
| 322 | levelDataTag = worldAccess.getUnfixedDataTag(false);
|
| 323 | summary = worldAccess.fixAndGetSummaryFromTag(levelDataTag);
|
| 324 | } catch (IOException | NbtException | ReportedNbtException e) {
|
| 325 | this.minecraft.gui.setScreen(new RecoverWorldDataScreen(this.minecraft, success -> {
|
| 326 | if (success) {
|
| 327 | this.openWorldLoadLevelData(worldAccess, onCancel);
|
| 328 | } else {
|
| 329 | worldAccess.safeClose();
|
| 330 | onCancel.run();
|
| 331 | }
|
| 332 | }, worldAccess));
|
| 333 | return;
|
| 334 | } catch (OutOfMemoryError e) {
|
| 335 | MemoryReserve.release();
|
| 336 | String detailedMessage = "Ran out of memory trying to read level data of world folder \"" + worldAccess.getLevelId() + "\"";
|
| 337 | LOGGER.error(LogUtils.FATAL_MARKER, "{}", detailedMessage);
|
| 338 | OutOfMemoryError detailedException = new OutOfMemoryError("Ran out of memory reading level data");
|
| 339 | detailedException.initCause(e);
|
| 340 | CrashReport crashReport = CrashReport.forThrowable(detailedException, detailedMessage);
|
| 341 | CrashReportCategory worldDetails = crashReport.addCategory("World details");
|
| 342 | worldDetails.setDetail("World folder", worldAccess.getLevelId());
|
| 343 | throw new ReportedException(crashReport);
|
| 344 | }
|
| 345 |
|
| 346 | this.openWorldCheckVersionCompatibility(worldAccess, summary, levelDataTag, onCancel);
|
| 347 | }
|
| 348 |
|
| 349 | private void openWorldCheckVersionCompatibility(
|
| 350 | final LevelStorageAccess worldAccess, final LevelSummary summary, final Dynamic<?> levelDataTag, final Runnable onCancel
|
| 351 | ) {
|
| 352 | if (!summary.isCompatible()) {
|
| 353 | worldAccess.safeClose();
|
| 354 | this.minecraft
|
| 355 | .gui
|
| 356 | .setScreen(
|
| 357 | new AlertScreen(
|
| 358 | onCancel,
|
| 359 | Component.translatable("selectWorld.incompatible.title").withColor(-65536),
|
| 360 | Component.translatable("selectWorld.incompatible.description", summary.getWorldVersionName())
|
| 361 | )
|
| 362 | );
|
| 363 | } else {
|
| 364 | BackupStatus backupStatus = summary.backupStatus();
|
| 365 | if (backupStatus.shouldBackup()) {
|
| 366 | String questionKey = "selectWorld.backupQuestion." + backupStatus.getTranslationKey();
|
| 367 | String warningKey = "selectWorld.backupWarning." + backupStatus.getTranslationKey();
|
| 368 | MutableComponent backupQuestion = Component.translatable(questionKey);
|
| 369 | if (backupStatus.isSevere()) {
|
| 370 | backupQuestion.withColor(-2142128);
|
| 371 | }
|
| 372 |
|
| 373 | Component backupWarning = Component.translatable(warningKey, summary.getWorldVersionName(), SharedConstants.getCurrentVersion().name());
|
| 374 | this.minecraft.gui.setScreen(new BackupConfirmScreen(() -> {
|
| 375 | worldAccess.safeClose();
|
| 376 | onCancel.run();
|
| 377 | }, (backup, eraseCache) -> this.createBackupAndOpenWorld(worldAccess, levelDataTag, onCancel, backup), backupQuestion, backupWarning, false));
|
| 378 | } else {
|
| 379 | this.upgradeAndOpenWorld(worldAccess, levelDataTag, onCancel);
|
| 380 | }
|
| 381 | }
|
| 382 | }
|
| 383 |
|
| 384 | private void createBackupAndOpenWorld(final LevelStorageAccess levelAccess, final Dynamic<?> levelDataTag, final Runnable onCancel, final boolean backup) {
|
| 385 | EditWorldScreen.conditionallyMakeBackupAndShowToast(backup, levelAccess)
|
| 386 | .thenAcceptAsync(var4 -> this.upgradeAndOpenWorld(levelAccess, levelDataTag, onCancel), this.minecraft);
|
| 387 | }
|
| 388 |
|
| 389 | private void upgradeAndOpenWorld(final LevelStorageAccess worldAccess, final Dynamic<?> levelDataTag, final Runnable onCancel) {
|
| 390 | Runnable cleanup = () -> {
|
| 391 | worldAccess.safeClose();
|
| 392 | onCancel.run();
|
| 393 | };
|
| 394 | int dataVersion = NbtUtils.getDataVersion(levelDataTag);
|
| 395 | boolean requiresFileFixing = DataFixers.getFileFixer().requiresFileFixing(dataVersion);
|
| 396 | UpgradeProgress upgradeProgress = new UpgradeProgress();
|
| 397 | if (requiresFileFixing) {
|
| 398 | FileFixerProgressScreen progressScreen = new FileFixerProgressScreen(upgradeProgress);
|
| 399 | this.minecraft.setScreenAndShow(progressScreen);
|
| 400 | }
|
| 401 |
|
| 402 | Util.backgroundExecutor().execute(() -> {
|
| 403 | Dynamic<?> levelDataTagFixed = this.tryFileFixAndReportErrors(worldAccess, levelDataTag, upgradeProgress, cleanup);
|
| 404 | if (levelDataTagFixed != null) {
|
| 405 | this.minecraft.execute(() -> {
|
| 406 | if (requiresFileFixing) {
|
| 407 | ConfirmScreen loadConfirmScreen = new ConfirmScreen(result -> {
|
| 408 | if (result) {
|
| 409 | this.openWorldLoadLevelStem(worldAccess, levelDataTagFixed, false, onCancel);
|
| 410 | } else {
|
| 411 | cleanup.run();
|
| 412 | }
|
| 413 | }, Component.translatable("upgradeWorld.done"), Component.translatable("upgradeWorld.joinNow"));
|
| 414 | this.minecraft.setScreenAndShow(loadConfirmScreen);
|
| 415 | } else {
|
| 416 | this.openWorldLoadLevelStem(worldAccess, levelDataTagFixed, false, onCancel);
|
| 417 | }
|
| 418 | });
|
| 419 | }
|
| 420 | });
|
| 421 | }
|
| 422 |
|
| 423 | private @Nullable Dynamic<?> tryFileFixAndReportErrors(
|
| 424 | final LevelStorageAccess worldAccess, final Dynamic<?> levelDataTag, final UpgradeProgress upgradeProgress, final Runnable cleanup
|
| 425 | ) {
|
| 426 | try {
|
| 427 | return DataFixers.getFileFixer().fix(worldAccess, levelDataTag, upgradeProgress);
|
| 428 | } catch (CanceledFileFixException e) {
|
| 429 | this.minecraft
|
| 430 | .execute(
|
| 431 | () -> this.minecraft
|
| 432 | .setScreenAndShow(
|
| 433 | new AlertScreen(
|
| 434 | cleanup, Component.translatable("upgradeWorld.canceled.title"), Component.translatable("upgradeWorld.canceled.message"), CommonComponents.GUI_OK, true
|
| 435 | )
|
| 436 | )
|
| 437 | );
|
| 438 | return null;
|
| 439 | } catch (AbortedFileFixException e) {
|
| 440 | this.minecraft
|
| 441 | .execute(
|
| 442 | () -> {
|
| 443 | LOGGER.error("File fixing was aborted", e);
|
| 444 | if (e.getCause() instanceof CowFSSymlinkException) {
|
| 445 | this.minecraft
|
| 446 | .setScreenAndShow(
|
| 447 | new AlertScreen(cleanup, Component.translatable("upgradeWorld.symlink.title"), Component.translatable("upgradeWorld.symlink.message"))
|
| 448 | );
|
| 449 | } else {
|
| 450 | Component message;
|
| 451 | if (e.getCause() instanceof FileAlreadyExistsException) {
|
| 452 | message = Component.translatable("upgradeWorld.aborted.file_already_exists");
|
| 453 | } else {
|
| 454 | message = Component.translatable("upgradeWorld.aborted.message");
|
| 455 | }
|
| 456 |
|
| 457 | this.minecraft.setScreenAndShow(new FileFixerAbortedScreen(cleanup, message));
|
| 458 | }
|
| 459 | }
|
| 460 | );
|
| 461 | return null;
|
| 462 | } catch (FailedCleanupFileFixException e) {
|
| 463 | this.minecraft
|
| 464 | .execute(
|
| 465 | () -> this.minecraft
|
| 466 | .setScreenAndShow(
|
| 467 | new AlertScreen(
|
| 468 | cleanup,
|
| 469 | Component.translatable("upgradeWorld.failed_cleanup.title"),
|
| 470 | Component.translatable("upgradeWorld.failed_cleanup.message", Component.literal(e.newWorldFolderName()).withColor(-8355712))
|
| 471 | )
|
| 472 | )
|
| 473 | );
|
| 474 | return null;
|
| 475 | } catch (FileFixException e) {
|
| 476 | this.minecraft.delayCrash(e.makeReportedException().getReport());
|
| 477 | return null;
|
| 478 | } catch (Exception e) {
|
| 479 | LOGGER.error("Failed to upgrade the file structure of the world.", e);
|
| 480 | CrashReport report = CrashReport.forThrowable(e, "Failed to update file structure");
|
| 481 | this.minecraft.delayCrash(report);
|
| 482 | return null;
|
| 483 | }
|
| 484 | }
|
| 485 |
|
| 486 | private void openWorldLoadLevelStem(final LevelStorageAccess worldAccess, final Dynamic<?> levelDataTag, final boolean safeMode, final Runnable onCancel) {
|
| 487 | this.minecraft.setScreenAndShow(new GenericMessageScreen(Component.translatable("selectWorld.resource_load")));
|
| 488 | PackRepository packRepository = ServerPacksSource.createPackRepository(worldAccess);
|
| 489 |
|
| 490 | WorldStem worldStem;
|
| 491 | try {
|
| 492 | worldStem = this.loadWorldStem(worldAccess, levelDataTag, safeMode, packRepository);
|
| 493 |
|
| 494 | for (LevelStem levelStem : worldStem.registries().compositeAccess().lookupOrThrow(Registries.LEVEL_STEM)) {
|
| 495 | levelStem.generator().validate();
|
| 496 | }
|
| 497 | } catch (Exception e) {
|
| 498 | LOGGER.warn("Failed to load level data or datapacks, can't proceed with server load", e);
|
| 499 | if (!safeMode) {
|
| 500 | this.minecraft.gui.setScreen(new DatapackLoadFailureScreen(() -> {
|
| 501 | worldAccess.safeClose();
|
| 502 | onCancel.run();
|
| 503 | }, () -> this.openWorldLoadLevelStem(worldAccess, levelDataTag, true, onCancel)));
|
| 504 | } else {
|
| 505 | worldAccess.safeClose();
|
| 506 | this.minecraft
|
| 507 | .gui
|
| 508 | .setScreen(
|
| 509 | new AlertScreen(
|
| 510 | onCancel,
|
| 511 | Component.translatable("datapackFailure.safeMode.failed.title"),
|
| 512 | Component.translatable("datapackFailure.safeMode.failed.description"),
|
| 513 | CommonComponents.GUI_BACK,
|
| 514 | true
|
| 515 | )
|
| 516 | );
|
| 517 | }
|
| 518 |
|
| 519 | return;
|
| 520 | }
|
| 521 |
|
| 522 | this.openWorldCheckWorldStemCompatibility(worldAccess, worldStem, packRepository, onCancel);
|
| 523 | }
|
| 524 |
|
| 525 | private void openWorldCheckWorldStemCompatibility(
|
| 526 | final LevelStorageAccess worldAccess, final WorldStem worldStem, final PackRepository packRepository, final Runnable onCancel
|
| 527 | ) {
|
| 528 | WorldDataAndGenSettings worldDataAndGenSettings = worldStem.worldDataAndGenSettings();
|
| 529 | WorldData data = worldDataAndGenSettings.data();
|
| 530 | boolean oldCustomized = worldDataAndGenSettings.genSettings().options().isOldCustomizedWorld();
|
| 531 | boolean unstable = data.worldGenSettingsLifecycle() != Lifecycle.stable();
|
| 532 | if (!oldCustomized && !unstable) {
|
| 533 | this.openWorldLoadBundledResourcePack(worldAccess, worldStem, packRepository, onCancel);
|
| 534 | } else {
|
| 535 | this.askForBackup(worldAccess, oldCustomized, () -> this.openWorldLoadBundledResourcePack(worldAccess, worldStem, packRepository, onCancel), () -> {
|
| 536 | worldStem.close();
|
| 537 | worldAccess.safeClose();
|
| 538 | onCancel.run();
|
| 539 | });
|
| 540 | }
|
| 541 | }
|
| 542 |
|
| 543 | private void openWorldLoadBundledResourcePack(
|
| 544 | final LevelStorageAccess worldAccess, final WorldStem worldStem, final PackRepository packRepository, final Runnable onCancel
|
| 545 | ) {
|
| 546 | DownloadedPackSource packSource = this.minecraft.getDownloadedPackSource();
|
| 547 | this.loadBundledResourcePack(packSource, worldAccess).thenApply(unused -> true).exceptionallyComposeAsync(t -> {
|
| 548 | LOGGER.warn("Failed to load pack: ", t);
|
| 549 | return this.promptBundledPackLoadFailure();
|
| 550 | }, this.minecraft).thenAcceptAsync(result -> {
|
| 551 | if (result) {
|
| 552 | this.openWorldCheckDiskSpace(worldAccess, worldStem, packSource, packRepository, onCancel);
|
| 553 | } else {
|
| 554 | packSource.popAll();
|
| 555 | worldStem.close();
|
| 556 | worldAccess.safeClose();
|
| 557 | onCancel.run();
|
| 558 | }
|
| 559 | }, this.minecraft).exceptionally(e -> {
|
| 560 | this.minecraft.delayCrash(CrashReport.forThrowable(e, "Load world"));
|
| 561 | return null;
|
| 562 | });
|
| 563 | }
|
| 564 |
|
| 565 | private void openWorldCheckDiskSpace(
|
| 566 | final LevelStorageAccess worldAccess,
|
| 567 | final WorldStem worldStem,
|
| 568 | final DownloadedPackSource packSource,
|
| 569 | final PackRepository packRepository,
|
| 570 | final Runnable onCancel
|
| 571 | ) {
|
| 572 | if (worldAccess.checkForLowDiskSpace()) {
|
| 573 | Screen screen = new ConfirmScreen(
|
| 574 | skip -> {
|
| 575 | if (skip) {
|
| 576 | this.openWorldDoLoad(worldAccess, worldStem, packRepository);
|
| 577 | } else {
|
| 578 | packSource.popAll();
|
| 579 | worldStem.close();
|
| 580 | worldAccess.safeClose();
|
| 581 | onCancel.run();
|
| 582 | }
|
| 583 | },
|
| 584 | Component.translatable("selectWorld.warning.lowDiskSpace.title").withStyle(ChatFormatting.RED),
|
| 585 | Component.translatable("selectWorld.warning.lowDiskSpace.description"),
|
| 586 | CommonComponents.GUI_CONTINUE,
|
| 587 | CommonComponents.GUI_BACK
|
| 588 | );
|
| 589 | this.minecraft.gui.setScreen(screen);
|
| 590 | } else {
|
| 591 | this.openWorldDoLoad(worldAccess, worldStem, packRepository);
|
| 592 | }
|
| 593 | }
|
| 594 |
|
| 595 | private void openWorldDoLoad(final LevelStorageAccess worldAccess, final WorldStem worldStem, final PackRepository packRepository) {
|
| 596 | this.minecraft.doWorldLoad(worldAccess, packRepository, worldStem, Optional.empty(), false);
|
| 597 | }
|
| 598 |
|
| 599 | private CompletableFuture<Void> loadBundledResourcePack(final DownloadedPackSource packSource, final LevelStorageAccess levelSourceAccess) {
|
| 600 | Path mapResourceFile = levelSourceAccess.getLevelPath(LevelResource.MAP_RESOURCE_FILE);
|
| 601 | if (Files.exists(mapResourceFile) && !Files.isDirectory(mapResourceFile)) {
|
| 602 | packSource.configureForLocalWorld();
|
| 603 | CompletableFuture<Void> result = packSource.waitForPackFeedback(WORLD_PACK_ID);
|
| 604 | packSource.pushLocalPack(WORLD_PACK_ID, mapResourceFile);
|
| 605 | return result;
|
| 606 | } else {
|
| 607 | return CompletableFuture.completedFuture(null);
|
| 608 | }
|
| 609 | }
|
| 610 |
|
| 611 | private CompletableFuture<Boolean> promptBundledPackLoadFailure() {
|
| 612 | CompletableFuture<Boolean> result = new CompletableFuture<>();
|
| 613 | Screen screen = new ConfirmScreen(
|
| 614 | result::complete,
|
| 615 | Component.translatable("multiplayer.texturePrompt.failure.line1"),
|
| 616 | Component.translatable("multiplayer.texturePrompt.failure.line2"),
|
| 617 | CommonComponents.GUI_PROCEED,
|
| 618 | CommonComponents.GUI_CANCEL
|
| 619 | );
|
| 620 | this.minecraft.gui.setScreen(screen);
|
| 621 | return result;
|
| 622 | }
|
| 623 | }
|
| 624 |
|