Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-03 14:50:30 +01:00
Merge pull request #1084 from Gerrygames/faster_palette
Faster palette
Dieser Commit ist enthalten in:
Commit
ca716a65d2
@ -1,11 +1,13 @@
|
||||
package us.myles.ViaVersion.api.minecraft.chunks;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ChunkSection {
|
||||
/**
|
||||
@ -16,8 +18,8 @@ public class ChunkSection {
|
||||
* 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)
|
||||
@Getter
|
||||
private final List<Integer> palette = Lists.newArrayList();
|
||||
private List<Integer> palette = new ArrayList<>();
|
||||
private Map<Integer, Integer> inversePalette = new HashMap<>();
|
||||
private final int[] blocks;
|
||||
private NibbleArray blockLight;
|
||||
private NibbleArray skyLight;
|
||||
@ -28,7 +30,8 @@ public class ChunkSection {
|
||||
public ChunkSection() {
|
||||
this.blocks = new int[SIZE];
|
||||
this.blockLight = new NibbleArray(SIZE);
|
||||
palette.add(0); // AIR
|
||||
palette.add(0);
|
||||
inversePalette.put(0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,6 +82,40 @@ public class ChunkSection {
|
||||
return blocks[idx];
|
||||
}
|
||||
|
||||
public int getPaletteSize() {
|
||||
return palette.size();
|
||||
}
|
||||
|
||||
public int getPaletteEntry(int index) {
|
||||
if (index < 0 || index >= palette.size()) throw new IndexOutOfBoundsException();
|
||||
return palette.get(index);
|
||||
}
|
||||
|
||||
public void setPaletteEntry(int index, int id) {
|
||||
if (index < 0 || index >= palette.size()) throw new IndexOutOfBoundsException();
|
||||
palette.set(index, id);
|
||||
inversePalette.put(id, index);
|
||||
}
|
||||
|
||||
public void replacePaletteEntry(int oldId, int newId) {
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a block state in the chunk
|
||||
* This method will not update non-air blocks count
|
||||
@ -87,13 +124,14 @@ public class ChunkSection {
|
||||
* @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 = inversePalette.get(id);
|
||||
if (index == null) {
|
||||
index = palette.size();
|
||||
palette.add(id);
|
||||
inversePalette.put(id, index);
|
||||
}
|
||||
|
||||
blocks[idx] = index;
|
||||
blocks[idx] = index;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,8 +4,6 @@ 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<ChunkSection> {
|
||||
|
||||
public ChunkSectionType1_13() {
|
||||
@ -15,8 +13,6 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
|
||||
@Override
|
||||
public ChunkSection read(ByteBuf buffer) throws Exception {
|
||||
ChunkSection chunkSection = new ChunkSection();
|
||||
List<Integer> palette = chunkSection.getPalette();
|
||||
palette.clear();
|
||||
|
||||
// Reaad bits per block
|
||||
int bitsPerBlock = buffer.readUnsignedByte();
|
||||
@ -33,8 +29,9 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
|
||||
}
|
||||
int paletteLength = bitsPerBlock == 14 ? 0 : Type.VAR_INT.read(buffer);
|
||||
// Read palette
|
||||
chunkSection.clearPalette();
|
||||
for (int i = 0; i < paletteLength; i++) {
|
||||
palette.add(Type.VAR_INT.read(buffer));
|
||||
chunkSection.addPaletteEntry(Type.VAR_INT.read(buffer));
|
||||
}
|
||||
|
||||
// Read blocks
|
||||
@ -69,10 +66,8 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
|
||||
|
||||
@Override
|
||||
public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception {
|
||||
List<Integer> palette = chunkSection.getPalette();
|
||||
|
||||
int bitsPerBlock = 4;
|
||||
while (palette.size() > 1 << bitsPerBlock) {
|
||||
while (chunkSection.getPaletteSize() > 1 << bitsPerBlock) {
|
||||
bitsPerBlock += 1;
|
||||
}
|
||||
|
||||
@ -86,9 +81,9 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
|
||||
|
||||
// Write pallet (or not)
|
||||
if (bitsPerBlock != 14) {
|
||||
Type.VAR_INT.write(buffer, palette.size());
|
||||
for (int mappedId : palette) {
|
||||
Type.VAR_INT.write(buffer, mappedId);
|
||||
Type.VAR_INT.write(buffer, chunkSection.getPaletteSize());
|
||||
for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
|
||||
Type.VAR_INT.write(buffer, chunkSection.getPaletteEntry(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,6 @@ 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<ChunkSection> {
|
||||
|
||||
@ -18,8 +17,7 @@ public class ChunkSectionType1_8 extends Type<ChunkSection> {
|
||||
@Override
|
||||
public ChunkSection read(ByteBuf buffer) throws Exception {
|
||||
ChunkSection chunkSection = new ChunkSection();
|
||||
List<Integer> palette = chunkSection.getPalette();
|
||||
palette.clear();
|
||||
chunkSection.clearPalette();
|
||||
|
||||
byte[] blockData = new byte[ChunkSection.SIZE * 2];
|
||||
buffer.readBytes(blockData);
|
||||
|
@ -1,11 +1,10 @@
|
||||
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<ChunkSection> {
|
||||
|
||||
public ChunkSectionType1_9() {
|
||||
@ -15,8 +14,6 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
|
||||
@Override
|
||||
public ChunkSection read(ByteBuf buffer) throws Exception {
|
||||
ChunkSection chunkSection = new ChunkSection();
|
||||
List<Integer> palette = chunkSection.getPalette();
|
||||
palette.clear();
|
||||
|
||||
// Reaad bits per block
|
||||
int bitsPerBlock = buffer.readUnsignedByte();
|
||||
@ -33,9 +30,10 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
|
||||
}
|
||||
int paletteLength = Type.VAR_INT.read(buffer);
|
||||
// Read palette
|
||||
chunkSection.clearPalette();
|
||||
for (int i = 0; i < paletteLength; i++) {
|
||||
if (bitsPerBlock != 13) {
|
||||
palette.add(Type.VAR_INT.read(buffer));
|
||||
chunkSection.addPaletteEntry(Type.VAR_INT.read(buffer));
|
||||
} else {
|
||||
Type.VAR_INT.read(buffer);
|
||||
}
|
||||
@ -73,19 +71,17 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
|
||||
|
||||
@Override
|
||||
public void write(ByteBuf buffer, ChunkSection chunkSection) throws Exception {
|
||||
List<Integer> palette = chunkSection.getPalette();
|
||||
|
||||
int bitsPerBlock = 4;
|
||||
while (palette.size() > 1 << bitsPerBlock) {
|
||||
while (chunkSection.getPaletteSize() > 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);
|
||||
Type.VAR_INT.write(buffer, chunkSection.getPaletteSize());
|
||||
for (int i = 0; i < chunkSection.getPaletteSize(); i++) {
|
||||
Type.VAR_INT.write(buffer, chunkSection.getPaletteEntry(i));
|
||||
}
|
||||
|
||||
int length = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0);
|
||||
|
@ -27,16 +27,11 @@ 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;
|
||||
for (int i = 0; i < section.getPaletteSize(); i++) {
|
||||
section.setPaletteEntry(i, Protocol1_13_1To1_13.getNewBlockStateId(section.getPaletteEntry(i)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -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,13 @@ public class WorldPackets {
|
||||
|
||||
boolean willStoreAnyBlock = false;
|
||||
|
||||
for (int p = 0; p < section.getPalette().size(); p++) {
|
||||
int old = section.getPalette().get(p);
|
||||
for (int p = 0; p < section.getPaletteSize(); p++) {
|
||||
int old = section.getPaletteEntry(p);
|
||||
int newId = toNewId(old);
|
||||
if (storage.isWelcome(newId)) {
|
||||
willStoreAnyBlock = true;
|
||||
}
|
||||
section.getPalette().set(p, newId);
|
||||
section.setPaletteEntry(p, newId);
|
||||
}
|
||||
|
||||
if (willStoreAnyBlock) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.primitives.Bytes;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import us.myles.ViaVersion.api.PacketWrapper;
|
||||
@ -110,13 +111,13 @@ public class WorldPackets {
|
||||
for (ChunkSection section : chunk.getSections()) {
|
||||
if (section == null) continue;
|
||||
boolean hasBlock = false;
|
||||
for (int i = 0; i < section.getPalette().size(); i++) {
|
||||
int old = section.getPalette().get(i);
|
||||
for (int i = 0; i < section.getPaletteSize(); i++) {
|
||||
int old = section.getPaletteEntry(i);
|
||||
int newId = Protocol1_14To1_13_2.getNewBlockStateId(old);
|
||||
if (!hasBlock && newId != AIR && newId != VOID_AIR && newId != CAVE_AIR) { // air, void_air, cave_air
|
||||
hasBlock = true;
|
||||
}
|
||||
section.getPalette().set(i, newId);
|
||||
section.setPaletteEntry(i, newId);
|
||||
}
|
||||
if (!hasBlock) {
|
||||
section.setNonAirBlocksCount(0);
|
||||
|
@ -55,9 +55,7 @@ public class Chunk1_9_1_2Type extends PartialType<Chunk, ClientWorld> {
|
||||
section.readSkyLight(input);
|
||||
}
|
||||
if (replacePistons) {
|
||||
if (section.getPalette().contains(36)) {
|
||||
section.getPalette().set(section.getPalette().indexOf(36), replacementId);
|
||||
}
|
||||
section.replacePaletteEntry(36, replacementId);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -89,8 +89,8 @@ public class Chunk1_9to1_8Type extends PartialType<Chunk, ClientChunks> {
|
||||
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.replacePaletteEntry(36, replacementId);
|
||||
}
|
||||
}
|
||||
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren