Skip to content

使用枚举类管理

由于无法在 attribute.yml 中修改属性优先级,也不可能每次修改属性名等之类的东西都去脚本文件里找,那样很麻烦

所以我们需要一个东西来统一管理属性配置,我推荐使用一个枚举类,系统属性就是使用了一个名为 AttributeName 的枚举类

仅作为推荐,不强制

介绍

截止至 2026/06/11,它的代码是这样的

Groovy
package script.libs

import groovy.transform.CompileStatic

@CompileStatic
enum AttributeName {

    // ————————————————————————————————— ATTACK_AND_DEFENSE —————————————————————————————————

    // 物理攻击.groovy
    // 对应构造函数 AttributeName(String id, String name, int priority, double power, double max)
    // 也就是分别是 id,name,priority,power,max
    HIT_CHANCE("HIT_CHANCE", "命中几率", 1, 1.0D, -1),
    // 其他枚举...
    
    // ———————————————————————— 成员和构造函数 ————————————————————————
    final String id
    final String name
    final int priority
    final double power
    final double max

    AttributeName(String id, String name, int priority, double power, double max) {
        this.id = id
        this.name = name
        this.priority = priority
        this.power = power
        this.max = max
    }

}

命中几率.groovy 为例,AttributeName.HIT_CHANCE.name 就可以调用 AttributeName 这个枚举类中的 HIT_CHANCEnameid priority power max 同理)

Groovy
package scripts.attributes.attack_and_defense

import cn.org.bukkit.craneattribute.core.attribute.AttributeTypes
import cn.org.bukkit.craneattribute.core.attribute.handler.AttackAndDefenseHandler
import scripts.libs.AttributeName
import scripts.libs.GroovyAttribute
import groovy.transform.CompileStatic
import org.bukkit.entity.LivingEntity
import scripts.libs.Utils

@CompileStatic
class HitChance extends GroovyAttribute {

    HitChance() {
        super(AttributeTypes.ATTACK_AND_DEFENSE, AttributeName.HIT_CHANCE)

        this.setSkipFilter(true)
        Utils.registerDefaultAttribute(AttributeName.DOGE_CHANCE)
    }

    @Override
    boolean onAttackAndDefense(LivingEntity attacker, LivingEntity entity, AttackAndDefenseHandler handler) {
        double v = handler.getRandomValue(attacker, AttributeName.HIT_CHANCE.name) - handler.getRandomValue(entity, AttributeName.DOGE_CHANCE.name)

        if (!Utils.chance(v)) {
            handler.trigger(entity, AttributeName.DOGE_CHANCE.name)
            handler.setCancelled(true)
            attacker.sendMessage("§e${entity.getName()} 闪避了你的攻击!")
            return false
        }

        return true
    }

}