9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-26 02:19:23 +00:00

perf(network): 发包优化

This commit is contained in:
jhqwqmc
2025-06-17 01:38:56 +08:00
parent c19696a8e1
commit 88a982a183
3 changed files with 38 additions and 0 deletions

View File

@@ -160,6 +160,8 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
registerNMSPacketConsumer(PacketConsumers.ENTITY_EVENT, NetworkReflections.clazz$ClientboundEntityEventPacket);
registerNMSPacketConsumer(PacketConsumers.MOVE_POS_AND_ROTATE_ENTITY, NetworkReflections.clazz$ClientboundMoveEntityPacket$PosRot);
registerNMSPacketConsumer(PacketConsumers.MOVE_POS_ENTITY, NetworkReflections.clazz$ClientboundMoveEntityPacket$Pos);
registerNMSPacketConsumer(PacketConsumers.ROTATE_HEAD, NetworkReflections.clazz$ClientboundRotateHeadPacket);
registerNMSPacketConsumer(PacketConsumers.SET_ENTITY_MOTION, NetworkReflections.clazz$ClientboundSetEntityMotionPacket);
registerS2CByteBufPacketConsumer(PacketConsumers.LEVEL_CHUNK_WITH_LIGHT, this.packetIds.clientboundLevelChunkWithLightPacket());
registerS2CByteBufPacketConsumer(PacketConsumers.SECTION_BLOCK_UPDATE, this.packetIds.clientboundSectionBlocksUpdatePacket());
registerS2CByteBufPacketConsumer(PacketConsumers.BLOCK_UPDATE, this.packetIds.clientboundBlockUpdatePacket());
@@ -490,6 +492,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
NMSPacketEvent event = new NMSPacketEvent(packet);
onNMSPacketSend(player, event, packet);
if (event.isCancelled()) return;
System.out.println(packet.getClass());
if (event.isUsingNewPacket()) {
super.write(context, event.optionalNewPacket(), channelPromise);
} else {

View File

@@ -2365,6 +2365,9 @@ public class PacketConsumers {
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> MOVE_POS_AND_ROTATE_ENTITY = (user, event, packet) -> {
try {
int entityId = ProtectedFieldVisitor.get().field$ClientboundMoveEntityPacket$entityId(packet);
if (BukkitFurnitureManager.instance().isFurnitureRealEntity(entityId)) {
event.setCancelled(true);
}
EntityPacketHandler handler = user.entityPacketHandlers().get(entityId);
if (handler != null) {
handler.handleMoveAndRotate(user, event, packet);
@@ -2385,4 +2388,26 @@ public class PacketConsumers {
CraftEngine.instance().logger().warn("Failed to handle ClientboundMoveEntityPacket", e);
}
};
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> ROTATE_HEAD = (user, event, packet) -> {
try {
int entityId = (int) NetworkReflections.methodHandle$ClientboundRotateHeadPacket$entityIdGetter.invokeExact(packet);
if (BukkitFurnitureManager.instance().isFurnitureRealEntity(entityId)) {
event.setCancelled(true);
}
} catch (Throwable e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundRotateHeadPacket", e);
}
};
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> SET_ENTITY_MOTION = (user, event, packet) -> {
try {
int entityId = (int) NetworkReflections.methodHandle$ClientboundSetEntityMotionPacket$idGetter.invokeExact(packet);
if (BukkitFurnitureManager.instance().isFurnitureRealEntity(entityId)) {
event.setCancelled(true);
}
} catch (Throwable e) {
CraftEngine.instance().logger().warn("Failed to handle ClientboundSetEntityMotionPacket", e);
}
};
}

View File

@@ -1329,6 +1329,8 @@ public final class NetworkReflections {
public static final MethodHandle methodHandle$ServerboundPickItemFromBlockPacket$posGetter;
public static final MethodHandle methodHandle$ServerboundPickItemFromEntityPacket$idGetter;
public static final MethodHandle methodHandle$ServerboundCustomPayloadPacket$payloadGetter;
public static final MethodHandle methodHandle$ClientboundRotateHeadPacket$entityIdGetter;
public static final MethodHandle methodHandle$ClientboundSetEntityMotionPacket$idGetter;
static {
try {
@@ -1400,6 +1402,14 @@ public final class NetworkReflections {
ReflectionUtils.unreflectGetter(field$ClientIntentionPacket$protocolVersion)
.asType(MethodType.methodType(int.class, Object.class))
);
methodHandle$ClientboundRotateHeadPacket$entityIdGetter = requireNonNull(
ReflectionUtils.unreflectGetter(field$ClientboundRotateHeadPacket$entityId)
.asType(MethodType.methodType(int.class, Object.class))
);
methodHandle$ClientboundSetEntityMotionPacket$idGetter = requireNonNull(
ReflectionUtils.unreflectGetter(field$ClientboundSetEntityMotionPacket$id)
.asType(MethodType.methodType(int.class, Object.class))
);
if (field$ServerboundCustomPayloadPacket$payload != null) {
methodHandle$ServerboundCustomPayloadPacket$payloadGetter = requireNonNull(
ReflectionUtils.unreflectGetter(field$ServerboundCustomPayloadPacket$payload)