Refactor region file loading for better nullability analysis

This commit is contained in:
t00thpick1 2021-01-02 18:57:51 -05:00
parent 26ef4cc411
commit 00a6d52717

View File

@ -40,7 +40,7 @@ public class HashChunkManager implements ChunkManager {
} }
private synchronized @Nullable ChunkStore readChunkStore(@NotNull World world, int cx, int cz) throws IOException { private synchronized @Nullable ChunkStore readChunkStore(@NotNull World world, int cx, int cz) throws IOException {
McMMOSimpleRegionFile rf = getSimpleRegionFile(world, cx, cz, false); McMMOSimpleRegionFile rf = getReadableSimpleRegionFile(world, cx, cz);
if (rf == null) if (rf == null)
return null; // If there is no region file, there can't be a chunk return null; // If there is no region file, there can't be a chunk
try (DataInputStream in = rf.getInputStream(cx, cz)) { // Get input stream for chunk try (DataInputStream in = rf.getInputStream(cx, cz)) { // Get input stream for chunk
@ -54,7 +54,7 @@ public class HashChunkManager implements ChunkManager {
if (!data.isDirty()) if (!data.isDirty())
return; // Don't save unchanged data return; // Don't save unchanged data
try { try {
McMMOSimpleRegionFile rf = getSimpleRegionFile(world, data.getChunkX(), data.getChunkZ(), true); McMMOSimpleRegionFile rf = getWriteableSimpleRegionFile(world, data.getChunkX(), data.getChunkZ());
try (DataOutputStream out = rf.getOutputStream(data.getChunkX(), data.getChunkZ())) { try (DataOutputStream out = rf.getOutputStream(data.getChunkX(), data.getChunkZ())) {
BitSetChunkStore.Serialization.writeChunkStore(out, data); BitSetChunkStore.Serialization.writeChunkStore(out, data);
} }
@ -65,21 +65,33 @@ public class HashChunkManager implements ChunkManager {
} }
} }
private synchronized @Nullable McMMOSimpleRegionFile getSimpleRegionFile(@NotNull World world, int cx, int cz, boolean createIfAbsent) { private synchronized @NotNull McMMOSimpleRegionFile getWriteableSimpleRegionFile(@NotNull World world, int cx, int cz) {
CoordinateKey regionKey = toRegionKey(world.getUID(), cx, cz); CoordinateKey regionKey = toRegionKey(world.getUID(), cx, cz);
return regionMap.computeIfAbsent(regionKey, k -> { return regionMap.computeIfAbsent(regionKey, k -> {
File worldRegionsDirectory = new File(world.getWorldFolder(), "mcmmo_regions"); File regionFile = getRegionFile(world, regionKey);
if (!createIfAbsent && !worldRegionsDirectory.isDirectory()) regionFile.getParentFile().mkdirs();
return null; // Don't create the directory on read-only operations return new McMMOSimpleRegionFile(regionFile, regionKey.x, regionKey.z);
worldRegionsDirectory.mkdirs(); // Ensure directory exists });
File regionFile = new File(worldRegionsDirectory, "mcmmo_" + regionKey.x + "_" + regionKey.z + "_.mcm"); }
if (!createIfAbsent && !regionFile.exists())
private synchronized @Nullable McMMOSimpleRegionFile getReadableSimpleRegionFile(@NotNull World world, int cx, int cz) {
CoordinateKey regionKey = toRegionKey(world.getUID(), cx, cz);
return regionMap.computeIfAbsent(regionKey, k -> {
File regionFile = getRegionFile(world, regionKey);
if (!regionFile.exists())
return null; // Don't create the file on read-only operations return null; // Don't create the file on read-only operations
return new McMMOSimpleRegionFile(regionFile, regionKey.x, regionKey.z); return new McMMOSimpleRegionFile(regionFile, regionKey.x, regionKey.z);
}); });
} }
private @NotNull File getRegionFile(@NotNull World world, @NotNull CoordinateKey regionKey) {
if (world.getUID() != regionKey.worldID)
throw new IllegalArgumentException();
return new File(new File(world.getWorldFolder(), "mcmmo_regions"), "mcmmo_" + regionKey.x + "_" + regionKey.z + "_.mcm");
}
private @Nullable ChunkStore loadChunk(int cx, int cz, @NotNull World world) { private @Nullable ChunkStore loadChunk(int cx, int cz, @NotNull World world) {
try { try {
return readChunkStore(world, cx, cz); return readChunkStore(world, cx, cz);