Files
2019-02-24 22:29:41 +08:00

91 lines
3.4 KiB
Diff

--- ../src-base/minecraft/net/minecraft/world/chunk/storage/RegionFileCache.java
+++ ../src-work/minecraft/net/minecraft/world/chunk/storage/RegionFileCache.java
@@ -1,15 +1,21 @@
package net.minecraft.world.chunk.storage;
import com.google.common.collect.Maps;
+import net.minecraft.nbt.CompressedStreamTools;
+import net.minecraft.nbt.NBTTagCompound;
+
+import javax.annotation.Nullable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.Map;
public class RegionFileCache
{
- private static final Map<File, RegionFile> REGIONS_BY_FILE = Maps.<File, RegionFile>newHashMap();
+ public static final Map<File, RegionFile> REGIONS_BY_FILE = new LinkedHashMap(256, 0.75f, true); // Spigot - private -> public, Paper - HashMap -> LinkedHashMap
public static synchronized RegionFile createOrLoadRegionFile(File worldDir, int chunkX, int chunkZ)
{
@@ -30,7 +36,7 @@
if (REGIONS_BY_FILE.size() >= 256)
{
- clearRegionFileReferences();
+ trimCache(); // Paper
}
RegionFile regionfile1 = new RegionFile(file2);
@@ -39,6 +45,21 @@
}
}
+ // Paper Start
+ private static synchronized void trimCache() {
+ Iterator<Map.Entry<File, RegionFile>> itr = RegionFileCache.REGIONS_BY_FILE.entrySet().iterator();
+ int count = RegionFileCache.REGIONS_BY_FILE.size() - 256;
+ while (count-- >= 0 && itr.hasNext()) {
+ try {
+ itr.next().getValue().close();
+ } catch (IOException ioexception) {
+ ioexception.printStackTrace();
+ }
+ itr.remove();
+ }
+ }
+ // Paper End
+
public static synchronized RegionFile getRegionFileIfExists(File worldDir, int chunkX, int chunkZ)
{
File file1 = new File(worldDir, "region");
@@ -92,12 +113,33 @@
return regionfile.getChunkDataInputStream(chunkX & 31, chunkZ & 31);
}
+ @Nullable
+ public static synchronized NBTTagCompound getChunkInputStreamCB(File worldDir, int chunkX, int chunkZ) throws IOException
+ {
+ RegionFile regionfile = createOrLoadRegionFile(worldDir, chunkX, chunkZ);
+ DataInputStream datainputstream = regionfile.getChunkDataInputStream(chunkX & 31, chunkZ & 31);
+
+ if (datainputstream == null) {
+ return null;
+ }
+
+ return CompressedStreamTools.read(datainputstream);
+ }
+
public static DataOutputStream getChunkOutputStream(File worldDir, int chunkX, int chunkZ)
{
RegionFile regionfile = createOrLoadRegionFile(worldDir, chunkX, chunkZ);
return regionfile.getChunkDataOutputStream(chunkX & 31, chunkZ & 31);
}
+ public static synchronized void getChunkOutputStream(File worldDir, int chunkX, int chunkZ, NBTTagCompound nbttagcompound) throws IOException
+ {
+ RegionFile regionfile = createOrLoadRegionFile(worldDir, chunkX, chunkZ);
+ DataOutputStream dataoutputstream = regionfile.getChunkDataOutputStream(chunkX & 31, chunkZ & 31);
+ CompressedStreamTools.write(nbttagcompound, dataoutputstream);
+ dataoutputstream.close();
+ }
+
public static boolean chunkExists(File worldDir, int chunkX, int chunkZ)
{
RegionFile regionfile = getRegionFileIfExists(worldDir, chunkX, chunkZ);