Cache round log neighborhood data for faster chunk re-rendering

This commit is contained in:
octarine-noise
2019-12-21 15:08:29 +01:00
parent d0265483d2
commit d9cc03511a
6 changed files with 274 additions and 75 deletions

View File

@@ -2,6 +2,7 @@
@file:Suppress("NOTHING_TO_INLINE")
package mods.octarinecore
import mods.betterfoliage.loader.Refs
import net.minecraft.tileentity.TileEntity
import net.minecraft.util.ResourceLocation
import net.minecraft.util.math.BlockPos
@@ -101,11 +102,12 @@ fun nextPowerOf2(x: Int): Int {
/**
* Check if the Chunk containing the given [BlockPos] is loaded.
* Works for both [World] and [ChunkCache] instances.
* Works for both [World] and [ChunkCache] (vanilla and OptiFine) instances.
*/
fun IBlockAccess.isBlockLoaded(pos: BlockPos) = when(this) {
is World -> isBlockLoaded(pos, false)
is ChunkCache -> world.isBlockLoaded(pos, false)
fun IBlockAccess.isBlockLoaded(pos: BlockPos) = when {
this is World -> isBlockLoaded(pos, false)
this is ChunkCache -> world.isBlockLoaded(pos, false)
Refs.OptifineChunkCache.isInstance(this) -> (Refs.CCOFChunkCache.get(this) as ChunkCache).world.isBlockLoaded(pos, false)
else -> false
}
@@ -113,6 +115,6 @@ fun IBlockAccess.isBlockLoaded(pos: BlockPos) = when(this) {
* Get the [TileEntity] at the given position, suppressing exceptions.
* Also returns null if the chunk is unloaded, which can happen because of multithreaded rendering.
*/
fun IBlockAccess.getTileEntitySafe(pos: BlockPos): TileEntity? = tryDefault(null) {
fun IBlockAccess.getTileEntitySafe(pos: BlockPos): TileEntity? = tryDefault(null as TileEntity?) {
if (isBlockLoaded(pos)) getTileEntity(pos) else null
}

View File

@@ -71,10 +71,10 @@ data class BlockData(val state: IBlockState, val color: Int, val brightness: Int
* Represents the block being rendered. Has properties and methods to query the neighborhood of the block in
* block-relative coordinates.
*/
class BlockContext {
var world: IBlockAccess? = null
var pos = BlockPos.ORIGIN
class BlockContext(
var world: IBlockAccess? = null,
var pos: BlockPos = BlockPos.ORIGIN
) {
fun set(world: IBlockAccess, pos: BlockPos) { this.world = world; this.pos = pos; }
val block: Block get() = block(Int3.zero)