From b136c863ca22674d412c2e5317fc2a28718bf8ba Mon Sep 17 00:00:00 2001 From: jhqwqmc <2110242767@qq.com> Date: Wed, 9 Apr 2025 15:24:31 +0800 Subject: [PATCH] =?UTF-8?q?perf(bukkit):=20=E4=BC=98=E5=8C=96=E7=B2=92?= =?UTF-8?q?=E5=AD=90=E6=95=88=E6=9E=9C=E5=A4=84=E7=90=86=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/network/PacketConsumers.java | 119 +++++++++++++++--- gradle.properties | 2 +- 2 files changed, 100 insertions(+), 21 deletions(-) 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 70bf2c36a..6d02b36b5 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 @@ -917,28 +917,107 @@ public class PacketConsumers { public static final BiConsumer LEVEL_PARTICLE = (user, event) -> { try { FriendlyByteBuf buf = event.getBuffer(); - Object mcByteBuf; - Method writeMethod; - if (VersionHelper.isVersionNewerThan1_20_5()) { - mcByteBuf = Reflections.constructor$RegistryFriendlyByteBuf.newInstance(buf, Reflections.instance$registryAccess); - writeMethod = Reflections.method$ClientboundLevelParticlesPacket$write; + if (VersionHelper.isVersionNewerThan1_21_3()) { + boolean overrideLimiter = buf.readBoolean(); + boolean alwaysShow = buf.readBoolean(); + double x = buf.readDouble(); + double y = buf.readDouble(); + double z = buf.readDouble(); + float xDist = buf.readFloat(); + float yDist = buf.readFloat(); + float zDist = buf.readFloat(); + float maxSpeed = buf.readFloat(); + int count = buf.readInt(); + Object option = FastNMS.INSTANCE.method$ParticleTypes$STREAM_CODEC$decode(buf); + if (option == null) return; + if (!Reflections.clazz$BlockParticleOption.isInstance(option)) return; + Object blockState = FastNMS.INSTANCE.field$BlockParticleOption$blockState(option); + int id = BlockStateUtils.blockStateToId(blockState); + int remapped = remap(id); + if (remapped == id) return; + Object type = FastNMS.INSTANCE.method$BlockParticleOption$getType(option); + Object remappedOption = FastNMS.INSTANCE.constructor$BlockParticleOption(type, BlockStateUtils.idToBlockState(remapped)); + event.setChanged(true); + buf.clear(); + buf.writeVarInt(event.packetID()); + buf.writeBoolean(overrideLimiter); + buf.writeBoolean(alwaysShow); + buf.writeDouble(x); + buf.writeDouble(y); + buf.writeDouble(z); + buf.writeFloat(xDist); + buf.writeFloat(yDist); + buf.writeFloat(zDist); + buf.writeFloat(maxSpeed); + buf.writeInt(count); + FastNMS.INSTANCE.method$ParticleTypes$STREAM_CODEC$encode(buf, remappedOption); + } else if (VersionHelper.isVersionNewerThan1_20_5()) { + boolean overrideLimiter = buf.readBoolean(); + double x = buf.readDouble(); + double y = buf.readDouble(); + double z = buf.readDouble(); + float xDist = buf.readFloat(); + float yDist = buf.readFloat(); + float zDist = buf.readFloat(); + float maxSpeed = buf.readFloat(); + int count = buf.readInt(); + Object option = FastNMS.INSTANCE.method$ParticleTypes$STREAM_CODEC$decode(buf); + if (option == null) return; + if (!Reflections.clazz$BlockParticleOption.isInstance(option)) return; + Object blockState = FastNMS.INSTANCE.field$BlockParticleOption$blockState(option); + int id = BlockStateUtils.blockStateToId(blockState); + int remapped = remap(id); + if (remapped == id) return; + Object type = FastNMS.INSTANCE.method$BlockParticleOption$getType(option); + Object remappedOption = FastNMS.INSTANCE.constructor$BlockParticleOption(type, BlockStateUtils.idToBlockState(remapped)); + event.setChanged(true); + buf.clear(); + buf.writeVarInt(event.packetID()); + buf.writeBoolean(overrideLimiter); + buf.writeDouble(x); + buf.writeDouble(y); + buf.writeDouble(z); + buf.writeFloat(xDist); + buf.writeFloat(yDist); + buf.writeFloat(zDist); + buf.writeFloat(maxSpeed); + buf.writeInt(count); + FastNMS.INSTANCE.method$ParticleTypes$STREAM_CODEC$encode(buf, remappedOption); } else { - mcByteBuf = Reflections.constructor$FriendlyByteBuf.newInstance(event.getBuffer().source()); - writeMethod = Reflections.method$Packet$write; + Object particleType = FastNMS.INSTANCE.method$FriendlyByteBuf$readById(buf, Reflections.instance$BuiltInRegistries$PARTICLE_TYPE); + boolean overrideLimiter = buf.readBoolean(); + double x = buf.readDouble(); + double y = buf.readDouble(); + double z = buf.readDouble(); + float xDist = buf.readFloat(); + float yDist = buf.readFloat(); + float zDist = buf.readFloat(); + float maxSpeed = buf.readFloat(); + int count = buf.readInt(); + Object option = FastNMS.INSTANCE.method$ClientboundLevelParticlesPacket$readParticle(buf, particleType); + if (option == null) return; + if (!Reflections.clazz$BlockParticleOption.isInstance(option)) return; + Object blockState = FastNMS.INSTANCE.field$BlockParticleOption$blockState(option); + int id = BlockStateUtils.blockStateToId(blockState); + int remapped = remap(id); + if (remapped == id) return; + Object type = FastNMS.INSTANCE.method$BlockParticleOption$getType(option); + Object remappedOption = FastNMS.INSTANCE.constructor$BlockParticleOption(type, BlockStateUtils.idToBlockState(remapped)); + event.setChanged(true); + buf.clear(); + buf.writeVarInt(event.packetID()); + FastNMS.INSTANCE.method$FriendlyByteBuf$writeId(buf, remappedOption, Reflections.instance$BuiltInRegistries$PARTICLE_TYPE); + buf.writeBoolean(overrideLimiter); + buf.writeDouble(x); + buf.writeDouble(y); + buf.writeDouble(z); + buf.writeFloat(xDist); + buf.writeFloat(yDist); + buf.writeFloat(zDist); + buf.writeFloat(maxSpeed); + buf.writeInt(count); + FastNMS.INSTANCE.method$ParticleOptions$writeToNetwork(remappedOption, buf); } - Object packet = Reflections.constructor$ClientboundLevelParticlesPacket.newInstance(mcByteBuf); - Object option = FastNMS.INSTANCE.field$ClientboundLevelParticlesPacket$particle(packet); - if (option == null) return; - if (!Reflections.clazz$BlockParticleOption.isInstance(option)) return; - Object blockState = FastNMS.INSTANCE.field$BlockParticleOption$blockState(option); - int id = BlockStateUtils.blockStateToId(blockState); - int remapped = remap(id); - if (remapped == id) return; - Reflections.field$BlockParticleOption$blockState.set(option, BlockStateUtils.idToBlockState(remapped)); - event.setChanged(true); - buf.clear(); - buf.writeVarInt(event.packetID()); - writeMethod.invoke(packet, mcByteBuf); } catch (Exception e) { CraftEngine.instance().logger().warn("Failed to handle ClientboundLevelParticlesPacket", e); } diff --git a/gradle.properties b/gradle.properties index 49c395910..88b5b7aaf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -51,7 +51,7 @@ byte_buddy_version=1.17.5 ahocorasick_version=0.6.3 snake_yaml_version=2.4 anti_grief_version=0.13 -nms_helper_version=0.48 +nms_helper_version=0.50 # Ignite Dependencies mixinextras_version=0.4.1 mixin_version=0.15.2+mixin.0.8.7