Archived
0

добавлен RegionManager

This commit is contained in:
2018-10-18 01:29:32 +03:00
parent 5dff4bbe6a
commit ec275caf54

View File

@@ -0,0 +1,44 @@
package mc.world.anvil;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import lombok.extern.slf4j.Slf4j;
import mc.core.utils.CompactedCoords;
import org.springframework.lang.Nullable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@Slf4j
class RegionManager {
private final Path regionFilesPath;
private final TIntObjectMap<Region> regions = new TIntObjectHashMap<>();
RegionManager(Path regionFilesPath) {
this.regionFilesPath = regionFilesPath;
}
@Nullable
public Region getRegion(int x, int z) {
final int xz = CompactedCoords.compressXZ(x, z);
if (regions.containsKey(xz)) {
return regions.get(xz);
} else {
Path regionFilePath = regionFilesPath.resolve("r." + x + "." + z + ".mca");
if (Files.exists(regionFilePath)) {
try {
Region region = new Region(regionFilePath.toFile());
regions.put(xz, region);
return region;
} catch (IOException e) {
log.error("load region from file", e);
return null;
}
} else {
return null;
}
}
}
}