> 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](/files/-MBr-jDj9TpdnwUEnIe9)

| 类型                      | 方法                                                   | 描述                             |
| ----------------------- | ---------------------------------------------------- | ------------------------------ |
| 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"** 就可以看到效果了！\
是不是炒鸡简单?


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ersha.gitbook.io/attributeplus/kai-fa-wen-dang-1/zhu-ce-xin-shu-xing-baseattribute.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
