Skip to content

Commit

Permalink
Don't load chunks when accessing blocks via WorldDataManager
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed May 8, 2024
1 parent ee74f6f commit 83cc558
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,44 +108,51 @@ object WorldDataManager : Listener {
}

fun getBlockState(pos: BlockPos): NovaBlockState? =
runBlocking { getOrLoadChunk(pos.chunkPos).getBlockState(pos) }
getChunkOrThrow(pos.chunkPos).getBlockState(pos)

fun setBlockState(pos: BlockPos, state: NovaBlockState?) =
runBlocking { getOrLoadChunk(pos.chunkPos).setBlockState(pos, state) }
getChunkOrThrow(pos.chunkPos).setBlockState(pos, state)

fun getTileEntity(pos: BlockPos): TileEntity? =
runBlocking { getOrLoadChunk(pos.chunkPos).getTileEntity(pos) }
getChunkOrThrow(pos.chunkPos).getTileEntity(pos)

fun getTileEntities(pos: ChunkPos): List<TileEntity> =
runBlocking { getOrLoadChunk(pos).getTileEntities() }
getChunkOrThrow(pos).getTileEntities()

fun getTileEntities(world: World): List<TileEntity> =
runBlocking { getWorldStorage(world).getTileEntities() }
getWorldStorage(world).getTileEntities()

fun getTileEntities(): List<TileEntity> =
runBlocking { worlds.values.flatMap { it.getTileEntities() } }
worlds.values.flatMap { it.getTileEntities() }

fun setTileEntity(pos: BlockPos, tileEntity: TileEntity?): TileEntity? =
runBlocking { getOrLoadChunk(pos.chunkPos).setTileEntity(pos, tileEntity) }
getChunkOrThrow(pos.chunkPos).setTileEntity(pos, tileEntity)

internal fun getVanillaTileEntity(pos: BlockPos): VanillaTileEntity? =
runBlocking { getOrLoadChunk(pos.chunkPos).getVanillaTileEntity(pos) }
getChunkOrThrow(pos.chunkPos).getVanillaTileEntity(pos)

internal fun getVanillaTileEntities(pos: ChunkPos): List<VanillaTileEntity> =
runBlocking { getOrLoadChunk(pos).getVanillaTileEntities() }
getChunkOrThrow(pos).getVanillaTileEntities()

internal fun setVanillaTileEntity(pos: BlockPos, tileEntity: VanillaTileEntity?): VanillaTileEntity? =
getChunkOrThrow(pos.chunkPos).setVanillaTileEntity(pos, tileEntity)

internal fun getVanillaTileEntities(world: World): List<VanillaTileEntity> =
runBlocking { getWorldStorage(world).getVanillaTileEntities() }
getWorldStorage(world).getVanillaTileEntities()

internal fun getVanillaTileEntities(): List<VanillaTileEntity> =
runBlocking { worlds.values.flatMap { it.getVanillaTileEntities() } }

internal fun setVanillaTileEntity(pos: BlockPos, tileEntity: VanillaTileEntity?): VanillaTileEntity? =
runBlocking { getOrLoadChunk(pos.chunkPos).setVanillaTileEntity(pos, tileEntity) }
worlds.values.flatMap { it.getVanillaTileEntities() }

private suspend fun getOrLoadChunk(pos: ChunkPos): RegionChunk =
getOrLoadRegion(pos).getChunk(pos)

private fun getChunkOrThrow(pos: ChunkPos): RegionChunk {
if (!initialized)
throw IllegalStateException("Tried to get chunk before initialization")

return getWorldStorage(pos.world!!).getBlockChunkOrThrow(pos)
}

private suspend fun getOrLoadRegion(pos: ChunkPos): RegionFile {
if (!initialized)
throw IllegalStateException("Tried to load region before initialization")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ internal class WorldDataStorage(val world: World) {
}
}

fun getBlockChunkOrThrow(pos: ChunkPos): RegionChunk =
blockRegionFiles[getRegionId(pos)]?.getCompleted()?.getChunk(pos)
?: throw IllegalStateException("Block chunk at $pos is not loaded")

fun getNetworkChunkOrThrow(pos: ChunkPos): NetworkChunk =
networkRegionFiles[getRegionId(pos)]?.getCompleted()?.getChunk(pos)
?: throw IllegalStateException("Network chunk at $pos is not loaded")
Expand All @@ -87,24 +91,27 @@ internal class WorldDataStorage(val world: World) {
/**
* Gets a snapshot of all loaded [TileEntities][TileEntity] in this world.
*/
suspend fun getTileEntities(): List<TileEntity> =
fun getTileEntities(): List<TileEntity> =
collectFromChunks { it.getTileEntities() }

/**
* Gets a snapshot of all loaded [VanillaTileEntities][VanillaTileEntity] in this world.
*/
suspend fun getVanillaTileEntities(): List<VanillaTileEntity> =
fun getVanillaTileEntities(): List<VanillaTileEntity> =
collectFromChunks { it.getVanillaTileEntities() }

private suspend fun <T> collectFromChunks(collector: (RegionChunk) -> List<T>): List<T> = coroutineScope {
private fun <T> collectFromChunks(collector: (RegionChunk) -> List<T>): List<T> {
val list = ArrayList<T>()
for (regionFile in blockRegionFiles.values) {
for (chunk in regionFile.await().chunks) {
if (!regionFile.isCompleted)
continue

for (chunk in regionFile.getCompleted().chunks) {
list += collector(chunk)
}
}

return@coroutineScope list
return list
}

/**
Expand Down

0 comments on commit 83cc558

Please sign in to comment.