mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-23 08:59:27 +00:00
perf(entity): 优化投射物发包过多
This commit is contained in:
@@ -161,6 +161,7 @@ public class BukkitProjectileManager implements Listener, ProjectileManager {
|
|||||||
public class ProjectileInjectTask implements Runnable {
|
public class ProjectileInjectTask implements Runnable {
|
||||||
private final Projectile projectile;
|
private final Projectile projectile;
|
||||||
private final SchedulerTask task;
|
private final SchedulerTask task;
|
||||||
|
private Object cachedServerEntity;
|
||||||
private boolean injected;
|
private boolean injected;
|
||||||
|
|
||||||
public ProjectileInjectTask(Projectile projectile) {
|
public ProjectileInjectTask(Projectile projectile) {
|
||||||
@@ -180,30 +181,42 @@ public class BukkitProjectileManager implements Listener, ProjectileManager {
|
|||||||
}
|
}
|
||||||
Object nmsEntity = FastNMS.INSTANCE.method$CraftEntity$getHandle(this.projectile);
|
Object nmsEntity = FastNMS.INSTANCE.method$CraftEntity$getHandle(this.projectile);
|
||||||
if (!this.injected) {
|
if (!this.injected) {
|
||||||
Object trackedEntity = FastNMS.INSTANCE.field$Entity$trackedEntity(nmsEntity);
|
injectProjectile(nmsEntity, 1);
|
||||||
if (trackedEntity == null) {
|
this.injected = true;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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);
|
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 (!FastNMS.INSTANCE.field$Entity$wasTouchingWater(nmsEntity)) return false;
|
||||||
if (CoreReflections.clazz$AbstractArrow.isInstance(nmsEntity)) {
|
if (CoreReflections.clazz$AbstractArrow.isInstance(nmsEntity)) {
|
||||||
return !FastNMS.INSTANCE.method$AbstractArrow$isInGround(nmsEntity);
|
return !inGround;
|
||||||
}
|
}
|
||||||
return true;
|
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(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.SET_SCORE_1_20_3, VersionHelper.isOrAbove1_20_3() ? this.packetIds.clientboundSetScorePacket() : -1);
|
||||||
registerS2CByteBufPacketConsumer(PacketConsumers.REMOVE_ENTITY, this.packetIds.clientboundRemoveEntitiesPacket());
|
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.SOUND, this.packetIds.clientboundSoundPacket());
|
||||||
registerS2CByteBufPacketConsumer(PacketConsumers.SET_ENTITY_DATA, this.packetIds.clientboundSetEntityDataPacket());
|
registerS2CByteBufPacketConsumer(PacketConsumers.SET_ENTITY_DATA, this.packetIds.clientboundSetEntityDataPacket());
|
||||||
registerS2CByteBufPacketConsumer(PacketConsumers.CONTAINER_SET_CONTENT, this.packetIds.clientboundContainerSetContentPacket());
|
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 {
|
try {
|
||||||
FriendlyByteBuf buf = event.getBuffer();
|
FriendlyByteBuf buf = event.getBuffer();
|
||||||
buf.readVarInt();
|
buf.readVarInt();
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ public abstract class AbstractCustomProjectile implements CustomProjectile {
|
|||||||
protected final ProjectileMeta meta;
|
protected final ProjectileMeta meta;
|
||||||
protected final Projectile projectile;
|
protected final Projectile projectile;
|
||||||
protected final Item<?> item;
|
protected final Item<?> item;
|
||||||
|
private int inGroundTime;
|
||||||
|
|
||||||
protected AbstractCustomProjectile(ProjectileMeta meta, Projectile projectile, Item<?> item) {
|
protected AbstractCustomProjectile(ProjectileMeta meta, Projectile projectile, Item<?> item) {
|
||||||
this.meta = meta;
|
this.meta = meta;
|
||||||
@@ -27,4 +28,14 @@ public abstract class AbstractCustomProjectile implements CustomProjectile {
|
|||||||
public Item<?> item() {
|
public Item<?> item() {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int inGroundTime() {
|
||||||
|
return inGroundTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setInGroundTime(int inGroundTime) {
|
||||||
|
this.inGroundTime = inGroundTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,4 +9,8 @@ public interface CustomProjectile {
|
|||||||
Projectile projectile();
|
Projectile projectile();
|
||||||
|
|
||||||
Item<?> item();
|
Item<?> item();
|
||||||
|
|
||||||
|
int inGroundTime();
|
||||||
|
|
||||||
|
void setInGroundTime(int inGroundTime);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user