Merge pull request #590 from DaMatrix/palette-optimization

allow duplicate entries in palette
This commit is contained in:
Steven Smith 2020-10-18 14:36:20 -07:00 committed by GitHub
commit 1b01b1ffef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 7 deletions

View file

@ -33,13 +33,7 @@ public class Chunk {
int blockCount = in.readShort();
int bitsPerEntry = in.readUnsignedByte();
Palette palette = createPalette(bitsPerEntry);
if(!(palette instanceof GlobalPalette)) {
int paletteLength = in.readVarInt();
for(int i = 0; i < paletteLength; i++) {
palette.stateToId(in.readVarInt());
}
}
Palette palette = readPalette(bitsPerEntry, in);
BitStorage storage = new BitStorage(bitsPerEntry, CHUNK_SIZE, in.readLongs(in.readVarInt()));
return new Chunk(blockCount, palette, storage);
@ -120,6 +114,16 @@ public class Chunk {
}
}
private static Palette readPalette(int bitsPerEntry, NetInput in) throws IOException {
if(bitsPerEntry <= MIN_PALETTE_BITS_PER_ENTRY) {
return new ListPalette(bitsPerEntry, in);
} else if(bitsPerEntry <= MAX_PALETTE_BITS_PER_ENTRY) {
return new MapPalette(bitsPerEntry, in);
} else {
return new GlobalPalette();
}
}
private static int index(int x, int y, int z) {
return y << 8 | z << 4 | x;
}

View file

@ -1,7 +1,10 @@
package com.github.steveice10.mc.protocol.data.game.chunk.palette;
import com.github.steveice10.packetlib.io.NetInput;
import lombok.EqualsAndHashCode;
import java.io.IOException;
/**
* A palette backed by a List.
*/
@ -18,6 +21,16 @@ public class ListPalette implements Palette {
this.data = new int[this.maxId + 1];
}
public ListPalette(int bitsPerEntry, NetInput in) throws IOException {
this(bitsPerEntry);
int paletteLength = in.readVarInt();
for(int i = 0; i < paletteLength; i++) {
this.data[i] = in.readVarInt();
}
this.nextId = paletteLength;
}
@Override
public int size() {
return this.nextId;

View file

@ -1,9 +1,12 @@
package com.github.steveice10.mc.protocol.data.game.chunk.palette;
import com.github.steveice10.packetlib.io.NetInput;
import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.collection.IntObjectMap;
import lombok.EqualsAndHashCode;
import java.io.IOException;
/**
* A palette backed by a map.
*/
@ -21,6 +24,18 @@ public class MapPalette implements Palette {
this.idToState = new int[this.maxId + 1];
}
public MapPalette(int bitsPerEntry, NetInput in) throws IOException {
this(bitsPerEntry);
int paletteLength = in.readVarInt();
for(int i = 0; i < paletteLength; i++) {
int state = in.readVarInt();
this.idToState[i] = state;
this.stateToId.putIfAbsent(state, i);
}
this.nextId = paletteLength;
}
@Override
public int size() {
return this.nextId;