do not access TileEntities in unloaded chunks

This commit is contained in:
octarine-noise
2019-09-03 14:24:26 +02:00
parent d96ac1c94c
commit 6801304bd1
3 changed files with 29 additions and 2 deletions

View File

@@ -11,6 +11,8 @@ import mods.betterfoliage.loader.Refs
import mods.octarinecore.client.resource.ModelProcessor import mods.octarinecore.client.resource.ModelProcessor
import mods.octarinecore.client.resource.ModelVariant import mods.octarinecore.client.resource.ModelVariant
import mods.octarinecore.client.resource.registerSprite import mods.octarinecore.client.resource.registerSprite
import mods.octarinecore.getTileEntitySafe
import mods.octarinecore.isBlockLoaded
import mods.octarinecore.metaprog.ClassRef import mods.octarinecore.metaprog.ClassRef
import mods.octarinecore.metaprog.FieldRef import mods.octarinecore.metaprog.FieldRef
import mods.octarinecore.metaprog.MethodRef import mods.octarinecore.metaprog.MethodRef
@@ -117,7 +119,7 @@ object ForestryLeavesSupport : ILeafRegistry {
} }
// extract leaf texture information from TileEntity // extract leaf texture information from TileEntity
val tile = tryDefault(null) { world.getTileEntity(pos) } ?: return null val tile = world.getTileEntitySafe(pos) ?: return null
if (!ForestryIntegration.TileLeaves.isInstance(tile)) return null if (!ForestryIntegration.TileLeaves.isInstance(tile)) return null
val textureLoc = ForestryIntegration.TiLgetLeaveSprite.invoke(tile, Minecraft.isFancyGraphicsEnabled()) ?: return null val textureLoc = ForestryIntegration.TiLgetLeaveSprite.invoke(tile, Minecraft.isFancyGraphicsEnabled()) ?: return null
return textureToValue[textureLoc] return textureToValue[textureLoc]

View File

@@ -2,7 +2,12 @@
@file:Suppress("NOTHING_TO_INLINE") @file:Suppress("NOTHING_TO_INLINE")
package mods.octarinecore package mods.octarinecore
import net.minecraft.tileentity.TileEntity
import net.minecraft.util.ResourceLocation import net.minecraft.util.ResourceLocation
import net.minecraft.util.math.BlockPos
import net.minecraft.world.ChunkCache
import net.minecraft.world.IBlockAccess
import net.minecraft.world.World
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
import java.lang.Math.* import java.lang.Math.*
@@ -92,4 +97,22 @@ fun Int.minmax(minVal: Int, maxVal: Int) = min(max(this, minVal), maxVal)
fun nextPowerOf2(x: Int): Int { fun nextPowerOf2(x: Int): Int {
return 1 shl (if (x == 0) 0 else 32 - Integer.numberOfLeadingZeros(x - 1)) return 1 shl (if (x == 0) 0 else 32 - Integer.numberOfLeadingZeros(x - 1))
}
/**
* Check if the Chunk containing the given [BlockPos] is loaded.
* Works for both [World] and [ChunkCache] instances.
*/
fun IBlockAccess.isBlockLoaded(pos: BlockPos) = when(this) {
is World -> isBlockLoaded(pos, false)
is ChunkCache -> world.isBlockLoaded(pos, false)
else -> false
}
/**
* 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) {
if (isBlockLoaded(pos)) getTileEntity(pos) else null
} }

View File

@@ -10,4 +10,6 @@ public net.minecraft.client.renderer.BufferBuilder field_178999_b # rawIntBuffer
public net.minecraft.client.renderer.BufferBuilder func_181670_b(I)V # growBuffer public net.minecraft.client.renderer.BufferBuilder func_181670_b(I)V # growBuffer
public net.minecraft.client.renderer.BufferBuilder func_181664_j()I # getBufferSize public net.minecraft.client.renderer.BufferBuilder func_181664_j()I # getBufferSize
public net.minecraft.client.renderer.BlockModelRenderer field_187499_a # blockColors public net.minecraft.client.renderer.BlockModelRenderer field_187499_a # blockColors
public net.minecraft.world.ChunkCache field_72815_e # world