From 9a87adf51b9d0064d2f182fa25cb9fd2797c2efb Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Mon, 12 Nov 2018 14:36:01 +0100 Subject: [PATCH] Use BiHashMap as palette --- .../api/minecraft/chunks/ChunkSection.java | 32 +-- .../types/version/ChunkSectionType1_13.java | 183 +++++++++--------- .../types/version/ChunkSectionType1_8.java | 47 +++-- .../types/version/ChunkSectionType1_9.java | 181 +++++++++-------- .../packets/WorldPackets.java | 13 +- .../packets/WorldPackets.java | 8 +- .../types/Chunk1_9_1_2Type.java | 6 +- .../types/Chunk1_9to1_8Type.java | 4 +- 8 files changed, 233 insertions(+), 241 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/minecraft/chunks/ChunkSection.java b/common/src/main/java/us/myles/ViaVersion/api/minecraft/chunks/ChunkSection.java index b3341b92e..c08a89eb5 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/minecraft/chunks/ChunkSection.java +++ b/common/src/main/java/us/myles/ViaVersion/api/minecraft/chunks/ChunkSection.java @@ -1,12 +1,11 @@ package us.myles.ViaVersion.api.minecraft.chunks; -import com.google.common.collect.Lists; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; import io.netty.buffer.ByteBuf; import lombok.Getter; import lombok.Setter; -import java.util.List; - public class ChunkSection { /** * Size (dimensions) of blocks in a chunks section. @@ -17,7 +16,7 @@ public class ChunkSection { */ public static final int LIGHT_LENGTH = 16 * 16 * 16 / 2; // size * size * size / 2 (nibble bit count) @Getter - private final List palette = Lists.newArrayList(); + private BiMap palette = HashBiMap.create(); private final int[] blocks; private NibbleArray blockLight; private NibbleArray skyLight; @@ -28,11 +27,12 @@ public class ChunkSection { public ChunkSection() { this.blocks = new int[SIZE]; this.blockLight = new NibbleArray(SIZE); - palette.add(0); // AIR + palette.put(0, 0); } /** * Set a block in the chunks + * This method will not update non-air blocks count * * @param x Block X * @param y Block Y @@ -58,12 +58,12 @@ public class ChunkSection { public int getFlatBlock(int x, int y, int z) { int index = blocks[index(x, y, z)]; - return palette.get(index); + return palette.inverse().get(index); } public int getFlatBlock(int idx) { int index = blocks[idx]; - return palette.get(index); + return palette.inverse().get(index); } public void setBlock(int idx, int type, int data) { @@ -71,24 +71,24 @@ public class ChunkSection { } public void setPaletteIndex(int idx, int index) { - blocks[idx] = index; + blocks[idx] = index; } public int getPaletteIndex(int idx) { - return blocks[idx]; + return blocks[idx]; } /** - * Set a block in the chunks based on the index + * Set a block state in the chunk + * This method will not update non-air blocks count * - * @param idx Index - * @param id The raw or flat id of the block + * @param idx Index + * @param id The raw or flat id of the block */ public void setFlatBlock(int idx, int id) { - int index = palette.indexOf(id); - if (index == -1) { - index = palette.size(); - palette.add(id); + Integer index = palette.get(id); + if (index == null) { + palette.put(id, index = palette.size()); } blocks[idx] = index; diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_13.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_13.java index ee6b89721..b2d7cf080 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_13.java @@ -1,114 +1,113 @@ 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; -import java.util.List; - public class ChunkSectionType1_13 extends Type { - public ChunkSectionType1_13() { - super("Chunk Section Type", ChunkSection.class); - } + public ChunkSectionType1_13() { + super("Chunk Section Type", ChunkSection.class); + } - @Override - public ChunkSection read(ByteBuf buffer) throws Exception { - ChunkSection chunkSection = new ChunkSection(); - List palette = chunkSection.getPalette(); - palette.clear(); + @Override + public ChunkSection read(ByteBuf buffer) throws Exception { + ChunkSection chunkSection = new ChunkSection(); + BiMap palette = chunkSection.getPalette(); + palette.clear(); - // Reaad bits per block - int bitsPerBlock = buffer.readUnsignedByte(); - long maxEntryValue = (1L << bitsPerBlock) - 1; + // Reaad bits per block + int bitsPerBlock = buffer.readUnsignedByte(); + 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); - // Read palette - for (int i = 0; i < paletteLength; i++) { - palette.add(Type.VAR_INT.read(buffer)); - } + 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); + // Read palette + for (int i = 0; i < paletteLength; i++) { + palette.put(Type.VAR_INT.read(buffer), palette.size()); + } - // Read blocks - long[] blockData = new long[Type.VAR_INT.read(buffer)]; - if (blockData.length > 0) { - for (int i = 0; i < blockData.length; i++) { - blockData[i] = buffer.readLong(); - } - for (int i = 0; i < ChunkSection.SIZE; i++) { - int bitIndex = i * bitsPerBlock; - int startIndex = bitIndex / 64; - int endIndex = ((i + 1) * bitsPerBlock - 1) / 64; - int startBitSubIndex = bitIndex % 64; - int val; - if (startIndex == endIndex) { - val = (int) (blockData[startIndex] >>> startBitSubIndex & maxEntryValue); - } else { - int endBitSubIndex = 64 - startBitSubIndex; - val = (int) ((blockData[startIndex] >>> startBitSubIndex | blockData[endIndex] << endBitSubIndex) & maxEntryValue); - } + // Read blocks + long[] blockData = new long[Type.VAR_INT.read(buffer)]; + if (blockData.length > 0) { + for (int i = 0; i < blockData.length; i++) { + blockData[i] = buffer.readLong(); + } + for (int i = 0; i < ChunkSection.SIZE; i++) { + int bitIndex = i * bitsPerBlock; + int startIndex = bitIndex / 64; + int endIndex = ((i + 1) * bitsPerBlock - 1) / 64; + int startBitSubIndex = bitIndex % 64; + int val; + if (startIndex == endIndex) { + val = (int) (blockData[startIndex] >>> startBitSubIndex & maxEntryValue); + } else { + int endBitSubIndex = 64 - startBitSubIndex; + val = (int) ((blockData[startIndex] >>> startBitSubIndex | blockData[endIndex] << endBitSubIndex) & maxEntryValue); + } - if (bitsPerBlock == 14) { - chunkSection.setFlatBlock(i, val); - } else { - chunkSection.setPaletteIndex(i, val); - } - } - } + if (bitsPerBlock == 14) { + chunkSection.setFlatBlock(i, val); + } else { + chunkSection.setPaletteIndex(i, val); + } + } + } - return chunkSection; - } + return chunkSection; + } - @Override - public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception { - List palette = chunkSection.getPalette(); + @Override + public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception { + BiMap palette = chunkSection.getPalette(); - int bitsPerBlock = 4; - while (palette.size() > 1 << bitsPerBlock) { - bitsPerBlock += 1; - } + int bitsPerBlock = 4; + while (palette.size() > 1 << bitsPerBlock) { + bitsPerBlock += 1; + } - if (bitsPerBlock > 8) { - bitsPerBlock = 14; - } + if (bitsPerBlock > 8) { + bitsPerBlock = 14; + } - long maxEntryValue = (1L << bitsPerBlock) - 1; - buffer.writeByte(bitsPerBlock); + long maxEntryValue = (1L << bitsPerBlock) - 1; + buffer.writeByte(bitsPerBlock); - // Write pallet (or not) - if (bitsPerBlock != 14) { - Type.VAR_INT.write(buffer, palette.size()); - for (int mappedId : palette) { - Type.VAR_INT.write(buffer, mappedId); - } - } + // Write pallet (or not) + if (bitsPerBlock != 14) { + Type.VAR_INT.write(buffer, palette.size()); + for (int i = 0; i < palette.size(); i++) { + Type.VAR_INT.write(buffer, palette.inverse().get(i)); + } + } - 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 = bitsPerBlock == 14 ? chunkSection.getFlatBlock(index) : chunkSection.getPaletteIndex(index); - int bitIndex = index * bitsPerBlock; - int startIndex = bitIndex / 64; - int endIndex = ((index + 1) * bitsPerBlock - 1) / 64; - int startBitSubIndex = bitIndex % 64; - data[startIndex] = data[startIndex] & ~(maxEntryValue << startBitSubIndex) | ((long) value & maxEntryValue) << startBitSubIndex; - if (startIndex != endIndex) { - int endBitSubIndex = 64 - startBitSubIndex; - data[endIndex] = data[endIndex] >>> endBitSubIndex << endBitSubIndex | ((long) value & maxEntryValue) >> endBitSubIndex; - } - } - for (long l : data) { - buffer.writeLong(l); - } - } + 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 = bitsPerBlock == 14 ? chunkSection.getFlatBlock(index) : chunkSection.getPaletteIndex(index); + int bitIndex = index * bitsPerBlock; + int startIndex = bitIndex / 64; + int endIndex = ((index + 1) * bitsPerBlock - 1) / 64; + int startBitSubIndex = bitIndex % 64; + data[startIndex] = data[startIndex] & ~(maxEntryValue << startBitSubIndex) | ((long) value & maxEntryValue) << startBitSubIndex; + if (startIndex != endIndex) { + int endBitSubIndex = 64 - startBitSubIndex; + data[endIndex] = data[endIndex] >>> endBitSubIndex << endBitSubIndex | ((long) value & maxEntryValue) >> endBitSubIndex; + } + } + for (long l : data) { + buffer.writeLong(l); + } + } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_8.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_8.java index 66de27a4a..e132d7f35 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_8.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_8.java @@ -1,43 +1,40 @@ package us.myles.ViaVersion.api.type.types.version; import io.netty.buffer.ByteBuf; -import sun.reflect.generics.reflectiveObjects.NotImplementedException; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.type.Type; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.ShortBuffer; -import java.util.List; public class ChunkSectionType1_8 extends Type { - public ChunkSectionType1_8() { - super("Chunk Section Type", ChunkSection.class); - } + public ChunkSectionType1_8() { + super("Chunk Section Type", ChunkSection.class); + } - @Override - public ChunkSection read(ByteBuf buffer) throws Exception { - ChunkSection chunkSection = new ChunkSection(); - List palette = chunkSection.getPalette(); - palette.clear(); + @Override + public ChunkSection read(ByteBuf buffer) throws Exception { + ChunkSection chunkSection = new ChunkSection(); + chunkSection.getPalette().clear(); - byte[] blockData = new byte[ChunkSection.SIZE * 2]; - buffer.readBytes(blockData); - ShortBuffer blockBuf = ByteBuffer.wrap(blockData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); + byte[] blockData = new byte[ChunkSection.SIZE * 2]; + buffer.readBytes(blockData); + ShortBuffer blockBuf = ByteBuffer.wrap(blockData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); - for (int i = 0; i < ChunkSection.SIZE; i++) { - int mask = blockBuf.get(); - int type = mask >> 4; - int data = mask & 0xF; - chunkSection.setBlock(i, type, data); - } + for (int i = 0; i < ChunkSection.SIZE; i++) { + int mask = blockBuf.get(); + int type = mask >> 4; + int data = mask & 0xF; + chunkSection.setBlock(i, type, data); + } - return chunkSection; - } + return chunkSection; + } - @Override - public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception { - throw new NotImplementedException(); - } + @Override + public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception { + throw new UnsupportedOperationException(); + } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_9.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_9.java index 98d6b3b4d..37758816c 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_9.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_9.java @@ -1,110 +1,109 @@ 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; -import java.util.List; - public class ChunkSectionType1_9 extends Type { - public ChunkSectionType1_9() { - super("Chunk Section Type", ChunkSection.class); - } + public ChunkSectionType1_9() { + super("Chunk Section Type", ChunkSection.class); + } - @Override - public ChunkSection read(ByteBuf buffer) throws Exception { - ChunkSection chunkSection = new ChunkSection(); - List palette = chunkSection.getPalette(); - palette.clear(); + @Override + public ChunkSection read(ByteBuf buffer) throws Exception { + ChunkSection chunkSection = new ChunkSection(); + BiMap palette = chunkSection.getPalette(); + palette.clear(); - // Reaad bits per block - int bitsPerBlock = buffer.readUnsignedByte(); - long maxEntryValue = (1L << bitsPerBlock) - 1; + // Reaad bits per block + int bitsPerBlock = buffer.readUnsignedByte(); + long maxEntryValue = (1L << bitsPerBlock) - 1; - if (bitsPerBlock == 0) { - bitsPerBlock = 13; - } - if (bitsPerBlock < 4) { - bitsPerBlock = 4; - } - if (bitsPerBlock > 8) { - bitsPerBlock = 13; - } - int paletteLength = Type.VAR_INT.read(buffer); - // Read palette - for (int i = 0; i < paletteLength; i++) { - if (bitsPerBlock != 13) { - palette.add(Type.VAR_INT.read(buffer)); - } else { - Type.VAR_INT.read(buffer); - } - } + if (bitsPerBlock == 0) { + bitsPerBlock = 13; + } + if (bitsPerBlock < 4) { + bitsPerBlock = 4; + } + if (bitsPerBlock > 8) { + bitsPerBlock = 13; + } + int paletteLength = Type.VAR_INT.read(buffer); + // Read palette + for (int i = 0; i < paletteLength; i++) { + if (bitsPerBlock != 13) { + palette.put(Type.VAR_INT.read(buffer), palette.size()); + } else { + Type.VAR_INT.read(buffer); + } + } - // Read blocks - long[] blockData = new long[Type.VAR_INT.read(buffer)]; - if (blockData.length > 0) { - for (int i = 0; i < blockData.length; i++) { - blockData[i] = buffer.readLong(); - } - for (int i = 0; i < ChunkSection.SIZE; i++) { - int bitIndex = i * bitsPerBlock; - int startIndex = bitIndex / 64; - int endIndex = ((i + 1) * bitsPerBlock - 1) / 64; - int startBitSubIndex = bitIndex % 64; - int val; - if (startIndex == endIndex) { - val = (int) (blockData[startIndex] >>> startBitSubIndex & maxEntryValue); - } else { - int endBitSubIndex = 64 - startBitSubIndex; - val = (int) ((blockData[startIndex] >>> startBitSubIndex | blockData[endIndex] << endBitSubIndex) & maxEntryValue); - } + // Read blocks + long[] blockData = new long[Type.VAR_INT.read(buffer)]; + if (blockData.length > 0) { + for (int i = 0; i < blockData.length; i++) { + blockData[i] = buffer.readLong(); + } + for (int i = 0; i < ChunkSection.SIZE; i++) { + int bitIndex = i * bitsPerBlock; + int startIndex = bitIndex / 64; + int endIndex = ((i + 1) * bitsPerBlock - 1) / 64; + int startBitSubIndex = bitIndex % 64; + int val; + if (startIndex == endIndex) { + val = (int) (blockData[startIndex] >>> startBitSubIndex & maxEntryValue); + } else { + int endBitSubIndex = 64 - startBitSubIndex; + val = (int) ((blockData[startIndex] >>> startBitSubIndex | blockData[endIndex] << endBitSubIndex) & maxEntryValue); + } - if (bitsPerBlock == 13) { - chunkSection.setBlock(i, val >> 4, val & 0xF); - } else { - chunkSection.setPaletteIndex(i, val); - } - } - } + if (bitsPerBlock == 13) { + chunkSection.setBlock(i, val >> 4, val & 0xF); + } else { + chunkSection.setPaletteIndex(i, val); + } + } + } - return chunkSection; - } + return chunkSection; + } - @Override - public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception { - List palette = chunkSection.getPalette(); + @Override + public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception { + BiMap palette = chunkSection.getPalette(); - int bitsPerBlock = 4; - while (palette.size() > 1 << bitsPerBlock) { - bitsPerBlock += 1; - } - long maxEntryValue = (1L << bitsPerBlock) - 1; - buffer.writeByte(bitsPerBlock); + int bitsPerBlock = 4; + while (palette.size() > 1 << bitsPerBlock) { + bitsPerBlock += 1; + } + long maxEntryValue = (1L << bitsPerBlock) - 1; + buffer.writeByte(bitsPerBlock); - // Write pallet - Type.VAR_INT.write(buffer, palette.size()); - for (int mappedId : palette) { - Type.VAR_INT.write(buffer, mappedId); - } + // Write pallet + Type.VAR_INT.write(buffer, palette.size()); + for (int i = 0; i < palette.size(); i++) { + Type.VAR_INT.write(buffer, palette.inverse().get(i)); + } - 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 bitIndex = index * bitsPerBlock; - int startIndex = bitIndex / 64; - int endIndex = ((index + 1) * bitsPerBlock - 1) / 64; - int startBitSubIndex = bitIndex % 64; - data[startIndex] = data[startIndex] & ~(maxEntryValue << startBitSubIndex) | ((long) value & maxEntryValue) << startBitSubIndex; - if (startIndex != endIndex) { - int endBitSubIndex = 64 - startBitSubIndex; - data[endIndex] = data[endIndex] >>> endBitSubIndex << endBitSubIndex | ((long) value & maxEntryValue) >> endBitSubIndex; - } - } - for (long l : data) { - buffer.writeLong(l); - } - } + 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 bitIndex = index * bitsPerBlock; + int startIndex = bitIndex / 64; + int endIndex = ((index + 1) * bitsPerBlock - 1) / 64; + int startBitSubIndex = bitIndex % 64; + data[startIndex] = data[startIndex] & ~(maxEntryValue << startBitSubIndex) | ((long) value & maxEntryValue) << startBitSubIndex; + if (startIndex != endIndex) { + int endBitSubIndex = 64 - startBitSubIndex; + data[endIndex] = data[endIndex] >>> endBitSubIndex << endBitSubIndex | ((long) value & maxEntryValue) >> endBitSubIndex; + } + } + for (long l : data) { + buffer.writeLong(l); + } + } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java index 4e91dfee5..edae54c15 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java @@ -1,5 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets; +import com.google.common.collect.BiMap; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; @@ -27,16 +28,12 @@ public class WorldPackets { Chunk chunk = wrapper.passthrough(new Chunk1_13Type(clientWorld)); for (ChunkSection section : chunk.getSections()) { - if (section != null) { - for (int i = 0; i < section.getPalette().size(); i++) { - section.getPalette().set( - i, - Protocol1_13_1To1_13.getNewBlockStateId(section.getPalette().get(i)) - ); - } + if (section == null) continue; + BiMap inverse = section.getPalette().inverse(); + for (int i = 0; i < inverse.size(); i++) { + inverse.put(i, Protocol1_13_1To1_13.getNewBlockStateId(inverse.get(i))); } } - } }); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java index f6135bf05..9dd01c2de 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java @@ -2,6 +2,7 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.google.common.base.Optional; +import com.google.common.collect.BiMap; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; @@ -241,13 +242,14 @@ public class WorldPackets { boolean willStoreAnyBlock = false; - for (int p = 0; p < section.getPalette().size(); p++) { - int old = section.getPalette().get(p); + BiMap inverse = section.getPalette().inverse(); + for (int p = 0; p < inverse.size(); p++) { + int old = inverse.get(p); int newId = toNewId(old); if (storage.isWelcome(newId)) { willStoreAnyBlock = true; } - section.getPalette().set(p, newId); + inverse.put(p, newId); } if (willStoreAnyBlock) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java index 63ff5695d..6f3d1f6ee 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java @@ -54,10 +54,8 @@ public class Chunk1_9_1_2Type extends PartialType { if (world.getEnvironment() == Environment.NORMAL) { section.readSkyLight(input); } - if (replacePistons) { - if (section.getPalette().contains(36)) { - section.getPalette().set(section.getPalette().indexOf(36), replacementId); - } + if (replacePistons && section.getPalette().containsKey(36)) { + section.getPalette().put(replacementId, section.getPalette().remove(36)); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/Chunk1_9to1_8Type.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/Chunk1_9to1_8Type.java index 877a5aa5c..eae734f1b 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/Chunk1_9to1_8Type.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/Chunk1_9to1_8Type.java @@ -89,8 +89,8 @@ public class Chunk1_9to1_8Type extends PartialType { ChunkSection section = Types1_8.CHUNK_SECTION.read(input); sections[i] = section; - if (replacePistons && section.getPalette().contains(36)) { - section.getPalette().set(section.getPalette().indexOf(36), replacementId); + if (replacePistons && section.getPalette().containsKey(36)) { + section.getPalette().put(replacementId, section.getPalette().remove(36)); } }