NibbleArray .java

net.minecraft.world.chunk.NibbleArray (class, 82 lines): members: getFromIndex, setIndex, getData, set, get, data.

net.minecraft.world.chunkclass82 linescommon1.9 KB

API Surface (5)

int get(int x, int y, int z)
void set(int x, int y, int z, int value)
int getFromIndex(int index)
void setIndex(int index, int value)
byte[] getData()

Fields & Constants (1)

data

String literals (1)

"ChunkNibbleArrays should be 2048 bytes not: "

Full Source

NibbleArray.java82 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.world.chunk;
2
3
public class NibbleArray
4
{
5
    /**
6
     * Byte array of data stored in this holder. Possibly a light map or some chunk data. Data is accessed in 4-bit
7
     * pieces.
8
     */
9
    private final byte[] data;
10
11
    public NibbleArray()
12
    {
13
        this.data = new byte[2048];
14
    }
15
16
    public NibbleArray(byte[] storageArray)
17
    {
18
        this.data = storageArray;
19
20
        if (storageArray.length != 2048)
21
        {
22
            throw new IllegalArgumentException("ChunkNibbleArrays should be 2048 bytes not: " + storageArray.length);
23
        }
24
    }
25
26
    /**
27
     * Returns the nibble of data corresponding to the passed in x, y, z. y is at most 6 bits, z is at most 4.
28
     */
29
    public int get(int x, int y, int z)
30
    {
31
        return this.getFromIndex(this.getCoordinateIndex(x, y, z));
32
    }
33
34
    /**
35
     * Arguments are x, y, z, val. Sets the nibble of data at x << 11 | z << 7 | y to val.
36
     */
37
    public void set(int x, int y, int z, int value)
38
    {
39
        this.setIndex(this.getCoordinateIndex(x, y, z), value);
40
    }
41
42
    private int getCoordinateIndex(int x, int y, int z)
43
    {
44
        return y << 8 | z << 4 | x;
45
    }
46
47
    public int getFromIndex(int index)
48
    {
49
        int i = this.getNibbleIndex(index);
50
        return this.isLowerNibble(index) ? this.data[i] & 15 : this.data[i] >> 4 & 15;
51
    }
52
53
    public void setIndex(int index, int value)
54
    {
55
        int i = this.getNibbleIndex(index);
56
57
        if (this.isLowerNibble(index))
58
        {
59
            this.data[i] = (byte)(this.data[i] & 240 | value & 15);
60
        }
61
        else
62
        {
63
            this.data[i] = (byte)(this.data[i] & 15 | (value & 15) << 4);
64
        }
65
    }
66
67
    private boolean isLowerNibble(int index)
68
    {
69
        return (index & 1) == 0;
70
    }
71
72
    private int getNibbleIndex(int index)
73
    {
74
        return index >> 1;
75
    }
76
77
    public byte[] getData()
78
    {
79
        return this.data;
80
    }
81
}
82

Deobfuscated MCP 1.8.9 source of NibbleArray from Marcelektro/MavenMCP-1.8.9 (net/minecraft/world/chunk/NibbleArray.java), stored verbatim.