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

Use BiHashMap as palette

Dieser Commit ist enthalten in:
Gerrygames 2018-11-12 14:36:01 +01:00
Ursprung 4b1c291fca
Commit 41ef4f6284
9 geänderte Dateien mit 41 neuen und 48 gelöschten Zeilen

Datei anzeigen

@ -1,12 +1,11 @@
package us.myles.ViaVersion.api.minecraft.chunks; 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 io.netty.buffer.ByteBuf;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.List;
public class ChunkSection { public class ChunkSection {
/** /**
* Size (dimensions) of blocks in a chunks section. * 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) public static final int LIGHT_LENGTH = 16 * 16 * 16 / 2; // size * size * size / 2 (nibble bit count)
@Getter @Getter
private final List<Integer> palette = Lists.newArrayList(); private BiMap<Integer, Integer> palette = HashBiMap.create();
private final int[] blocks; private final int[] blocks;
private NibbleArray blockLight; private NibbleArray blockLight;
private NibbleArray skyLight; private NibbleArray skyLight;
@ -28,7 +27,7 @@ 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.add(0); // AIR palette.put(0, 0);
} }
/** /**
@ -59,12 +58,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.get(index); return palette.inverse().get(index);
} }
public int getFlatBlock(int idx) { public int getFlatBlock(int idx) {
int index = blocks[idx]; int index = blocks[idx];
return palette.get(index); return palette.inverse().get(index);
} }
public void setBlock(int idx, int type, int data) { public void setBlock(int idx, int type, int data) {
@ -87,10 +86,9 @@ 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) {
int index = palette.indexOf(id); Integer index = palette.get(id);
if (index == -1) { if (index == null) {
index = palette.size(); palette.put(id, index = palette.size());
palette.add(id);
} }
blocks[idx] = index; blocks[idx] = index;

Datei anzeigen

@ -1,11 +1,10 @@
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;
import java.util.List;
public class ChunkSectionType1_13 extends Type<ChunkSection> { public class ChunkSectionType1_13 extends Type<ChunkSection> {
public ChunkSectionType1_13() { public ChunkSectionType1_13() {
@ -15,7 +14,7 @@ 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();
List<Integer> palette = chunkSection.getPalette(); BiMap<Integer, Integer> palette = chunkSection.getPalette();
palette.clear(); palette.clear();
// Reaad bits per block // Reaad bits per block
@ -34,7 +33,7 @@ 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
for (int i = 0; i < paletteLength; i++) { for (int i = 0; i < paletteLength; i++) {
palette.add(Type.VAR_INT.read(buffer)); palette.put(Type.VAR_INT.read(buffer), palette.size());
} }
// Read blocks // Read blocks
@ -69,7 +68,7 @@ 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 {
List<Integer> palette = chunkSection.getPalette(); BiMap<Integer, Integer> palette = chunkSection.getPalette();
int bitsPerBlock = 4; int bitsPerBlock = 4;
while (palette.size() > 1 << bitsPerBlock) { while (palette.size() > 1 << bitsPerBlock) {
@ -87,8 +86,8 @@ 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, palette.size());
for (int mappedId : palette) { for (int i = 0; i < palette.size(); i++) {
Type.VAR_INT.write(buffer, mappedId); Type.VAR_INT.write(buffer, palette.inverse().get(i));
} }
} }

Datei anzeigen

@ -7,7 +7,6 @@ import us.myles.ViaVersion.api.type.Type;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
import java.util.List;
public class ChunkSectionType1_8 extends Type<ChunkSection> { public class ChunkSectionType1_8 extends Type<ChunkSection> {
@ -18,8 +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();
List<Integer> palette = chunkSection.getPalette(); chunkSection.getPalette().clear();
palette.clear();
byte[] blockData = new byte[ChunkSection.SIZE * 2]; byte[] blockData = new byte[ChunkSection.SIZE * 2];
buffer.readBytes(blockData); buffer.readBytes(blockData);

Datei anzeigen

@ -1,11 +1,10 @@
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;
import java.util.List;
public class ChunkSectionType1_9 extends Type<ChunkSection> { public class ChunkSectionType1_9 extends Type<ChunkSection> {
public ChunkSectionType1_9() { public ChunkSectionType1_9() {
@ -15,7 +14,7 @@ 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();
List<Integer> palette = chunkSection.getPalette(); BiMap<Integer, Integer> palette = chunkSection.getPalette();
palette.clear(); palette.clear();
// Reaad bits per block // Reaad bits per block
@ -35,7 +34,7 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
// Read palette // Read palette
for (int i = 0; i < paletteLength; i++) { for (int i = 0; i < paletteLength; i++) {
if (bitsPerBlock != 13) { if (bitsPerBlock != 13) {
palette.add(Type.VAR_INT.read(buffer)); palette.put(Type.VAR_INT.read(buffer), palette.size());
} else { } else {
Type.VAR_INT.read(buffer); Type.VAR_INT.read(buffer);
} }
@ -73,7 +72,7 @@ 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 {
List<Integer> palette = chunkSection.getPalette(); BiMap<Integer, Integer> palette = chunkSection.getPalette();
int bitsPerBlock = 4; int bitsPerBlock = 4;
while (palette.size() > 1 << bitsPerBlock) { while (palette.size() > 1 << bitsPerBlock) {
@ -84,8 +83,8 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
// Write pallet // Write pallet
Type.VAR_INT.write(buffer, palette.size()); Type.VAR_INT.write(buffer, palette.size());
for (int mappedId : palette) { for (int i = 0; i < palette.size(); i++) {
Type.VAR_INT.write(buffer, mappedId); Type.VAR_INT.write(buffer, palette.inverse().get(i));
} }
int length = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0); int length = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0);

Datei anzeigen

@ -1,5 +1,6 @@
package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets; 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.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; import us.myles.ViaVersion.api.minecraft.BlockChangeRecord;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
@ -27,17 +28,13 @@ public class WorldPackets {
Chunk chunk = wrapper.passthrough(new Chunk1_13Type(clientWorld)); Chunk chunk = wrapper.passthrough(new Chunk1_13Type(clientWorld));
for (ChunkSection section : chunk.getSections()) { for (ChunkSection section : chunk.getSections()) {
if (section != null) { if (section == null) continue;
for (int i = 0; i < section.getPalette().size(); i++) { BiMap<Integer, Integer> inverse = section.getPalette().inverse();
section.getPalette().set( for (int i = 0; i < inverse.size(); i++) {
i, inverse.put(i, Protocol1_13_1To1_13.getNewBlockStateId(inverse.get(i)));
Protocol1_13_1To1_13.getNewBlockStateId(section.getPalette().get(i))
);
} }
} }
} }
}
}); });
} }
}); });

Datei anzeigen

@ -2,6 +2,7 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.collect.BiMap;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
@ -241,13 +242,14 @@ public class WorldPackets {
boolean willStoreAnyBlock = false; boolean willStoreAnyBlock = false;
for (int p = 0; p < section.getPalette().size(); p++) { BiMap<Integer, Integer> inverse = section.getPalette().inverse();
int old = section.getPalette().get(p); for (int p = 0; p < inverse.size(); p++) {
int old = inverse.get(p);
int newId = toNewId(old); int newId = toNewId(old);
if (storage.isWelcome(newId)) { if (storage.isWelcome(newId)) {
willStoreAnyBlock = true; willStoreAnyBlock = true;
} }
section.getPalette().set(p, newId); inverse.put(p, newId);
} }
if (willStoreAnyBlock) { if (willStoreAnyBlock) {

Datei anzeigen

@ -1,5 +1,6 @@
package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets; package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets;
import com.google.common.collect.BiMap;
import com.google.common.primitives.Bytes; import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
@ -110,13 +111,14 @@ public class WorldPackets {
for (ChunkSection section : chunk.getSections()) { for (ChunkSection section : chunk.getSections()) {
if (section == null) continue; if (section == null) continue;
boolean hasBlock = false; boolean hasBlock = false;
for (int i = 0; i < section.getPalette().size(); i++) { BiMap<Integer, Integer> inverse = section.getPalette().inverse();
int old = section.getPalette().get(i); for (int i = 0; i < inverse.size(); i++) {
int old = inverse.get(i);
int newId = Protocol1_14To1_13_2.getNewBlockStateId(old); int newId = Protocol1_14To1_13_2.getNewBlockStateId(old);
if (!hasBlock && newId != AIR && newId != VOID_AIR && newId != CAVE_AIR) { // air, void_air, cave_air if (!hasBlock && newId != AIR && newId != VOID_AIR && newId != CAVE_AIR) { // air, void_air, cave_air
hasBlock = true; hasBlock = true;
} }
section.getPalette().set(i, newId); inverse.put(i, newId);
} }
if (!hasBlock) { if (!hasBlock) {
section.setNonAirBlocksCount(0); section.setNonAirBlocksCount(0);

Datei anzeigen

@ -54,10 +54,8 @@ public class Chunk1_9_1_2Type extends PartialType<Chunk, ClientWorld> {
if (world.getEnvironment() == Environment.NORMAL) { if (world.getEnvironment() == Environment.NORMAL) {
section.readSkyLight(input); section.readSkyLight(input);
} }
if (replacePistons) { if (replacePistons && section.getPalette().containsKey(36)) {
if (section.getPalette().contains(36)) { section.getPalette().put(replacementId, section.getPalette().remove(36));
section.getPalette().set(section.getPalette().indexOf(36), replacementId);
}
} }
} }

Datei anzeigen

@ -89,8 +89,8 @@ public class Chunk1_9to1_8Type extends PartialType<Chunk, ClientChunks> {
ChunkSection section = Types1_8.CHUNK_SECTION.read(input); ChunkSection section = Types1_8.CHUNK_SECTION.read(input);
sections[i] = section; sections[i] = section;
if (replacePistons && section.getPalette().contains(36)) { if (replacePistons && section.getPalette().containsKey(36)) {
section.getPalette().set(section.getPalette().indexOf(36), replacementId); section.getPalette().put(replacementId, section.getPalette().remove(36));
} }
} }