| 1 | package net.minecraft.world.level.chunk;
|
| 2 |
|
| 3 | import java.util.Arrays;
|
| 4 | import net.minecraft.util.Util;
|
| 5 | import net.minecraft.util.VisibleForDebug;
|
| 6 | import org.jspecify.annotations.Nullable;
|
| 7 |
|
| 8 | public class DataLayer {
|
| 9 | public static final int LAYER_COUNT = 16;
|
| 10 | public static final int LAYER_SIZE = 128;
|
| 11 | public static final int SIZE = 2048;
|
| 12 | private static final int NIBBLE_SIZE = 4;
|
| 13 | protected byte @Nullable [] data;
|
| 14 | private int defaultValue;
|
| 15 |
|
| 16 | public DataLayer() {
|
| 17 | this(0);
|
| 18 | }
|
| 19 |
|
| 20 | public DataLayer(final int defaultValue) {
|
| 21 | this.defaultValue = defaultValue;
|
| 22 | }
|
| 23 |
|
| 24 | public DataLayer(final byte[] data) {
|
| 25 | this.data = data;
|
| 26 | this.defaultValue = 0;
|
| 27 | if (data.length != 2048) {
|
| 28 | throw (IllegalArgumentException)Util.pauseInIde(new IllegalArgumentException("DataLayer should be 2048 bytes not: " + data.length));
|
| 29 | }
|
| 30 | }
|
| 31 |
|
| 32 | public int get(final int x, final int y, final int z) {
|
| 33 | return this.get(getIndex(x, y, z));
|
| 34 | }
|
| 35 |
|
| 36 | public void set(final int x, final int y, final int z, final int val) {
|
| 37 | this.set(getIndex(x, y, z), val);
|
| 38 | }
|
| 39 |
|
| 40 | private static int getIndex(final int x, final int y, final int z) {
|
| 41 | return y << 8 | z << 4 | x;
|
| 42 | }
|
| 43 |
|
| 44 | private int get(final int index) {
|
| 45 | if (this.data == null) {
|
| 46 | return this.defaultValue;
|
| 47 | }
|
| 48 |
|
| 49 | int position = getByteIndex(index);
|
| 50 | int nibble = getNibbleIndex(index);
|
| 51 | return this.data[position] >> 4 * nibble & 15;
|
| 52 | }
|
| 53 |
|
| 54 | private void set(final int index, final int val) {
|
| 55 | byte[] data = this.getData();
|
| 56 | int position = getByteIndex(index);
|
| 57 | int nibble = getNibbleIndex(index);
|
| 58 | int mask = ~(15 << 4 * nibble);
|
| 59 | int valueToSet = (val & 15) << 4 * nibble;
|
| 60 | data[position] = (byte)(data[position] & mask | valueToSet);
|
| 61 | }
|
| 62 |
|
| 63 | private static int getNibbleIndex(final int index) {
|
| 64 | return index & 1;
|
| 65 | }
|
| 66 |
|
| 67 | private static int getByteIndex(final int position) {
|
| 68 | return position >> 1;
|
| 69 | }
|
| 70 |
|
| 71 | public void fill(final int value) {
|
| 72 | this.defaultValue = value;
|
| 73 | this.data = null;
|
| 74 | }
|
| 75 |
|
| 76 | private static byte packFilled(final int value) {
|
| 77 | byte packed = (byte)value;
|
| 78 |
|
| 79 | for (int i = 4; i < 8; i += 4) {
|
| 80 | packed = (byte)(packed | value << i);
|
| 81 | }
|
| 82 |
|
| 83 | return packed;
|
| 84 | }
|
| 85 |
|
| 86 | public byte[] getData() {
|
| 87 | if (this.data == null) {
|
| 88 | this.data = new byte[2048];
|
| 89 | if (this.defaultValue != 0) {
|
| 90 | Arrays.fill(this.data, packFilled(this.defaultValue));
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | return this.data;
|
| 95 | }
|
| 96 |
|
| 97 | public DataLayer copy() {
|
| 98 | return this.data == null ? new DataLayer(this.defaultValue) : new DataLayer((byte[])this.data.clone());
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public String toString() {
|
| 103 | StringBuilder builder = new StringBuilder();
|
| 104 |
|
| 105 | for (int i = 0; i < 4096; i++) {
|
| 106 | builder.append(Integer.toHexString(this.get(i)));
|
| 107 | if ((i & 15) == 15) {
|
| 108 | builder.append("\n");
|
| 109 | }
|
| 110 |
|
| 111 | if ((i & 0xFF) == 255) {
|
| 112 | builder.append("\n");
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | return builder.toString();
|
| 117 | }
|
| 118 |
|
| 119 | @VisibleForDebug
|
| 120 | public String layerToString(final int layer) {
|
| 121 | StringBuilder builder = new StringBuilder();
|
| 122 |
|
| 123 | for (int i = 0; i < 256; i++) {
|
| 124 | builder.append(Integer.toHexString(this.get(i)));
|
| 125 | if ((i & 15) == 15) {
|
| 126 | builder.append("\n");
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | return builder.toString();
|
| 131 | }
|
| 132 |
|
| 133 | public boolean isDefinitelyHomogenous() {
|
| 134 | return this.data == null;
|
| 135 | }
|
| 136 |
|
| 137 | public boolean isDefinitelyFilledWith(final int value) {
|
| 138 | return this.data == null && this.defaultValue == value;
|
| 139 | }
|
| 140 |
|
| 141 | public boolean isEmpty() {
|
| 142 | return this.data == null && this.defaultValue == 0;
|
| 143 | }
|
| 144 | }
|
| 145 |
|