cleanup disk clipboard code

This commit is contained in:
Jesse Boyd 2017-07-31 12:48:30 +10:00
parent 3726a69f13
commit 6305919397
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F

View File

@ -58,7 +58,6 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
private RandomAccessFile braf; private RandomAccessFile braf;
private MappedByteBuffer mbb; private MappedByteBuffer mbb;
private int last;
private FileChannel fc; private FileChannel fc;
private boolean hasBiomes; private boolean hasBiomes;
@ -74,11 +73,9 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
this.braf = new RandomAccessFile(file, "rw"); this.braf = new RandomAccessFile(file, "rw");
braf.setLength(file.length()); braf.setLength(file.length());
init(); init();
mbb.position(2); width = (int) mbb.getChar(2);
last = Integer.MIN_VALUE; height = (int) mbb.getChar(4);
width = (int) mbb.getChar(); length = (int) mbb.getChar(6);
height = (int) mbb.getChar();
length = (int) mbb.getChar();
area = width * length; area = width * length;
this.volume = length * width * height; this.volume = length * width * height;
@ -145,10 +142,10 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
public void streamBiomes(NBTStreamer.ByteReader task) { public void streamBiomes(NBTStreamer.ByteReader task) {
if (!hasBiomes()) return; if (!hasBiomes()) return;
int index = 0; int index = 0;
int offset = HEADER_SIZE + (volume << 1); int mbbIndex = HEADER_SIZE + (volume << 1);
for (int z = 0; z < length; z++) { for (int z = 0; z < length; z++) {
for (int x = 0; x < width; x++, index++) { for (int x = 0; x < width; x++, index++, mbbIndex++) {
int biome = mbb.get(offset + index) & 0xFF; int biome = mbb.get(mbbIndex) & 0xFF;
task.run(index, biome); task.run(index, biome);
} }
} }
@ -172,11 +169,9 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
return true; return true;
} }
}; };
mbb.position(8); int ox = mbb.getShort(8);
last = Integer.MIN_VALUE; int oy = mbb.getShort(10);
int ox = mbb.getShort(); int oz = mbb.getShort(12);
int oy = mbb.getShort();
int oz = mbb.getShort();
BlockArrayClipboard clipboard = new BlockArrayClipboard(region, this); BlockArrayClipboard clipboard = new BlockArrayClipboard(region, this);
clipboard.setOrigin(new Vector(ox, oy, oz)); clipboard.setOrigin(new Vector(ox, oy, oz));
return clipboard; return clipboard;
@ -214,11 +209,9 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
if (width * height * length != 0) { if (width * height * length != 0) {
init(); init();
// write length etc // write length etc
mbb.position(2); mbb.putChar(2, (char) width);
last = Integer.MIN_VALUE; mbb.putChar(4, (char) height);
mbb.putChar((char) width); mbb.putChar(6, (char) length);
mbb.putChar((char) height);
mbb.putChar((char) length);
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -228,11 +221,9 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
@Override @Override
public void setOrigin(Vector offset) { public void setOrigin(Vector offset) {
try { try {
mbb.position(8); mbb.putShort(8, (short) offset.getBlockX());
last = Integer.MIN_VALUE; mbb.putShort(10, (short) offset.getBlockY());
mbb.putShort((short) offset.getBlockX()); mbb.putShort(12, (short) offset.getBlockZ());
mbb.putShort((short) offset.getBlockY());
mbb.putShort((short) offset.getBlockZ());
} catch (Throwable e) { } catch (Throwable e) {
MainUtil.handleError(e); MainUtil.handleError(e);
} }
@ -249,11 +240,9 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
long size = width * height * length * 2l + HEADER_SIZE + (hasBiomes() ? area : 0); long size = width * height * length * 2l + HEADER_SIZE + (hasBiomes() ? area : 0);
braf.setLength(size); braf.setLength(size);
init(); init();
mbb.position(2); mbb.putChar(2, (char) width);
last = Integer.MIN_VALUE; mbb.putChar(4, (char) height);
mbb.putChar((char) width); mbb.putChar(6, (char) length);
mbb.putChar((char) height);
mbb.putChar((char) length);
} catch (IOException e) { } catch (IOException e) {
MainUtil.handleError(e); MainUtil.handleError(e);
} }
@ -332,12 +321,12 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
public void streamIds(NBTStreamer.ByteReader task) { public void streamIds(NBTStreamer.ByteReader task) {
try { try {
mbb.force(); mbb.force();
mbb.position(HEADER_SIZE); int pos = HEADER_SIZE;
int index = 0; int index = 0;
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int z = 0; z < length; z++) { for (int z = 0; z < length; z++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++, pos += 2) {
int combinedId = mbb.getChar(); int combinedId = mbb.getChar(pos);
task.run(index++, FaweCache.getId(combinedId)); task.run(index++, FaweCache.getId(combinedId));
} }
} }
@ -351,12 +340,12 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
public void streamDatas(NBTStreamer.ByteReader task) { public void streamDatas(NBTStreamer.ByteReader task) {
try { try {
mbb.force(); mbb.force();
mbb.position(HEADER_SIZE); int pos = HEADER_SIZE;
int index = 0; int index = 0;
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int z = 0; z < length; z++) { for (int z = 0; z < length; z++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++, pos += 2) {
int combinedId = mbb.getChar(); int combinedId = mbb.getChar(pos);
task.run(index++, FaweCache.getData(combinedId)); task.run(index++, FaweCache.getData(combinedId));
} }
} }
@ -375,15 +364,15 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
public void forEach(final BlockReader task, boolean air) { public void forEach(final BlockReader task, boolean air) {
try { try {
mbb.force(); mbb.force();
mbb.position(HEADER_SIZE); int pos = HEADER_SIZE;
IntegerTrio trio = new IntegerTrio(); IntegerTrio trio = new IntegerTrio();
final boolean hasTile = !nbtMap.isEmpty(); final boolean hasTile = !nbtMap.isEmpty();
if (air) { if (air) {
if (hasTile) { if (hasTile) {
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int z = 0; z < length; z++) { for (int z = 0; z < length; z++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++, pos += 2) {
char combinedId = mbb.getChar(); char combinedId = mbb.getChar(pos);
BaseBlock block = FaweCache.CACHE_BLOCK[combinedId]; BaseBlock block = FaweCache.CACHE_BLOCK[combinedId];
if (block.canStoreNBTData()) { if (block.canStoreNBTData()) {
trio.set(x, y, z); trio.set(x, y, z);
@ -400,8 +389,8 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
} else { } else {
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int z = 0; z < length; z++) { for (int z = 0; z < length; z++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++, pos += 2) {
char combinedId = mbb.getChar(); char combinedId = mbb.getChar(pos);
BaseBlock block = FaweCache.CACHE_BLOCK[combinedId]; BaseBlock block = FaweCache.CACHE_BLOCK[combinedId];
task.run(x, y, z, block); task.run(x, y, z, block);
} }
@ -411,8 +400,8 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
} else { } else {
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int z = 0; z < length; z++) { for (int z = 0; z < length; z++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++, pos += 2) {
int combinedId = mbb.getChar(); int combinedId = mbb.getChar(pos);
if (combinedId != 0) { if (combinedId != 0) {
BaseBlock block = FaweCache.CACHE_BLOCK[combinedId]; BaseBlock block = FaweCache.CACHE_BLOCK[combinedId];
if (block.canStoreNBTData()) { if (block.canStoreNBTData()) {
@ -441,12 +430,8 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
@Override @Override
public BaseBlock getBlock(int x, int y, int z) { public BaseBlock getBlock(int x, int y, int z) {
try { try {
int i = getIndex(x, y, z); int index = HEADER_SIZE + (getIndex(x, y, z) << 1);
if (i != last + 1) { int combinedId = mbb.getChar(index);
mbb.position((HEADER_SIZE) + (i << 1));
}
last = i;
int combinedId = mbb.getChar();
BaseBlock block = FaweCache.CACHE_BLOCK[combinedId]; BaseBlock block = FaweCache.CACHE_BLOCK[combinedId];
if (block.canStoreNBTData() && !nbtMap.isEmpty()) { if (block.canStoreNBTData() && !nbtMap.isEmpty()) {
CompoundTag nbt = nbtMap.get(new IntegerTrio(x, y, z)); CompoundTag nbt = nbtMap.get(new IntegerTrio(x, y, z));
@ -465,11 +450,8 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
@Override @Override
public BaseBlock getBlock(int i) { public BaseBlock getBlock(int i) {
try { try {
if (i != last + 1) { int diskIndex = (HEADER_SIZE) + (i << 1);
mbb.position((HEADER_SIZE) + (i << 1)); int combinedId = mbb.getChar(diskIndex);
}
last = i;
int combinedId = mbb.getChar();
BaseBlock block = FaweCache.CACHE_BLOCK[combinedId]; BaseBlock block = FaweCache.CACHE_BLOCK[combinedId];
if (block.canStoreNBTData() && !nbtMap.isEmpty()) { if (block.canStoreNBTData() && !nbtMap.isEmpty()) {
CompoundTag nbt; CompoundTag nbt;
@ -516,15 +498,11 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
@Override @Override
public boolean setBlock(int x, int y, int z, BaseBlock block) { public boolean setBlock(int x, int y, int z, BaseBlock block) {
try { try {
int i = x + ((ylast == y) ? ylasti : (ylasti = ((ylast = y)) * area)) + ((zlast == z) ? zlasti : (zlasti = (zlast = z) * width)); int index = (HEADER_SIZE) + (getIndex(x, y, z) << 1);
if (i != last + 1) {
mbb.position((HEADER_SIZE) + (i << 1));
}
last = i;
final int id = block.getId(); final int id = block.getId();
final int data = block.getData(); final int data = block.getData();
int combined = (id << 4) + data; int combined = (id << 4) + data;
mbb.putChar((char) combined); mbb.putChar(index, (char) combined);
CompoundTag tile = block.getNbtData(); CompoundTag tile = block.getNbtData();
if (tile != null) { if (tile != null) {
setTile(x, y, z, tile); setTile(x, y, z, tile);
@ -538,55 +516,32 @@ public class DiskOptimizedClipboard extends FaweClipboard implements Closeable {
@Override @Override
public void setId(int i, int id) { public void setId(int i, int id) {
int index; int index = (HEADER_SIZE) + (i << 1);
if (i != last + 1) {
index = (HEADER_SIZE) + (i << 1);
} else {
index = mbb.position();
}
last = i;
mbb.position(index + 1);
// 00000000 00000000 // 00000000 00000000
// [ id ]data // [ id ]data
byte id2 = mbb.get(); byte id2 = mbb.get(index + 1);
mbb.position(index); mbb.put(index, (byte) (id >> 4));
mbb.put((byte) (id >> 4)); mbb.put(index + 1, (byte) (((id & 0xFF) << 4) + (id2 & 0xFF)));
mbb.put((byte) (((id & 0xFF) << 4) + (id2 & 0xFF)));
} }
public void setCombined(int i, int combined) { public void setCombined(int i, int combined) {
if (i != last + 1) { mbb.putChar((HEADER_SIZE) + (i << 1), (char) combined);
mbb.position((HEADER_SIZE) + (i << 1));
}
last = i;
mbb.putChar((char) combined);
} }
@Override @Override
public void setAdd(int i, int add) { public void setAdd(int i, int add) {
last = i;
int index = (HEADER_SIZE) + (i << 1); int index = (HEADER_SIZE) + (i << 1);
mbb.position(index);
// 00000000 00000000 // 00000000 00000000
// [ id ]data // [ id ]data
char combined = mbb.getChar(); char combined = mbb.getChar(index);
mbb.position(index); mbb.putChar(index, (char) ((combined & 0xFFFF) + (add << 12)));
mbb.putChar((char) ((combined & 0xFFFF) + (add << 12)));
} }
@Override @Override
public void setData(int i, int data) { public void setData(int i, int data) {
int index; int index = (HEADER_SIZE) + (i << 1) + 1;
if (i != last + 1) { byte id = mbb.get(index);
index = (HEADER_SIZE) + (i << 1) + 1; mbb.put(index, (byte) ((id & 0xF0) + data));
} else {
index = mbb.position() + 1;
}
mbb.position(index);
last = i;
byte id = mbb.get();
mbb.position(index);
mbb.put((byte) ((id & 0xF0) + data));
} }
@Override @Override