9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-22 08:29:21 +00:00

perf(network): 优化数据包的处理逻辑

This commit is contained in:
jhqwqmc
2025-03-24 17:43:39 +08:00
parent ebf0cd4dea
commit 6e91b3f8cf

View File

@@ -39,6 +39,7 @@ import java.util.function.Consumer;
public class PacketConsumers { public class PacketConsumers {
private static int[] mappings; private static int[] mappings;
private static int[] mappingsMOD;
private static IntIdentityList BLOCK_LIST; private static IntIdentityList BLOCK_LIST;
private static IntIdentityList BIOME_LIST; private static IntIdentityList BIOME_LIST;
@@ -51,6 +52,16 @@ public class PacketConsumers {
for (Map.Entry<Integer, Integer> entry : map.entrySet()) { for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
mappings[entry.getKey()] = entry.getValue(); mappings[entry.getKey()] = entry.getValue();
} }
mappingsMOD = new int[registrySize];
Arrays.fill(mappingsMOD, -1);
for (int i = 0; i < registrySize; i++) {
mappingsMOD[i] = i;
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (BlockStateUtils.isVanillaBlock(entry.getKey())) {
mappingsMOD[entry.getKey()] = entry.getValue();
}
}
BLOCK_LIST = new IntIdentityList(registrySize); BLOCK_LIST = new IntIdentityList(registrySize);
BIOME_LIST = new IntIdentityList(RegistryUtils.currentBiomeRegistrySize()); BIOME_LIST = new IntIdentityList(RegistryUtils.currentBiomeRegistrySize());
} }
@@ -59,6 +70,10 @@ public class PacketConsumers {
return mappings[stateId]; return mappings[stateId];
} }
public static int remapMOD(int stateId) {
return mappingsMOD[stateId];
}
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> LEVEL_CHUNK_WITH_LIGHT = (user, event, packet) -> { public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> LEVEL_CHUNK_WITH_LIGHT = (user, event, packet) -> {
try { try {
if (!user.usingClientMod()) { if (!user.usingClientMod()) {
@@ -105,11 +120,11 @@ public class PacketConsumers {
PalettedContainer<Integer> container = mcSection.blockStateContainer(); PalettedContainer<Integer> container = mcSection.blockStateContainer();
Palette<Integer> palette = container.data().palette(); Palette<Integer> palette = container.data().palette();
if (palette.canRemap()) { if (palette.canRemap()) {
palette.remap(state -> BlockStateUtils.isVanillaBlock(state) ? remap(state) : state); palette.remap(PacketConsumers::remapMOD);
} else { } else {
for (int j = 0; j < 4096; j++) { for (int j = 0; j < 4096; j++) {
int state = container.get(j); int state = container.get(j);
int newState = BlockStateUtils.isVanillaBlock(state) ? remap(state) : state; int newState = remapMOD(state);
if (newState != state) { if (newState != state) {
container.set(j, newState); container.set(j, newState);
} }
@@ -157,8 +172,7 @@ public class PacketConsumers {
for (int i = 0; i < blocks; i++) { for (int i = 0; i < blocks; i++) {
long k = buf.readVarLong(); long k = buf.readVarLong();
positions[i] = (short) ((int) (k & 4095L)); positions[i] = (short) ((int) (k & 4095L));
int stateId = (int) (k >>> 12); states[i] = remapMOD((int) (k >>> 12));
states[i] = BlockStateUtils.isVanillaBlock(stateId) ? remap(stateId) : stateId;
} }
buf.clear(); buf.clear();
buf.writeVarInt(event.packetID()); buf.writeVarInt(event.packetID());