🧲自定义MM选择器
ScriptTargeter下
类型
| 选择器类型 | 描述 | 需要重写的方法 |
|---|---|---|
| ENTITY | 实体类型 | 见下例子 |
| LOCATION | 坐标类型 | 见下例子 |
示例
yaml
- test-message{message=AttributeMM牛逼} @APMM:AEIC{r=5;a=60;rot=0;al={移动速度=>100}}javascript
//唯一选择器名
var targeterName = "AttributeLivingEntitiesInCone|APMM:ALEIC|APMM:AEIC"
/*
ENTITY 实体 重写getEntities(utils, data, skillMetadata)方法 返回HashSet<AbstractEntity>
LOCATION 坐标 重写getLocations(utils, data, skillMetadata)方法 返回HashSet<AbstractLocation>
*/
var targeterType = "ENTITY"
/**
* 加载时运行
*
* @param utils 脚本工具
* @param data 脚本数据
* @param manager MythicMobs5独有的 在4.x版本中 manager是一个字符串"manager"
* @param lineParse 解析器 其实就是MythicConfigLine
*/
function onLoad(utils, data, manager, lineParse) {
//获取并存入参数
skillAttributeString = lineParse.getString("al", null)
var attributesHashMap = new Packages.java.util.HashMap()
if (skillAttributeString != null) {
//过滤 { 和 } 符号
skillAttributeString = skillAttributeString.replace("{", "").replace("}", "")
//通过 ; 分割配置项
var list = skillAttributeString.split(";")
var attributeList = AttributePlus.attributeManager.attributeNameList
for (var i in list) {
//通过 = 得到属性名和MythicMobs的RangedDouble语句
var s = list[i].split("=")
//AttributePlus中必须拥有这个属性
if (attributeList.containsKey(s[0])) {
attributesHashMap.put(s[0], utils.getRangedDouble(s[1]))
}
}
}
data.set("attributesHashMap", attributesHashMap)
data.set("range", lineParse.getDouble("r", 10.0))
data.set("angle", lineParse.getDouble("a", 30.0))
data.set("rotation", lineParse.getDouble("rot", 0.0))
}
/**
* 获取目标
*
* @param utils 脚本工具
* @param data 脚本数据
* @param skillMetadata 技能元数据
*/
function getEntities(utils, data, skillMetadata) {
var attributesHashMap = data.get("attributesHashMap")
var range = data.get("range")
var angle = data.get("angle")
var rotation = data.get("rotation")
//获取技能释放者
var caster = skillMetadata.getCaster().getEntity()
//通过MythicMobs的方法获取这个世界上所有取抽象实体
var entitys = skillMetadata.getCaster().getEntity().getWorld().getLivingEntities()
//通过MythicMobs的方法获取矢量
var dir = caster.getLocation().getDirection()
dir.setY(0);
//角度调整
if (rotation > 0.0) dir.rotate(rotation)
//遍历世界内的实体并将在设定范围内的实体存入possible
var possible = utils.getAbstractEntityHashSet()
entitys.forEach(function (entity) {
if (caster.getLocation().getWorld().equals(entity.getWorld()) &&
!caster.getUniqueId().equals(entity.getUniqueId()) &&
caster.getLocation().distanceSquared(entity.getLocation()) < Math.pow(range, 2.0)) {
possible.add(entity)
}
})
//遍历possible内的实体并将在设定范围内的实体存入targets
var targets = utils.getAbstractEntityHashSet()
var cos = Math.cos(angle * Math.PI / 180.0)
var cosSq = cos * cos
possible.forEach(function (entity) {
var relative = entity.getLocation().subtract(caster.getLocation()).toVector()
relative.setY(0)
var dot = relative.getX() * dir.getX() + relative.getY() * dir.getY() + relative.getZ() * dir.getZ()
var value = dot * dot / relative.lengthSquared()
if ((angle < 180.0 && dot > 0.0 && value >= cosSq) || (angle >= 180.0 && (dot > 0.0 || dot <= cosSq))) {
if (attributesHashMap.size() == 0) {
targets.add(entity)
} else {
//如果有属性值要求则判断属性值要求
for (var i in attributesHashMap.keySet().toArray()) {
var attributeName = attributesHashMap.keySet().toArray()[i]
var attributeValue = AttributeAPI.getAttrData(entity.getBukkitEntity()).getAttributeValue(attributeName)[1]
//通过RangedDouble提供的方法检测条件语句
if (attributesHashMap.get(attributeName).equals(attributeValue)) {
//满足则添加至目标集合
targets.add(entity)
}
}
}
}
})
//必须返回集合
return targets
}