原创文章,未经许可,严禁转载!
原理
玩家射箭时赋予箭药水效果(例如潮涌能量),其中duration设置为攻击力数值,在击中怪物瞬间,怪物会被赋予时间为攻击力数值的药水效果,读取该药水效果的时间数值并扣除相应记分板血量,然后清除该药水效果。
代码解决方案(适用版本:1.20.4。1.20.5+由于物品nbt的更改,请自行更改;1.20.1的药水nbt与1.20.4不同,也自行更改)
假设记分板血量为Health(dummy类型)
被测试的使用记分板血量的怪物带有tag为test_entity
射箭检测的记分板为bow_used(minecraft.used:minecraft.bow类型)
玩家射出的箭会被标记tag为player_arrow
造成的伤害的记分板为hurt(dummy类型)
能造成记分板伤害的武器为带有test_damage:<攻击力>(e.g.test_damage:652)的nbt的弓
为了防止在1tick内同时造成多次伤害时被吞伤害,我们需要写入\datapacks\test_bow\data\minecraft\tags\damage_type\bypasses_cooldown.json,代码如下:
{
"values": [
"minecraft:generic",
"minecraft:player_attack",
"minecraft:arrow"
]
}
射箭瞬间弓箭记录玩家武器攻击力等数值应在tick.mcfunction完成\datapacks\test_bow\data\test_bow\functions\tick.mcfunction
execute as @e[type=arrow,tag=!player_arrow] at @s on origin if entity @s[scores={
bow_used=1..}] run function test_bow:bow_used
scoreboard players reset @a bow_used
\datapacks\test_bow\data\test_bow\functions\bow_used.mcfunction
tag @e[type=arrow,sort=nearest,limit=1] add player_arrow
#伤害计算
execute store result score @s hurt run data get entity @s SelectedItem.tag.test_damage
execute at @e[tag=player_arrow,sort=nearest,limit=1] unless data entity @e[tag=player_arrow,sort=nearest,limit=1]