| 1 | package net.minecraft.world.level.storage;
|
| 2 |
|
| 3 | import java.nio.file.Path;
|
| 4 | import net.minecraft.ChatFormatting;
|
| 5 | import net.minecraft.SharedConstants;
|
| 6 | import net.minecraft.WorldVersion;
|
| 7 | import net.minecraft.network.chat.CommonComponents;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.network.chat.MutableComponent;
|
| 10 | import net.minecraft.util.StringUtil;
|
| 11 | import net.minecraft.util.datafix.DataFixers;
|
| 12 | import net.minecraft.world.level.GameType;
|
| 13 | import net.minecraft.world.level.LevelSettings;
|
| 14 | import org.apache.commons.lang3.StringUtils;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | public class LevelSummary implements Comparable<LevelSummary> {
|
| 18 | public static final Component PLAY_WORLD = Component.translatable("selectWorld.select");
|
| 19 | public static final Component UPGRADE_AND_PLAY_WORLD = Component.translatable("selectWorld.upgrade_and_play");
|
| 20 | private final LevelSettings settings;
|
| 21 | private final LevelVersion levelVersion;
|
| 22 | private final String levelId;
|
| 23 | private final boolean requiresManualConversion;
|
| 24 | private final boolean requiresFileFixing;
|
| 25 | private final boolean locked;
|
| 26 | private final boolean experimental;
|
| 27 | private final Path icon;
|
| 28 | private @Nullable Component info;
|
| 29 |
|
| 30 | public LevelSummary(
|
| 31 | final LevelSettings settings,
|
| 32 | final LevelVersion levelVersion,
|
| 33 | final String levelId,
|
| 34 | final boolean requiresManualConversion,
|
| 35 | final boolean requiresFileFixing,
|
| 36 | final boolean locked,
|
| 37 | final boolean experimental,
|
| 38 | final Path icon
|
| 39 | ) {
|
| 40 | this.settings = settings;
|
| 41 | this.levelVersion = levelVersion;
|
| 42 | this.levelId = levelId;
|
| 43 | this.requiresFileFixing = requiresFileFixing;
|
| 44 | this.locked = locked;
|
| 45 | this.experimental = experimental;
|
| 46 | this.icon = icon;
|
| 47 | this.requiresManualConversion = requiresManualConversion;
|
| 48 | }
|
| 49 |
|
| 50 | public String getLevelId() {
|
| 51 | return this.levelId;
|
| 52 | }
|
| 53 |
|
| 54 | public String getLevelName() {
|
| 55 | return StringUtils.isEmpty(this.settings.levelName()) ? this.levelId : this.settings.levelName();
|
| 56 | }
|
| 57 |
|
| 58 | public Path getIcon() {
|
| 59 | return this.icon;
|
| 60 | }
|
| 61 |
|
| 62 | public boolean requiresManualConversion() {
|
| 63 | return this.requiresManualConversion;
|
| 64 | }
|
| 65 |
|
| 66 | public boolean requiresFileFixing() {
|
| 67 | return this.requiresFileFixing;
|
| 68 | }
|
| 69 |
|
| 70 | public boolean isExperimental() {
|
| 71 | return this.experimental;
|
| 72 | }
|
| 73 |
|
| 74 | public long getLastPlayed() {
|
| 75 | return this.levelVersion.lastPlayed();
|
| 76 | }
|
| 77 |
|
| 78 | public int compareTo(final LevelSummary rhs) {
|
| 79 | if (this.getLastPlayed() < rhs.getLastPlayed()) {
|
| 80 | return 1;
|
| 81 | } else {
|
| 82 | return this.getLastPlayed() > rhs.getLastPlayed() ? -1 : this.levelId.compareTo(rhs.levelId);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public LevelSettings getSettings() {
|
| 87 | return this.settings;
|
| 88 | }
|
| 89 |
|
| 90 | public GameType getGameMode() {
|
| 91 | return this.settings.gameType();
|
| 92 | }
|
| 93 |
|
| 94 | public boolean isHardcore() {
|
| 95 | return this.settings.difficultySettings().hardcore();
|
| 96 | }
|
| 97 |
|
| 98 | public boolean hasCommands() {
|
| 99 | return this.settings.allowCommands();
|
| 100 | }
|
| 101 |
|
| 102 | public MutableComponent getWorldVersionName() {
|
| 103 | return StringUtil.isNullOrEmpty(this.levelVersion.minecraftVersionName())
|
| 104 | ? Component.translatable("selectWorld.versionUnknown")
|
| 105 | : Component.literal(this.levelVersion.minecraftVersionName());
|
| 106 | }
|
| 107 |
|
| 108 | public LevelVersion levelVersion() {
|
| 109 | return this.levelVersion;
|
| 110 | }
|
| 111 |
|
| 112 | public boolean shouldBackup() {
|
| 113 | return this.backupStatus().shouldBackup();
|
| 114 | }
|
| 115 |
|
| 116 | public boolean isDowngrade() {
|
| 117 | return this.backupStatus() == LevelSummary.BackupStatus.DOWNGRADE;
|
| 118 | }
|
| 119 |
|
| 120 | public LevelSummary.BackupStatus backupStatus() {
|
| 121 | WorldVersion currentVersion = SharedConstants.getCurrentVersion();
|
| 122 | int currentVersionNumber = currentVersion.dataVersion().version();
|
| 123 | int levelVersionNumber = this.levelVersion.minecraftVersion().version();
|
| 124 | if (DataFixers.getFileFixer().requiresFileFixing(levelVersionNumber)) {
|
| 125 | return LevelSummary.BackupStatus.FILE_FIXING_REQUIRED;
|
| 126 | } else if (!currentVersion.stable() && levelVersionNumber < currentVersionNumber) {
|
| 127 | return LevelSummary.BackupStatus.UPGRADE_TO_SNAPSHOT;
|
| 128 | } else {
|
| 129 | return levelVersionNumber > currentVersionNumber ? LevelSummary.BackupStatus.DOWNGRADE : LevelSummary.BackupStatus.NONE;
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | public boolean isLocked() {
|
| 134 | return this.locked;
|
| 135 | }
|
| 136 |
|
| 137 | public boolean isDisabled() {
|
| 138 | return !this.isLocked() && !this.requiresManualConversion() ? !this.isCompatible() : true;
|
| 139 | }
|
| 140 |
|
| 141 | public boolean isCompatible() {
|
| 142 | return SharedConstants.getCurrentVersion().dataVersion().isCompatible(this.levelVersion.minecraftVersion());
|
| 143 | }
|
| 144 |
|
| 145 | public Component getInfo() {
|
| 146 | if (this.info == null) {
|
| 147 | this.info = this.createInfo();
|
| 148 | }
|
| 149 |
|
| 150 | return this.info;
|
| 151 | }
|
| 152 |
|
| 153 | private Component createInfo() {
|
| 154 | if (this.isLocked()) {
|
| 155 | return Component.translatable("selectWorld.locked").withStyle(ChatFormatting.RED);
|
| 156 | }
|
| 157 |
|
| 158 | if (this.requiresManualConversion()) {
|
| 159 | return Component.translatable("selectWorld.conversion").withStyle(ChatFormatting.RED);
|
| 160 | }
|
| 161 |
|
| 162 | if (!this.isCompatible()) {
|
| 163 | return Component.translatable("selectWorld.incompatible.info", this.getWorldVersionName()).withStyle(ChatFormatting.RED);
|
| 164 | }
|
| 165 |
|
| 166 | MutableComponent result = this.isHardcore()
|
| 167 | ? Component.empty().append(Component.translatable("gameMode.hardcore").withColor(-65536))
|
| 168 | : Component.translatable("gameMode." + this.getGameMode().getName());
|
| 169 | if (this.hasCommands()) {
|
| 170 | result.append(", ").append(Component.translatable("selectWorld.commands"));
|
| 171 | }
|
| 172 |
|
| 173 | if (this.isExperimental()) {
|
| 174 | result.append(", ").append(Component.translatable("selectWorld.experimental").withStyle(ChatFormatting.YELLOW));
|
| 175 | }
|
| 176 |
|
| 177 | MutableComponent worldVersionName = this.getWorldVersionName();
|
| 178 | MutableComponent decoratedVersionName = Component.literal(", ").append(Component.translatable("selectWorld.version")).append(CommonComponents.SPACE);
|
| 179 | if (this.shouldBackup()) {
|
| 180 | decoratedVersionName.append(worldVersionName.withStyle(this.isDowngrade() ? ChatFormatting.RED : ChatFormatting.ITALIC));
|
| 181 | } else {
|
| 182 | decoratedVersionName.append(worldVersionName);
|
| 183 | }
|
| 184 |
|
| 185 | result.append(decoratedVersionName);
|
| 186 | return result;
|
| 187 | }
|
| 188 |
|
| 189 | public Component primaryActionMessage() {
|
| 190 | return this.requiresFileFixing() ? UPGRADE_AND_PLAY_WORLD : PLAY_WORLD;
|
| 191 | }
|
| 192 |
|
| 193 | public boolean primaryActionActive() {
|
| 194 | return !this.isDisabled();
|
| 195 | }
|
| 196 |
|
| 197 | public boolean canUpload() {
|
| 198 | return !this.requiresManualConversion() && !this.isLocked();
|
| 199 | }
|
| 200 |
|
| 201 | public boolean canEdit() {
|
| 202 | return !this.isDisabled() && !this.requiresFileFixing();
|
| 203 | }
|
| 204 |
|
| 205 | public boolean canRecreate() {
|
| 206 | return !this.isDisabled() && !this.requiresFileFixing();
|
| 207 | }
|
| 208 |
|
| 209 | public boolean canDelete() {
|
| 210 | return true;
|
| 211 | }
|
| 212 |
|
| 213 | public enum BackupStatus {
|
| 214 | NONE(false, false, ""),
|
| 215 | DOWNGRADE(true, true, "downgrade"),
|
| 216 | UPGRADE_TO_SNAPSHOT(true, false, "snapshot"),
|
| 217 | FILE_FIXING_REQUIRED(true, false, "file_fixing_required");
|
| 218 |
|
| 219 | private final boolean shouldBackup;
|
| 220 | private final boolean severe;
|
| 221 | private final String translationKey;
|
| 222 |
|
| 223 | BackupStatus(final boolean shouldBackup, final boolean severe, final String translationKey) {
|
| 224 | this.shouldBackup = shouldBackup;
|
| 225 | this.severe = severe;
|
| 226 | this.translationKey = translationKey;
|
| 227 | }
|
| 228 |
|
| 229 | public boolean shouldBackup() {
|
| 230 | return this.shouldBackup;
|
| 231 | }
|
| 232 |
|
| 233 | public boolean isSevere() {
|
| 234 | return this.severe;
|
| 235 | }
|
| 236 |
|
| 237 | public String getTranslationKey() {
|
| 238 | return this.translationKey;
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 | public static class CorruptedLevelSummary extends LevelSummary {
|
| 243 | private static final Component INFO = Component.translatable("recover_world.warning").withStyle(style -> style.withColor(-65536));
|
| 244 | private static final Component RECOVER = Component.translatable("recover_world.button");
|
| 245 | private final long lastPlayed;
|
| 246 |
|
| 247 | public CorruptedLevelSummary(final String levelId, final Path icon, final long lastPlayed) {
|
| 248 | super(null, null, levelId, false, false, false, false, icon);
|
| 249 | this.lastPlayed = lastPlayed;
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | public String getLevelName() {
|
| 254 | return this.getLevelId();
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | public Component getInfo() {
|
| 259 | return INFO;
|
| 260 | }
|
| 261 |
|
| 262 | @Override
|
| 263 | public long getLastPlayed() {
|
| 264 | return this.lastPlayed;
|
| 265 | }
|
| 266 |
|
| 267 | @Override
|
| 268 | public boolean isDisabled() {
|
| 269 | return false;
|
| 270 | }
|
| 271 |
|
| 272 | @Override
|
| 273 | public Component primaryActionMessage() {
|
| 274 | return RECOVER;
|
| 275 | }
|
| 276 |
|
| 277 | @Override
|
| 278 | public boolean primaryActionActive() {
|
| 279 | return true;
|
| 280 | }
|
| 281 |
|
| 282 | @Override
|
| 283 | public boolean canUpload() {
|
| 284 | return false;
|
| 285 | }
|
| 286 |
|
| 287 | @Override
|
| 288 | public boolean canEdit() {
|
| 289 | return false;
|
| 290 | }
|
| 291 |
|
| 292 | @Override
|
| 293 | public boolean canRecreate() {
|
| 294 | return false;
|
| 295 | }
|
| 296 | }
|
| 297 |
|
| 298 | public static class SymlinkLevelSummary extends LevelSummary {
|
| 299 | private static final Component MORE_INFO_BUTTON = Component.translatable("symlink_warning.more_info");
|
| 300 | private static final Component INFO = Component.translatable("symlink_warning.title").withColor(-65536);
|
| 301 |
|
| 302 | public SymlinkLevelSummary(final String levelId, final Path icon) {
|
| 303 | super(null, null, levelId, false, false, false, false, icon);
|
| 304 | }
|
| 305 |
|
| 306 | @Override
|
| 307 | public String getLevelName() {
|
| 308 | return this.getLevelId();
|
| 309 | }
|
| 310 |
|
| 311 | @Override
|
| 312 | public Component getInfo() {
|
| 313 | return INFO;
|
| 314 | }
|
| 315 |
|
| 316 | @Override
|
| 317 | public long getLastPlayed() {
|
| 318 | return -1L;
|
| 319 | }
|
| 320 |
|
| 321 | @Override
|
| 322 | public boolean isDisabled() {
|
| 323 | return false;
|
| 324 | }
|
| 325 |
|
| 326 | @Override
|
| 327 | public Component primaryActionMessage() {
|
| 328 | return MORE_INFO_BUTTON;
|
| 329 | }
|
| 330 |
|
| 331 | @Override
|
| 332 | public boolean primaryActionActive() {
|
| 333 | return true;
|
| 334 | }
|
| 335 |
|
| 336 | @Override
|
| 337 | public boolean canUpload() {
|
| 338 | return false;
|
| 339 | }
|
| 340 |
|
| 341 | @Override
|
| 342 | public boolean canEdit() {
|
| 343 | return false;
|
| 344 | }
|
| 345 |
|
| 346 | @Override
|
| 347 | public boolean canRecreate() {
|
| 348 | return false;
|
| 349 | }
|
| 350 | }
|
| 351 | }
|
| 352 |
|