RegionFile -> Region
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package mc.world.anvil;
|
||||
|
||||
import com.flowpowered.nbt.CompoundTag;
|
||||
import com.flowpowered.nbt.Tag;
|
||||
import com.flowpowered.nbt.stream.NBTInputStream;
|
||||
import gnu.trove.list.TByteList;
|
||||
@@ -9,30 +10,48 @@ import mc.core.world.chunk.Chunk;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.zip.InflaterInputStream;
|
||||
|
||||
@Slf4j
|
||||
public class RegionFile implements Closeable {
|
||||
class Region implements Closeable {
|
||||
private static final byte BYTE_TRUE = 1,
|
||||
BYTE_FALSE = 0;
|
||||
private static final byte[] EMPTY_SECTOR = new byte[4096];
|
||||
|
||||
private RandomAccessFile file;
|
||||
private TByteList sectorFree;
|
||||
private final int[] offsets = new int[1024];
|
||||
|
||||
public RegionFile(File file) throws IOException {
|
||||
private void correctingSizeRegionFile() throws IOException {
|
||||
if (this.file.length() < 4096L) {
|
||||
this.file.write(EMPTY_SECTOR);
|
||||
this.file.write(EMPTY_SECTOR);
|
||||
}
|
||||
|
||||
if ((this.file.length() & 4095L) != 0L) {
|
||||
for (int i = 0; i < (this.file.length() & 4095L); i++) {
|
||||
this.file.write(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Region(File file) throws IOException {
|
||||
this.file = new RandomAccessFile(file, "rw");
|
||||
|
||||
//TODO ???
|
||||
correctingSizeRegionFile();
|
||||
|
||||
int sizeOfSectorFree = (int)this.file.length() / 4096;
|
||||
sectorFree = new TByteArrayList(sizeOfSectorFree);
|
||||
|
||||
sectorFree.add(BYTE_FALSE);
|
||||
sectorFree.add(BYTE_FALSE);
|
||||
for (int i = 0; i < sizeOfSectorFree-2; i++) {
|
||||
sectorFree.add(BYTE_TRUE);
|
||||
}
|
||||
|
||||
this.file.seek(0L);
|
||||
|
||||
for (int i = 0; i < offsets.length; ++i) {
|
||||
int read = this.file.readInt();
|
||||
offsets[i] = read;
|
||||
@@ -48,13 +67,13 @@ public class RegionFile implements Closeable {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Chunk getChunk(int x, int z) {
|
||||
Chunk getChunk(int x, int z) {
|
||||
int offset = getOffset(x, z);
|
||||
|
||||
if (offset == 0) return null;
|
||||
|
||||
int v1 = offset >> 8; // j
|
||||
int v2 = offset & 255; // k
|
||||
int v1 = offset >> 8;
|
||||
int v2 = offset & 255;
|
||||
|
||||
if (v1 + v2 > sectorFree.size()) return null;
|
||||
|
||||
@@ -77,9 +96,8 @@ public class RegionFile implements Closeable {
|
||||
NBTInputStream nbtInputStream = new NBTInputStream(inputStream, false);
|
||||
|
||||
Tag rootTag = nbtInputStream.readTag();
|
||||
log.info(rootTag.toString());
|
||||
|
||||
nbtInputStream.close();
|
||||
return new AnvilChunk((CompoundTag) rootTag);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Get chunk", e);
|
||||
Reference in New Issue
Block a user