mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-06 15:52:03 +00:00
优化调色盘判断
This commit is contained in:
@@ -6,7 +6,7 @@ import net.momirealms.craftengine.core.registry.WritableRegistry;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
|
||||
public class BlockEntityTypes {
|
||||
public abstract class BlockEntityTypes {
|
||||
|
||||
public static <T extends BlockEntity> BlockEntityType<T> register(Key id, BlockEntity.Factory<T> factory) {
|
||||
BlockEntityType<T> type = new BlockEntityType<>(id, factory);
|
||||
|
||||
@@ -10,7 +10,7 @@ import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BlockEntityElementConfigs {
|
||||
public abstract class BlockEntityElementConfigs {
|
||||
public static final Key ITEM_DISPLAY = Key.of("craftengine:item_display");
|
||||
public static final Key TEXT_DISPLAY = Key.of("craftengine:text_display");
|
||||
|
||||
|
||||
@@ -35,15 +35,36 @@ public class Int2ObjectBiMap<K> implements IndexedIterable<K> {
|
||||
|
||||
public void remapValues(Function<K, K> function) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] == null) break;
|
||||
values[i] = function.apply(values[i]);
|
||||
K prev = values[i];
|
||||
if (prev == null) break;
|
||||
values[i] = function.apply(prev);
|
||||
}
|
||||
for (int i = 0; i < idToValues.length; i++) {
|
||||
if (idToValues[i] == null) break;
|
||||
idToValues[i] = function.apply(idToValues[i]);
|
||||
K prev = idToValues[i];
|
||||
if (prev == null) break;
|
||||
idToValues[i] = function.apply(prev);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean remapValuesAndCheck(Function<K, K> function) {
|
||||
boolean changed = false;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
K prev = values[i];
|
||||
if (prev == null) break;
|
||||
K apply = function.apply(prev);
|
||||
values[i] = apply;
|
||||
if (apply != prev) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < idToValues.length; i++) {
|
||||
K prev = idToValues[i];
|
||||
if (prev == null) break;
|
||||
idToValues[i] = function.apply(prev);
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
public static <A> Int2ObjectBiMap<A> create(int expectedSize) {
|
||||
return new Int2ObjectBiMap<>((int) ((float) expectedSize / LOAD_FACTOR));
|
||||
}
|
||||
|
||||
@@ -108,11 +108,27 @@ public class ArrayPalette<T> implements Palette<T> {
|
||||
@Override
|
||||
public void remap(Function<T, T> function) {
|
||||
for (int i = 0; i < this.array.length; i++) {
|
||||
if (this.array[i] == null) return;
|
||||
this.array[i] = function.apply(this.array[i]);
|
||||
T prev = this.array[i];
|
||||
if (prev == null) return;
|
||||
this.array[i] = function.apply(prev);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remapAndCheck(Function<T, T> function) {
|
||||
boolean changed = false;
|
||||
for (int i = 0; i < this.array.length; i++) {
|
||||
T prev = this.array[i];
|
||||
if (prev == null) return changed;
|
||||
T newV = function.apply(prev);
|
||||
this.array[i] = newV;
|
||||
if (newV != prev) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRemap() {
|
||||
return true;
|
||||
|
||||
@@ -106,6 +106,11 @@ public class BiMapPalette<T> implements Palette<T> {
|
||||
this.map.remapValues(function);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remapAndCheck(Function<T, T> function) {
|
||||
return this.map.remapValuesAndCheck(function);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRemap() {
|
||||
return true;
|
||||
|
||||
@@ -63,6 +63,11 @@ public class IdListPalette<T> implements Palette<T> {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remapAndCheck(Function<T, T> function) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readPacket(FriendlyByteBuf buf) {
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ public interface Palette<T> {
|
||||
|
||||
void remap(Function<T, T> function);
|
||||
|
||||
boolean remapAndCheck(Function<T, T> function);
|
||||
|
||||
boolean canRemap();
|
||||
|
||||
interface Factory {
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.function.Predicate;
|
||||
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() ?
|
||||
@@ -75,10 +74,7 @@ public class PalettedContainer<T> implements PaletteResizeListener<T>, ReadableC
|
||||
return false;
|
||||
}
|
||||
|
||||
public PalettedContainer<T> downgradeTo(IndexedIterable<T> idList) {
|
||||
if (!NEED_DOWNGRADE) {
|
||||
return this;
|
||||
}
|
||||
public PalettedContainer<T> getClientCompatiblePalettedContainer(IndexedIterable<T> idList) {
|
||||
Palette<T> palette = this.data.palette;
|
||||
if (!(palette instanceof IdListPalette<T> idListPalette)) {
|
||||
return this;
|
||||
|
||||
@@ -73,6 +73,13 @@ public class SingularPalette<T> implements Palette<T> {
|
||||
this.entry = function.apply(this.entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remapAndCheck(Function<T, T> function) {
|
||||
T previous = this.entry;
|
||||
this.entry = function.apply(previous);
|
||||
return previous == this.entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRemap() {
|
||||
return true;
|
||||
|
||||
@@ -26,7 +26,7 @@ public class MCSection {
|
||||
|
||||
public void writePacket(FriendlyByteBuf buf) {
|
||||
buf.writeShort(this.nonEmptyBlockCount);
|
||||
this.serverBlockStateContainer.downgradeTo(this.clientBlockStateList).writePacket(buf);
|
||||
this.serverBlockStateContainer.getClientCompatiblePalettedContainer(this.clientBlockStateList).writePacket(buf);
|
||||
this.biomeContainer.writePacket(buf);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user