Skip to content

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)
    }
}

🔧 构造函数参数说明

参数类型说明
typeAttributeType/String属性类型分类
idString属性唯一标识符
nameString属性显示名称
priorityint执行优先级
powerdouble属性基础威力值
maxdouble属性最大值限制

📌 注意事项

  1. 必须实现抽象方法:根据属性类型,需要重写对应的处理方法(如 onAttackAndDefense
  2. 推荐使用枚举:优先使用 AttributeName 枚举来保证类型安全
  3. 优先级设置:合理设置 priority 确保属性按预期顺序执行
  4. 编译注解:保持 @CompileStatic 注解以获得更好的性能