перемещение NibbleArray в Core
This commit is contained in:
65
core/src/main/java/mc/core/utils/NibbleArray.java
Normal file
65
core/src/main/java/mc/core/utils/NibbleArray.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user