From 2405fbecda40b398309089566e6d830a3d2effac Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Tue, 19 Aug 2025 06:17:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=85=A8=E5=B1=80=E8=B0=83?= =?UTF-8?q?=E8=89=B2=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bukkit/block/BukkitBlockManager.java | 5 +- .../plugin/network/PacketConsumers.java | 66 +++++++++---------- .../core/world/chunk/IdListPalette.java | 4 ++ .../core/world/chunk/PalettedContainer.java | 46 +++++++------ .../core/world/chunk/packet/MCSection.java | 18 ++--- gradle.properties | 2 +- 6 files changed, 75 insertions(+), 66 deletions(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java index 47d6cf89e..97d99df9c 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java @@ -43,6 +43,7 @@ import net.momirealms.craftengine.core.registry.WritableRegistry; import net.momirealms.craftengine.core.sound.SoundData; import net.momirealms.craftengine.core.sound.Sounds; import net.momirealms.craftengine.core.util.*; +import net.momirealms.craftengine.core.world.chunk.PalettedContainer; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -327,7 +328,9 @@ public final class BukkitBlockManager extends AbstractBlockManager { this.realBlockArranger = builder3.build(); this.registeredBlocks = builder4.build(); this.blockRegisterOrder = ImmutableList.copyOf(order); - + if (MCUtils.ceilLog2(BlockStateUtils.vanillaStateSize() + counter) == MCUtils.ceilLog2(BlockStateUtils.vanillaStateSize())) { + PalettedContainer.NEED_DOWNGRADE = false; + } for (Object block : (Iterable) MBuiltInRegistries.BLOCK) { Object soundType = CoreReflections.field$BlockBehaviour$soundType.get(block); if (affectedBlockSounds.contains(soundType)) { diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java index 8d9028a65..2fc91c8af 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java @@ -89,7 +89,8 @@ 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 BLOCK_LIST; + private static IntIdentityList SERVER_BLOCK_LIST; + private static IntIdentityList CLIENT_BLOCK_LIST; private static IntIdentityList BIOME_LIST; public static void initEntities(int registrySize) { @@ -245,7 +246,8 @@ public class PacketConsumers { } BLOCK_STATE_MAPPINGS = newMappings; MOD_BLOCK_STATE_MAPPINGS = newMappingsMOD; - BLOCK_LIST = new IntIdentityList(registrySize); + SERVER_BLOCK_LIST = new IntIdentityList(registrySize); + CLIENT_BLOCK_LIST = new IntIdentityList(BlockStateUtils.vanillaStateSize()); BIOME_LIST = new IntIdentityList(RegistryUtils.currentBiomeRegistrySize()); } @@ -306,26 +308,22 @@ public class PacketConsumers { FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(byteBuf); FriendlyByteBuf newBuf = new FriendlyByteBuf(Unpooled.buffer()); for (int i = 0, count = player.clientSideSectionCount(); i < count; i++) { - try { - MCSection mcSection = new MCSection(BLOCK_LIST, BIOME_LIST); - mcSection.readPacket(friendlyByteBuf); - PalettedContainer container = mcSection.blockStateContainer(); - Palette palette = container.data().palette(); - if (palette.canRemap()) { - palette.remap(PacketConsumers::remapMOD); - } else { - for (int j = 0; j < 4096; j++) { - int state = container.get(j); - int newState = remapMOD(state); - if (newState != state) { - container.set(j, newState); - } + MCSection mcSection = new MCSection(SERVER_BLOCK_LIST, SERVER_BLOCK_LIST, BIOME_LIST); + mcSection.readPacket(friendlyByteBuf); + PalettedContainer container = mcSection.blockStateContainer(); + Palette palette = container.data().palette(); + if (palette.canRemap()) { + palette.remap(PacketConsumers::remapMOD); + } else { + for (int j = 0; j < 4096; j++) { + int state = container.get(j); + int newState = remapMOD(state); + if (newState != state) { + container.set(j, newState); } } - mcSection.writePacket(newBuf); - } catch (Exception e) { - break; } + mcSection.writePacket(newBuf); } buffer = newBuf.array(); } else { @@ -333,26 +331,22 @@ public class PacketConsumers { FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(byteBuf); FriendlyByteBuf newBuf = new FriendlyByteBuf(Unpooled.buffer()); for (int i = 0, count = player.clientSideSectionCount(); i < count; i++) { - try { - MCSection mcSection = new MCSection(BLOCK_LIST, BIOME_LIST); - mcSection.readPacket(friendlyByteBuf); - PalettedContainer container = mcSection.blockStateContainer(); - Palette palette = container.data().palette(); - if (palette.canRemap()) { - palette.remap(PacketConsumers::remap); - } else { - for (int j = 0; j < 4096; j++) { - int state = container.get(j); - int newState = remap(state); - if (newState != state) { - container.set(j, newState); - } + MCSection mcSection = new MCSection(CLIENT_BLOCK_LIST, SERVER_BLOCK_LIST, BIOME_LIST); + mcSection.readPacket(friendlyByteBuf); + PalettedContainer container = mcSection.blockStateContainer(); + Palette palette = container.data().palette(); + if (palette.canRemap()) { + palette.remap(PacketConsumers::remap); + } else { + for (int j = 0; j < 4096; j++) { + int state = container.get(j); + int newState = remap(state); + if (newState != state) { + container.set(j, newState); } } - mcSection.writePacket(newBuf); - } catch (Exception e) { - break; } + mcSection.writePacket(newBuf); } buffer = newBuf.array(); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/world/chunk/IdListPalette.java b/core/src/main/java/net/momirealms/craftengine/core/world/chunk/IdListPalette.java index 362948919..2775ccc51 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/world/chunk/IdListPalette.java +++ b/core/src/main/java/net/momirealms/craftengine/core/world/chunk/IdListPalette.java @@ -39,6 +39,10 @@ public class IdListPalette implements Palette { } } + public IndexedIterable idList() { + return idList; + } + @Override public int getSize() { return this.idList.size(); diff --git a/core/src/main/java/net/momirealms/craftengine/core/world/chunk/PalettedContainer.java b/core/src/main/java/net/momirealms/craftengine/core/world/chunk/PalettedContainer.java index de064dbe6..e5f704471 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/world/chunk/PalettedContainer.java +++ b/core/src/main/java/net/momirealms/craftengine/core/world/chunk/PalettedContainer.java @@ -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 implements PaletteResizeListener, ReadableContainer { + public static boolean NEED_DOWNGRADE = true; private static final BiConsumer RAW_DATA_WRITER = VersionHelper.isOrAbove1_21_5() ? (FriendlyByteBuf::writeFixedSizeLongArray) : (FriendlyByteBuf::writeLongArray); private static final BiConsumer RAW_DATA_READER = VersionHelper.isOrAbove1_21_5() ? @@ -74,31 +72,34 @@ public class PalettedContainer implements PaletteResizeListener, ReadableC return false; } + public PalettedContainer downgradeTo(IndexedIterable idList) { + if (!NEED_DOWNGRADE) { + return this; + } + Palette palette = this.data.palette; + if (!(palette instanceof IdListPalette idListPalette)) { + return this; + } + Data newData = getCompatibleData(this.data, idList, 128); + newData.importFrom(idListPalette, this.data.storage); + return new PalettedContainer<>(idList, PaletteProvider.BLOCK_STATE, newData); + } + public Data data() { return data; } public void readPacket(FriendlyByteBuf buf) { - this.lock(); - try { - int i = buf.readByte(); - Data 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 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 getCompatibleData(@Nullable Data previousData, int bits) { @@ -106,6 +107,11 @@ public class PalettedContainer implements PaletteResizeListener, ReadableC return previousData != null && dataProvider.equals(previousData.configuration()) ? previousData : dataProvider.createData(this.idList, this, this.paletteProvider.getContainerSize()); } + private Data getCompatibleData(@Nullable Data previousData, IndexedIterable idList, int bits) { + DataProvider 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 oldData = this.data; diff --git a/core/src/main/java/net/momirealms/craftengine/core/world/chunk/packet/MCSection.java b/core/src/main/java/net/momirealms/craftengine/core/world/chunk/packet/MCSection.java index 8c9f37387..d0931a2ee 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/world/chunk/packet/MCSection.java +++ b/core/src/main/java/net/momirealms/craftengine/core/world/chunk/packet/MCSection.java @@ -7,17 +7,19 @@ import net.momirealms.craftengine.core.world.chunk.ReadableContainer; public class MCSection { private short nonEmptyBlockCount; - private final PalettedContainer blockStateContainer; + private final PalettedContainer serverBlockStateContainer; + private final IndexedIterable clientBlockStateList; private ReadableContainer biomeContainer; - public MCSection(IndexedIterable blockStateList, IndexedIterable biomeList) { - this.blockStateContainer = new PalettedContainer<>(blockStateList, 0, PalettedContainer.PaletteProvider.BLOCK_STATE); + public MCSection(IndexedIterable clientBlockStateList, IndexedIterable serverBlockStateList, IndexedIterable 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 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 blockStateContainer() { - return blockStateContainer; + return serverBlockStateContainer; } } diff --git a/gradle.properties b/gradle.properties index 4ce25d07a..b0232b2c6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx1G # Project settings # Rule: [major update].[feature update].[bug fix] -project_version=0.0.61.8 +project_version=0.0.61.9 config_version=44 lang_version=24 project_group=net.momirealms