Archived
0

перемещение NibbleArray в Core

This commit is contained in:
2018-11-10 17:28:35 +03:00
parent bd2991abaa
commit 82c5345693
4 changed files with 67 additions and 32 deletions

View File

@@ -0,0 +1,65 @@
package mc.core.utils;
import lombok.RequiredArgsConstructor;
import mc.core.world.block.BlockLocation;
/**
* Сжатый массив значений 0-15
*/
@RequiredArgsConstructor
public class NibbleArray {
private final byte[] data;
public NibbleArray(int capacity) {
this.data = new byte[capacity];
}
public NibbleArray() {
this.data = new byte[2048];
}
private int coordsToIndex(int x, int y, int z) {
return y << 8 | z << 4 | x;
}
private int nibbleIndex(int index) {
return index >> 1;
}
private boolean isLowerNibble(int index) {
return (index & 1) == 0;
}
public int get(BlockLocation location) {
return get(location.getX(), location.getY(), location.getZ());
}
public int get(int x, int y, int z) {
final int idx = coordsToIndex(x, y, z);
final int ni = nibbleIndex(idx);
return isLowerNibble(idx) ? this.data[ni] & 0x0F : this.data[ni] >> 4 & 0x0F;
}
public void set(BlockLocation location, int value) {
set(location.getX(), location.getY(), location.getZ(), value);
}
public void set(int x, int y, int z, int value) {
if (value < 0) value = 0;
else if (value > 15) value = 15;
final int idx = coordsToIndex(x, y, z);
final int ni = nibbleIndex(idx);
if (isLowerNibble(idx)) {
this.data[ni] = (byte)(value);
} else {
this.data[ni] = (byte)(this.data[ni] | value << 4);
}
}
public byte[] getRawData() {
return data;
}
}