mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-22 16:39:28 +00:00
perf(entity): 优化投射物发包过多
This commit is contained in:
@@ -161,6 +161,7 @@ public class BukkitProjectileManager implements Listener, ProjectileManager {
|
||||
public class ProjectileInjectTask implements Runnable {
|
||||
private final Projectile projectile;
|
||||
private final SchedulerTask task;
|
||||
private Object cachedServerEntity;
|
||||
private boolean injected;
|
||||
|
||||
public ProjectileInjectTask(Projectile projectile) {
|
||||
@@ -180,30 +181,42 @@ public class BukkitProjectileManager implements Listener, ProjectileManager {
|
||||
}
|
||||
Object nmsEntity = FastNMS.INSTANCE.method$CraftEntity$getHandle(this.projectile);
|
||||
if (!this.injected) {
|
||||
Object trackedEntity = FastNMS.INSTANCE.field$Entity$trackedEntity(nmsEntity);
|
||||
if (trackedEntity == null) {
|
||||
return;
|
||||
}
|
||||
Object serverEntity = FastNMS.INSTANCE.filed$ChunkMap$TrackedEntity$serverEntity(trackedEntity);
|
||||
if (serverEntity == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
CoreReflections.field$ServerEntity$updateInterval.set(serverEntity, 1);
|
||||
this.injected = true;
|
||||
} catch (ReflectiveOperationException e) {
|
||||
plugin.logger().warn("Failed to update server entity tracking interval", e);
|
||||
}
|
||||
injectProjectile(nmsEntity, 1);
|
||||
this.injected = true;
|
||||
}
|
||||
if (canSpawnParticle(nmsEntity)) {
|
||||
boolean inGround = FastNMS.INSTANCE.method$AbstractArrow$isInGround(nmsEntity);
|
||||
if (canSpawnParticle(nmsEntity, inGround)) {
|
||||
this.projectile.getWorld().spawnParticle(ParticleUtils.BUBBLE, this.projectile.getLocation(), 3, 0.1, 0.1, 0.1, 0);
|
||||
}
|
||||
projectileByEntityId(this.projectile.getEntityId()).ifPresent(customProjectile -> {
|
||||
customProjectile.setInGroundTime(inGround ? customProjectile.inGroundTime() + 1 : 0);
|
||||
if (customProjectile.inGroundTime() > 5) {
|
||||
injectProjectile(nmsEntity, Integer.MAX_VALUE);
|
||||
} else {
|
||||
this.injected = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void injectProjectile(Object entity, int updateInterval) {
|
||||
if (this.cachedServerEntity == null) {
|
||||
Object trackedEntity = FastNMS.INSTANCE.field$Entity$trackedEntity(entity);
|
||||
if (trackedEntity == null) return;
|
||||
Object serverEntity = FastNMS.INSTANCE.filed$ChunkMap$TrackedEntity$serverEntity(trackedEntity);
|
||||
if (serverEntity == null) return;
|
||||
this.cachedServerEntity = serverEntity;
|
||||
}
|
||||
try {
|
||||
CoreReflections.handle$ServerEntity$updateIntervalSetter.invokeExact(this.cachedServerEntity, updateInterval);
|
||||
} catch (Throwable e) {
|
||||
plugin.logger().warn("Failed to update server entity tracking interval", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean canSpawnParticle(Object nmsEntity) {
|
||||
private static boolean canSpawnParticle(Object nmsEntity, boolean inGround) {
|
||||
if (!FastNMS.INSTANCE.field$Entity$wasTouchingWater(nmsEntity)) return false;
|
||||
if (CoreReflections.clazz$AbstractArrow.isInstance(nmsEntity)) {
|
||||
return !FastNMS.INSTANCE.method$AbstractArrow$isInGround(nmsEntity);
|
||||
return !inGround;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
|
||||
registerS2CByteBufPacketConsumer(VersionHelper.isOrAbove1_20_3() ? PacketConsumers.SET_OBJECTIVE_1_20_3 : PacketConsumers.SET_OBJECTIVE_1_20, this.packetIds.clientboundSetObjectivePacket());
|
||||
registerS2CByteBufPacketConsumer(PacketConsumers.SET_SCORE_1_20_3, VersionHelper.isOrAbove1_20_3() ? this.packetIds.clientboundSetScorePacket() : -1);
|
||||
registerS2CByteBufPacketConsumer(PacketConsumers.REMOVE_ENTITY, this.packetIds.clientboundRemoveEntitiesPacket());
|
||||
registerS2CByteBufPacketConsumer(PacketConsumers.ADD_ENTITY_BYTEBUFFER, this.packetIds.clientboundAddEntityPacket());
|
||||
registerS2CByteBufPacketConsumer(PacketConsumers.ADD_ENTITY, this.packetIds.clientboundAddEntityPacket());
|
||||
registerS2CByteBufPacketConsumer(PacketConsumers.SOUND, this.packetIds.clientboundSoundPacket());
|
||||
registerS2CByteBufPacketConsumer(PacketConsumers.SET_ENTITY_DATA, this.packetIds.clientboundSetEntityDataPacket());
|
||||
registerS2CByteBufPacketConsumer(PacketConsumers.CONTAINER_SET_CONTENT, this.packetIds.clientboundContainerSetContentPacket());
|
||||
|
||||
@@ -1458,7 +1458,7 @@ public class PacketConsumers {
|
||||
}
|
||||
}
|
||||
|
||||
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> ADD_ENTITY_BYTEBUFFER = (user, event) -> {
|
||||
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> ADD_ENTITY = (user, event) -> {
|
||||
try {
|
||||
FriendlyByteBuf buf = event.getBuffer();
|
||||
buf.readVarInt();
|
||||
|
||||
Reference in New Issue
Block a user