414 lines
17 KiB
Java
414 lines
17 KiB
Java
package baritone.api.utils;
|
|
import intent.zeroday.util.BitSetHelper;
|
|
import intent.zeroday.util.BlockUtilHelper;
|
|
import intent.zeroday.util.IBlockStateHelper;
|
|
|
|
import java.util.Collection;
|
|
|
|
import baritone.pathing.BaritoneCore;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.util.ArrayList;
|
|
import net.minecraft.block.state.IBlockState;
|
|
import net.minecraft.util.BlockPos;
|
|
import java.io.DataInputStream;
|
|
import java.io.DataOutputStream;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.LinkOption;
|
|
import java.nio.file.attribute.FileAttribute;
|
|
import java.util.BitSet;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.zip.GZIPInputStream;
|
|
import java.util.zip.GZIPOutputStream;
|
|
import net.minecraft.block.Block;
|
|
|
|
public final class BaritoneBitSetHelper implements IBlockStateHelper {
|
|
private static int count;
|
|
private transient boolean hasPosY;
|
|
private static byte stateByte;
|
|
private transient int count2;
|
|
private transient int count3;
|
|
private transient BitSetHelper[][] bitSetHelperArray = new BitSetHelper[32][32];
|
|
private static byte stateByte2;
|
|
private transient int count4;
|
|
|
|
public final synchronized void syncStorage() {
|
|
|
|
long value = BaritoneCore.getBaritoneSettings().cachedChunksExpirySeconds.value;
|
|
if (value >= 0L) {
|
|
long timeMs = System.currentTimeMillis();
|
|
long timestampMs = timeMs - value * 1000L;
|
|
|
|
for (int idx = 0; idx < 32; idx++) {
|
|
for (int idx2 = 0; idx2 < 32; idx2++) {
|
|
if (this.bitSetHelperArray[idx][idx2] != null && this.bitSetHelperArray[idx][idx2].lastTimeMs < timestampMs) {
|
|
System.out
|
|
.println(
|
|
"Removing chunk "
|
|
+ (idx + 32 * this.count2)
|
|
+ ","
|
|
+ (idx2 + 32 * this.count3)
|
|
+ " because it was cached "
|
|
+ (timeMs - this.bitSetHelperArray[idx][idx2].lastTimeMs) / 1000L
|
|
+ " seconds ago, and max age is "
|
|
+ value
|
|
);
|
|
this.bitSetHelperArray[idx][idx2] = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public final synchronized void writeString(String str) {
|
|
if (!this.hasPosY) {
|
|
return;
|
|
}
|
|
this.syncStorage();
|
|
try {
|
|
Path path = Paths.get(str, new String[0]);
|
|
if (!Files.exists(path, new LinkOption[0])) {
|
|
Files.createDirectories(path, new FileAttribute[0]);
|
|
}
|
|
System.out.println("Saving region " + this.count2 + "," + this.count3 + " to disk " + path);
|
|
Path path2 = BaritoneBitSetHelper.transformPath(path, this.count2, this.count3);
|
|
if (!Files.exists(path2, new LinkOption[0])) {
|
|
Files.createFile(path2, new FileAttribute[0]);
|
|
}
|
|
Throwable throwable = null;
|
|
Object target = null;
|
|
try {
|
|
Object target2;
|
|
int value;
|
|
FileOutputStream fileOutputStream = new FileOutputStream(path2.toFile());
|
|
GZIPOutputStream gZIPOutputStream = new GZIPOutputStream((OutputStream)fileOutputStream, 16384);
|
|
DataOutputStream dataOutputStream = new DataOutputStream(gZIPOutputStream);
|
|
dataOutputStream.writeInt(456022910);
|
|
int value2 = 0;
|
|
while (value2 < 32) {
|
|
value = 0;
|
|
while (value < 32) {
|
|
target2 = this.bitSetHelperArray[value2][value];
|
|
if (target2 == null) {
|
|
dataOutputStream.write(0);
|
|
} else {
|
|
dataOutputStream.write(1);
|
|
byte[] byteArray = ((BitSetHelper)target2).getByteArray();
|
|
dataOutputStream.write(byteArray);
|
|
dataOutputStream.write(new byte[16384 - byteArray.length]);
|
|
}
|
|
++value;
|
|
}
|
|
++value2;
|
|
}
|
|
value2 = 0;
|
|
while (value2 < 32) {
|
|
value = 0;
|
|
while (value < 32) {
|
|
if (this.bitSetHelperArray[value2][value] != null) {
|
|
int value3 = 0;
|
|
while (value3 < 256) {
|
|
dataOutputStream.writeUTF(BlockUtilHelper.resolveString((Block)this.bitSetHelperArray[value2][value].getIBlockStateArray()[value3].getBlock()));
|
|
++value3;
|
|
}
|
|
}
|
|
++value;
|
|
}
|
|
++value2;
|
|
}
|
|
value2 = 0;
|
|
while (value2 < 32) {
|
|
value = 0;
|
|
while (value < 32) {
|
|
if (this.bitSetHelperArray[value2][value] != null) {
|
|
Map<?, ?> regionMap = this.bitSetHelperArray[value2][value].getLookupMap();
|
|
dataOutputStream.writeShort(regionMap.entrySet().size());
|
|
for (Map.Entry<?, ?> mapEntry : regionMap.entrySet()) {
|
|
dataOutputStream.writeUTF((String)mapEntry.getKey());
|
|
dataOutputStream.writeShort(((List<?>)mapEntry.getValue()).size());
|
|
for (Object posObj : (List<?>)mapEntry.getValue()) {
|
|
BlockPos blockPos = (BlockPos)posObj;
|
|
dataOutputStream.writeByte((byte)(blockPos.getZ() << 4 | blockPos.getX()));
|
|
dataOutputStream.writeByte((byte)blockPos.getY());
|
|
}
|
|
}
|
|
}
|
|
++value;
|
|
}
|
|
++value2;
|
|
}
|
|
value2 = 0;
|
|
while (value2 < 32) {
|
|
value = 0;
|
|
while (value < 32) {
|
|
if (this.bitSetHelperArray[value2][value] != null) {
|
|
dataOutputStream.writeLong(this.bitSetHelperArray[value2][value].lastTimeMs);
|
|
}
|
|
++value;
|
|
}
|
|
++value2;
|
|
}
|
|
if (dataOutputStream != null) {
|
|
dataOutputStream.close();
|
|
}
|
|
if (gZIPOutputStream != null) {
|
|
gZIPOutputStream.close();
|
|
}
|
|
if (fileOutputStream != null) {
|
|
fileOutputStream.close();
|
|
}
|
|
}
|
|
catch (Throwable throwable2) {
|
|
if (throwable == null) {
|
|
throwable = throwable2;
|
|
} else if (throwable != throwable2) {
|
|
throwable.addSuppressed(throwable2);
|
|
}
|
|
if (throwable instanceof Exception) throw (Exception)throwable;
|
|
if (throwable instanceof Error) throw (Error)throwable;
|
|
throw new RuntimeException(throwable);
|
|
}
|
|
this.hasPosY = false;
|
|
System.out.println("Saved region successfully");
|
|
}
|
|
catch (Exception exception) {
|
|
exception.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public final boolean canAcceptIndex(int value, int value2) {
|
|
return this.bitSetHelperArray[value >> 4][value2 >> 4] != null;
|
|
}
|
|
|
|
public synchronized void writeString2(String str) {
|
|
try {
|
|
Path path;
|
|
Path path2 = Paths.get(str, new String[0]);
|
|
if (!Files.exists(path2, new LinkOption[0])) {
|
|
Files.createDirectories(path2, new FileAttribute[0]);
|
|
}
|
|
if (!Files.exists(path = BaritoneBitSetHelper.transformPath(path2, this.count2, this.count3), new LinkOption[0])) {
|
|
return;
|
|
}
|
|
System.out.println("Loading region " + this.count2 + "," + this.count3 + " from disk " + path2);
|
|
long timestampMs = System.nanoTime() / 1000000L;
|
|
Throwable throwable = null;
|
|
Object target = null;
|
|
try {
|
|
int value;
|
|
int value2;
|
|
FileInputStream fileInputStream = new FileInputStream(path.toFile());
|
|
GZIPInputStream gZIPInputStream = new GZIPInputStream((InputStream)fileInputStream, 32768);
|
|
DataInputStream dataInputStream = new DataInputStream(gZIPInputStream);
|
|
int value3 = dataInputStream.readInt();
|
|
if (value3 != 456022910) {
|
|
throw new IOException("Bad magic value " + value3);
|
|
}
|
|
boolean[][] blArray = new boolean[32][32];
|
|
BitSet[][] bitSetArray = new BitSet[32][32];
|
|
Map[][] mapArray = new Map[32][32];
|
|
IBlockState[][][] iBlockStateArray = new IBlockState[32][32][];
|
|
long[][] lArray = new long[32][32];
|
|
int value4 = 0;
|
|
while (value4 < 32) {
|
|
value2 = 0;
|
|
while (value2 < 32) {
|
|
value = dataInputStream.read();
|
|
switch (value) {
|
|
case 1: {
|
|
byte[] byArray = new byte[16384];
|
|
dataInputStream.readFully(byArray);
|
|
bitSetArray[value4][value2] = BitSet.valueOf(byArray);
|
|
mapArray[value4][value2] = new HashMap();
|
|
iBlockStateArray[value4][value2] = new IBlockState[256];
|
|
blArray[value4][value2] = true;
|
|
break;
|
|
}
|
|
case 0: {
|
|
break;
|
|
}
|
|
default: {
|
|
throw new IOException("Malformed stream");
|
|
}
|
|
}
|
|
++value2;
|
|
}
|
|
++value4;
|
|
}
|
|
value4 = 0;
|
|
while (value4 < 32) {
|
|
value2 = 0;
|
|
while (value2 < 32) {
|
|
if (blArray[value4][value2]) {
|
|
value = 0;
|
|
while (value < 256) {
|
|
iBlockStateArray[value4][value2][value] = BlockUtilHelper.resolveBlock2((String)dataInputStream.readUTF()).getDefaultState();
|
|
++value;
|
|
}
|
|
}
|
|
++value2;
|
|
}
|
|
++value4;
|
|
}
|
|
value4 = 0;
|
|
while (value4 < 32) {
|
|
value2 = 0;
|
|
while (value2 < 32) {
|
|
if (blArray[value4][value2]) {
|
|
value = dataInputStream.readShort() & 0xFFFF;
|
|
int value5 = 0;
|
|
while (value5 < value) {
|
|
String readUTF = dataInputStream.readUTF();
|
|
BlockUtilHelper.resolveBlock2((String)readUTF);
|
|
ArrayList<BlockPos> list = new ArrayList<BlockPos>();
|
|
mapArray[value4][value2].put(readUTF, list);
|
|
int value6 = dataInputStream.readShort() & 0xFFFF;
|
|
if (value6 == 0) {
|
|
value6 = 65536;
|
|
}
|
|
int value7 = 0;
|
|
while (value7 < value6) {
|
|
byte byteData = dataInputStream.readByte();
|
|
int value8 = byteData & 0xF;
|
|
int value9 = byteData >>> 4 & 0xF;
|
|
int value10 = dataInputStream.readByte() & 0xFF;
|
|
list.add(new BlockPos(value8, value10, value9));
|
|
++value7;
|
|
}
|
|
++value5;
|
|
}
|
|
}
|
|
++value2;
|
|
}
|
|
++value4;
|
|
}
|
|
value4 = 0;
|
|
while (value4 < 32) {
|
|
value2 = 0;
|
|
while (value2 < 32) {
|
|
if (blArray[value4][value2]) {
|
|
lArray[value4][value2] = dataInputStream.readLong();
|
|
}
|
|
++value2;
|
|
}
|
|
++value4;
|
|
}
|
|
value4 = 0;
|
|
while (value4 < 32) {
|
|
value2 = 0;
|
|
while (value2 < 32) {
|
|
if (blArray[value4][value2]) {
|
|
value = this.count2;
|
|
int count3 = this.count3;
|
|
int value11 = value4 + 32 * value;
|
|
int value12 = value2 + 32 * count3;
|
|
this.bitSetHelperArray[value4][value2] = new BitSetHelper(value11, value12, bitSetArray[value4][value2], iBlockStateArray[value4][value2], mapArray[value4][value2], lArray[value4][value2]);
|
|
}
|
|
++value2;
|
|
}
|
|
++value4;
|
|
}
|
|
if (dataInputStream != null) {
|
|
dataInputStream.close();
|
|
}
|
|
if (gZIPInputStream != null) {
|
|
gZIPInputStream.close();
|
|
}
|
|
if (fileInputStream != null) {
|
|
fileInputStream.close();
|
|
}
|
|
}
|
|
catch (Throwable throwable2) {
|
|
if (throwable == null) {
|
|
throwable = throwable2;
|
|
} else if (throwable != throwable2) {
|
|
throwable.addSuppressed(throwable2);
|
|
}
|
|
if (throwable instanceof Exception) throw (Exception)throwable;
|
|
if (throwable instanceof Error) throw (Error)throwable;
|
|
throw new RuntimeException(throwable);
|
|
}
|
|
this.syncStorage();
|
|
this.hasPosY = false;
|
|
long timestampMs2 = System.nanoTime() / 1000000L;
|
|
System.out.println("Loaded region successfully in " + (timestampMs2 - timestampMs) + "ms");
|
|
}
|
|
catch (Exception exception) {
|
|
exception.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public final int getCount2() {
|
|
return this.count2;
|
|
}
|
|
|
|
@Override
|
|
public final int getCount3() {
|
|
return this.count3;
|
|
}
|
|
|
|
public final ArrayList<BlockPos> resolveArrayList(String str) {
|
|
ArrayList list = new ArrayList();
|
|
|
|
for (int idx = 0; idx < 32; idx++) {
|
|
for (int idx2 = 0; idx2 < 32; idx2++) {
|
|
if (this.bitSetHelperArray[idx][idx2] != null) {
|
|
ArrayList list2 = this.bitSetHelperArray[idx][idx2].resolveArrayList(str);
|
|
if (list2 != null) {
|
|
list.addAll(list2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
public final synchronized void handleIndex(int value, int value2, BitSetHelper bitSetHelper) {
|
|
this.bitSetHelperArray[value][value2] = bitSetHelper;
|
|
this.hasPosY = true;
|
|
}
|
|
|
|
public final synchronized BitSetHelper getBitSetHelper() {
|
|
BitSetHelper bitSetHelper2 = null;
|
|
|
|
for (int idx = 0; idx < 32; idx++) {
|
|
for (int idx2 = 0; idx2 < 32; idx2++) {
|
|
if (this.bitSetHelperArray[idx][idx2] != null
|
|
&& (bitSetHelper2 == null || this.bitSetHelperArray[idx][idx2].lastTimeMs > bitSetHelper2.lastTimeMs)) {
|
|
bitSetHelper2 = this.bitSetHelperArray[idx][idx2];
|
|
}
|
|
}
|
|
}
|
|
|
|
return bitSetHelper2;
|
|
}
|
|
|
|
@Override
|
|
public final IBlockState resolveBlockState(int posX, int posY, int posZ) {
|
|
BitSetHelper bitSetHelper3 = this.bitSetHelperArray[posX >> 4][posZ >> 4];
|
|
return bitSetHelper3 != null ? bitSetHelper3.resolveBlockState(posX & 15, posY, posZ & 15, this.count4) : null;
|
|
}
|
|
|
|
private static final Path transformPath(Path path3, int value, int value2) {
|
|
|
|
return Paths.get(path3.toString(), "r." + value + "." + value2 + ".bcr");
|
|
}
|
|
|
|
public BaritoneBitSetHelper(int count2, int count3, int count4) {
|
|
this.count2 = count2;
|
|
this.count3 = count3;
|
|
this.hasPosY = false;
|
|
this.count4 = count4;
|
|
}
|
|
}
|