RegionFileStorage .java

net.minecraft.world.level.chunk.storage.RegionFileStorage (class): members: ANVIL_EXTENSION, MAX_CACHE_SIZE, regionCache, sync, scanChunk, folder, flush.

net.minecraft.world.level.chunk.storageclass142 linescommon4.2 KB

Implements

API Surface (5)

void scanChunk(final ChunkPos pos, final StreamTagVisitor scanner)
void write(final ChunkPos pos, final @Nullable CompoundTag value)
void close()
void flush()
RegionStorageInfo info()

Fields & Constants (6)

ANVIL_EXTENSION
MAX_CACHE_SIZE
regionCache
info
folder
sync

String literals (1)

".mca"

Full Source

RegionFileStorage.java142 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.chunk.storage;
2
3
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
4
import java.io.DataInputStream;
5
import java.io.DataOutputStream;
6
import java.io.IOException;
7
import java.nio.file.Files;
8
import java.nio.file.Path;
9
import java.util.Objects;
10
import java.util.Optional;
11
import net.minecraft.SharedConstants;
12
import net.minecraft.nbt.CompoundTag;
13
import net.minecraft.nbt.NbtAccounter;
14
import net.minecraft.nbt.NbtIo;
15
import net.minecraft.nbt.StreamTagVisitor;
16
import net.minecraft.util.ExceptionCollector;
17
import net.minecraft.util.FileUtil;
18
import net.minecraft.world.level.ChunkPos;
19
import org.jspecify.annotations.Nullable;
20
21
public final class RegionFileStorage implements AutoCloseable {
22
	public static final String ANVIL_EXTENSION = ".mca";
23
	private static final int MAX_CACHE_SIZE = 256;
24
	private final Long2ObjectLinkedOpenHashMap<Optional<RegionFile>> regionCache = new Long2ObjectLinkedOpenHashMap<>();
25
	private final RegionStorageInfo info;
26
	private final Path folder;
27
	private final boolean sync;
28
29
	public RegionFileStorage(final RegionStorageInfo info, final Path folder, final boolean sync) {
30
		this.folder = folder;
31
		this.sync = sync;
32
		this.info = info;
33
	}
34
35
	private Path regionPath(final ChunkPos pos) {
36
		return this.folder.resolve("r." + pos.getRegionX() + "." + pos.getRegionZ() + ".mca");
37
	}
38
39
	private @Nullable RegionFile getRegionFile(final ChunkPos pos, final boolean create) throws IOException {
40
		long key = ChunkPos.pack(pos.getRegionX(), pos.getRegionZ());
41
		Optional<RegionFile> cached = this.regionCache.getAndMoveToFirst(key);
42
		if (cached != null) {
43
			if (cached.isPresent()) {
44
				return cached.get();
45
			}
46
47
			if (!create) {
48
				return null;
49
			}
50
		}
51
52
		Path path = this.regionPath(pos);
53
		if (!create && !Files.isRegularFile(path)) {
54
			this.cache(key, Optional.empty());
55
			return null;
56
		} else {
57
			FileUtil.createDirectoriesSafe(this.folder);
58
			RegionFile newRegion = new RegionFile(this.info, path, this.folder, this.sync);
59
			this.cache(key, Optional.of(newRegion));
60
			return newRegion;
61
		}
62
	}
63
64
	private RegionFile getOrCreateRegionFile(final ChunkPos pos) throws IOException {
65
		return Objects.requireNonNull(this.getRegionFile(pos, true));
66
	}
67
68
	private void cache(final long key, final Optional<RegionFile> entry) throws IOException {
69
		this.regionCache.putAndMoveToFirst(key, entry);
70
		if (this.regionCache.size() > 256) {
71
			Optional<RegionFile> evicted = this.regionCache.removeLast();
72
			if (evicted.isPresent()) {
73
				evicted.get().close();
74
			}
75
		}
76
	}
77
78
	public @Nullable CompoundTag read(final ChunkPos pos) throws IOException {
79
		RegionFile region = this.getRegionFile(pos, false);
80
		if (region == null) {
81
			return null;
82
		}
83
84
		try (DataInputStream regionChunkInputStream = region.getChunkDataInputStream(pos)) {
85
			return regionChunkInputStream == null ? null : NbtIo.read(regionChunkInputStream);
86
		}
87
	}
88
89
	public void scanChunk(final ChunkPos pos, final StreamTagVisitor scanner) throws IOException {
90
		RegionFile region = this.getRegionFile(pos, false);
91
		if (region != null) {
92
			try (DataInputStream regionChunkInputStream = region.getChunkDataInputStream(pos)) {
93
				if (regionChunkInputStream != null) {
94
					NbtIo.parse(regionChunkInputStream, scanner, NbtAccounter.unlimitedHeap());
95
				}
96
			}
97
		}
98
	}
99
100
	public void write(final ChunkPos pos, final @Nullable CompoundTag value) throws IOException {
101
		if (!SharedConstants.DEBUG_DONT_SAVE_WORLD) {
102
			RegionFile region = this.getOrCreateRegionFile(pos);
103
			if (value == null) {
104
				region.clear(pos);
105
			} else {
106
				try (DataOutputStream output = region.getChunkDataOutputStream(pos)) {
107
					NbtIo.write(value, output);
108
				}
109
			}
110
		}
111
	}
112
113
	@Override
114
	public void close() throws IOException {
115
		ExceptionCollector<IOException> exception = new ExceptionCollector<>();
116
117
		for (Optional<RegionFile> entry : this.regionCache.values()) {
118
			if (!entry.isEmpty()) {
119
				try {
120
					entry.get().close();
121
				} catch (IOException e) {
122
					exception.add(e);
123
				}
124
			}
125
		}
126
127
		exception.throwIfPresent();
128
	}
129
130
	public void flush() throws IOException {
131
		for (Optional<RegionFile> entry : this.regionCache.values()) {
132
			if (entry.isPresent()) {
133
				entry.get().flush();
134
			}
135
		}
136
	}
137
138
	public RegionStorageInfo info() {
139
		return this.info;
140
	}
141
}
142

Decompiled Minecraft 26.3 source of RegionFileStorage (net/minecraft/world/level/chunk/storage/RegionFileStorage.java) with Mojang official mappings, stored verbatim on the site.