> For the complete documentation index, see [llms.txt](https://ersha.gitbook.io/attributeplus/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ersha.gitbook.io/attributeplus/kai-fa-wen-dang-1/zhu-ce-xin-shu-xing-baseattribute.md).

# 注册新属性 (BaseAttribute)

看完你就可以自己注册一个新的属性了

![BaseAttribute.class](https://4213040442-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MBoLfs8TQZgHdsr3gnG%2F-MBr-28Pz78vdXOyV8a3%2F-MBr-jDj9TpdnwUEnIe9%2Fimage.png?alt=media\&token=2badb155-2bc1-44a5-bd03-cc7031665147)

| 类型                      | 方法                                                   | 描述                             |
| ----------------------- | ---------------------------------------------------- | ------------------------------ |
| protected void          | setDefaultAttributeValue(int i)                      | 设置属性的默认值                       |
| protected void          | setCancelled(Entity entity, boolean b)               | 是否取消该实体此次攻击事件(不触发任何属性)         |
| protected Object        | javaScript(Entity entity, String plan)               | 计算公式                           |
| protected void          | setHolo(String text)                                 | 设置 Holographic 提示内容            |
| protected void          | addDamage(double damage)                             | 提高此次攻击者的攻击伤害                   |
| protected void          | takeDamage(double damage)                            | 降低此次攻击者的攻击伤害                   |
| protected void          | damage(Entity entity, Entity damager, double damage) | 对 entity 实体造成一次伤害,攻击者为 damager |
| protected boolean       | chance(double chance)                                | 触发几率                           |
| protected AttributeData | attributeData(Entity entity)                         | 实体属性数据                         |
| protected EquipmentData | equipmentData(Entity entity)                         | 实体装备数据                         |

## 属性类型

| 枚举名                   | 说明           |
| --------------------- | ------------ |
| AttributeType.DAMAGE  | 攻击时触发类属性     |
| AttributeType.INJURED | 被攻击时触发类属性    |
| AttributeType.RUNTIME | 属性更新时触发类属性   |
| AttributeType.NULL    | 不会触发,仅提供属性标签 |

## 构造器说明

```java
/**
* @param attributeType 属性类型
* @param attributeName 属性名
* @param placeholder 属性变量 (%ap_<属性变量>%)
*/
public BaseAttribute(AttributeType attributeType, String attributeName, String placeholder);
```

## 我怎么新注册一个 "伤害提升" 属性标签呢?

**具体效果:** \
&#x20;   攻击时提高 **XXX - XXX** 伤害

```java
public class DamageAttribute extends BaseAttribute {

    public DamageAttribute() {
        super(AttributeType.DAMAGE, "伤害提升", "test1");
    }
    
    @Override
    public void run(Entity damager, Entity entity, double attributeValue) {
        //获取攻击者属性数据
        AttributeData attributeData = this.attributeData(damager);
        //属性最小值
        double min = attributeValue;
        //属性最大值 (所有属性最大值都是在 标签后加[1])
        //最小值: 伤害提升
        //最大值: 伤害提升[1]
        double max= attributeData.getAttributeValue("伤害提升[1]");
        //随机值
        double r = new Random().nextDouble() * (max - min) + min;
        //增加此次攻击者攻击伤害
        this.addDamage(r);
    }
}
```

## 我怎么给 "伤害提升" 加上触发几率呢?

**具体效果:** \
&#x20;   攻击时有 **XX** 几率,提高 **XXX - XXX** 伤害\
&#x20;   那么将 **"伤害提升"** 标签的属性类型改为 **NULL** 类型\
&#x20;   再新注册一个 **"伤害提升几率"** 的属性标签,属性类型为 **DAMAGE**

```java
public class DamageChanceAttribute extends BaseAttribute {

    public DamageChanceAttribute() {
        super(AttributeType.DAMAGE, "伤害提升几率", "test2");
    }
    
    @Override
    public void run(Entity damager, Entity entity, double attributeValue) {
        //触发几率判断
        if (this.chance(attributeValue)){
            AttributeData attributeData = this.attributeData(damager);
            //获取最小值
            double min = attributeData.getAttributeValue("伤害提升");
            //获取最大值
            double max= attributeData.getAttributeValue("伤害提升[1]");
            double r = new Random().nextDouble() * (max - min) + min;
            this.addDamage(r);
        }
    }
}

public class DamageAttribute extends BaseAttribute {
    public DamageAttribute() {
        super(AttributeType.NULL, "伤害提升", "test1");
    }
}
```

## **那么我该怎么把属性注册至服务器呢?**

```java
@Override
public void onEnable(){
    //将属性草(一种植物)进服务器里
    new DamageChanceAttribute().registerAttribute();
    new DamageAttribute().registerAttribute();
}
```

```java
注册 AttributePlus 插件属性,那就必须在插件 plugin.yml 内添加以下内容
depend: [AttributePlus]
```

注册完成后在装备上加上 **"伤害提升几率: 100"** 跟 **"伤害提升: 100 - 200"** 就可以看到效果了！\
是不是炒鸡简单?
