diff --git a/src/main/kotlin/mods/betterfoliage/client/integration/ForestryIntegration.kt b/src/main/kotlin/mods/betterfoliage/client/integration/ForestryIntegration.kt index 685272e..03ae17f 100644 --- a/src/main/kotlin/mods/betterfoliage/client/integration/ForestryIntegration.kt +++ b/src/main/kotlin/mods/betterfoliage/client/integration/ForestryIntegration.kt @@ -11,6 +11,8 @@ import mods.betterfoliage.loader.Refs import mods.octarinecore.client.resource.ModelProcessor import mods.octarinecore.client.resource.ModelVariant import mods.octarinecore.client.resource.registerSprite +import mods.octarinecore.getTileEntitySafe +import mods.octarinecore.isBlockLoaded import mods.octarinecore.metaprog.ClassRef import mods.octarinecore.metaprog.FieldRef import mods.octarinecore.metaprog.MethodRef @@ -117,7 +119,7 @@ object ForestryLeavesSupport : ILeafRegistry { } // 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 val textureLoc = ForestryIntegration.TiLgetLeaveSprite.invoke(tile, Minecraft.isFancyGraphicsEnabled()) ?: return null return textureToValue[textureLoc] diff --git a/src/main/kotlin/mods/octarinecore/Utils.kt b/src/main/kotlin/mods/octarinecore/Utils.kt index 1a58a3f..0b6cf14 100644 --- a/src/main/kotlin/mods/octarinecore/Utils.kt +++ b/src/main/kotlin/mods/octarinecore/Utils.kt @@ -2,7 +2,12 @@ @file:Suppress("NOTHING_TO_INLINE") package mods.octarinecore +import net.minecraft.tileentity.TileEntity 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 java.lang.Math.* @@ -92,4 +97,22 @@ fun Int.minmax(minVal: Int, maxVal: Int) = min(max(this, minVal), maxVal) fun nextPowerOf2(x: Int): Int { 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 } \ No newline at end of file diff --git a/src/main/resources/META-INF/BetterFoliage_at.cfg b/src/main/resources/META-INF/BetterFoliage_at.cfg index 47787ed..a856162 100644 --- a/src/main/resources/META-INF/BetterFoliage_at.cfg +++ b/src/main/resources/META-INF/BetterFoliage_at.cfg @@ -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_181664_j()I # getBufferSize -public net.minecraft.client.renderer.BlockModelRenderer field_187499_a # blockColors \ No newline at end of file +public net.minecraft.client.renderer.BlockModelRenderer field_187499_a # blockColors + +public net.minecraft.world.ChunkCache field_72815_e # world \ No newline at end of file