3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-24 23:30:15 +01:00

Merge pull request #1367 from Gerrygames/master

Fix 1.13 chunk reading, fix 1.9 chunk type not using global palette
Dieser Commit ist enthalten in:
Myles 2019-06-19 09:40:26 +01:00 committet von GitHub
Commit dcefaa6b19
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
2 geänderte Dateien mit 42 neuen und 25 gelöschten Zeilen

Datei anzeigen

@ -5,6 +5,7 @@ import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.type.Type;
public class ChunkSectionType1_13 extends Type<ChunkSection> {
private static final int GLOBAL_PALETTE = 14;
public ChunkSectionType1_13() {
super("Chunk Section Type", ChunkSection.class);
@ -16,18 +17,15 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
// Reaad bits per block
int bitsPerBlock = buffer.readUnsignedByte();
int originalBitsPerBlock = bitsPerBlock;
if (bitsPerBlock == 0 || bitsPerBlock > 8) {
bitsPerBlock = GLOBAL_PALETTE;
}
long maxEntryValue = (1L << bitsPerBlock) - 1;
if (bitsPerBlock == 0) {
bitsPerBlock = 14;
}
if (bitsPerBlock < 4) {
bitsPerBlock = 4;
}
if (bitsPerBlock > 8) {
bitsPerBlock = 14;
}
int paletteLength = bitsPerBlock == 14 ? 0 : Type.VAR_INT.read(buffer);
int paletteLength = bitsPerBlock == GLOBAL_PALETTE ? 0 : Type.VAR_INT.read(buffer);
// Read palette
chunkSection.clearPalette();
for (int i = 0; i < paletteLength; i++) {
@ -37,6 +35,11 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
// Read blocks
long[] blockData = new long[Type.VAR_INT.read(buffer)];
if (blockData.length > 0) {
int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0);
if (blockData.length != expectedLength) {
throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock);
}
for (int i = 0; i < blockData.length; i++) {
blockData[i] = buffer.readLong();
}
@ -53,7 +56,7 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
val = (int) ((blockData[startIndex] >>> startBitSubIndex | blockData[endIndex] << endBitSubIndex) & maxEntryValue);
}
if (bitsPerBlock == 14) {
if (bitsPerBlock == GLOBAL_PALETTE) {
chunkSection.setFlatBlock(i, val);
} else {
chunkSection.setPaletteIndex(i, val);
@ -72,15 +75,14 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
}
if (bitsPerBlock > 8) {
bitsPerBlock = 14;
bitsPerBlock = GLOBAL_PALETTE;
}
long maxEntryValue = (1L << bitsPerBlock) - 1;
buffer.writeByte(bitsPerBlock);
// Write pallet (or not)
if (bitsPerBlock != 14) {
if (bitsPerBlock != GLOBAL_PALETTE) {
Type.VAR_INT.write(buffer, chunkSection.getPaletteSize());
for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
Type.VAR_INT.write(buffer, chunkSection.getPaletteEntry(i));
@ -91,7 +93,7 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
Type.VAR_INT.write(buffer, length);
long[] data = new long[length];
for (int index = 0; index < ChunkSection.SIZE; index++) {
int value = bitsPerBlock == 14 ? chunkSection.getFlatBlock(index) : chunkSection.getPaletteIndex(index);
int value = bitsPerBlock == GLOBAL_PALETTE ? chunkSection.getFlatBlock(index) : chunkSection.getPaletteIndex(index);
int bitIndex = index * bitsPerBlock;
int startIndex = bitIndex / 64;
int endIndex = ((index + 1) * bitsPerBlock - 1) / 64;

Datei anzeigen

@ -1,11 +1,11 @@
package us.myles.ViaVersion.api.type.types.version;
import com.google.common.collect.BiMap;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.type.Type;
public class ChunkSectionType1_9 extends Type<ChunkSection> {
private static final int GLOBAL_PALETTE = 13;
public ChunkSectionType1_9() {
super("Chunk Section Type", ChunkSection.class);
@ -17,22 +17,23 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
// Reaad bits per block
int bitsPerBlock = buffer.readUnsignedByte();
int originalBitsPerBlock = bitsPerBlock;
long maxEntryValue = (1L << bitsPerBlock) - 1;
if (bitsPerBlock == 0) {
bitsPerBlock = 13;
bitsPerBlock = GLOBAL_PALETTE;
}
if (bitsPerBlock < 4) {
bitsPerBlock = 4;
}
if (bitsPerBlock > 8) {
bitsPerBlock = 13;
bitsPerBlock = GLOBAL_PALETTE;
}
int paletteLength = Type.VAR_INT.read(buffer);
// Read palette
chunkSection.clearPalette();
for (int i = 0; i < paletteLength; i++) {
if (bitsPerBlock != 13) {
if (bitsPerBlock != GLOBAL_PALETTE) {
chunkSection.addPaletteEntry(Type.VAR_INT.read(buffer));
} else {
Type.VAR_INT.read(buffer);
@ -42,6 +43,11 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
// Read blocks
long[] blockData = new long[Type.VAR_INT.read(buffer)];
if (blockData.length > 0) {
int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0);
if (blockData.length != expectedLength) {
throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock);
}
for (int i = 0; i < blockData.length; i++) {
blockData[i] = buffer.readLong();
}
@ -58,7 +64,7 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
val = (int) ((blockData[startIndex] >>> startBitSubIndex | blockData[endIndex] << endBitSubIndex) & maxEntryValue);
}
if (bitsPerBlock == 13) {
if (bitsPerBlock == GLOBAL_PALETTE) {
chunkSection.setBlock(i, val >> 4, val & 0xF);
} else {
chunkSection.setPaletteIndex(i, val);
@ -75,20 +81,29 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
while (chunkSection.getPaletteSize() > 1 << bitsPerBlock) {
bitsPerBlock += 1;
}
if (bitsPerBlock > 8) {
bitsPerBlock = GLOBAL_PALETTE;
}
long maxEntryValue = (1L << bitsPerBlock) - 1;
buffer.writeByte(bitsPerBlock);
// Write pallet
Type.VAR_INT.write(buffer, chunkSection.getPaletteSize());
for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
Type.VAR_INT.write(buffer, chunkSection.getPaletteEntry(i));
// Write pallet (or not)
if (bitsPerBlock != GLOBAL_PALETTE) {
Type.VAR_INT.write(buffer, chunkSection.getPaletteSize());
for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
Type.VAR_INT.write(buffer, chunkSection.getPaletteEntry(i));
}
} else {
Type.VAR_INT.write(buffer, 0);
}
int length = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0);
Type.VAR_INT.write(buffer, length);
long[] data = new long[length];
for (int index = 0; index < ChunkSection.SIZE; index++) {
int value = chunkSection.getPaletteIndex(index);
int value = bitsPerBlock == GLOBAL_PALETTE ? chunkSection.getFlatBlock(index) : chunkSection.getPaletteIndex(index);
int bitIndex = index * bitsPerBlock;
int startIndex = bitIndex / 64;
int endIndex = ((index + 1) * bitsPerBlock - 1) / 64;