Skip to content

Groovy注册插件中的内容

插件可以自动加载一些特定的 Groovy 类为 属性组件、读取组件,也可以实现 GroovyScript 接口注册属性类型

警告

若代码中有 TaskTimer、监听器等长期持有的对象,一定要在生命周期结束时(onDisable())清除或关闭,否则会造成内存泄漏。

注册属性组件

attributes 中创建一个继承对应类的 Groovy 类文件,并重写对应方法,插件便会自动将其加载并注册为属性组件

cn.org.bukkit.craneattribute.core.attribute.Attribute: 属性组件

同样会在其生命周期的不同阶段调用相应的方法( onEnable()onDisable()

已在属性编写教程中介绍,此篇不再说明

注册读取组件

可搭配开发文档一同使用
expansions 中创建一个继承对应类的 Groovy 类,并重写对应方法,插件便会自动将其加载并注册为读取组件

同样会在其生命周期的不同阶段调用相应的方法( onEnable()onDisable()

注册数值读取组件

继承 cn.org.bukkit.craneattribute.core.read.ReadValue 类重写对应方法

Groovy
注册条件读取组件

继承 cn.org.bukkit.craneattribute.core.read.ReadCondition 类重写对应方法

Groovy
注册映射读取组件

继承 cn.org.bukkit.craneattribute.core.read.ReadMapping 类重写对应方法

Groovy

注册属性类型

可搭配开发文档一同使用
属性类型续对应一套触发器,处理器(当然也可以复用或重写已有的)

同样会在其生命周期的不同阶段调用相应的方法( onEnable()onDisable()

注册属性类型

在其他 Groovy 脚本中 import expansions.AttributeManager
即可通过 AttributeManager.GROOVY 使用此属性类型

Groovy
package expansions

import cn.org.bukkit.craneattribute.api.attribute.AttributeType
import cn.org.bukkit.craneattribute.core.attribute.AttributeTypes
import groovy.transform.CompileStatic
import cn.org.bukkit.craneattribute.api.expansions.GroovyScript
import org.bukkit.event.player.PlayerExpChangeEvent

@CompileStatic
class AttributeManager implements GroovyScript {

    public static AttributeType GROOVY = AttributeType.create("GROOVY", { it ->
        it.triggerSetting("test", "value!", "GROOVY 注释")
    })

    /**
     * 启用时注册属性类型
     */
    @Override
    void onEnable() {
        GROOVY.register(触发器实例)
    }

    /**
     * 一定要在 disable 时 unregister 属性类型
     * 否则可能会内存泄漏
     */
    @Override
    void onDisable() {
        GROOVY.unregister()
    }

}
编写属性触发器
Groovy
编写属性处理器
Groovy

注册属性源类型

可搭配开发文档一同使用

定义一个属性源类型

实现以下任一接口即可
cn.org.bukkit.craneattribute.api.attribute.source.PersistentAttributeSource: 持久化属性源,属性源可以自动存储到数据库
cn.org.bukkit.craneattribute.api.attribute.source.AttributeSource: 属性源

不一定所有属性源都要存储到数据库,按照实际需求使用即可,避免过渡保存数据库

Groovy
package libs.source

import cn.org.bukkit.craneattribute.api.attribute.source.AttributeSource
import groovy.transform.CompileStatic

@CompileStatic
class GroovyAttributeSource implements AttributeSource {

    String name;
    boolean condition
    Map<String, double[]> values = new HashMap<>()
    Map<String, double[]> percents = new HashMap<>()
    long time
    // 尽量设置为 true,当实体属性更新时移除此属性源
    // 然后在属性更新事件中重新赋予玩家属性源
    boolean shouldRemoveOnUpdate = false

    GroovyAttributeSource(String attributeName, double value, boolean percent, long time) {
        this.name = "测试~"
        this.condition = true
        if (percent) {
            this.percents.put(attributeName, new double[]{value, value})
        } else {
            this.values.put(attributeName, new double[]{value, value})
        }

        this.time = time
    }


    @Override
    String getName() {
        return this.name
    }

    @Override
    void setName(String s) {
        this.name = s
    }

    @Override
    boolean getCondition() {
        return this.condition
    }

    @Override
    void setCondition(boolean b) {
        this.condition = b
    }

    @Override
    Map<String, double[]> getValues() {
        return this.values
    }

    @Override
    void setValues(Map<String, double[]> stringMap) {
        this.values = stringMap
    }

    @Override
    Map<String, double[]> getPercents() {
        return this.percents
    }

    @Override
    void setPercents(Map<String, double[]> stringMap) {
        this.percents = stringMap
    }

    @Override
    long getTime() {
        return this.time
    }

    @Override
    void setTime(long l) {
        this.time = l
    }

    @Override
    boolean getShouldRemoveOnUpdate() {
        return this.shouldRemoveOnUpdate
    }

    @Override
    void setShouldRemoveOnUpdate(boolean b) {
        this.shouldRemoveOnUpdate = b
    }
}
使用自己定义的属性源

仅是一个示例,这个可以通过使用命令触发

Groovy
package expansions

import cn.org.bukkit.craneattribute.api.AttributeAPI
import groovy.transform.CompileStatic
import cn.org.bukkit.craneattribute.api.expansions.GroovyScript
import libs.source.GroovyAttributeSource
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

@CompileStatic
class CommandTest implements GroovyScript {

    @Override
    void invoke(CommandSender sender, String... string) {
        if (sender instanceof Player) {
            GroovyAttributeSource source = new GroovyAttributeSource("物理攻击", 1.0D, false, -1L)
            AttributeAPI.getAttrData(sender).addAttributeSource(source)
            sender.sendMessage("${source.getName()} 增加成功")
        } else {
            sender.sendMessage("${''}请使用玩家执行")
        }

    }
}