GroovyAttribute - 属性系统基类
GroovyAttribute 是所有自定义属性脚本的抽象基类,继承自 Kotlin 核心的 Attribute 类。它提供了简化的构造函数,让开发者专注于属性逻辑。
类定义
groovy
package scripts.libs
import cn.org.bukkit.craneattribute.api.attribute.AttributeType
import cn.org.bukkit.craneattribute.core.attribute.Attribute
import groovy.transform.CompileStatic
@CompileStatic
abstract class GroovyAttribute extends Attribute {
// 完整构造函数
GroovyAttribute(AttributeType type, String id, String name, int priority, double power, double max) {
super(type, id, name, priority, power, max)
}
// 简化构造函数
GroovyAttribute(String type, String id, String name, int priority, double power, double max) {
super(type, id, name, 0, priority, max)
}
// 简化构造函数(字符串类型,priority 默认 0)
GroovyAttribute(String type, String id, String name, double power, double max) {
super(type, id, name, 0, power, max)
}
// 枚举构造函数(从 AttributeName 获取所有配置)
GroovyAttribute(AttributeType type, AttributeName attributeName) {
this(type, attributeName.id, attributeName.name, attributeName.priority, attributeName.power, attributeName.max)
}
}构造函数
三种构造方式,按需选择:
| 构造函数 | 适用场景 |
|---|---|
(type, id, name, priority, power, max) | 完整控制所有参数 |
(type, AttributeName) | 使用枚举统一管理配置 |
(typeStr, id, name, priority, power, max) | 字符串类型 |
(typeStr, id, name, power, max) | 字符串类型,默认 priority=0 |
使用示例
groovy
package scripts.attributes
import cn.org.bukkit.craneattribute.core.attribute.AttributeTypes
import cn.org.bukkit.craneattribute.core.attribute.handler.AttackAndDefenseHandler
import groovy.transform.CompileStatic
import scripts.libs.AttributeName
import scripts.libs.GroovyAttribute
import org.bukkit.entity.LivingEntity
@CompileStatic
class PhysicalAttack extends GroovyAttribute {
PhysicalAttack() {
// 使用枚举构造,推荐方式
super(AttributeTypes.ATTACK_AND_DEFENSE, AttributeName.PHYSICAL_ATTACK)
}
@Override
boolean onAttackAndDefense(LivingEntity attacker, LivingEntity entity, AttackAndDefenseHandler handler) {
double damage = handler.getRandomValue(attacker, AttributeName.PHYSICAL_ATTACK.name)
if (damage > 0) {
handler.addDamage(attacker, damage)
}
return true
}
}注意事项
- 子类必须提供无参构造函数,否则插件无法实例化
- 根据属性类型重写对应的处理方法(如
onAttackAndDefense) - 推荐使用
AttributeName枚举来管理属性配置 - 保持
@CompileStatic注解以获得更好性能
