GroovyAttribute - 属性系统基类
本篇由 AI 生成
📖 概述
GroovyAttribute 是所有自定义属性脚本的抽象基类,继承自 Kotlin 核心的 Attribute 类。它提供了简化的构造函数和类型转换逻辑,让开发者能够专注于属性业务逻辑的实现。
🎯 核心功能
- 简化构造:提供多种构造函数重载,支持不同的参数组合
- 类型转换:自动处理字符串到
AttributeType的转换 - 元数据管理:继承完整的属性生命周期管理能力
- 编译优化:使用
@CompileStatic确保类型安全和性能
📝 类定义
groovy
@CompileStatic
abstract class GroovyAttribute extends Attribute {
// 完整构造函数
GroovyAttribute(AttributeType type, String id, String name, int priority, double power, double max) {
// ...
}
// 简化构造函数(自动设置 priority=0)
GroovyAttribute(String type, String id, String name, double power, double max) {
// ...
}
// 枚举构造函数(从 AttributeName 获取配置)
GroovyAttribute(AttributeType type, AttributeName attributeName) {
// ...
}
}💡 使用示例
基础用法
groovy
import scripts.libs.GroovyAttribute
import scripts.libs.AttributeName
import cn.org.bukkit.craneattribute.core.attribute.AttributeTypes
class PhysicalAttack extends GroovyAttribute {
PhysicalAttack() {
super(AttributeTypes.ATTACK_AND_DEFENSE, AttributeName.PHYSICAL_ATTACK)
}
@Override
boolean onAttackAndDefense(LivingEntity attacker, LivingEntity entity, AttackAndDefenseHandler handler) {
// 实现攻击逻辑
return true
}
}使用简化构造函数
groovy
class CustomAttribute extends GroovyAttribute {
CustomAttribute() {
super("CUSTOM_TYPE", "custom_id", "自定义属性", 10.0, 100.0)
}
}🔧 构造函数参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
type | AttributeType/String | 属性类型分类 |
id | String | 属性唯一标识符 |
name | String | 属性显示名称 |
priority | int | 执行优先级 |
power | double | 属性基础威力值 |
max | double | 属性最大值限制 |
📌 注意事项
- 必须实现抽象方法:根据属性类型,需要重写对应的处理方法(如
onAttackAndDefense) - 推荐使用枚举:优先使用
AttributeName枚举来保证类型安全 - 优先级设置:合理设置
priority确保属性按预期顺序执行 - 编译注解:保持
@CompileStatic注解以获得更好的性能
