3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 00:22:51 +01:00

Use List and Map, fixes duplicate block ids in palette causing issues

Dieser Commit ist enthalten in:
Gerrygames 2018-11-12 17:10:19 +01:00
Ursprung 3b14dd3b04
Commit cf0bd6f021
4 geänderte Dateien mit 50 neuen und 35 gelöschten Zeilen

Datei anzeigen

@ -1,11 +1,14 @@
package us.myles.ViaVersion.api.minecraft.chunks; package us.myles.ViaVersion.api.minecraft.chunks;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ChunkSection { public class ChunkSection {
/** /**
* Size (dimensions) of blocks in a chunks section. * Size (dimensions) of blocks in a chunks section.
@ -15,8 +18,8 @@ public class ChunkSection {
* Length of the sky and block light nibble arrays. * Length of the sky and block light nibble arrays.
*/ */
public static final int LIGHT_LENGTH = 16 * 16 * 16 / 2; // size * size * size / 2 (nibble bit count) public static final int LIGHT_LENGTH = 16 * 16 * 16 / 2; // size * size * size / 2 (nibble bit count)
@Getter private List<Integer> palette = new ArrayList<>();
private BiMap<Integer, Integer> palette = HashBiMap.create(); private Map<Integer, Integer> inversePalette = new HashMap<>();
private final int[] blocks; private final int[] blocks;
private NibbleArray blockLight; private NibbleArray blockLight;
private NibbleArray skyLight; private NibbleArray skyLight;
@ -27,7 +30,8 @@ public class ChunkSection {
public ChunkSection() { public ChunkSection() {
this.blocks = new int[SIZE]; this.blocks = new int[SIZE];
this.blockLight = new NibbleArray(SIZE); this.blockLight = new NibbleArray(SIZE);
palette.put(0, 0); palette.add(0);
inversePalette.put(0, 0);
} }
/** /**
@ -58,12 +62,12 @@ public class ChunkSection {
public int getFlatBlock(int x, int y, int z) { public int getFlatBlock(int x, int y, int z) {
int index = blocks[index(x, y, z)]; int index = blocks[index(x, y, z)];
return palette.inverse().get(index); return palette.get(index);
} }
public int getFlatBlock(int idx) { public int getFlatBlock(int idx) {
int index = blocks[idx]; int index = blocks[idx];
return palette.inverse().get(index); return palette.get(index);
} }
public void setBlock(int idx, int type, int data) { public void setBlock(int idx, int type, int data) {
@ -84,16 +88,32 @@ public class ChunkSection {
public int getPaletteEntry(int index) { public int getPaletteEntry(int index) {
if (index < 0 || index >= palette.size()) throw new IndexOutOfBoundsException(); if (index < 0 || index >= palette.size()) throw new IndexOutOfBoundsException();
return palette.inverse().get(index); return palette.get(index);
} }
public void setPaletteEntry(int index, int id) { public void setPaletteEntry(int index, int id) {
if (index < 0 || index >= palette.size()) throw new IndexOutOfBoundsException(); if (index < 0 || index >= palette.size()) throw new IndexOutOfBoundsException();
palette.forcePut(id, index); palette.set(index, id);
inversePalette.put(id, index);
} }
public void replacePaletteEntry(int oldId, int newId) { public void replacePaletteEntry(int oldId, int newId) {
if (palette.containsKey(oldId)) palette.put(newId, palette.remove(oldId)); Integer index = inversePalette.remove(oldId);
if (index == null) return;
inversePalette.put(newId, index);
for (int i = 0; i < palette.size(); i++) {
if (palette.get(i) == oldId) palette.set(i, newId);
}
}
public void addPaletteEntry(int id) {
inversePalette.put(id, palette.size());
palette.add(id);
}
public void clearPalette() {
palette.clear();
inversePalette.clear();
} }
/** /**
@ -104,9 +124,11 @@ public class ChunkSection {
* @param id The raw or flat id of the block * @param id The raw or flat id of the block
*/ */
public void setFlatBlock(int idx, int id) { public void setFlatBlock(int idx, int id) {
Integer index = palette.get(id); Integer index = inversePalette.get(id);
if (index == null) { if (index == null) {
palette.put(id, index = palette.size()); index = palette.size();
palette.add(id);
inversePalette.put(id, index);
} }
blocks[idx] = index; blocks[idx] = index;

Datei anzeigen

@ -1,6 +1,5 @@
package us.myles.ViaVersion.api.type.types.version; package us.myles.ViaVersion.api.type.types.version;
import com.google.common.collect.BiMap;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
@ -14,8 +13,6 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
@Override @Override
public ChunkSection read(ByteBuf buffer) throws Exception { public ChunkSection read(ByteBuf buffer) throws Exception {
ChunkSection chunkSection = new ChunkSection(); ChunkSection chunkSection = new ChunkSection();
BiMap<Integer, Integer> palette = chunkSection.getPalette();
palette.clear();
// Reaad bits per block // Reaad bits per block
int bitsPerBlock = buffer.readUnsignedByte(); int bitsPerBlock = buffer.readUnsignedByte();
@ -32,8 +29,9 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
} }
int paletteLength = bitsPerBlock == 14 ? 0 : Type.VAR_INT.read(buffer); int paletteLength = bitsPerBlock == 14 ? 0 : Type.VAR_INT.read(buffer);
// Read palette // Read palette
chunkSection.clearPalette();
for (int i = 0; i < paletteLength; i++) { for (int i = 0; i < paletteLength; i++) {
palette.put(Type.VAR_INT.read(buffer), palette.size()); chunkSection.addPaletteEntry(Type.VAR_INT.read(buffer));
} }
// Read blocks // Read blocks
@ -68,10 +66,8 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
@Override @Override
public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception { public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception {
BiMap<Integer, Integer> palette = chunkSection.getPalette();
int bitsPerBlock = 4; int bitsPerBlock = 4;
while (palette.size() > 1 << bitsPerBlock) { while (chunkSection.getPaletteSize() > 1 << bitsPerBlock) {
bitsPerBlock += 1; bitsPerBlock += 1;
} }
@ -85,9 +81,9 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
// Write pallet (or not) // Write pallet (or not)
if (bitsPerBlock != 14) { if (bitsPerBlock != 14) {
Type.VAR_INT.write(buffer, palette.size()); Type.VAR_INT.write(buffer, chunkSection.getPaletteSize());
for (int i = 0; i < palette.size(); i++) { for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
Type.VAR_INT.write(buffer, palette.inverse().get(i)); Type.VAR_INT.write(buffer, chunkSection.getPaletteEntry(i));
} }
} }

Datei anzeigen

@ -17,7 +17,7 @@ public class ChunkSectionType1_8 extends Type<ChunkSection> {
@Override @Override
public ChunkSection read(ByteBuf buffer) throws Exception { public ChunkSection read(ByteBuf buffer) throws Exception {
ChunkSection chunkSection = new ChunkSection(); ChunkSection chunkSection = new ChunkSection();
chunkSection.getPalette().clear(); chunkSection.clearPalette();
byte[] blockData = new byte[ChunkSection.SIZE * 2]; byte[] blockData = new byte[ChunkSection.SIZE * 2];
buffer.readBytes(blockData); buffer.readBytes(blockData);

Datei anzeigen

@ -14,8 +14,6 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
@Override @Override
public ChunkSection read(ByteBuf buffer) throws Exception { public ChunkSection read(ByteBuf buffer) throws Exception {
ChunkSection chunkSection = new ChunkSection(); ChunkSection chunkSection = new ChunkSection();
BiMap<Integer, Integer> palette = chunkSection.getPalette();
palette.clear();
// Reaad bits per block // Reaad bits per block
int bitsPerBlock = buffer.readUnsignedByte(); int bitsPerBlock = buffer.readUnsignedByte();
@ -32,9 +30,10 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
} }
int paletteLength = Type.VAR_INT.read(buffer); int paletteLength = Type.VAR_INT.read(buffer);
// Read palette // Read palette
chunkSection.clearPalette();
for (int i = 0; i < paletteLength; i++) { for (int i = 0; i < paletteLength; i++) {
if (bitsPerBlock != 13) { if (bitsPerBlock != 13) {
palette.put(Type.VAR_INT.read(buffer), palette.size()); chunkSection.addPaletteEntry(Type.VAR_INT.read(buffer));
} else { } else {
Type.VAR_INT.read(buffer); Type.VAR_INT.read(buffer);
} }
@ -72,19 +71,17 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
@Override @Override
public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception { public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception {
BiMap<Integer, Integer> palette = chunkSection.getPalette();
int bitsPerBlock = 4; int bitsPerBlock = 4;
while (palette.size() > 1 << bitsPerBlock) { while (chunkSection.getPaletteSize() > 1 << bitsPerBlock) {
bitsPerBlock += 1; bitsPerBlock += 1;
} }
long maxEntryValue = (1L << bitsPerBlock) - 1; long maxEntryValue = (1L << bitsPerBlock) - 1;
buffer.writeByte(bitsPerBlock); buffer.writeByte(bitsPerBlock);
// Write pallet // Write pallet
Type.VAR_INT.write(buffer, palette.size()); Type.VAR_INT.write(buffer, chunkSection.getPaletteSize());
for (int i = 0; i < palette.size(); i++) { for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
Type.VAR_INT.write(buffer, palette.inverse().get(i)); Type.VAR_INT.write(buffer, chunkSection.getPaletteEntry(i));
} }
int length = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0); int length = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0);