9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-30 20:39:10 +00:00

perf(network): 优化 Trident 实体添加处理

This commit is contained in:
jhqwqmc
2025-05-31 14:43:43 +08:00
parent 483df4c911
commit 44fef0aead
2 changed files with 40 additions and 21 deletions

View File

@@ -117,6 +117,15 @@ public class PacketConsumers {
buf.writeShort(za);
}
};
ADD_ENTITY_HANDLERS[Reflections.instance$EntityType$TRIDENT$registryId] = (user, event) -> {
FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt();
BukkitProjectileManager.instance().projectileByEntityId(id).ifPresent(customProjectile -> {
ProjectilePacketHandler handler = new ProjectilePacketHandler(customProjectile, id);
handler.convertAddCustomProjectilePacket(buf, event);
user.entityPacketHandlers().put(id, handler);
});
};
ADD_ENTITY_HANDLERS[Reflections.instance$EntityType$BLOCK_DISPLAY$registryId] = simpleAddEntityHandler(BlockDisplayPacketHandler.INSTANCE);
ADD_ENTITY_HANDLERS[Reflections.instance$EntityType$TEXT_DISPLAY$registryId] = simpleAddEntityHandler(TextDisplayPacketHandler.INSTANCE);
ADD_ENTITY_HANDLERS[Reflections.instance$EntityType$ARMOR_STAND$registryId] = simpleAddEntityHandler(ArmorStandPacketHandler.INSTANCE);
@@ -1442,12 +1451,6 @@ public class PacketConsumers {
event.setCancelled(true);
user.entityPacketHandlers().put(entityId, FurnitureCollisionPacketHandler.INSTANCE);
}
} else {
BukkitProjectileManager.instance().projectileByEntityId(entityId).ifPresent(customProjectile -> {
ProjectilePacketHandler handler = new ProjectilePacketHandler(customProjectile, entityId);
event.replacePacket(handler.convertAddCustomProjectilePacket(packet));
user.entityPacketHandlers().put(entityId, handler);
});
}
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundAddEntityPacket", e);

View File

@@ -25,12 +25,14 @@ import java.util.Optional;
import java.util.UUID;
public class ProjectilePacketHandler implements EntityPacketHandler {
private final int entityId;
private final CustomProjectile projectile;
private final Object cachedPacket;
private final List<Object> cachedData;
public ProjectilePacketHandler(CustomProjectile projectile, int entityId) {
this.projectile = projectile;
this.entityId = entityId;
this.cachedData = createCustomProjectileEntityDataValues();
this.cachedPacket = FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(entityId, this.cachedData);
}
@@ -61,21 +63,35 @@ public class ProjectilePacketHandler implements EntityPacketHandler {
)));
}
public Object convertAddCustomProjectilePacket(Object packet) {
int entityId = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$entityId(packet);
UUID uuid = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$uuid(packet);
double x = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$x(packet);
double y = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$y(packet);
double z = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$z(packet);
float yRot = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$yRot(packet);
float xRot = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$xRot(packet);
Object type = Reflections.instance$EntityType$ITEM_DISPLAY;
int data = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$data(packet);
double xa = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$xa(packet);
double ya = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$ya(packet);
double za = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$za(packet);
double yHeadRot = FastNMS.INSTANCE.field$ClientboundAddEntityPacket$yHeadRot(packet);
return FastNMS.INSTANCE.constructor$ClientboundAddEntityPacket(entityId, uuid, x, y, z, MCUtils.clamp(-xRot, -90.0F, 90.0F), -yRot, type, data, FastNMS.INSTANCE.constructor$Vec3(xa, ya, za), yHeadRot);
public void convertAddCustomProjectilePacket(FriendlyByteBuf buf, ByteBufPacketEvent event) {
UUID uuid = buf.readUUID();
buf.readVarInt(); // type
double x = buf.readDouble();
double y = buf.readDouble();
double z = buf.readDouble();
byte xRot = buf.readByte();
byte yRot = buf.readByte();
byte yHeadRot = buf.readByte();
int data = buf.readVarInt();
int xa = buf.readShort();
int ya = buf.readShort();
int za = buf.readShort();
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(this.entityId);
buf.writeUUID(uuid);
buf.writeVarInt(Reflections.instance$EntityType$ITEM_DISPLAY$registryId);
buf.writeDouble(x);
buf.writeDouble(y);
buf.writeDouble(z);
buf.writeByte(MCUtils.packDegrees(MCUtils.clamp(-MCUtils.unpackDegrees(xRot), -90.0F, 90.0F)));
buf.writeByte(MCUtils.packDegrees(-MCUtils.unpackDegrees(yRot)));
buf.writeByte(yHeadRot);
buf.writeVarInt(data);
buf.writeShort(xa);
buf.writeShort(ya);
buf.writeShort(za);
}
private Object convertCustomProjectilePositionSyncPacket(Object packet) {