добавлен RegionManager
This commit is contained in:
44
anvil-loader/src/main/java/mc/world/anvil/RegionManager.java
Normal file
44
anvil-loader/src/main/java/mc/world/anvil/RegionManager.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user