package common.util;

public class NibbleArray {
	private final byte[] data;

	public NibbleArray() {
		this.data = new byte[2048];
	}

	public NibbleArray(byte[] data) {
		if(data.length != 2048)
			throw new IllegalArgumentException("NibbleArray sollte 2048 Bytes groß sein, nicht " + data.length);
		this.data = data;
	}

	public int get(int x, int y, int z) {
		int idx = y << 8 | z << 4 | x;
		int off = idx >> 1;
		return (idx & 1) == 0 ? this.data[off] & 15 : this.data[off] >> 4 & 15;
	}

	public void set(int x, int y, int z, int value) {
		int idx = y << 8 | z << 4 | x;
		int off = idx >> 1;
		this.data[off] = (idx & 1) == 0 ? (byte)(this.data[off] & 240 | value & 15) : (byte)(this.data[off] & 15 | (value & 15) << 4);
	}

	public byte[] getData() {
		return this.data;
	}
}
