This commit is contained in:
Leijurv 2018-08-05 09:30:42 -04:00
parent 6b0bc568de
commit 2769f93f71
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 26 additions and 26 deletions

View File

@ -3,13 +3,15 @@ package baritone.bot.chunk;
import baritone.bot.pathing.util.PathingBlockType; import baritone.bot.pathing.util.PathingBlockType;
import baritone.bot.utils.GZIPUtils; import baritone.bot.utils.GZIPUtils;
import java.io.ByteArrayOutputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.BitSet; import java.util.BitSet;
import java.util.zip.GZIPOutputStream;
/** /**
* @author Brady * @author Brady
@ -19,7 +21,7 @@ public final class CachedRegion implements ICachedChunkAccess {
/** /**
* All of the chunks in this region. A 16x16 array of them. * All of the chunks in this region. A 16x16 array of them.
* * <p>
* I would make these 32x32 regions to be in line with the Anvil format, but 16 is a nice number. * I would make these 32x32 regions to be in line with the Anvil format, but 16 is a nice number.
*/ */
private final CachedChunk[][] chunks = new CachedChunk[32][32]; private final CachedChunk[][] chunks = new CachedChunk[32][32];
@ -67,28 +69,24 @@ public final class CachedRegion implements ICachedChunkAccess {
if (!Files.exists(path)) if (!Files.exists(path))
Files.createDirectories(path); Files.createDirectories(path);
ByteArrayOutputStream bos = new ByteArrayOutputStream(32 * 32 * CachedChunk.SIZE_IN_BYTES);
for (int z = 0; z < 32; z++) {
for (int x = 0; x < 32; x++) {
CachedChunk chunk = this.chunks[x][z];
if (chunk == null) {
bos.write(CachedChunk.EMPTY_CHUNK);
} else {
byte[] chunkBytes = chunk.toByteArray();
bos.write(chunkBytes);
// Messy, but fills the empty 0s that should be trailing to fill up the space.
bos.write(new byte[CachedChunk.SIZE_IN_BYTES - chunkBytes.length]);
}
}
}
Path regionFile = getRegionFile(path, this.x, this.z); Path regionFile = getRegionFile(path, this.x, this.z);
if (!Files.exists(regionFile)) if (!Files.exists(regionFile))
Files.createFile(regionFile); Files.createFile(regionFile);
try (FileOutputStream fileOut = new FileOutputStream(regionFile.toFile()); GZIPOutputStream out = new GZIPOutputStream(fileOut)) {
byte[] compressed = GZIPUtils.compress(bos.toByteArray()); for (int z = 0; z < 32; z++) {
if (compressed != null) for (int x = 0; x < 32; x++) {
Files.write(regionFile, compressed); CachedChunk chunk = this.chunks[x][z];
if (chunk == null) {
out.write(CachedChunk.EMPTY_CHUNK);
} else {
byte[] chunkBytes = chunk.toByteArray();
out.write(chunkBytes);
// Messy, but fills the empty 0s that should be trailing to fill up the space.
out.write(new byte[CachedChunk.SIZE_IN_BYTES - chunkBytes.length]);
}
}
}
}
} catch (IOException ignored) {} } catch (IOException ignored) {}
} }
@ -101,9 +99,11 @@ public final class CachedRegion implements ICachedChunkAccess {
Path regionFile = getRegionFile(path, this.x, this.z); Path regionFile = getRegionFile(path, this.x, this.z);
if (!Files.exists(regionFile)) if (!Files.exists(regionFile))
return; return;
byte[] decompressed;
try (FileInputStream in = new FileInputStream(regionFile.toFile())) {
decompressed = GZIPUtils.decompress(in);
}
byte[] fileBytes = Files.readAllBytes(regionFile);
byte[] decompressed = GZIPUtils.decompress(fileBytes);
if (decompressed == null) if (decompressed == null)
return; return;

View File

@ -1,8 +1,8 @@
package baritone.bot.utils; package baritone.bot.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@ -24,10 +24,10 @@ public final class GZIPUtils {
return byteStream.toByteArray(); return byteStream.toByteArray();
} }
public static byte[] decompress(byte[] in) throws IOException { public static byte[] decompress(InputStream in) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream();
try (GZIPInputStream gzipStream = new GZIPInputStream(new ByteArrayInputStream(in))) { try (GZIPInputStream gzipStream = new GZIPInputStream(in)) {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int len; int len;
while ((len = gzipStream.read(buffer)) > 0) { while ((len = gzipStream.read(buffer)) > 0) {