9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-31 04:46:37 +00:00

perf(bukkit): 反射性能优化

This commit is contained in:
jhqwqmc
2025-06-17 07:57:50 +08:00
parent b9dc2976ae
commit f3c8d9c8c5
4 changed files with 23 additions and 4 deletions

View File

@@ -2879,7 +2879,7 @@ public final class CoreReflections {
public static final Method method$ServerPlayer$getAttribute = requireNonNull(
VersionHelper.isOrAbove1_20_5() ?
ReflectionUtils.getMethod(clazz$ServerPlayer, clazz$AttributeInstance, CoreReflections.clazz$Holder) :
ReflectionUtils.getMethod(clazz$ServerPlayer, clazz$AttributeInstance, clazz$Holder) :
ReflectionUtils.getMethod(clazz$ServerPlayer, clazz$AttributeInstance, clazz$Attribute)
);
@@ -3275,6 +3275,7 @@ public final class CoreReflections {
public static final MethodHandle methodHandle$ServerEntity$broadcastSetter;
public static final MethodHandle methodHandle$ServerEntity$updateIntervalSetter;
public static final MethodHandle methodHandle$ServerPlayer$connectionGetter;
public static final MethodHandle methodHandle$ServerPlayer$getAttributeMethod;
static {
try {
@@ -3290,6 +3291,10 @@ public final class CoreReflections {
ReflectionUtils.unreflectGetter(field$ServerPlayer$connection)
.asType(MethodType.methodType(Object.class, Object.class))
);
methodHandle$ServerPlayer$getAttributeMethod = requireNonNull(
ReflectionUtils.unreflectMethod(method$ServerPlayer$getAttribute)
.asType(MethodType.methodType(Object.class, Object.class, Object.class))
);
} catch (IllegalAccessException e) {
throw new ReflectionInitException("Failed to initialize reflection", e);
}

View File

@@ -1331,6 +1331,7 @@ public final class NetworkReflections {
public static final MethodHandle methodHandle$ServerboundCustomPayloadPacket$payloadGetter;
public static final MethodHandle methodHandle$ClientboundRotateHeadPacket$entityIdGetter;
public static final MethodHandle methodHandle$ClientboundSetEntityMotionPacket$idGetter;
public static final MethodHandle methodHandle$ClientboundUpdateAttributesPacket0Constructor;
static {
try {
@@ -1410,6 +1411,10 @@ public final class NetworkReflections {
ReflectionUtils.unreflectGetter(field$ClientboundSetEntityMotionPacket$id)
.asType(MethodType.methodType(int.class, Object.class))
);
methodHandle$ClientboundUpdateAttributesPacket0Constructor = requireNonNull(
ReflectionUtils.unreflectConstructor(constructor$ClientboundUpdateAttributesPacket0)
.asType(MethodType.methodType(Object.class, int.class, List.class))
);
if (field$ServerboundCustomPayloadPacket$payload != null) {
methodHandle$ServerboundCustomPayloadPacket$payloadGetter = requireNonNull(
ReflectionUtils.unreflectGetter(field$ServerboundCustomPayloadPacket$payload)

View File

@@ -498,8 +498,8 @@ public class BukkitServerPlayer extends Player {
if (canBreak) {
if (VersionHelper.isOrAbove1_20_5()) {
Object serverPlayer = serverPlayer();
Object attributeInstance = CoreReflections.method$ServerPlayer$getAttribute.invoke(serverPlayer, MAttributeHolders.BLOCK_BREAK_SPEED);
Object newPacket = NetworkReflections.constructor$ClientboundUpdateAttributesPacket0.newInstance(entityID(), Lists.newArrayList(attributeInstance));
Object attributeInstance = CoreReflections.methodHandle$ServerPlayer$getAttributeMethod.invokeExact(serverPlayer, MAttributeHolders.BLOCK_BREAK_SPEED);
Object newPacket = NetworkReflections.methodHandle$ClientboundUpdateAttributesPacket0Constructor.invokeExact(entityID(), (List<?>) Lists.newArrayList(attributeInstance));
sendPacket(newPacket, true);
} else {
resetEffect(MMobEffects.MINING_FATIGUE);
@@ -519,7 +519,7 @@ public class BukkitServerPlayer extends Player {
sendPackets(List.of(fatiguePacket, hastePacket), true);
}
}
} catch (ReflectiveOperationException e) {
} catch (Throwable e) {
plugin.logger().warn("Failed to set attribute for player " + platformPlayer().getName(), e);
}
}