| 1 | package net.minecraft.client.gui.screens.worldselection;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.Blaze3D;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
| 6 | import java.io.IOException;
|
| 7 | import java.nio.file.Files;
|
| 8 | import java.nio.file.Path;
|
| 9 | import java.util.concurrent.CompletableFuture;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.ChatFormatting;
|
| 13 | import net.minecraft.client.Minecraft;
|
| 14 | import net.minecraft.client.gui.Font;
|
| 15 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 16 | import net.minecraft.client.gui.components.Button;
|
| 17 | import net.minecraft.client.gui.components.EditBox;
|
| 18 | import net.minecraft.client.gui.components.StringWidget;
|
| 19 | import net.minecraft.client.gui.components.toasts.SystemToast;
|
| 20 | import net.minecraft.client.gui.layouts.FrameLayout;
|
| 21 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 22 | import net.minecraft.client.gui.layouts.SpacerElement;
|
| 23 | import net.minecraft.client.gui.screens.BackupConfirmScreen;
|
| 24 | import net.minecraft.client.gui.screens.GenericWaitingScreen;
|
| 25 | import net.minecraft.client.gui.screens.Screen;
|
| 26 | import net.minecraft.client.input.KeyEvent;
|
| 27 | import net.minecraft.nbt.NbtException;
|
| 28 | import net.minecraft.nbt.ReportedNbtException;
|
| 29 | import net.minecraft.network.chat.CommonComponents;
|
| 30 | import net.minecraft.network.chat.Component;
|
| 31 | import net.minecraft.util.FileUtil;
|
| 32 | import net.minecraft.util.Mth;
|
| 33 | import net.minecraft.util.StringUtil;
|
| 34 | import net.minecraft.util.Util;
|
| 35 | import net.minecraft.world.level.storage.LevelResource;
|
| 36 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 37 | import net.minecraft.world.level.storage.LevelSummary;
|
| 38 | import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
|
| 39 | import org.apache.commons.io.FileUtils;
|
| 40 | import org.slf4j.Logger;
|
| 41 |
|
| 42 | @Environment(EnvType.CLIENT)
|
| 43 | public class EditWorldScreen extends Screen {
|
| 44 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 45 | private static final Component NAME_LABEL = Component.translatable("selectWorld.enterName").withStyle(ChatFormatting.GRAY);
|
| 46 | private static final Component RESET_ICON_BUTTON = Component.translatable("selectWorld.edit.resetIcon");
|
| 47 | private static final Component FOLDER_BUTTON = Component.translatable("selectWorld.edit.openFolder");
|
| 48 | private static final Component BACKUP_BUTTON = Component.translatable("selectWorld.edit.backup");
|
| 49 | private static final Component BACKUP_FOLDER_BUTTON = Component.translatable("selectWorld.edit.backupFolder");
|
| 50 | private static final Component OPTIMIZE_BUTTON = Component.translatable("selectWorld.edit.optimize");
|
| 51 | private static final Component OPTIMIZE_TITLE = Component.translatable("optimizeWorld.confirm.title");
|
| 52 | private static final Component OPTIMIZE_DESCRIPTION = Component.translatable("optimizeWorld.confirm.description");
|
| 53 | private static final Component OPTIMIZE_CONFIRMATION = Component.translatable("optimizeWorld.confirm.proceed");
|
| 54 | private static final Component SAVE_BUTTON = Component.translatable("selectWorld.edit.save");
|
| 55 | private static final int DEFAULT_WIDTH = 200;
|
| 56 | private static final int VERTICAL_SPACING = 4;
|
| 57 | private static final int HALF_WIDTH = 98;
|
| 58 | private final LinearLayout layout = LinearLayout.vertical().spacing(5);
|
| 59 | private final BooleanConsumer callback;
|
| 60 | private final LevelStorageAccess levelAccess;
|
| 61 | private final EditBox nameEdit;
|
| 62 |
|
| 63 | public static EditWorldScreen create(final Minecraft minecraft, final LevelStorageAccess levelAccess, final BooleanConsumer callback) throws IOException {
|
| 64 | LevelSummary summary = levelAccess.fixAndGetSummary();
|
| 65 | return new EditWorldScreen(minecraft, levelAccess, summary.getLevelName(), callback);
|
| 66 | }
|
| 67 |
|
| 68 | private EditWorldScreen(final Minecraft minecraft, final LevelStorageAccess levelAccess, final String name, final BooleanConsumer callback) {
|
| 69 | super(Component.translatable("selectWorld.edit.title"));
|
| 70 | this.callback = callback;
|
| 71 | this.levelAccess = levelAccess;
|
| 72 | Font font = minecraft.font;
|
| 73 | this.layout.addChild(new SpacerElement(200, 20));
|
| 74 | this.layout.addChild(new StringWidget(NAME_LABEL, font));
|
| 75 | this.nameEdit = this.layout.addChild(new EditBox(font, 200, 20, NAME_LABEL));
|
| 76 | this.nameEdit.setValue(name);
|
| 77 | LinearLayout bottomButtonRow = LinearLayout.horizontal().spacing(4);
|
| 78 | Button renameButton = bottomButtonRow.addChild(Button.builder(SAVE_BUTTON, button -> this.onRename(this.nameEdit.getValue())).width(98).build());
|
| 79 | bottomButtonRow.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).width(98).build());
|
| 80 | this.nameEdit.setResponder(newName -> renameButton.active = !StringUtil.isBlank(newName));
|
| 81 | this.layout.addChild(Button.builder(RESET_ICON_BUTTON, button -> {
|
| 82 | levelAccess.getIconFile().ifPresent(p -> FileUtils.deleteQuietly(p.toFile()));
|
| 83 | button.active = false;
|
| 84 | }).width(200).build()).active = levelAccess.getIconFile().filter(x$0 -> Files.isRegularFile(x$0)).isPresent();
|
| 85 | this.layout.addChild(Button.builder(FOLDER_BUTTON, button -> Blaze3D.openPath(levelAccess.getLevelPath(LevelResource.ROOT))).width(200).build());
|
| 86 | this.layout
|
| 87 | .addChild(
|
| 88 | Button.builder(BACKUP_BUTTON, button -> makeBackupAndShowToast(levelAccess).thenAcceptAsync(success -> this.callback.accept(!success), minecraft))
|
| 89 | .width(200)
|
| 90 | .build()
|
| 91 | );
|
| 92 | this.layout.addChild(Button.builder(BACKUP_FOLDER_BUTTON, button -> {
|
| 93 | LevelStorageSource levelSource = minecraft.getLevelSource();
|
| 94 | Path path = levelSource.getBackupPath();
|
| 95 |
|
| 96 | try {
|
| 97 | FileUtil.createDirectoriesSafe(path);
|
| 98 | } catch (IOException e) {
|
| 99 | throw new RuntimeException(e);
|
| 100 | }
|
| 101 |
|
| 102 | Blaze3D.openPath(path);
|
| 103 | }).width(200).build());
|
| 104 | this.layout
|
| 105 | .addChild(
|
| 106 | Button.builder(
|
| 107 | OPTIMIZE_BUTTON,
|
| 108 | button -> minecraft.gui
|
| 109 | .setScreen(
|
| 110 | new BackupConfirmScreen(
|
| 111 | () -> minecraft.gui.setScreen(this),
|
| 112 | (backup, eraseCache) -> conditionallyMakeBackupAndShowToast(backup, levelAccess)
|
| 113 | .thenAcceptAsync(
|
| 114 | var4x -> minecraft.gui.setScreen(OptimizeWorldScreen.create(minecraft, this.callback, minecraft.getFixerUpper(), levelAccess, eraseCache)),
|
| 115 | minecraft
|
| 116 | ),
|
| 117 | OPTIMIZE_TITLE,
|
| 118 | OPTIMIZE_DESCRIPTION,
|
| 119 | OPTIMIZE_CONFIRMATION,
|
| 120 | true
|
| 121 | )
|
| 122 | )
|
| 123 | )
|
| 124 | .width(200)
|
| 125 | .build()
|
| 126 | );
|
| 127 | this.layout.addChild(new SpacerElement(200, 20));
|
| 128 | this.layout.addChild(bottomButtonRow);
|
| 129 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | protected void setInitialFocus() {
|
| 134 | this.setInitialFocus(this.nameEdit);
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | protected void init() {
|
| 139 | this.repositionElements();
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | protected void repositionElements() {
|
| 144 | this.layout.arrangeElements();
|
| 145 | FrameLayout.centerInRectangle(this.layout, this.getRectangle());
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public boolean keyPressed(final KeyEvent event) {
|
| 150 | if (this.nameEdit.isFocused() && event.isConfirmation()) {
|
| 151 | this.onRename(this.nameEdit.getValue());
|
| 152 | this.onClose();
|
| 153 | return true;
|
| 154 | } else {
|
| 155 | return super.keyPressed(event);
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | public void onClose() {
|
| 161 | this.callback.accept(false);
|
| 162 | }
|
| 163 |
|
| 164 | private void onRename(final String newName) {
|
| 165 | try {
|
| 166 | this.levelAccess.renameLevel(newName);
|
| 167 | } catch (IOException | NbtException | ReportedNbtException e) {
|
| 168 | LOGGER.error("Failed to access world '{}'", this.levelAccess.getLevelId(), e);
|
| 169 | SystemToast.onWorldAccessFailure(this.minecraft, this.levelAccess.getLevelId());
|
| 170 | }
|
| 171 |
|
| 172 | this.callback.accept(true);
|
| 173 | }
|
| 174 |
|
| 175 | public static CompletableFuture<Boolean> conditionallyMakeBackupAndShowToast(final boolean createBackup, final LevelStorageAccess access) {
|
| 176 | return createBackup ? makeBackupAndShowToast(access) : CompletableFuture.completedFuture(false);
|
| 177 | }
|
| 178 |
|
| 179 | public static CompletableFuture<Boolean> makeBackupAndShowToast(final LevelStorageAccess access) {
|
| 180 | Minecraft minecraft = Minecraft.getInstance();
|
| 181 | minecraft.setScreenAndShow(
|
| 182 | GenericWaitingScreen.createWaitingWithoutButton(
|
| 183 | Component.translatable("selectWorld.waitingForBackup.title"), Component.translatable("selectWorld.waitingForBackup.message").withStyle(ChatFormatting.GRAY)
|
| 184 | )
|
| 185 | );
|
| 186 | return CompletableFuture.<Long>supplyAsync(() -> {
|
| 187 | try {
|
| 188 | return access.makeWorldBackup();
|
| 189 | } catch (IOException e) {
|
| 190 | throw new RuntimeException(e);
|
| 191 | }
|
| 192 | }, Util.backgroundExecutor()).thenApplyAsync(size -> {
|
| 193 | Component title = Component.translatable("selectWorld.edit.backupCreated", access.getLevelId());
|
| 194 | Component message = Component.translatable("selectWorld.edit.backupSize", Mth.ceil(size.longValue() / 1048576.0));
|
| 195 | minecraft.gui.toastManager().addToast(new SystemToast(SystemToast.SystemToastId.WORLD_BACKUP, title, message));
|
| 196 | return true;
|
| 197 | }, minecraft).exceptionallyAsync(exception -> {
|
| 198 | Component title = Component.translatable("selectWorld.edit.backupFailed");
|
| 199 | Component message = Component.literal(exception.getMessage());
|
| 200 | minecraft.gui.toastManager().addToast(new SystemToast(SystemToast.SystemToastId.WORLD_BACKUP, title, message));
|
| 201 | return false;
|
| 202 | }, minecraft);
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 207 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 208 | graphics.centeredText(this.font, this.title, this.width / 2, 15, -1);
|
| 209 | }
|
| 210 | }
|
| 211 |
|