|
23 | 23 | import com.viaversion.viaversion.util.Pair;
|
24 | 24 | import io.netty.buffer.ByteBuf;
|
25 | 25 |
|
26 |
| -import java.io.ByteArrayOutputStream; |
27 | 26 | import java.io.IOException;
|
28 | 27 | import java.util.zip.Deflater;
|
29 | 28 |
|
| 29 | +import static com.viaversion.viaversion.api.minecraft.chunks.ChunkSection.SIZE; |
| 30 | +import static com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLight.LIGHT_LENGTH; |
| 31 | + |
30 | 32 | public class ChunkType1_7_6 extends Type<Chunk> {
|
31 | 33 |
|
32 | 34 | public static final ChunkType1_7_6 TYPE = new ChunkType1_7_6();
|
@@ -94,46 +96,84 @@ public static Pair<byte[], Short> serialize(final Chunk chunk) throws IOExceptio
|
94 | 96 | }
|
95 | 97 | }
|
96 | 98 |
|
97 |
| - final ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 99 | + final boolean biomes = chunk.isFullChunk() && chunk.getBiomeData() != null; |
| 100 | + final int totalSize = calculateSize(storageArrays, chunk.getBitmask(), biomes); |
| 101 | + |
| 102 | + final byte[] output = new byte[totalSize]; |
| 103 | + int index = 0; |
98 | 104 |
|
99 | 105 | for (int i = 0; i < storageArrays.length; i++) {
|
100 | 106 | if ((chunk.getBitmask() & 1 << i) != 0) {
|
101 |
| - output.write(storageArrays[i].getBlockLSBArray()); |
| 107 | + final byte[] blockLSBArray = storageArrays[i].getBlockLSBArray(); |
| 108 | + System.arraycopy(blockLSBArray, 0, output, index, blockLSBArray.length); |
| 109 | + index += blockLSBArray.length; |
102 | 110 | }
|
103 | 111 | }
|
104 | 112 |
|
105 | 113 | for (int i = 0; i < storageArrays.length; i++) {
|
106 | 114 | if ((chunk.getBitmask() & 1 << i) != 0) {
|
107 |
| - output.write(storageArrays[i].getBlockMetadataArray().getHandle()); |
| 115 | + final byte[] blockMetadataArray = storageArrays[i].getBlockMetadataArray().getHandle(); |
| 116 | + System.arraycopy(blockMetadataArray, 0, output, index, blockMetadataArray.length); |
| 117 | + index += blockMetadataArray.length; |
108 | 118 | }
|
109 | 119 | }
|
110 | 120 |
|
111 | 121 | for (int i = 0; i < storageArrays.length; i++) {
|
112 | 122 | if ((chunk.getBitmask() & 1 << i) != 0) {
|
113 |
| - output.write(storageArrays[i].getBlockLightArray().getHandle()); |
| 123 | + final byte[] blockLightArray = storageArrays[i].getBlockLightArray().getHandle(); |
| 124 | + System.arraycopy(blockLightArray, 0, output, index, blockLightArray.length); |
| 125 | + index += blockLightArray.length; |
114 | 126 | }
|
115 | 127 | }
|
116 | 128 |
|
117 | 129 | for (int i = 0; i < storageArrays.length; i++) {
|
118 | 130 | if ((chunk.getBitmask() & 1 << i) != 0 && storageArrays[i].getSkyLightArray() != null) {
|
119 |
| - output.write(storageArrays[i].getSkyLightArray().getHandle()); |
| 131 | + final byte[] skyLightArray = storageArrays[i].getSkyLightArray().getHandle(); |
| 132 | + System.arraycopy(skyLightArray, 0, output, index, skyLightArray.length); |
| 133 | + index += skyLightArray.length; |
120 | 134 | }
|
121 | 135 | }
|
122 | 136 |
|
123 | 137 | short additionalBitMask = 0;
|
124 | 138 | for (int i = 0; i < storageArrays.length; i++) {
|
125 | 139 | if ((chunk.getBitmask() & 1 << i) != 0 && storageArrays[i].hasBlockMSBArray()) {
|
126 | 140 | additionalBitMask |= (short) (1 << i);
|
127 |
| - output.write(storageArrays[i].getOrCreateBlockMSBArray().getHandle()); |
| 141 | + final byte[] blockMSBArray = storageArrays[i].getOrCreateBlockMSBArray().getHandle(); |
| 142 | + System.arraycopy(blockMSBArray, 0, output, index, blockMSBArray.length); |
| 143 | + index += blockMSBArray.length; |
128 | 144 | }
|
129 | 145 | }
|
130 | 146 |
|
131 |
| - if (chunk.isFullChunk() && chunk.getBiomeData() != null) { |
| 147 | + if (biomes) { |
132 | 148 | for (int biome : chunk.getBiomeData()) {
|
133 |
| - output.write(biome); |
| 149 | + output[index++] = (byte) biome; |
134 | 150 | }
|
135 | 151 | }
|
136 | 152 |
|
137 |
| - return new Pair<>(output.toByteArray(), additionalBitMask); |
| 153 | + return new Pair<>(output, additionalBitMask); |
138 | 154 | }
|
| 155 | + |
| 156 | + private static int calculateSize(final ExtendedBlockStorage[] storageArrays, final int bitmask, final boolean biomes) { |
| 157 | + int totalSize = 0; |
| 158 | + for (int i = 0; i < storageArrays.length; i++) { |
| 159 | + if ((bitmask & 1 << i) != 0) { |
| 160 | + totalSize += SIZE; // Block lsb array |
| 161 | + totalSize += SIZE / 2; // Block metadata array |
| 162 | + totalSize += LIGHT_LENGTH; // Block light array |
| 163 | + |
| 164 | + if (storageArrays[i].getSkyLightArray() != null) { |
| 165 | + totalSize += LIGHT_LENGTH; |
| 166 | + } |
| 167 | + |
| 168 | + if (storageArrays[i].hasBlockMSBArray()) { |
| 169 | + totalSize += SIZE / 2; // Block msb array |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + if (biomes) { |
| 174 | + totalSize += 256; |
| 175 | + } |
| 176 | + return totalSize; |
| 177 | + } |
| 178 | + |
139 | 179 | }
|
0 commit comments