Skip to content

Commit

Permalink
Include file name in region file initialization exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Jan 13, 2023
1 parent 2db9e54 commit 9f3c7d5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ internal class RegionFile(val world: World, val file: File, val regionX: Int, va
}

fun init() {
if (file.length() != 0L) {
readFile(DataInputStream(file.inputStream()))
try {
if (file.length() != 0L) {
readFile(DataInputStream(file.inputStream()))
}
} catch (e: Exception) {
throw IllegalStateException("Could not initialize region file $file", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ internal class WorldDataStorage(val world: World) {
}

fun getRegion(pos: ChunkPos): RegionFile {
val rx = pos.x shr 5
val rz = pos.z shr 5
val rid = (rx.toLong() shl 32) or (rz.toLong() and 0xFFFFFFFF)

return regionFiles.getOrPut(rid) {
val file = File(regionsFolder, "r.$rx.$rz.nvr")
return@getOrPut RegionFile(this.world, file, rx, rz).apply(RegionFile::init)
try {
val rx = pos.x shr 5
val rz = pos.z shr 5
val rid = (rx.toLong() shl 32) or (rz.toLong() and 0xFFFFFFFF)

return regionFiles.getOrPut(rid) {
val file = File(regionsFolder, "r.$rx.$rz.nvr")
return@getOrPut RegionFile(this.world, file, rx, rz).apply(RegionFile::init)
}
} catch (e: Exception) {
throw IllegalStateException("Could not retrieve RegionFile for $pos", e)
}
}

fun getRegionOrNull(pos: ChunkPos): RegionFile? {
val rx = pos.x shr 5
val rz = pos.z shr 5
val rid = (rx.toLong() shl 32) or (rz.toLong() and 0xFFFFFFFF)

return regionFiles[rid]
}

Expand Down

0 comments on commit 9f3c7d5

Please sign in to comment.