GroovyMappingReader - 映射读取器基类
GroovyMappingReader 是映射读取组件的抽象基类,继承自 Kotlin 核心的 MappingReader 类。用于将特定文本行映射为属性列表——比如看到「这是一个红宝石」就自动赋予某些属性。
类定义
groovy
package scripts.libs
import cn.org.bukkit.craneattribute.api.utils.ConfigSetting
import cn.org.bukkit.craneattribute.core.read.MappingReader
import groovy.transform.CompileStatic
@CompileStatic
abstract class GroovyMappingReader extends MappingReader {
String name // 读取器名称
String configKey // 在 read.yml 中的配置键名
int priority // 优先级,决定执行顺序
List<ConfigSetting> settingConfig = new ArrayList<>()
GroovyMappingReader(String name, int priority) {
this.name = name
this.configKey = name
this.priority = priority
}
// ...
}需要重写的方法
groovy
/**
* 将一行文本映射为属性
* @return AttributeReadResult(映射成功)或 null(不匹配)
*/
@Override
AttributeReadResult read(ReadableLine readableLine, AttributeData data, AttributeSource attributeSource)使用示例
groovy
package scripts.reads
import cn.org.bukkit.craneattribute.api.attribute.data.AttributeData
import cn.org.bukkit.craneattribute.api.attribute.source.AttributeSource
import cn.org.bukkit.craneattribute.api.read.ReadableLine
import cn.org.bukkit.craneattribute.core.read.data.AttributeReadResult
import groovy.transform.CompileStatic
import scripts.libs.GroovyMappingReader
@CompileStatic
class CustomMappingReader extends GroovyMappingReader {
// 映射表
private static final Map<String, List<String>> MAPPING = [
"这是一个红宝石": ["测试脚本属性 1(%)"],
"这是一个绿宝石": ["生命值 100"]
]
CustomMappingReader() {
super("custom_mapping", 10)
}
@Override
AttributeReadResult read(ReadableLine readableLine, AttributeData data, AttributeSource attributeSource) {
String line = readableLine.getClearLine()
for (Map.Entry<String, List<String>> entry : MAPPING.entrySet()) {
if (line.contains(entry.getKey())) {
return new AttributeReadResult(entry.getValue())
}
}
return null
}
}返回值说明
| 返回值 | 含义 |
|---|---|
AttributeReadResult | 解析成功,包含映射出的属性列表 |
null | 当前行不匹配此映射规则 |
AttributeReadResult 中属性格式与 Lore 行一致,如 属性名 100 或 属性名 10(%)。
注意事项
- 映射表可以放在类中硬编码,也可以从外部配置或数据库加载
- 合理设置
priority确保映射在正确的时机执行 - 复杂的映射逻辑建议配合
CacheManager使用缓存 - 和系统
LineReadMapping的区别在于:脚本映射可以在运行时动态决定映射结果
