| 1 | package net.minecraft.world.level.chunk.storage;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFixer;
|
| 4 | import com.mojang.serialization.Dynamic;
|
| 5 | import java.io.IOException;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.concurrent.CompletableFuture;
|
| 9 | import java.util.function.Supplier;
|
| 10 | import net.minecraft.CrashReport;
|
| 11 | import net.minecraft.CrashReportCategory;
|
| 12 | import net.minecraft.ReportedException;
|
| 13 | import net.minecraft.SharedConstants;
|
| 14 | import net.minecraft.nbt.CompoundTag;
|
| 15 | import net.minecraft.nbt.NbtUtils;
|
| 16 | import net.minecraft.nbt.Tag;
|
| 17 | import net.minecraft.util.datafix.DataFixTypes;
|
| 18 | import net.minecraft.world.level.ChunkPos;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public class SimpleRegionStorage implements AutoCloseable {
|
| 22 | private final IOWorker worker;
|
| 23 | private final DataFixer fixerUpper;
|
| 24 | private final DataFixTypes dataFixType;
|
| 25 |
|
| 26 | public SimpleRegionStorage(
|
| 27 | final RegionStorageInfo info, final Path folder, final DataFixer fixerUpper, final boolean syncWrites, final DataFixTypes dataFixType
|
| 28 | ) {
|
| 29 | this.fixerUpper = fixerUpper;
|
| 30 | this.dataFixType = dataFixType;
|
| 31 | this.worker = new IOWorker(info, folder, syncWrites);
|
| 32 | }
|
| 33 |
|
| 34 | public boolean isOldChunkAround(final ChunkPos pos, final int range) {
|
| 35 | return this.worker.isOldChunkAround(pos, range);
|
| 36 | }
|
| 37 |
|
| 38 | public CompletableFuture<Optional<CompoundTag>> read(final ChunkPos pos) {
|
| 39 | return this.worker.loadAsync(pos);
|
| 40 | }
|
| 41 |
|
| 42 | public CompletableFuture<Void> write(final ChunkPos pos, final CompoundTag value) {
|
| 43 | return this.write(pos, () -> value);
|
| 44 | }
|
| 45 |
|
| 46 | public CompletableFuture<Void> write(final ChunkPos pos, final Supplier<CompoundTag> supplier) {
|
| 47 | return this.worker.store(pos, supplier);
|
| 48 | }
|
| 49 |
|
| 50 | public CompoundTag upgradeChunkTag(CompoundTag chunkTag, final int defaultVersion, final @Nullable CompoundTag dataFixContextTag, final int targetVersion) {
|
| 51 | int version = NbtUtils.getDataVersion(chunkTag, defaultVersion);
|
| 52 | if (version >= targetVersion) {
|
| 53 | return chunkTag;
|
| 54 | }
|
| 55 |
|
| 56 | try {
|
| 57 | injectDatafixingContext(chunkTag, dataFixContextTag);
|
| 58 | chunkTag = this.dataFixType.update(this.fixerUpper, chunkTag, version, targetVersion);
|
| 59 | removeDatafixingContext(chunkTag);
|
| 60 | NbtUtils.addDataVersion(chunkTag, targetVersion);
|
| 61 | return chunkTag;
|
| 62 | } catch (Exception e) {
|
| 63 | CrashReport report = CrashReport.forThrowable(e, "Updated chunk");
|
| 64 | CrashReportCategory details = report.addCategory("Updated chunk details");
|
| 65 | details.setDetail("Data version", version);
|
| 66 | details.setDetail("Target version", targetVersion);
|
| 67 | throw new ReportedException(report);
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | public CompoundTag upgradeChunkTag(final CompoundTag chunkTag, final int defaultVersion) {
|
| 72 | return this.upgradeChunkTag(chunkTag, defaultVersion, null, SharedConstants.getCurrentVersion().dataVersion().version());
|
| 73 | }
|
| 74 |
|
| 75 | public Dynamic<Tag> upgradeChunkTag(final Dynamic<Tag> chunkTag, final int defaultVersion) {
|
| 76 | return new Dynamic<>(
|
| 77 | chunkTag.getOps(), this.upgradeChunkTag((CompoundTag)chunkTag.getValue(), defaultVersion, null, SharedConstants.getCurrentVersion().dataVersion().version())
|
| 78 | );
|
| 79 | }
|
| 80 |
|
| 81 | public static void injectDatafixingContext(final CompoundTag chunkTag, final @Nullable CompoundTag contextTag) {
|
| 82 | if (contextTag != null) {
|
| 83 | chunkTag.put("__context", contextTag);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | private static void removeDatafixingContext(final CompoundTag chunkTag) {
|
| 88 | chunkTag.remove("__context");
|
| 89 | }
|
| 90 |
|
| 91 | public CompletableFuture<Void> synchronize(final boolean flush) {
|
| 92 | return this.worker.synchronize(flush);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void close() throws IOException {
|
| 97 | this.worker.close();
|
| 98 | }
|
| 99 |
|
| 100 | public ChunkScanAccess chunkScanner() {
|
| 101 | return this.worker;
|
| 102 | }
|
| 103 |
|
| 104 | public RegionStorageInfo storageInfo() {
|
| 105 | return this.worker.storageInfo();
|
| 106 | }
|
| 107 | }
|
| 108 |
|