mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-27 02:49:15 +00:00
修复全局调色盘
This commit is contained in:
@@ -39,6 +39,10 @@ public class IdListPalette<T> implements Palette<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public IndexedIterable<T> idList() {
|
||||
return idList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return this.idList.size();
|
||||
|
||||
@@ -16,13 +16,11 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.IntUnaryOperator;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.*;
|
||||
import java.util.stream.LongStream;
|
||||
|
||||
public class PalettedContainer<T> implements PaletteResizeListener<T>, ReadableContainer<T> {
|
||||
public static boolean NEED_DOWNGRADE = true;
|
||||
private static final BiConsumer<FriendlyByteBuf, long[]> RAW_DATA_WRITER = VersionHelper.isOrAbove1_21_5() ?
|
||||
(FriendlyByteBuf::writeFixedSizeLongArray) : (FriendlyByteBuf::writeLongArray);
|
||||
private static final BiConsumer<FriendlyByteBuf, long[]> RAW_DATA_READER = VersionHelper.isOrAbove1_21_5() ?
|
||||
@@ -74,31 +72,34 @@ public class PalettedContainer<T> implements PaletteResizeListener<T>, ReadableC
|
||||
return false;
|
||||
}
|
||||
|
||||
public PalettedContainer<T> downgradeTo(IndexedIterable<T> idList) {
|
||||
if (!NEED_DOWNGRADE) {
|
||||
return this;
|
||||
}
|
||||
Palette<T> palette = this.data.palette;
|
||||
if (!(palette instanceof IdListPalette<T> idListPalette)) {
|
||||
return this;
|
||||
}
|
||||
Data<T> newData = getCompatibleData(this.data, idList, 128);
|
||||
newData.importFrom(idListPalette, this.data.storage);
|
||||
return new PalettedContainer<>(idList, PaletteProvider.BLOCK_STATE, newData);
|
||||
}
|
||||
|
||||
public Data<T> data() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void readPacket(FriendlyByteBuf buf) {
|
||||
this.lock();
|
||||
try {
|
||||
int i = buf.readByte();
|
||||
Data<T> data = this.getCompatibleData(this.data, i);
|
||||
data.palette.readPacket(buf);
|
||||
RAW_DATA_READER.accept(buf, data.storage.getData());
|
||||
this.data = data;
|
||||
} finally {
|
||||
this.unlock();
|
||||
}
|
||||
int i = buf.readByte();
|
||||
Data<T> data = this.getCompatibleData(this.data, i);
|
||||
data.palette.readPacket(buf);
|
||||
RAW_DATA_READER.accept(buf, data.storage.getData());
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writePacket(FriendlyByteBuf buf) {
|
||||
this.lock();
|
||||
try {
|
||||
this.data.writePacket(buf);
|
||||
} finally {
|
||||
this.unlock();
|
||||
}
|
||||
this.data.writePacket(buf);
|
||||
}
|
||||
|
||||
private Data<T> getCompatibleData(@Nullable Data<T> previousData, int bits) {
|
||||
@@ -106,6 +107,11 @@ public class PalettedContainer<T> implements PaletteResizeListener<T>, ReadableC
|
||||
return previousData != null && dataProvider.equals(previousData.configuration()) ? previousData : dataProvider.createData(this.idList, this, this.paletteProvider.getContainerSize());
|
||||
}
|
||||
|
||||
private Data<T> getCompatibleData(@Nullable Data<T> previousData, IndexedIterable<T> idList, int bits) {
|
||||
DataProvider<T> dataProvider = this.paletteProvider.createDataProvider(idList, bits);
|
||||
return previousData != null && dataProvider.equals(previousData.configuration()) ? previousData : dataProvider.createData(this.idList, this, this.paletteProvider.getContainerSize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onResize(int i, T object) {
|
||||
Data<T> oldData = this.data;
|
||||
|
||||
@@ -7,17 +7,19 @@ import net.momirealms.craftengine.core.world.chunk.ReadableContainer;
|
||||
|
||||
public class MCSection {
|
||||
private short nonEmptyBlockCount;
|
||||
private final PalettedContainer<Integer> blockStateContainer;
|
||||
private final PalettedContainer<Integer> serverBlockStateContainer;
|
||||
private final IndexedIterable<Integer> clientBlockStateList;
|
||||
private ReadableContainer<Integer> biomeContainer;
|
||||
|
||||
public MCSection(IndexedIterable<Integer> blockStateList, IndexedIterable<Integer> biomeList) {
|
||||
this.blockStateContainer = new PalettedContainer<>(blockStateList, 0, PalettedContainer.PaletteProvider.BLOCK_STATE);
|
||||
public MCSection(IndexedIterable<Integer> clientBlockStateList, IndexedIterable<Integer> serverBlockStateList, IndexedIterable<Integer> biomeList) {
|
||||
this.serverBlockStateContainer = new PalettedContainer<>(serverBlockStateList, 0, PalettedContainer.PaletteProvider.BLOCK_STATE);
|
||||
this.biomeContainer = new PalettedContainer<>(biomeList, 0, PalettedContainer.PaletteProvider.BIOME);
|
||||
this.clientBlockStateList = clientBlockStateList;
|
||||
}
|
||||
|
||||
public void readPacket(FriendlyByteBuf buf) {
|
||||
this.nonEmptyBlockCount = buf.readShort();
|
||||
this.blockStateContainer.readPacket(buf);
|
||||
this.serverBlockStateContainer.readPacket(buf);
|
||||
PalettedContainer<Integer> palettedContainer = this.biomeContainer.slice();
|
||||
palettedContainer.readPacket(buf);
|
||||
this.biomeContainer = palettedContainer;
|
||||
@@ -25,19 +27,19 @@ public class MCSection {
|
||||
|
||||
public void writePacket(FriendlyByteBuf buf) {
|
||||
buf.writeShort(this.nonEmptyBlockCount);
|
||||
this.blockStateContainer.writePacket(buf);
|
||||
this.serverBlockStateContainer.downgradeTo(this.clientBlockStateList).writePacket(buf);
|
||||
this.biomeContainer.writePacket(buf);
|
||||
}
|
||||
|
||||
public void setBlockState(int x, int y, int z, int state) {
|
||||
this.blockStateContainer.set(x, y, z, state);
|
||||
this.serverBlockStateContainer.set(x, y, z, state);
|
||||
}
|
||||
|
||||
public int getBlockState(int x, int y, int z) {
|
||||
return this.blockStateContainer.get(x, y, z);
|
||||
return this.serverBlockStateContainer.get(x, y, z);
|
||||
}
|
||||
|
||||
public PalettedContainer<Integer> blockStateContainer() {
|
||||
return blockStateContainer;
|
||||
return serverBlockStateContainer;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user