9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 01:49:30 +00:00

perf(bukkit): 优化粒子效果处理性能

This commit is contained in:
jhqwqmc
2025-04-09 15:24:31 +08:00
parent a59dadf909
commit b136c863ca
2 changed files with 100 additions and 21 deletions

View File

@@ -917,28 +917,107 @@ public class PacketConsumers {
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> 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);
}

View File

@@ -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