9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-06 15:52:03 +00:00

优化一对一列表实现

This commit is contained in:
XiaoMoMi
2025-09-17 04:54:51 +08:00
parent 811523a0c8
commit 6faf6ca88b
3 changed files with 45 additions and 12 deletions

View File

@@ -87,14 +87,15 @@ import org.jetbrains.annotations.Nullable;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class PacketConsumers {
private static BukkitNetworkManager.Handlers[] ADD_ENTITY_HANDLERS;
private static int[] BLOCK_STATE_MAPPINGS;
private static int[] MOD_BLOCK_STATE_MAPPINGS;
private static IntIdentityList SERVER_BLOCK_LIST;
private static IntIdentityList CLIENT_BLOCK_LIST;
private static IntIdentityList BIOME_LIST;
private static Consumer<PalettedContainer<Integer>> BIOME_MAPPER;
public static void initEntities(int registrySize) {
ADD_ENTITY_HANDLERS = new BukkitNetworkManager.Handlers[registrySize];
@@ -230,6 +231,16 @@ public class PacketConsumers {
};
}
public static void setBiomeMapper(Consumer<PalettedContainer<Integer>> mapper) {
BIOME_MAPPER = mapper;
}
public static void remapBiomes(PalettedContainer<Integer> biomes) {
if (BIOME_MAPPER != null) {
BIOME_MAPPER.accept(biomes);
}
}
public static void initBlocks(Map<Integer, Integer> map, int registrySize) {
int[] newMappings = new int[registrySize];
for (int i = 0; i < registrySize; i++) {
@@ -250,7 +261,6 @@ public class PacketConsumers {
BLOCK_STATE_MAPPINGS = newMappings;
MOD_BLOCK_STATE_MAPPINGS = newMappingsMOD;
SERVER_BLOCK_LIST = new IntIdentityList(registrySize);
CLIENT_BLOCK_LIST = new IntIdentityList(BlockStateUtils.vanillaStateSize());
BIOME_LIST = new IntIdentityList(RegistryUtils.currentBiomeRegistrySize());
}
@@ -340,6 +350,7 @@ public class PacketConsumers {
MCSection mcSection = new MCSection(user.clientBlockList(), SERVER_BLOCK_LIST, BIOME_LIST);
mcSection.readPacket(friendlyByteBuf);
PalettedContainer<Integer> container = mcSection.blockStateContainer();
remapBiomes(mcSection.biomeContainer());
Palette<Integer> palette = container.data().palette();
if (palette.canRemap()) {
palette.remap(PacketConsumers::remapMOD);
@@ -363,6 +374,7 @@ public class PacketConsumers {
MCSection mcSection = new MCSection(user.clientBlockList(), SERVER_BLOCK_LIST, BIOME_LIST);
mcSection.readPacket(friendlyByteBuf);
PalettedContainer<Integer> container = mcSection.blockStateContainer();
remapBiomes(mcSection.biomeContainer());
Palette<Integer> palette = container.data().palette();
if (palette.canRemap()) {
palette.remap(PacketConsumers::remap);

View File

@@ -1,22 +1,16 @@
package net.momirealms.craftengine.core.util;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class IntIdentityList implements IndexedIterable<Integer> {
private final int size;
private final List<Integer> list;
public IntIdentityList(int size) {
this.size = size;
list = new IntArrayList(size);
for (int i = 0; i < size; i++) {
list.add(i);
}
}
@Override
@@ -36,6 +30,29 @@ public class IntIdentityList implements IndexedIterable<Integer> {
@Override
public @NotNull Iterator<Integer> iterator() {
return list.iterator();
return new IntIterator(size);
}
}
private static class IntIterator implements Iterator<Integer> {
private final int size;
private int current;
public IntIterator(int size) {
this.size = size;
this.current = 0;
}
@Override
public boolean hasNext() {
return current < size;
}
@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return current++;
}
}
}

View File

@@ -9,7 +9,7 @@ public class MCSection {
private short nonEmptyBlockCount;
private final PalettedContainer<Integer> serverBlockStateContainer;
private final IndexedIterable<Integer> clientBlockStateList;
private ReadableContainer<Integer> biomeContainer;
private PalettedContainer<Integer> biomeContainer;
public MCSection(IndexedIterable<Integer> clientBlockStateList, IndexedIterable<Integer> serverBlockStateList, IndexedIterable<Integer> biomeList) {
this.serverBlockStateContainer = new PalettedContainer<>(serverBlockStateList, 0, PalettedContainer.PaletteProvider.BLOCK_STATE);
@@ -42,4 +42,8 @@ public class MCSection {
public PalettedContainer<Integer> blockStateContainer() {
return serverBlockStateContainer;
}
public PalettedContainer<Integer> biomeContainer() {
return biomeContainer;
}
}