minecraft-source/src/net/minecraft/world/level/chunk/storage/RegionFileStorage.java

67 lines
2.4 KiB
Java

package net.minecraft.world.level.chunk.storage;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import java.io.DataOutputStream;
import java.io.DataOutput;
import javax.annotation.Nullable;
import java.io.DataInputStream;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.CompoundTag;
import java.io.IOException;
import net.minecraft.world.level.ChunkPos;
import java.io.File;
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
public final class RegionFileStorage implements AutoCloseable {
private final Long2ObjectLinkedOpenHashMap<RegionFile> regionCache;
private final File folder;
RegionFileStorage(final File file) {
this.regionCache = (Long2ObjectLinkedOpenHashMap<RegionFile>)new Long2ObjectLinkedOpenHashMap();
this.folder = file;
}
private RegionFile getRegionFile(final ChunkPos bje) throws IOException {
final long long3 = ChunkPos.asLong(bje.getRegionX(), bje.getRegionZ());
final RegionFile caz5 = (RegionFile)this.regionCache.getAndMoveToFirst(long3);
if (caz5 != null) {
return caz5;
}
if (this.regionCache.size() >= 256) {
((RegionFile)this.regionCache.removeLast()).close();
}
if (!this.folder.exists()) {
this.folder.mkdirs();
}
final File file6 = new File(this.folder, "r." + bje.getRegionX() + "." + bje.getRegionZ() + ".mca");
final RegionFile caz6 = new RegionFile(file6, this.folder);
this.regionCache.putAndMoveToFirst(long3, caz6);
return caz6;
}
@Nullable
public CompoundTag read(final ChunkPos bje) throws IOException {
final RegionFile caz3 = this.getRegionFile(bje);
try (final DataInputStream dataInputStream4 = caz3.getChunkDataInputStream(bje)) {
if (dataInputStream4 == null) {
return null;
}
return NbtIo.read(dataInputStream4);
}
}
protected void write(final ChunkPos bje, final CompoundTag jt) throws IOException {
final RegionFile caz4 = this.getRegionFile(bje);
try (final DataOutputStream dataOutputStream5 = caz4.getChunkDataOutputStream(bje)) {
NbtIo.write(jt, dataOutputStream5);
}
}
@Override
public void close() throws IOException {
for (final RegionFile caz3 : this.regionCache.values()) {
caz3.close();
}
}
}