Archived
0

RegionFile -> Region

This commit is contained in:
2018-10-18 01:29:11 +03:00
parent 14fb66cefb
commit 5dff4bbe6a

View File

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