перемещение NibbleArray в Core
This commit is contained in:
@@ -5,6 +5,7 @@ import gnu.trove.list.TByteList;
|
||||
import gnu.trove.list.array.TByteArrayList;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.utils.NibbleArray;
|
||||
import mc.core.world.Biome;
|
||||
import mc.core.world.chunk.Chunk;
|
||||
import mc.core.world.chunk.ChunkSection;
|
||||
|
||||
@@ -4,6 +4,7 @@ import gnu.trove.list.TByteList;
|
||||
import gnu.trove.list.linked.TByteLinkedList;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mc.core.utils.NibbleArray;
|
||||
import mc.core.world.Biome;
|
||||
import mc.core.world.block.Block;
|
||||
import mc.core.world.chunk.ChunkSection;
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package mc.world.anvil;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.world.block.BlockLocation;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class NibbleArray {
|
||||
private final byte[] data;
|
||||
|
||||
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] & 15 : this.data[ni] >> 4 & 15;
|
||||
}
|
||||
}
|
||||
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