From a93af3404bfa555e087e3e890c6a92801557709f Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 31 Aug 2018 11:51:43 -0700 Subject: [PATCH] cache chunk in BlockStateInterface, fixes #113 --- .../java/baritone/utils/BlockStateInterface.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/baritone/utils/BlockStateInterface.java b/src/main/java/baritone/utils/BlockStateInterface.java index a1013d7f..f21ddc50 100644 --- a/src/main/java/baritone/utils/BlockStateInterface.java +++ b/src/main/java/baritone/utils/BlockStateInterface.java @@ -30,6 +30,8 @@ import net.minecraft.world.chunk.Chunk; public class BlockStateInterface implements Helper { + private static Chunk prev = null; + public static IBlockState get(BlockPos pos) { // wrappers for chunk caching capability // Invalid vertical position @@ -37,8 +39,18 @@ public class BlockStateInterface implements Helper { return Blocks.AIR.getDefaultState(); if (!Baritone.settings().pathThroughCachedOnly.get()) { + Chunk cached = prev; + // there's great cache locality in block state lookups + // generally it's within each movement + // if it's the same chunk as last time + // we can just skip the mc.world.getChunk lookup + // which is a Long2ObjectOpenHashMap.get + if (cached != null && cached.x == pos.getX() >> 4 && cached.z == pos.getZ() >> 4) { + return cached.getBlockState(pos); + } Chunk chunk = mc.world.getChunk(pos); if (chunk.isLoaded()) { + prev = chunk; return chunk.getBlockState(pos); } }