Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-03 14:50:30 +01:00
Use FU collections in ChunkSection, pre-size palette
Dieser Commit ist enthalten in:
Ursprung
074edf1b48
Commit
414f561673
@ -1,13 +1,12 @@
|
|||||||
package us.myles.ViaVersion.api.minecraft.chunks;
|
package us.myles.ViaVersion.api.minecraft.chunks;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||||
|
import it.unimi.dsi.fastutil.ints.IntList;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ChunkSection {
|
public class ChunkSection {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,8 +17,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)
|
||||||
private final List<Integer> palette = new ArrayList<>();
|
private final IntList palette;
|
||||||
private final Map<Integer, Integer> inversePalette = new HashMap<>();
|
private final Int2IntMap inversePalette;
|
||||||
private final int[] blocks;
|
private final int[] blocks;
|
||||||
private NibbleArray blockLight;
|
private NibbleArray blockLight;
|
||||||
private NibbleArray skyLight;
|
private NibbleArray skyLight;
|
||||||
@ -28,7 +27,19 @@ 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);
|
||||||
addPaletteEntry(0);
|
palette = new IntArrayList();
|
||||||
|
inversePalette = new Int2IntOpenHashMap();
|
||||||
|
inversePalette.defaultReturnValue(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChunkSection(int expectedPaletteLength) {
|
||||||
|
this.blocks = new int[SIZE];
|
||||||
|
this.blockLight = new NibbleArray(SIZE);
|
||||||
|
|
||||||
|
// Pre-size the palette array/map
|
||||||
|
palette = new IntArrayList(expectedPaletteLength);
|
||||||
|
inversePalette = new Int2IntOpenHashMap(expectedPaletteLength);
|
||||||
|
inversePalette.defaultReturnValue(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,12 +70,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.getInt(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.getInt(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBlock(int idx, int type, int data) {
|
public void setBlock(int idx, int type, int data) {
|
||||||
@ -85,18 +96,20 @@ 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.get(index);
|
return palette.getInt(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();
|
||||||
|
|
||||||
int oldId = palette.set(index, id);
|
int oldId = palette.set(index, id);
|
||||||
if (oldId == id) return;
|
if (oldId == id) return;
|
||||||
|
|
||||||
inversePalette.put(id, index);
|
inversePalette.put(id, index);
|
||||||
if (inversePalette.get(oldId) == index) {
|
if (inversePalette.get(oldId) == index) {
|
||||||
inversePalette.remove(oldId);
|
inversePalette.remove(oldId);
|
||||||
for (int i = 0; i < palette.size(); i++) {
|
for (int i = 0; i < palette.size(); i++) {
|
||||||
if (palette.get(i) == oldId) {
|
if (palette.getInt(i) == oldId) {
|
||||||
inversePalette.put(oldId, i);
|
inversePalette.put(oldId, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -105,11 +118,14 @@ public class ChunkSection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void replacePaletteEntry(int oldId, int newId) {
|
public void replacePaletteEntry(int oldId, int newId) {
|
||||||
Integer index = inversePalette.remove(oldId);
|
int index = inversePalette.remove(oldId);
|
||||||
if (index == null) return;
|
if (index == -1) return;
|
||||||
|
|
||||||
inversePalette.put(newId, index);
|
inversePalette.put(newId, index);
|
||||||
for (int i = 0; i < palette.size(); i++) {
|
for (int i = 0; i < palette.size(); i++) {
|
||||||
if (palette.get(i) == oldId) palette.set(i, newId);
|
if (palette.getInt(i) == oldId) {
|
||||||
|
palette.set(i, newId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,8 +147,8 @@ 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 = inversePalette.get(id);
|
int index = inversePalette.get(id);
|
||||||
if (index == null) {
|
if (index == -1) {
|
||||||
index = palette.size();
|
index = palette.size();
|
||||||
palette.add(id);
|
palette.add(id);
|
||||||
inversePalette.put(id, index);
|
inversePalette.put(id, index);
|
||||||
|
@ -14,8 +14,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();
|
|
||||||
|
|
||||||
// Reaad bits per block
|
// Reaad bits per block
|
||||||
int bitsPerBlock = buffer.readUnsignedByte();
|
int bitsPerBlock = buffer.readUnsignedByte();
|
||||||
int originalBitsPerBlock = bitsPerBlock;
|
int originalBitsPerBlock = bitsPerBlock;
|
||||||
@ -24,11 +22,16 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
|
|||||||
bitsPerBlock = GLOBAL_PALETTE;
|
bitsPerBlock = GLOBAL_PALETTE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int paletteLength = bitsPerBlock == GLOBAL_PALETTE ? 0 : Type.VAR_INT.readPrimitive(buffer);
|
|
||||||
// Read palette
|
// Read palette
|
||||||
chunkSection.clearPalette();
|
ChunkSection chunkSection;
|
||||||
for (int i = 0; i < paletteLength; i++) {
|
if (bitsPerBlock != GLOBAL_PALETTE) {
|
||||||
chunkSection.addPaletteEntry(Type.VAR_INT.readPrimitive(buffer));
|
int paletteLength = Type.VAR_INT.readPrimitive(buffer);
|
||||||
|
chunkSection = new ChunkSection(paletteLength);
|
||||||
|
for (int i = 0; i < paletteLength; i++) {
|
||||||
|
chunkSection.addPaletteEntry(Type.VAR_INT.readPrimitive(buffer));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chunkSection = new ChunkSection();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read blocks
|
// Read blocks
|
||||||
|
@ -14,8 +14,6 @@ public class ChunkSectionType1_16 extends Type<ChunkSection> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChunkSection read(ByteBuf buffer) throws Exception {
|
public ChunkSection read(ByteBuf buffer) throws Exception {
|
||||||
ChunkSection chunkSection = new ChunkSection();
|
|
||||||
|
|
||||||
// Reaad bits per block
|
// Reaad bits per block
|
||||||
int bitsPerBlock = buffer.readUnsignedByte();
|
int bitsPerBlock = buffer.readUnsignedByte();
|
||||||
int originalBitsPerBlock = bitsPerBlock;
|
int originalBitsPerBlock = bitsPerBlock;
|
||||||
@ -24,11 +22,16 @@ public class ChunkSectionType1_16 extends Type<ChunkSection> {
|
|||||||
bitsPerBlock = GLOBAL_PALETTE;
|
bitsPerBlock = GLOBAL_PALETTE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int paletteLength = bitsPerBlock == GLOBAL_PALETTE ? 0 : Type.VAR_INT.readPrimitive(buffer);
|
|
||||||
// Read palette
|
// Read palette
|
||||||
chunkSection.clearPalette();
|
ChunkSection chunkSection;
|
||||||
for (int i = 0; i < paletteLength; i++) {
|
if (bitsPerBlock != GLOBAL_PALETTE) {
|
||||||
chunkSection.addPaletteEntry(Type.VAR_INT.readPrimitive(buffer));
|
int paletteLength = Type.VAR_INT.readPrimitive(buffer);
|
||||||
|
chunkSection = new ChunkSection(paletteLength);
|
||||||
|
for (int i = 0; i < paletteLength; i++) {
|
||||||
|
chunkSection.addPaletteEntry(Type.VAR_INT.readPrimitive(buffer));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chunkSection = new ChunkSection();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read blocks
|
// Read blocks
|
||||||
|
@ -15,7 +15,8 @@ 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();
|
||||||
// Don't clear palette because 0 index needs to be air in 1.9 version
|
// 0 index needs to be air in 1.9
|
||||||
|
chunkSection.addPaletteEntry(0);
|
||||||
|
|
||||||
ByteBuf littleEndianView = buffer.order(ByteOrder.LITTLE_ENDIAN);
|
ByteBuf littleEndianView = buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
|
||||||
|
@ -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();
|
|
||||||
|
|
||||||
// Reaad bits per block
|
// Reaad bits per block
|
||||||
int bitsPerBlock = buffer.readUnsignedByte();
|
int bitsPerBlock = buffer.readUnsignedByte();
|
||||||
int originalBitsPerBlock = bitsPerBlock;
|
int originalBitsPerBlock = bitsPerBlock;
|
||||||
@ -29,9 +27,10 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
|
|||||||
if (bitsPerBlock > 8) {
|
if (bitsPerBlock > 8) {
|
||||||
bitsPerBlock = GLOBAL_PALETTE;
|
bitsPerBlock = GLOBAL_PALETTE;
|
||||||
}
|
}
|
||||||
int paletteLength = Type.VAR_INT.readPrimitive(buffer);
|
|
||||||
// Read palette
|
// Read palette
|
||||||
chunkSection.clearPalette();
|
int paletteLength = Type.VAR_INT.readPrimitive(buffer);
|
||||||
|
ChunkSection chunkSection = bitsPerBlock != GLOBAL_PALETTE ? new ChunkSection(paletteLength) : new ChunkSection();
|
||||||
for (int i = 0; i < paletteLength; i++) {
|
for (int i = 0; i < paletteLength; i++) {
|
||||||
if (bitsPerBlock != GLOBAL_PALETTE) {
|
if (bitsPerBlock != GLOBAL_PALETTE) {
|
||||||
chunkSection.addPaletteEntry(Type.VAR_INT.readPrimitive(buffer));
|
chunkSection.addPaletteEntry(Type.VAR_INT.readPrimitive(buffer));
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren