扩展属性源
属性源,所有属性值都位于属性源中,就像一个箱子一样
预设属性源
插件中默认拥有这些属性源
非静态属性源EquipmentAttributeSource: 解析装备格子上的物品的 Lore 和 NBT 中的属性ItemStackAttributeSource: 解析物品的 Lore 和 NBT 中的属性ListAttributeSource: 解析 List<String> 中的属性,等同于 Lore 或 NBT
静态属性源StaticItemStackAttributeSource: 解析物品的 Lore 和 NBT 中的属性,不再判断条件StaticListAttributeSource: 解析 List<String> 中的属性,不再判断条件FormatListAttributeSource: 必须按照 属性名:属性值 或 属性名:最小值-最大值(注意是英文符号 :)的格式解析 List<String> 中的属性,不再判断条件
Map属性源ArrayMapAttributeSource: 解析 Map<String, DoubleArray> 中的属性,无条件判断MapAttributeSource: 解析 Map<String, Double> 中的属性,无条件判断
编写属性源类
需要实现 cn.org.bukkit.craneattribute.api.attribute.source.AttributeSource 接口。以下示例展示了一个简单的自定义属性源——直接存储固定的属性值:
Kotlin
class CustomAttributeSource(
override val name: String, // 属性源名称
override val attributes: Map<String, DoubleArray>, // 属性名 -> [最小值, 最大值]
override val percents: Map<String, DoubleArray>, // 属性名 -> [百分比最小值, 百分比最大值]
override var duration: Long = -1 // 持续时间(ms),-1 表示永久
) : AttributeSource {
override var removeTriggers: MutableSet<String> = mutableSetOf()
override fun onAdd(data: AttributeData) {
// 属性源被添加到实体时调用
}
override fun onRemove(data: AttributeData) {
// 属性源被移除时调用
}
}
// 使用示例
val source = CustomAttributeSource(
name = "自定义属性源",
attributes = mapOf("物理攻击" to doubleArrayOf(100.0, 200.0)),
percents = mapOf("暴击几率" to doubleArrayOf(10.0, 10.0)),
duration = 10000 // 10 秒后过期
)
data.addAttributeSource(source)