| 1 | package net.minecraft.world.level.storage;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Dynamic;
|
| 7 | import com.mojang.serialization.Lifecycle;
|
| 8 | import java.util.HashSet;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.UUID;
|
| 12 | import java.util.stream.Collectors;
|
| 13 | import net.minecraft.CrashReportCategory;
|
| 14 | import net.minecraft.SharedConstants;
|
| 15 | import net.minecraft.core.UUIDUtil;
|
| 16 | import net.minecraft.nbt.CompoundTag;
|
| 17 | import net.minecraft.nbt.IntTag;
|
| 18 | import net.minecraft.nbt.ListTag;
|
| 19 | import net.minecraft.nbt.NbtUtils;
|
| 20 | import net.minecraft.nbt.StringTag;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import net.minecraft.world.Difficulty;
|
| 23 | import net.minecraft.world.level.GameType;
|
| 24 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 25 | import net.minecraft.world.level.LevelSettings;
|
| 26 | import net.minecraft.world.level.WorldDataConfiguration;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | public class PrimaryLevelData implements ServerLevelData, WorldData {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | public static final String LEVEL_NAME = "LevelName";
|
| 33 | protected static final String OLD_PLAYER = "Player";
|
| 34 | protected static final String SINGLEPLAYER_UUID = "singleplayer_uuid";
|
| 35 | protected static final String OLD_WORLD_GEN_SETTINGS = "WorldGenSettings";
|
| 36 | private LevelSettings settings;
|
| 37 | private final PrimaryLevelData.SpecialWorldProperty specialWorldProperty;
|
| 38 | private final Lifecycle worldGenSettingsLifecycle;
|
| 39 | private LevelData.RespawnData respawnData;
|
| 40 | private long gameTime;
|
| 41 | private final @Nullable UUID singlePlayerUUID;
|
| 42 | private final int version;
|
| 43 | private boolean initialized;
|
| 44 | private final Set<String> knownServerBrands;
|
| 45 | private boolean wasModded;
|
| 46 | private final Set<String> removedFeatureFlags;
|
| 47 | private List<Integer> versionHistory;
|
| 48 |
|
| 49 | private PrimaryLevelData(
|
| 50 | final @Nullable UUID singlePlayerUUID,
|
| 51 | final boolean wasModded,
|
| 52 | final LevelData.RespawnData respawnData,
|
| 53 | final long gameTime,
|
| 54 | final int version,
|
| 55 | final boolean initialized,
|
| 56 | final Set<String> knownServerBrands,
|
| 57 | final Set<String> removedFeatureFlags,
|
| 58 | final LevelSettings settings,
|
| 59 | final PrimaryLevelData.SpecialWorldProperty specialWorldProperty,
|
| 60 | final Lifecycle worldGenSettingsLifecycle,
|
| 61 | final List<Integer> versionHistory
|
| 62 | ) {
|
| 63 | this.wasModded = wasModded;
|
| 64 | this.respawnData = respawnData;
|
| 65 | this.gameTime = gameTime;
|
| 66 | this.version = version;
|
| 67 | this.initialized = initialized;
|
| 68 | this.knownServerBrands = knownServerBrands;
|
| 69 | this.removedFeatureFlags = removedFeatureFlags;
|
| 70 | this.singlePlayerUUID = singlePlayerUUID;
|
| 71 | this.settings = settings;
|
| 72 | this.specialWorldProperty = specialWorldProperty;
|
| 73 | this.worldGenSettingsLifecycle = worldGenSettingsLifecycle;
|
| 74 | this.versionHistory = versionHistory;
|
| 75 | }
|
| 76 |
|
| 77 | public PrimaryLevelData(final LevelSettings levelSettings, final PrimaryLevelData.SpecialWorldProperty specialWorldProperty, final Lifecycle lifecycle) {
|
| 78 | this(
|
| 79 | null,
|
| 80 | false,
|
| 81 | LevelData.RespawnData.DEFAULT,
|
| 82 | 0L,
|
| 83 | 19133,
|
| 84 | false,
|
| 85 | Sets.newLinkedHashSet(),
|
| 86 | new HashSet<>(),
|
| 87 | levelSettings.copy(),
|
| 88 | specialWorldProperty,
|
| 89 | lifecycle,
|
| 90 | List.of()
|
| 91 | );
|
| 92 | }
|
| 93 |
|
| 94 | public static <T> PrimaryLevelData parse(
|
| 95 | final Dynamic<T> input,
|
| 96 | final LevelSettings settings,
|
| 97 | final PrimaryLevelData.SpecialWorldProperty specialWorldProperty,
|
| 98 | final Lifecycle worldGenSettingsLifecycle
|
| 99 | ) {
|
| 100 | long gameTime = input.get("Time").asLong(0L);
|
| 101 | LevelVersion levelVersion = LevelVersion.parse(input);
|
| 102 | return new PrimaryLevelData(
|
| 103 | input.get("singleplayer_uuid").flatMap(UUIDUtil.CODEC::parse).result().orElse(null),
|
| 104 | input.get("WasModded").asBoolean(false),
|
| 105 | input.get("spawn").read(LevelData.RespawnData.CODEC).result().orElse(LevelData.RespawnData.DEFAULT),
|
| 106 | gameTime,
|
| 107 | levelVersion.levelDataVersion(),
|
| 108 | input.get("initialized").asBoolean(true),
|
| 109 | input.get("ServerBrands").asStream().flatMap(b -> b.asString().result().stream()).collect(Collectors.toCollection(Sets::newLinkedHashSet)),
|
| 110 | input.get("removed_features").asStream().flatMap(b -> b.asString().result().stream()).collect(Collectors.toSet()),
|
| 111 | settings,
|
| 112 | specialWorldProperty,
|
| 113 | worldGenSettingsLifecycle,
|
| 114 | input.get("version_history").orElseEmptyList().asList(d -> d.asInt(-1))
|
| 115 | );
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public CompoundTag createTag(@Nullable UUID singlePlayerUUID) {
|
| 120 | if (singlePlayerUUID == null) {
|
| 121 | singlePlayerUUID = this.singlePlayerUUID;
|
| 122 | }
|
| 123 |
|
| 124 | CompoundTag tag = new CompoundTag();
|
| 125 | this.setTagData(tag, singlePlayerUUID);
|
| 126 | return tag;
|
| 127 | }
|
| 128 |
|
| 129 | private void setTagData(final CompoundTag tag, final @Nullable UUID singlePlayerUUID) {
|
| 130 | tag.put("ServerBrands", stringCollectionToTag(this.knownServerBrands));
|
| 131 | tag.putBoolean("WasModded", this.wasModded);
|
| 132 | if (!this.removedFeatureFlags.isEmpty()) {
|
| 133 | tag.put("removed_features", stringCollectionToTag(this.removedFeatureFlags));
|
| 134 | }
|
| 135 |
|
| 136 | writeVersionTag(tag);
|
| 137 | this.writeVersionHistory(tag);
|
| 138 | NbtUtils.addCurrentDataVersion(tag);
|
| 139 | tag.putInt("GameType", this.settings.gameType().getId());
|
| 140 | tag.store("spawn", LevelData.RespawnData.CODEC, this.respawnData);
|
| 141 | tag.putLong("Time", this.gameTime);
|
| 142 | writeLastPlayed(tag);
|
| 143 | tag.putString("LevelName", this.settings.levelName());
|
| 144 | tag.putInt("version", 19133);
|
| 145 | tag.putBoolean("allowCommands", this.settings.allowCommands());
|
| 146 | tag.putBoolean("initialized", this.initialized);
|
| 147 | tag.store("difficulty_settings", LevelSettings.DifficultySettings.CODEC, this.settings.difficultySettings());
|
| 148 | if (singlePlayerUUID != null) {
|
| 149 | tag.storeNullable("singleplayer_uuid", UUIDUtil.CODEC, singlePlayerUUID);
|
| 150 | }
|
| 151 |
|
| 152 | tag.store(WorldDataConfiguration.MAP_CODEC, this.settings.dataConfiguration());
|
| 153 | }
|
| 154 |
|
| 155 | public static void writeLastPlayed(final CompoundTag tag) {
|
| 156 | tag.putLong("LastPlayed", Util.getEpochMillis());
|
| 157 | }
|
| 158 |
|
| 159 | public static Dynamic<?> writeLastPlayed(final Dynamic<?> tag) {
|
| 160 | return tag.set("LastPlayed", tag.createLong(Util.getEpochMillis()));
|
| 161 | }
|
| 162 |
|
| 163 | public static void writeVersionTag(final CompoundTag tag) {
|
| 164 | CompoundTag worldVersion = new CompoundTag();
|
| 165 | worldVersion.putString("Name", SharedConstants.getCurrentVersion().name());
|
| 166 | worldVersion.putInt("Id", SharedConstants.getCurrentVersion().dataVersion().version());
|
| 167 | worldVersion.putBoolean("Snapshot", !SharedConstants.getCurrentVersion().stable());
|
| 168 | worldVersion.putString("Series", SharedConstants.getCurrentVersion().dataVersion().series());
|
| 169 | tag.put("Version", worldVersion);
|
| 170 | }
|
| 171 |
|
| 172 | public static Dynamic<?> writeVersionTag(final Dynamic<?> tag) {
|
| 173 | Dynamic<?> worldVersion = tag.emptyMap()
|
| 174 | .set("Name", tag.createString(SharedConstants.getCurrentVersion().name()))
|
| 175 | .set("Id", tag.createInt(SharedConstants.getCurrentVersion().dataVersion().version()))
|
| 176 | .set("Snapshot", tag.createBoolean(!SharedConstants.getCurrentVersion().stable()))
|
| 177 | .set("Series", tag.createString(SharedConstants.getCurrentVersion().dataVersion().series()));
|
| 178 | return tag.set("Version", worldVersion);
|
| 179 | }
|
| 180 |
|
| 181 | public void writeVersionHistory(final CompoundTag tag) {
|
| 182 | ListTag list = new ListTag();
|
| 183 | this.versionHistory.stream().map(IntTag::valueOf).forEach(list::add);
|
| 184 | int currentVersion = SharedConstants.getCurrentVersion().dataVersion().version();
|
| 185 | if (this.versionHistory.isEmpty() || this.versionHistory.getLast() != currentVersion) {
|
| 186 | list.add(IntTag.valueOf(currentVersion));
|
| 187 | }
|
| 188 |
|
| 189 | tag.put("version_history", list);
|
| 190 | }
|
| 191 |
|
| 192 | private static ListTag stringCollectionToTag(final Set<String> values) {
|
| 193 | ListTag result = new ListTag();
|
| 194 | values.stream().map(StringTag::valueOf).forEach(result::add);
|
| 195 | return result;
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | public LevelData.RespawnData getRespawnData() {
|
| 200 | return this.respawnData;
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | public long getGameTime() {
|
| 205 | return this.gameTime;
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public @Nullable UUID getSinglePlayerUUID() {
|
| 210 | return this.singlePlayerUUID;
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | public void setGameTime(final long time) {
|
| 215 | this.gameTime = time;
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | public void setSpawn(final LevelData.RespawnData respawnData) {
|
| 220 | this.respawnData = respawnData;
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | public String getLevelName() {
|
| 225 | return this.settings.levelName();
|
| 226 | }
|
| 227 |
|
| 228 | @Override
|
| 229 | public int getVersion() {
|
| 230 | return this.version;
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public GameType getGameType() {
|
| 235 | return this.settings.gameType();
|
| 236 | }
|
| 237 |
|
| 238 | @Override
|
| 239 | public void setGameType(final GameType gameType) {
|
| 240 | this.settings = this.settings.withGameType(gameType);
|
| 241 | }
|
| 242 |
|
| 243 | @Override
|
| 244 | public boolean isHardcore() {
|
| 245 | return this.settings.difficultySettings().hardcore();
|
| 246 | }
|
| 247 |
|
| 248 | @Override
|
| 249 | public boolean isAllowCommands() {
|
| 250 | return this.settings.allowCommands();
|
| 251 | }
|
| 252 |
|
| 253 | @Override
|
| 254 | public void setAllowCommands(final boolean allowCommands) {
|
| 255 | this.settings = this.settings.withAllowCommands(allowCommands);
|
| 256 | }
|
| 257 |
|
| 258 | @Override
|
| 259 | public boolean isInitialized() {
|
| 260 | return this.initialized;
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | public void setInitialized(final boolean initialized) {
|
| 265 | this.initialized = initialized;
|
| 266 | }
|
| 267 |
|
| 268 | @Override
|
| 269 | public Difficulty getDifficulty() {
|
| 270 | return this.settings.difficultySettings().difficulty();
|
| 271 | }
|
| 272 |
|
| 273 | @Override
|
| 274 | public void setDifficulty(final Difficulty difficulty) {
|
| 275 | this.settings = this.settings.withDifficulty(difficulty);
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public boolean isDifficultyLocked() {
|
| 280 | return this.settings.difficultySettings().locked();
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | public void setDifficultyLocked(final boolean difficultyLocked) {
|
| 285 | this.settings = this.settings.withDifficultyLock(difficultyLocked);
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | public void fillCrashReportCategory(final CrashReportCategory category, final LevelHeightAccessor levelHeightAccessor) {
|
| 290 | ServerLevelData.super.fillCrashReportCategory(category, levelHeightAccessor);
|
| 291 | WorldData.super.fillCrashReportCategory(category);
|
| 292 | }
|
| 293 |
|
| 294 | @Override
|
| 295 | public boolean isFlatWorld() {
|
| 296 | return this.specialWorldProperty == PrimaryLevelData.SpecialWorldProperty.FLAT;
|
| 297 | }
|
| 298 |
|
| 299 | @Override
|
| 300 | public boolean isDebugWorld() {
|
| 301 | return this.specialWorldProperty == PrimaryLevelData.SpecialWorldProperty.DEBUG;
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | public Lifecycle worldGenSettingsLifecycle() {
|
| 306 | return this.worldGenSettingsLifecycle;
|
| 307 | }
|
| 308 |
|
| 309 | @Override
|
| 310 | public WorldDataConfiguration getDataConfiguration() {
|
| 311 | return this.settings.dataConfiguration();
|
| 312 | }
|
| 313 |
|
| 314 | @Override
|
| 315 | public void setDataConfiguration(final WorldDataConfiguration dataConfiguration) {
|
| 316 | this.settings = this.settings.withDataConfiguration(dataConfiguration);
|
| 317 | }
|
| 318 |
|
| 319 | @Override
|
| 320 | public void setModdedInfo(final String serverBrand, final boolean isModded) {
|
| 321 | this.knownServerBrands.add(serverBrand);
|
| 322 | this.wasModded |= isModded;
|
| 323 | }
|
| 324 |
|
| 325 | @Override
|
| 326 | public boolean wasModded() {
|
| 327 | return this.wasModded;
|
| 328 | }
|
| 329 |
|
| 330 | @Override
|
| 331 | public Set<String> getKnownServerBrands() {
|
| 332 | return ImmutableSet.copyOf(this.knownServerBrands);
|
| 333 | }
|
| 334 |
|
| 335 | @Override
|
| 336 | public Set<String> getRemovedFeatureFlags() {
|
| 337 | return Set.copyOf(this.removedFeatureFlags);
|
| 338 | }
|
| 339 |
|
| 340 | @Override
|
| 341 | public ServerLevelData overworldData() {
|
| 342 | return this;
|
| 343 | }
|
| 344 |
|
| 345 | @Override
|
| 346 | public LevelSettings getLevelSettings() {
|
| 347 | return this.settings.copy();
|
| 348 | }
|
| 349 |
|
| 350 | @Deprecated
|
| 351 | public enum SpecialWorldProperty {
|
| 352 | NONE,
|
| 353 | FLAT,
|
| 354 | DEBUG;
|
| 355 | }
|
| 356 | }
|
| 357 |
|