9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-29 11:59:11 +00:00

refactor(bukkit): 重构属性相关反射代码

This commit is contained in:
jhqwqmc
2025-03-31 17:16:40 +08:00
parent 0804b17073
commit ab3b4db541
2 changed files with 8 additions and 18 deletions

View File

@@ -63,7 +63,7 @@ public class ShulkerHitBox extends AbstractHitBox {
Reflections.method$FriendlyByteBuf$writeVarIntArray.invoke(friendlyByteBuf, (Object) new int[] {entityIds[1]});
packets.accept(Reflections.constructor$ClientboundSetPassengersPacket.newInstance(friendlyByteBuf));
if (VersionHelper.isVersionNewerThan1_20_5()) {
Object attributeInstance = Reflections.constructor$AttributeInstance.newInstance(Reflections.instance$Attributes$SCALE, (Consumer<?>) (o) -> {});
Object attributeInstance = Reflections.constructor$AttributeInstance.newInstance(Reflections.instance$Holder$Attribute$scale, (Consumer<?>) (o) -> {});
Reflections.method$AttributeInstance$setBaseValue.invoke(attributeInstance, scale);
packets.accept(Reflections.constructor$ClientboundUpdateAttributesPacket0.newInstance(entityIds[1], Collections.singletonList(attributeInstance)));
}

View File

@@ -2808,6 +2808,7 @@ public class Reflections {
public static final Object instance$Holder$Attribute$block_break_speed;
public static final Object instance$Holder$Attribute$block_interaction_range;
public static final Object instance$Holder$Attribute$scale;
static {
try {
@@ -2821,9 +2822,15 @@ public class Reflections {
@SuppressWarnings("unchecked")
Optional<Object> blockInteractionRangeHolder = (Optional<Object>) method$Registry$getHolder0.invoke(instance$BuiltInRegistries$ATTRIBUTE, block_interaction_range);
instance$Holder$Attribute$block_interaction_range = blockInteractionRangeHolder.orElse(null);
Object scale = method$ResourceLocation$fromNamespaceAndPath.invoke(null, "minecraft", VersionHelper.isVersionNewerThan1_21_2() ? "scale" : "generic.scale");
@SuppressWarnings("unchecked")
Optional<Object> scaleHolder = (Optional<Object>) method$Registry$getHolder0.invoke(instance$BuiltInRegistries$ATTRIBUTE, scale);
instance$Holder$Attribute$scale = scaleHolder.orElse(null);
} else {
instance$Holder$Attribute$block_break_speed = null;
instance$Holder$Attribute$block_interaction_range = null;
instance$Holder$Attribute$scale = null;
}
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
@@ -5862,23 +5869,6 @@ public class Reflections {
)
);
// 1.20.5+
public static final Field field$Attributes$SCALE =
ReflectionUtils.getDeclaredField(
clazz$Attributes, "SCALE"
);
// 1.20.5+
public static final Object instance$Attributes$SCALE = Optional.ofNullable(field$Attributes$SCALE)
.map(it -> {
try {
return it.get(null);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
})
.orElse(null);
// 1.20.5+
public static final Constructor<?> constructor$AttributeInstance =
ReflectionUtils.getConstructor(