| 1 | package net.minecraft.client.gui.screens.worldselection;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.blaze3d.platform.NativeImage;
|
| 5 | import com.mojang.datafixers.util.Pair;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import java.io.IOException;
|
| 8 | import java.io.InputStream;
|
| 9 | import java.nio.file.Files;
|
| 10 | import java.nio.file.LinkOption;
|
| 11 | import java.nio.file.NoSuchFileException;
|
| 12 | import java.nio.file.Path;
|
| 13 | import java.nio.file.attribute.BasicFileAttributes;
|
| 14 | import java.time.Instant;
|
| 15 | import java.time.ZoneId;
|
| 16 | import java.time.ZonedDateTime;
|
| 17 | import java.time.format.DateTimeFormatter;
|
| 18 | import java.time.format.FormatStyle;
|
| 19 | import java.util.ArrayList;
|
| 20 | import java.util.Date;
|
| 21 | import java.util.List;
|
| 22 | import java.util.Locale;
|
| 23 | import java.util.Optional;
|
| 24 | import java.util.concurrent.CancellationException;
|
| 25 | import java.util.concurrent.CompletableFuture;
|
| 26 | import java.util.concurrent.CompletionException;
|
| 27 | import java.util.function.Consumer;
|
| 28 | import net.fabricmc.api.EnvType;
|
| 29 | import net.fabricmc.api.Environment;
|
| 30 | import net.minecraft.ChatFormatting;
|
| 31 | import net.minecraft.CrashReport;
|
| 32 | import net.minecraft.SharedConstants;
|
| 33 | import net.minecraft.client.Minecraft;
|
| 34 | import net.minecraft.client.gui.Font;
|
| 35 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 36 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 37 | import net.minecraft.client.gui.components.SelectableEntry;
|
| 38 | import net.minecraft.client.gui.components.StringWidget;
|
| 39 | import net.minecraft.client.gui.components.Tooltip;
|
| 40 | import net.minecraft.client.gui.components.toasts.SystemToast;
|
| 41 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 42 | import net.minecraft.client.gui.screens.AlertScreen;
|
| 43 | import net.minecraft.client.gui.screens.ConfirmScreen;
|
| 44 | import net.minecraft.client.gui.screens.ErrorScreen;
|
| 45 | import net.minecraft.client.gui.screens.FaviconTexture;
|
| 46 | import net.minecraft.client.gui.screens.GenericMessageScreen;
|
| 47 | import net.minecraft.client.gui.screens.LoadingDotsText;
|
| 48 | import net.minecraft.client.gui.screens.NoticeWithLinkScreen;
|
| 49 | import net.minecraft.client.gui.screens.ProgressScreen;
|
| 50 | import net.minecraft.client.gui.screens.Screen;
|
| 51 | import net.minecraft.client.input.KeyEvent;
|
| 52 | import net.minecraft.client.input.MouseButtonEvent;
|
| 53 | import net.minecraft.client.renderer.RenderPipelines;
|
| 54 | import net.minecraft.client.resources.sounds.SimpleSoundInstance;
|
| 55 | import net.minecraft.nbt.NbtException;
|
| 56 | import net.minecraft.nbt.ReportedNbtException;
|
| 57 | import net.minecraft.network.chat.CommonComponents;
|
| 58 | import net.minecraft.network.chat.Component;
|
| 59 | import net.minecraft.network.chat.ComponentUtils;
|
| 60 | import net.minecraft.network.chat.Style;
|
| 61 | import net.minecraft.resources.Identifier;
|
| 62 | import net.minecraft.sounds.SoundEvents;
|
| 63 | import net.minecraft.util.Util;
|
| 64 | import net.minecraft.world.level.LevelSettings;
|
| 65 | import net.minecraft.world.level.storage.LevelResource;
|
| 66 | import net.minecraft.world.level.storage.LevelStorageException;
|
| 67 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 68 | import net.minecraft.world.level.storage.LevelSummary;
|
| 69 | import net.minecraft.world.level.storage.LevelStorageSource.LevelCandidates;
|
| 70 | import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
|
| 71 | import net.minecraft.world.level.storage.LevelSummary.CorruptedLevelSummary;
|
| 72 | import net.minecraft.world.level.storage.LevelSummary.SymlinkLevelSummary;
|
| 73 | import net.minecraft.world.level.validation.ContentValidationException;
|
| 74 | import net.minecraft.world.level.validation.ForbiddenSymlinkInfo;
|
| 75 | import org.jspecify.annotations.Nullable;
|
| 76 | import org.slf4j.Logger;
|
| 77 |
|
| 78 | @Environment(EnvType.CLIENT)
|
| 79 | public class WorldSelectionList extends ObjectSelectionList<WorldSelectionList.Entry> {
|
| 80 | public static final DateTimeFormatter DATE_FORMAT = Util.localizedDateFormatter(FormatStyle.SHORT);
|
| 81 | private static final Identifier ERROR_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("world_list/error_highlighted");
|
| 82 | private static final Identifier ERROR_SPRITE = Identifier.withDefaultNamespace("world_list/error");
|
| 83 | private static final Identifier MARKED_JOIN_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("world_list/marked_join_highlighted");
|
| 84 | private static final Identifier MARKED_JOIN_SPRITE = Identifier.withDefaultNamespace("world_list/marked_join");
|
| 85 | private static final Identifier WARNING_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("world_list/warning_highlighted");
|
| 86 | private static final Identifier WARNING_SPRITE = Identifier.withDefaultNamespace("world_list/warning");
|
| 87 | private static final Identifier JOIN_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("world_list/join_highlighted");
|
| 88 | private static final Identifier JOIN_SPRITE = Identifier.withDefaultNamespace("world_list/join");
|
| 89 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 90 | private static final Component FROM_NEWER_TOOLTIP_1 = Component.translatable("selectWorld.tooltip.fromNewerVersion1").withStyle(ChatFormatting.RED);
|
| 91 | private static final Component FROM_NEWER_TOOLTIP_2 = Component.translatable("selectWorld.tooltip.fromNewerVersion2").withStyle(ChatFormatting.RED);
|
| 92 | private static final Component SNAPSHOT_TOOLTIP_1 = Component.translatable("selectWorld.tooltip.snapshot1").withStyle(ChatFormatting.GOLD);
|
| 93 | private static final Component SNAPSHOT_TOOLTIP_2 = Component.translatable("selectWorld.tooltip.snapshot2").withStyle(ChatFormatting.GOLD);
|
| 94 | private static final Component WORLD_LOCKED_TOOLTIP = Component.translatable("selectWorld.locked").withStyle(ChatFormatting.RED);
|
| 95 | private static final Component WORLD_REQUIRES_CONVERSION = Component.translatable("selectWorld.conversion.tooltip").withStyle(ChatFormatting.RED);
|
| 96 | private static final Component INCOMPATIBLE_VERSION_TOOLTIP = Component.translatable("selectWorld.incompatible.tooltip").withStyle(ChatFormatting.RED);
|
| 97 | private static final Component WORLD_EXPERIMENTAL = Component.translatable("selectWorld.experimental");
|
| 98 | private final Screen screen;
|
| 99 | private CompletableFuture<List<LevelSummary>> pendingLevels;
|
| 100 | private @Nullable List<LevelSummary> currentlyDisplayedLevels;
|
| 101 | private final WorldSelectionList.LoadingHeader loadingHeader;
|
| 102 | private final WorldSelectionList.EntryType entryType;
|
| 103 | private String filter;
|
| 104 | private boolean hasPolled;
|
| 105 | private final @Nullable Consumer<LevelSummary> onEntrySelect;
|
| 106 | private final @Nullable Consumer<WorldSelectionList.WorldListEntry> onEntryInteract;
|
| 107 |
|
| 108 | private WorldSelectionList(
|
| 109 | final Screen screen,
|
| 110 | final Minecraft minecraft,
|
| 111 | final int width,
|
| 112 | final int height,
|
| 113 | final String filter,
|
| 114 | final @Nullable WorldSelectionList oldList,
|
| 115 | final @Nullable Consumer<LevelSummary> onEntrySelect,
|
| 116 | final @Nullable Consumer<WorldSelectionList.WorldListEntry> onEntryInteract,
|
| 117 | final WorldSelectionList.EntryType entryType
|
| 118 | ) {
|
| 119 | super(minecraft, width, height, 0, 36);
|
| 120 | this.screen = screen;
|
| 121 | this.loadingHeader = new WorldSelectionList.LoadingHeader(minecraft);
|
| 122 | this.filter = filter;
|
| 123 | this.onEntrySelect = onEntrySelect;
|
| 124 | this.onEntryInteract = onEntryInteract;
|
| 125 | this.entryType = entryType;
|
| 126 | if (oldList != null) {
|
| 127 | this.pendingLevels = oldList.pendingLevels;
|
| 128 | } else {
|
| 129 | this.pendingLevels = this.loadLevels();
|
| 130 | }
|
| 131 |
|
| 132 | this.addEntry(this.loadingHeader);
|
| 133 | this.handleNewLevels(this.pollLevelsIgnoreErrors());
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | protected void clearEntries() {
|
| 138 | this.children().forEach(WorldSelectionList.Entry::close);
|
| 139 | super.clearEntries();
|
| 140 | }
|
| 141 |
|
| 142 | private @Nullable List<LevelSummary> pollLevelsIgnoreErrors() {
|
| 143 | try {
|
| 144 | List<LevelSummary> completedLevels = this.pendingLevels.getNow(null);
|
| 145 | if (this.entryType == WorldSelectionList.EntryType.UPLOAD_WORLD) {
|
| 146 | if (completedLevels == null || this.hasPolled) {
|
| 147 | return null;
|
| 148 | }
|
| 149 |
|
| 150 | this.hasPolled = true;
|
| 151 | completedLevels = completedLevels.stream().filter(LevelSummary::canUpload).toList();
|
| 152 | }
|
| 153 |
|
| 154 | return completedLevels;
|
| 155 | } catch (CompletionException | CancellationException ignore) {
|
| 156 | return null;
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | public void reloadWorldList() {
|
| 161 | this.pendingLevels = this.loadLevels();
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 166 | List<LevelSummary> newLevels = this.pollLevelsIgnoreErrors();
|
| 167 | if (newLevels != this.currentlyDisplayedLevels) {
|
| 168 | this.handleNewLevels(newLevels);
|
| 169 | }
|
| 170 |
|
| 171 | super.extractWidgetRenderState(graphics, mouseX, mouseY, a);
|
| 172 | }
|
| 173 |
|
| 174 | private void handleNewLevels(final @Nullable List<LevelSummary> levels) {
|
| 175 | if (levels != null) {
|
| 176 | if (levels.isEmpty()) {
|
| 177 | switch (this.entryType) {
|
| 178 | case SINGLEPLAYER:
|
| 179 | CreateWorldScreen.openFresh(this.minecraft, () -> this.minecraft.gui.setScreen(null));
|
| 180 | break;
|
| 181 | case UPLOAD_WORLD:
|
| 182 | this.clearEntries();
|
| 183 | this.addEntry(new WorldSelectionList.NoWorldsEntry(Component.translatable("mco.upload.select.world.none"), this.screen.getFont()));
|
| 184 | }
|
| 185 | } else {
|
| 186 | this.fillLevels(this.filter, levels);
|
| 187 | this.currentlyDisplayedLevels = levels;
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | public void updateFilter(final String newFilter) {
|
| 193 | if (this.currentlyDisplayedLevels != null && !newFilter.equals(this.filter)) {
|
| 194 | this.fillLevels(newFilter, this.currentlyDisplayedLevels);
|
| 195 | }
|
| 196 |
|
| 197 | this.filter = newFilter;
|
| 198 | }
|
| 199 |
|
| 200 | private CompletableFuture<List<LevelSummary>> loadLevels() {
|
| 201 | LevelCandidates levelCandidates;
|
| 202 | try {
|
| 203 | levelCandidates = this.minecraft.getLevelSource().findLevelCandidates();
|
| 204 | } catch (LevelStorageException e) {
|
| 205 | LOGGER.error("Couldn't load level list", e);
|
| 206 | this.handleLevelLoadFailure(e.getMessageComponent());
|
| 207 | return CompletableFuture.failedFuture(e);
|
| 208 | }
|
| 209 |
|
| 210 | return this.minecraft.getLevelSource().loadLevelSummaries(levelCandidates).exceptionally(throwable -> {
|
| 211 | this.minecraft.delayCrash(CrashReport.forThrowable(throwable, "Couldn't load level list"));
|
| 212 | return List.of();
|
| 213 | });
|
| 214 | }
|
| 215 |
|
| 216 | private void fillLevels(final String filter, final List<LevelSummary> levels) {
|
| 217 | List<WorldSelectionList.Entry> worldEntries = new ArrayList<>();
|
| 218 | Optional<WorldSelectionList.WorldListEntry> selectedOpt = this.getSelectedOpt();
|
| 219 | WorldSelectionList.WorldListEntry entryToSelect = null;
|
| 220 |
|
| 221 | for (LevelSummary level : levels.stream().filter(levelx -> this.filterAccepts(filter.toLowerCase(Locale.ROOT), levelx)).toList()) {
|
| 222 | WorldSelectionList.WorldListEntry worldListEntry = new WorldSelectionList.WorldListEntry(this, level);
|
| 223 | if (selectedOpt.isPresent() && selectedOpt.get().getLevelSummary().getLevelId().equals(worldListEntry.getLevelSummary().getLevelId())) {
|
| 224 | entryToSelect = worldListEntry;
|
| 225 | }
|
| 226 |
|
| 227 | worldEntries.add(worldListEntry);
|
| 228 | }
|
| 229 |
|
| 230 | this.removeEntries(this.children().stream().filter(entry -> !worldEntries.contains(entry)).toList());
|
| 231 | worldEntries.forEach(entry -> {
|
| 232 | if (!this.children().contains(entry)) {
|
| 233 | this.addEntry(entry);
|
| 234 | }
|
| 235 | });
|
| 236 | this.setSelected(entryToSelect);
|
| 237 | this.notifyListUpdated();
|
| 238 | }
|
| 239 |
|
| 240 | private boolean filterAccepts(final String filter, final LevelSummary level) {
|
| 241 | return level.getLevelName().toLowerCase(Locale.ROOT).contains(filter) || level.getLevelId().toLowerCase(Locale.ROOT).contains(filter);
|
| 242 | }
|
| 243 |
|
| 244 | private void notifyListUpdated() {
|
| 245 | this.refreshScrollAmount();
|
| 246 | this.screen.triggerImmediateNarration(true);
|
| 247 | }
|
| 248 |
|
| 249 | private void handleLevelLoadFailure(final Component message) {
|
| 250 | this.minecraft.gui.setScreen(new ErrorScreen(Component.translatable("selectWorld.unable_to_load"), message));
|
| 251 | }
|
| 252 |
|
| 253 | @Override
|
| 254 | public int getRowWidth() {
|
| 255 | return 270;
|
| 256 | }
|
| 257 |
|
| 258 | public void setSelected(final WorldSelectionList.@Nullable Entry selected) {
|
| 259 | super.setSelected(selected);
|
| 260 | if (this.onEntrySelect != null) {
|
| 261 | this.onEntrySelect.accept(selected instanceof WorldSelectionList.WorldListEntry entry ? entry.summary : null);
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | public Optional<WorldSelectionList.WorldListEntry> getSelectedOpt() {
|
| 266 | WorldSelectionList.Entry selected = this.getSelected();
|
| 267 | return selected instanceof WorldSelectionList.WorldListEntry worldEntry ? Optional.of(worldEntry) : Optional.empty();
|
| 268 | }
|
| 269 |
|
| 270 | public void returnToScreen() {
|
| 271 | this.reloadWorldList();
|
| 272 | this.minecraft.gui.setScreen(this.screen);
|
| 273 | }
|
| 274 |
|
| 275 | public Screen getScreen() {
|
| 276 | return this.screen;
|
| 277 | }
|
| 278 |
|
| 279 | @Override
|
| 280 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 281 | if (this.children().contains(this.loadingHeader)) {
|
| 282 | this.loadingHeader.updateNarration(output);
|
| 283 | } else {
|
| 284 | super.updateWidgetNarration(output);
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | @Environment(EnvType.CLIENT)
|
| 289 | public static class Builder {
|
| 290 | private final Minecraft minecraft;
|
| 291 | private final Screen screen;
|
| 292 | private int width;
|
| 293 | private int height;
|
| 294 | private String filter = "";
|
| 295 | private WorldSelectionList.EntryType type = WorldSelectionList.EntryType.SINGLEPLAYER;
|
| 296 | private @Nullable WorldSelectionList oldList = null;
|
| 297 | private @Nullable Consumer<LevelSummary> onEntrySelect = null;
|
| 298 | private @Nullable Consumer<WorldSelectionList.WorldListEntry> onEntryInteract = null;
|
| 299 |
|
| 300 | public Builder(final Minecraft minecraft, final Screen screen) {
|
| 301 | this.minecraft = minecraft;
|
| 302 | this.screen = screen;
|
| 303 | }
|
| 304 |
|
| 305 | public WorldSelectionList.Builder width(final int width) {
|
| 306 | this.width = width;
|
| 307 | return this;
|
| 308 | }
|
| 309 |
|
| 310 | public WorldSelectionList.Builder height(final int height) {
|
| 311 | this.height = height;
|
| 312 | return this;
|
| 313 | }
|
| 314 |
|
| 315 | public WorldSelectionList.Builder filter(final String filter) {
|
| 316 | this.filter = filter;
|
| 317 | return this;
|
| 318 | }
|
| 319 |
|
| 320 | public WorldSelectionList.Builder oldList(final @Nullable WorldSelectionList oldList) {
|
| 321 | this.oldList = oldList;
|
| 322 | return this;
|
| 323 | }
|
| 324 |
|
| 325 | public WorldSelectionList.Builder onEntrySelect(final Consumer<LevelSummary> onEntrySelect) {
|
| 326 | this.onEntrySelect = onEntrySelect;
|
| 327 | return this;
|
| 328 | }
|
| 329 |
|
| 330 | public WorldSelectionList.Builder onEntryInteract(final Consumer<WorldSelectionList.WorldListEntry> onEntryInteract) {
|
| 331 | this.onEntryInteract = onEntryInteract;
|
| 332 | return this;
|
| 333 | }
|
| 334 |
|
| 335 | public WorldSelectionList.Builder uploadWorld() {
|
| 336 | this.type = WorldSelectionList.EntryType.UPLOAD_WORLD;
|
| 337 | return this;
|
| 338 | }
|
| 339 |
|
| 340 | public WorldSelectionList build() {
|
| 341 | return new WorldSelectionList(
|
| 342 | this.screen, this.minecraft, this.width, this.height, this.filter, this.oldList, this.onEntrySelect, this.onEntryInteract, this.type
|
| 343 | );
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | @Environment(EnvType.CLIENT)
|
| 348 | public abstract static class Entry extends ObjectSelectionList.Entry<WorldSelectionList.Entry> implements AutoCloseable {
|
| 349 | @Override
|
| 350 | public void close() {
|
| 351 | }
|
| 352 |
|
| 353 | public @Nullable LevelSummary getLevelSummary() {
|
| 354 | return null;
|
| 355 | }
|
| 356 | }
|
| 357 |
|
| 358 | @Environment(EnvType.CLIENT)
|
| 359 | public enum EntryType {
|
| 360 | SINGLEPLAYER,
|
| 361 | UPLOAD_WORLD;
|
| 362 | }
|
| 363 |
|
| 364 | @Environment(EnvType.CLIENT)
|
| 365 | public static class LoadingHeader extends WorldSelectionList.Entry {
|
| 366 | private static final Component LOADING_LABEL = Component.translatable("selectWorld.loading_list");
|
| 367 | private final Minecraft minecraft;
|
| 368 |
|
| 369 | public LoadingHeader(final Minecraft minecraft) {
|
| 370 | this.minecraft = minecraft;
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 375 | int labelX = (this.minecraft.gui.screen().width - this.minecraft.font.width(LOADING_LABEL)) / 2;
|
| 376 | int labelY = this.getContentY() + (this.getContentHeight() - 9) / 2;
|
| 377 | graphics.text(this.minecraft.font, LOADING_LABEL, labelX, labelY, -1);
|
| 378 | String dots = LoadingDotsText.get(Util.getMillis());
|
| 379 | int dotsX = (this.minecraft.gui.screen().width - this.minecraft.font.width(dots)) / 2;
|
| 380 | int dotsY = labelY + 9;
|
| 381 | graphics.text(this.minecraft.font, dots, dotsX, dotsY, -8355712);
|
| 382 | }
|
| 383 |
|
| 384 | @Override
|
| 385 | public Component getNarration() {
|
| 386 | return LOADING_LABEL;
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | @Environment(EnvType.CLIENT)
|
| 391 | public static final class NoWorldsEntry extends WorldSelectionList.Entry {
|
| 392 | private final StringWidget stringWidget;
|
| 393 |
|
| 394 | public NoWorldsEntry(final Component component, final Font font) {
|
| 395 | this.stringWidget = new StringWidget(component, font);
|
| 396 | }
|
| 397 |
|
| 398 | @Override
|
| 399 | public Component getNarration() {
|
| 400 | return this.stringWidget.getMessage();
|
| 401 | }
|
| 402 |
|
| 403 | @Override
|
| 404 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 405 | this.stringWidget.setPosition(this.getContentXMiddle() - this.stringWidget.getWidth() / 2, this.getContentYMiddle() - this.stringWidget.getHeight() / 2);
|
| 406 | this.stringWidget.extractRenderState(graphics, mouseX, mouseY, a);
|
| 407 | }
|
| 408 | }
|
| 409 |
|
| 410 | @Environment(EnvType.CLIENT)
|
| 411 | public final class WorldListEntry extends WorldSelectionList.Entry implements SelectableEntry {
|
| 412 | private static final int ICON_SIZE = 32;
|
| 413 | private final WorldSelectionList list;
|
| 414 | private final Minecraft minecraft;
|
| 415 | private final Screen screen;
|
| 416 | private final LevelSummary summary;
|
| 417 | private final FaviconTexture icon;
|
| 418 | private final StringWidget worldNameText;
|
| 419 | private final StringWidget idAndLastPlayedText;
|
| 420 | private final StringWidget infoText;
|
| 421 | private @Nullable Path iconFile;
|
| 422 |
|
| 423 | public WorldListEntry(final WorldSelectionList list, final LevelSummary summary) {
|
| 424 | this.list = list;
|
| 425 | this.minecraft = list.minecraft;
|
| 426 | this.screen = list.getScreen();
|
| 427 | this.summary = summary;
|
| 428 | this.icon = FaviconTexture.forWorld(this.minecraft.getTextureManager(), summary.getLevelId());
|
| 429 | this.iconFile = summary.getIcon();
|
| 430 | int maxTextWidth = list.getRowWidth() - this.getTextX() - 2;
|
| 431 | Component worldNameComponent = Component.literal(summary.getLevelName());
|
| 432 | this.worldNameText = new StringWidget(worldNameComponent, this.minecraft.font);
|
| 433 | this.worldNameText.setMaxWidth(maxTextWidth);
|
| 434 | if (this.minecraft.font.width(worldNameComponent) > maxTextWidth) {
|
| 435 | this.worldNameText.setTooltip(Tooltip.create(worldNameComponent));
|
| 436 | }
|
| 437 |
|
| 438 | String levelIdAndDate = summary.getLevelId();
|
| 439 | long lastPlayed = summary.getLastPlayed();
|
| 440 | if (lastPlayed != -1L) {
|
| 441 | ZonedDateTime lastPlayedTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(lastPlayed), ZoneId.systemDefault());
|
| 442 | levelIdAndDate = levelIdAndDate + " (" + WorldSelectionList.DATE_FORMAT.format(lastPlayedTime) + ")";
|
| 443 | }
|
| 444 |
|
| 445 | Component levelIdAndDateComponent = Component.literal(levelIdAndDate).withColor(-8355712);
|
| 446 | this.idAndLastPlayedText = new StringWidget(levelIdAndDateComponent, this.minecraft.font);
|
| 447 | this.idAndLastPlayedText.setMaxWidth(maxTextWidth);
|
| 448 | if (this.minecraft.font.width(levelIdAndDate) > maxTextWidth) {
|
| 449 | this.idAndLastPlayedText.setTooltip(Tooltip.create(levelIdAndDateComponent));
|
| 450 | }
|
| 451 |
|
| 452 | Component info = ComponentUtils.mergeStyles(summary.getInfo(), Style.EMPTY.withColor(-8355712));
|
| 453 | this.infoText = new StringWidget(info, this.minecraft.font);
|
| 454 | this.infoText.setMaxWidth(maxTextWidth);
|
| 455 | if (this.minecraft.font.width(info) > maxTextWidth) {
|
| 456 | this.infoText.setTooltip(Tooltip.create(info));
|
| 457 | }
|
| 458 |
|
| 459 | this.validateIconFile();
|
| 460 | this.loadIcon();
|
| 461 | }
|
| 462 |
|
| 463 | private void validateIconFile() {
|
| 464 | if (this.iconFile != null) {
|
| 465 | try {
|
| 466 | BasicFileAttributes attributes = Files.readAttributes(this.iconFile, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
| 467 | if (attributes.isSymbolicLink()) {
|
| 468 | List<ForbiddenSymlinkInfo> issues = this.minecraft.directoryValidator().validateSymlink(this.iconFile);
|
| 469 | if (!issues.isEmpty()) {
|
| 470 | WorldSelectionList.LOGGER.warn("{}", ContentValidationException.getMessage(this.iconFile, issues));
|
| 471 | this.iconFile = null;
|
| 472 | } else {
|
| 473 | attributes = Files.readAttributes(this.iconFile, BasicFileAttributes.class);
|
| 474 | }
|
| 475 | }
|
| 476 |
|
| 477 | if (!attributes.isRegularFile()) {
|
| 478 | this.iconFile = null;
|
| 479 | }
|
| 480 | } catch (NoSuchFileException e) {
|
| 481 | this.iconFile = null;
|
| 482 | } catch (IOException e) {
|
| 483 | WorldSelectionList.LOGGER.error("could not validate symlink", e);
|
| 484 | this.iconFile = null;
|
| 485 | }
|
| 486 | }
|
| 487 | }
|
| 488 |
|
| 489 | @Override
|
| 490 | public Component getNarration() {
|
| 491 | Component entryNarration = Component.translatable(
|
| 492 | "narrator.select.world_info", this.summary.getLevelName(), Component.translationArg(new Date(this.summary.getLastPlayed())), this.summary.getInfo()
|
| 493 | );
|
| 494 | if (this.summary.isLocked()) {
|
| 495 | entryNarration = CommonComponents.joinForNarration(entryNarration, WorldSelectionList.WORLD_LOCKED_TOOLTIP);
|
| 496 | }
|
| 497 |
|
| 498 | if (this.summary.isExperimental()) {
|
| 499 | entryNarration = CommonComponents.joinForNarration(entryNarration, WorldSelectionList.WORLD_EXPERIMENTAL);
|
| 500 | }
|
| 501 |
|
| 502 | return Component.translatable("narrator.select", entryNarration);
|
| 503 | }
|
| 504 |
|
| 505 | @Override
|
| 506 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 507 | int textX = this.getTextX();
|
| 508 | this.worldNameText.setPosition(textX, this.getContentY() + 1);
|
| 509 | this.worldNameText.extractRenderState(graphics, mouseX, mouseY, a);
|
| 510 | this.idAndLastPlayedText.setPosition(textX, this.getContentY() + 9 + 3);
|
| 511 | this.idAndLastPlayedText.extractRenderState(graphics, mouseX, mouseY, a);
|
| 512 | this.infoText.setPosition(textX, this.getContentY() + 9 + 9 + 3);
|
| 513 | this.infoText.extractRenderState(graphics, mouseX, mouseY, a);
|
| 514 | graphics.blit(RenderPipelines.GUI_TEXTURED, this.icon.textureLocation(), this.getContentX(), this.getContentY(), 0.0F, 0.0F, 32, 32, 32, 32);
|
| 515 | if (this.list.entryType == WorldSelectionList.EntryType.SINGLEPLAYER && hovered) {
|
| 516 | graphics.fill(this.getContentX(), this.getContentY(), this.getContentX() + 32, this.getContentY() + 32, -1601138544);
|
| 517 | int relX = mouseX - this.getContentX();
|
| 518 | int relY = mouseY - this.getContentY();
|
| 519 | boolean isOverIcon = this.mouseOverIcon(relX, relY, 32);
|
| 520 | Identifier joinSprite = isOverIcon ? WorldSelectionList.JOIN_HIGHLIGHTED_SPRITE : WorldSelectionList.JOIN_SPRITE;
|
| 521 | Identifier warningSprite = isOverIcon ? WorldSelectionList.WARNING_HIGHLIGHTED_SPRITE : WorldSelectionList.WARNING_SPRITE;
|
| 522 | Identifier errorSprite = isOverIcon ? WorldSelectionList.ERROR_HIGHLIGHTED_SPRITE : WorldSelectionList.ERROR_SPRITE;
|
| 523 | Identifier joinWithErrorSprite = isOverIcon ? WorldSelectionList.MARKED_JOIN_HIGHLIGHTED_SPRITE : WorldSelectionList.MARKED_JOIN_SPRITE;
|
| 524 | if (this.summary instanceof SymlinkLevelSummary || this.summary instanceof CorruptedLevelSummary) {
|
| 525 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, errorSprite, this.getContentX(), this.getContentY(), 32, 32);
|
| 526 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, joinWithErrorSprite, this.getContentX(), this.getContentY(), 32, 32);
|
| 527 | return;
|
| 528 | }
|
| 529 |
|
| 530 | if (this.summary.isLocked()) {
|
| 531 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, errorSprite, this.getContentX(), this.getContentY(), 32, 32);
|
| 532 | if (isOverIcon) {
|
| 533 | graphics.setTooltipForNextFrame(this.minecraft.font.split(WorldSelectionList.WORLD_LOCKED_TOOLTIP, 175), mouseX, mouseY);
|
| 534 | }
|
| 535 | } else if (this.summary.requiresManualConversion()) {
|
| 536 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, errorSprite, this.getContentX(), this.getContentY(), 32, 32);
|
| 537 | if (isOverIcon) {
|
| 538 | graphics.setTooltipForNextFrame(this.minecraft.font.split(WorldSelectionList.WORLD_REQUIRES_CONVERSION, 175), mouseX, mouseY);
|
| 539 | }
|
| 540 | } else if (!this.summary.isCompatible()) {
|
| 541 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, errorSprite, this.getContentX(), this.getContentY(), 32, 32);
|
| 542 | if (isOverIcon) {
|
| 543 | graphics.setTooltipForNextFrame(this.minecraft.font.split(WorldSelectionList.INCOMPATIBLE_VERSION_TOOLTIP, 175), mouseX, mouseY);
|
| 544 | }
|
| 545 | } else if (this.summary.shouldBackup()) {
|
| 546 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, joinWithErrorSprite, this.getContentX(), this.getContentY(), 32, 32);
|
| 547 | if (this.summary.isDowngrade()) {
|
| 548 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, errorSprite, this.getContentX(), this.getContentY(), 32, 32);
|
| 549 | if (isOverIcon) {
|
| 550 | graphics.setTooltipForNextFrame(
|
| 551 | ImmutableList.of(WorldSelectionList.FROM_NEWER_TOOLTIP_1.getVisualOrderText(), WorldSelectionList.FROM_NEWER_TOOLTIP_2.getVisualOrderText()),
|
| 552 | mouseX,
|
| 553 | mouseY
|
| 554 | );
|
| 555 | }
|
| 556 | } else if (!SharedConstants.getCurrentVersion().stable()) {
|
| 557 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, warningSprite, this.getContentX(), this.getContentY(), 32, 32);
|
| 558 | if (isOverIcon) {
|
| 559 | graphics.setTooltipForNextFrame(
|
| 560 | ImmutableList.of(WorldSelectionList.SNAPSHOT_TOOLTIP_1.getVisualOrderText(), WorldSelectionList.SNAPSHOT_TOOLTIP_2.getVisualOrderText()),
|
| 561 | mouseX,
|
| 562 | mouseY
|
| 563 | );
|
| 564 | }
|
| 565 | }
|
| 566 |
|
| 567 | if (isOverIcon) {
|
| 568 | WorldSelectionList.this.handleCursor(graphics);
|
| 569 | }
|
| 570 | } else {
|
| 571 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, joinSprite, this.getContentX(), this.getContentY(), 32, 32);
|
| 572 | if (isOverIcon) {
|
| 573 | WorldSelectionList.this.handleCursor(graphics);
|
| 574 | }
|
| 575 | }
|
| 576 | }
|
| 577 | }
|
| 578 |
|
| 579 | private int getTextX() {
|
| 580 | return this.getContentX() + 32 + 3;
|
| 581 | }
|
| 582 |
|
| 583 | @Override
|
| 584 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 585 | if (this.canInteract()) {
|
| 586 | int relX = (int)event.x() - this.getContentX();
|
| 587 | int relY = (int)event.y() - this.getContentY();
|
| 588 | if (doubleClick || this.mouseOverIcon(relX, relY, 32) && this.list.entryType == WorldSelectionList.EntryType.SINGLEPLAYER) {
|
| 589 | this.minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
|
| 590 | Consumer<WorldSelectionList.WorldListEntry> onEntryInteract = this.list.onEntryInteract;
|
| 591 | if (onEntryInteract != null) {
|
| 592 | onEntryInteract.accept(this);
|
| 593 | return true;
|
| 594 | }
|
| 595 | }
|
| 596 | }
|
| 597 |
|
| 598 | return super.mouseClicked(event, doubleClick);
|
| 599 | }
|
| 600 |
|
| 601 | @Override
|
| 602 | public boolean keyPressed(final KeyEvent event) {
|
| 603 | if (event.isSelection() && this.canInteract()) {
|
| 604 | this.minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
|
| 605 | Consumer<WorldSelectionList.WorldListEntry> onEntryInteract = this.list.onEntryInteract;
|
| 606 | if (onEntryInteract != null) {
|
| 607 | onEntryInteract.accept(this);
|
| 608 | return true;
|
| 609 | }
|
| 610 | }
|
| 611 |
|
| 612 | return super.keyPressed(event);
|
| 613 | }
|
| 614 |
|
| 615 | public boolean canInteract() {
|
| 616 | return this.summary.primaryActionActive() || this.list.entryType == WorldSelectionList.EntryType.UPLOAD_WORLD;
|
| 617 | }
|
| 618 |
|
| 619 | public void joinWorld() {
|
| 620 | if (this.summary.primaryActionActive()) {
|
| 621 | if (this.summary instanceof SymlinkLevelSummary) {
|
| 622 | this.minecraft.gui.setScreen(NoticeWithLinkScreen.createWorldSymlinkWarningScreen(() -> this.minecraft.gui.setScreen(this.screen)));
|
| 623 | } else {
|
| 624 | this.minecraft.createWorldOpenFlows().openWorld(this.summary.getLevelId(), this.list::returnToScreen);
|
| 625 | }
|
| 626 | }
|
| 627 | }
|
| 628 |
|
| 629 | public void deleteWorld() {
|
| 630 | this.minecraft
|
| 631 | .gui
|
| 632 | .setScreen(
|
| 633 | new ConfirmScreen(
|
| 634 | result -> {
|
| 635 | if (result) {
|
| 636 | this.minecraft.gui.setScreen(new ProgressScreen(true));
|
| 637 | this.doDeleteWorld();
|
| 638 | }
|
| 639 |
|
| 640 | this.list.returnToScreen();
|
| 641 | },
|
| 642 | Component.translatable("selectWorld.deleteQuestion"),
|
| 643 | Component.translatable("selectWorld.deleteWarning", this.summary.getLevelName()),
|
| 644 | Component.translatable("selectWorld.deleteButton"),
|
| 645 | CommonComponents.GUI_CANCEL
|
| 646 | )
|
| 647 | );
|
| 648 | }
|
| 649 |
|
| 650 | public void doDeleteWorld() {
|
| 651 | LevelStorageSource levelSource = this.minecraft.getLevelSource();
|
| 652 | String levelId = this.summary.getLevelId();
|
| 653 |
|
| 654 | try (LevelStorageAccess access = levelSource.createAccess(levelId)) {
|
| 655 | access.deleteLevel();
|
| 656 | } catch (IOException e) {
|
| 657 | SystemToast.onWorldDeleteFailure(this.minecraft, levelId);
|
| 658 | WorldSelectionList.LOGGER.error("Failed to delete world {}", levelId, e);
|
| 659 | }
|
| 660 | }
|
| 661 |
|
| 662 | public void editWorld() {
|
| 663 | this.queueLoadScreen();
|
| 664 | String levelId = this.summary.getLevelId();
|
| 665 |
|
| 666 | LevelStorageAccess access;
|
| 667 | try {
|
| 668 | access = this.minecraft.getLevelSource().validateAndCreateAccess(levelId);
|
| 669 | } catch (IOException e) {
|
| 670 | SystemToast.onWorldAccessFailure(this.minecraft, levelId);
|
| 671 | WorldSelectionList.LOGGER.error("Failed to access level {}", levelId, e);
|
| 672 | this.list.reloadWorldList();
|
| 673 | return;
|
| 674 | } catch (ContentValidationException e) {
|
| 675 | WorldSelectionList.LOGGER.warn("{}", e.getMessage());
|
| 676 | this.minecraft.gui.setScreen(NoticeWithLinkScreen.createWorldSymlinkWarningScreen(() -> this.minecraft.gui.setScreen(this.screen)));
|
| 677 | return;
|
| 678 | }
|
| 679 |
|
| 680 | EditWorldScreen editScreen;
|
| 681 | try {
|
| 682 | editScreen = EditWorldScreen.create(this.minecraft, access, result -> {
|
| 683 | access.safeClose();
|
| 684 | this.list.returnToScreen();
|
| 685 | });
|
| 686 | } catch (IOException | NbtException | ReportedNbtException e) {
|
| 687 | access.safeClose();
|
| 688 | SystemToast.onWorldAccessFailure(this.minecraft, levelId);
|
| 689 | WorldSelectionList.LOGGER.error("Failed to load world data {}", levelId, e);
|
| 690 | this.list.reloadWorldList();
|
| 691 | return;
|
| 692 | }
|
| 693 |
|
| 694 | this.minecraft.gui.setScreen(editScreen);
|
| 695 | }
|
| 696 |
|
| 697 | public void recreateWorld() {
|
| 698 | this.queueLoadScreen();
|
| 699 |
|
| 700 | try (LevelStorageAccess access = this.minecraft.getLevelSource().validateAndCreateAccess(this.summary.getLevelId())) {
|
| 701 | Pair<LevelSettings, WorldCreationContext> recreatedSettings = this.minecraft.createWorldOpenFlows().recreateWorldData(access);
|
| 702 | LevelSettings levelSettings = recreatedSettings.getFirst();
|
| 703 | WorldCreationContext creationContext = recreatedSettings.getSecond();
|
| 704 | Path dataPackDir = CreateWorldScreen.createTempDataPackDirFromExistingWorld(access.getLevelPath(LevelResource.DATAPACK_DIR), this.minecraft);
|
| 705 | creationContext.validate();
|
| 706 | if (creationContext.options().isOldCustomizedWorld()) {
|
| 707 | this.minecraft
|
| 708 | .gui
|
| 709 | .setScreen(
|
| 710 | new ConfirmScreen(
|
| 711 | result -> this.minecraft
|
| 712 | .gui
|
| 713 | .setScreen(
|
| 714 | result ? CreateWorldScreen.createFromExisting(this.minecraft, this.list::returnToScreen, levelSettings, creationContext, dataPackDir) : this.screen
|
| 715 | ),
|
| 716 | Component.translatable("selectWorld.recreate.customized.title"),
|
| 717 | Component.translatable("selectWorld.recreate.customized.text"),
|
| 718 | CommonComponents.GUI_PROCEED,
|
| 719 | CommonComponents.GUI_CANCEL
|
| 720 | )
|
| 721 | );
|
| 722 | } else {
|
| 723 | this.minecraft.gui.setScreen(CreateWorldScreen.createFromExisting(this.minecraft, this.list::returnToScreen, levelSettings, creationContext, dataPackDir));
|
| 724 | }
|
| 725 | } catch (ContentValidationException e) {
|
| 726 | WorldSelectionList.LOGGER.warn("{}", e.getMessage());
|
| 727 | this.minecraft.gui.setScreen(NoticeWithLinkScreen.createWorldSymlinkWarningScreen(() -> this.minecraft.gui.setScreen(this.screen)));
|
| 728 | } catch (Exception e) {
|
| 729 | WorldSelectionList.LOGGER.error("Unable to recreate world", e);
|
| 730 | this.minecraft
|
| 731 | .gui
|
| 732 | .setScreen(
|
| 733 | new AlertScreen(
|
| 734 | () -> this.minecraft.gui.setScreen(this.screen),
|
| 735 | Component.translatable("selectWorld.recreate.error.title"),
|
| 736 | Component.translatable("selectWorld.recreate.error.text")
|
| 737 | )
|
| 738 | );
|
| 739 | }
|
| 740 | }
|
| 741 |
|
| 742 | private void queueLoadScreen() {
|
| 743 | this.minecraft.setScreenAndShow(new GenericMessageScreen(Component.translatable("selectWorld.data_read")));
|
| 744 | }
|
| 745 |
|
| 746 | private void loadIcon() {
|
| 747 | boolean shouldHaveIcon = this.iconFile != null && Files.isRegularFile(this.iconFile);
|
| 748 | if (shouldHaveIcon) {
|
| 749 | try (InputStream stream = Files.newInputStream(this.iconFile)) {
|
| 750 | this.icon.upload(NativeImage.read(stream));
|
| 751 | } catch (Throwable t) {
|
| 752 | WorldSelectionList.LOGGER.error("Invalid icon for world {}", this.summary.getLevelId(), t);
|
| 753 | this.iconFile = null;
|
| 754 | }
|
| 755 | } else {
|
| 756 | this.icon.clear();
|
| 757 | }
|
| 758 | }
|
| 759 |
|
| 760 | @Override
|
| 761 | public void close() {
|
| 762 | if (!this.icon.isClosed()) {
|
| 763 | this.icon.close();
|
| 764 | }
|
| 765 | }
|
| 766 |
|
| 767 | public String getLevelName() {
|
| 768 | return this.summary.getLevelName();
|
| 769 | }
|
| 770 |
|
| 771 | @Override
|
| 772 | public LevelSummary getLevelSummary() {
|
| 773 | return this.summary;
|
| 774 | }
|
| 775 | }
|
| 776 | }
|
| 777 |
|