From 307214251375ab411861fbb733550969e88343cc Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 5 Aug 2018 10:06:20 -0400 Subject: [PATCH] don't read empty chunks --- .../java/baritone/bot/chunk/CachedRegion.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/baritone/bot/chunk/CachedRegion.java b/src/main/java/baritone/bot/chunk/CachedRegion.java index baa3f771..ebee5808 100644 --- a/src/main/java/baritone/bot/chunk/CachedRegion.java +++ b/src/main/java/baritone/bot/chunk/CachedRegion.java @@ -53,10 +53,11 @@ public final class CachedRegion implements ICachedChunkAccess { @Override public final void updateCachedChunk(int chunkX, int chunkZ, BitSet data) { CachedChunk chunk = this.getChunk(chunkX, chunkZ); - if (chunk == null) + if (chunk == null) { this.chunks[chunkX][chunkZ] = new CachedChunk(chunkX, chunkZ, data); - else + } else { chunk.updateContents(data); + } } private CachedChunk getChunk(int chunkX, int chunkZ) { @@ -99,6 +100,7 @@ public final class CachedRegion implements ICachedChunkAccess { Path regionFile = getRegionFile(path, this.x, this.z); if (!Files.exists(regionFile)) return; + byte[] decompressed; try (FileInputStream in = new FileInputStream(regionFile.toFile())) { decompressed = GZIPUtils.decompress(in); @@ -109,18 +111,28 @@ public final class CachedRegion implements ICachedChunkAccess { for (int z = 0; z < 32; z++) { for (int x = 0; x < 32; x++) { - CachedChunk chunk = this.chunks[x][z]; - if (chunk != null) { - int index = (x + (z << 5)) * CachedChunk.SIZE_IN_BYTES; - byte[] bytes = Arrays.copyOfRange(decompressed, index, index + CachedChunk.SIZE_IN_BYTES); + int index = (x + (z << 5)) * CachedChunk.SIZE_IN_BYTES; + byte[] bytes = Arrays.copyOfRange(decompressed, index, index + CachedChunk.SIZE_IN_BYTES); + if (isAllZeros(bytes)) { + this.chunks[x][z] = null; + } else { BitSet bits = BitSet.valueOf(bytes); - chunk.updateContents(bits); + updateCachedChunk(x, z, bits); } } } } catch (IOException ignored) {} } + private static boolean isAllZeros(final byte[] array) { + for (byte b : array) { + if (b != 0) { + return false; + } + } + return true; + } + /** * @return The region x coordinate */