cache chunk in BlockStateInterface

This commit is contained in:
Leijurv 2018-08-31 11:51:43 -07:00
parent 836b41a3aa
commit 0ee93aea23
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
1 changed files with 11 additions and 0 deletions

View File

@ -31,6 +31,7 @@ import net.minecraft.world.chunk.Chunk;
public class BlockStateInterface implements Helper {
public static int numBlockStateLookups = 0;
private static Chunk prev = null;
public static IBlockState get(BlockPos pos) { // wrappers for chunk caching capability
numBlockStateLookups++;
@ -39,8 +40,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);
}
}