- scoreboard objectives add lastHitter dummy
复制代码
- scoreboard objectives add id dummy
复制代码
那么怎么赋予玩家id呢?
- execute unless entity @e[name=constants] run summon minecraft:armor_stand ~ ~ ~ {CustomName:"[\"constants\"]",Invisible:1,NoGravity:1,Marker:1}
- scoreboard players add @e[name=constants] id 0
- scoreboard players add @e[name=constants,scores={id=0}] id 1
复制代码
从上述命令我们可以看到我们召唤了一个名为costants的盔甲架。给它加上分数0以让他拥有id记分板,随后如果他的id是0那么就加上1
- scoreboard players operation @s id = @e[name=constants,limit=1] id
- scoreboard players add @e[name=constants] id 1
复制代码
这两行命令的意图显而易见我就不解释了
- scoreboard players add @a id 0
- execute as @a[scores={id=0},limit=1] run function namespace:allocid
复制代码
同样我们让玩家拥有id记分板然后让所有id=0的玩家执行allocid
好了,既然id已经分配到了我们可以继续考虑攻击者的问题了。
- {
- "display": {
- "announce_to_chat": false,
- "hidden": true,
- "show_toast": false,
- "icon": {
- "item":"minecraft:stick"
- },
- "description":"",
- "title":""
- },
- "criteria": {
- "bow_hit": {
- "trigger": "minecraft:player_hurt_entity",
- "conditions": {
- "damage": {
- "direct_entity": {
- "type":"player"
- }
- }
- }
- }
- },
- "rewards": {
- "function":"namespace:stickhit"
- }
- }
复制代码
- {
- "display": {
- "announce_to_chat": false,
- "hidden": true,
- "show_toast": false,
- "icon": {
- "item":"minecraft:stick"
- },
- "description":"",
- "title":""
- },
- "criteria": {
- "bow_hit": {
- "trigger": "minecraft:entity_hurt_player",
- "conditions": {
- "damage": {
- "direct_entity": {
- "type":"player"
- }
- }
- }
- }
- },
- "rewards": {
- "function":"namespace:bestickhit"
- }
- }
复制代码
※ 好像对齐有点怪……不管了忍一下吧
- tag @s add stickhit
- advancement revoke @s only namespace:stickhit
复制代码
※ 超短的
- tag @s add bestickhit
- scoreboard players set @s fromDamaged 0 #请先忽视它
- advancement revoke @s only namespace:bestickhit
复制代码
干的事也一样,至于第二行后面会提到的,先忽视就好
- scoreboard players operation @a[sort=nearest,tag=bestickhit,limit=1,distance=0.3..] lastHitter = @s id
- tag @s remove stickhit
- tag @a[sort=nearest,tag=bestickhit,limit=1,distance=0.3..] remove bestickhit
复制代码
- # ...Previous Commands...
- execute as @a[tag=stickhit] run function namespace:stickhitrecord
复制代码
- scoreboard objectives add owner dummy
- scoreboard objectives add lifeTime dummy
复制代码
为什么是两个?过会就知道了
- scoreboard players add @e[type=arrow] lifeTime 1
- execute as @e[type=arrow,scores={lifeTime=1}] at @s run scoreboard players operation @s owner = @a[sort=nearest,limit=1] id
复制代码
我们每tick所有箭的lifeTime+1,然后让刚出生(?)的箭把离自己的owner附上最近的人的id,很好理解吧
- scoreboard objectives add possiblyArrowHit dummy
复制代码
※ 啊不要吐槽这个命名
- execute as @e[type=arrow,scores={lifeTime=2..},nbt={inGround:0b}] at @s run scoreboard players operation @a[distance=0.1..3] possiblyArrowHit = @s owner
复制代码
- {
- "display": {
- "announce_to_chat": false,
- "hidden": true,
- "show_toast": false,
- "icon": {
- "item":"minecraft:bow"
- },
- "description":"",
- "title":""
- },
- "criteria": {
- "bow_hit": {
- "trigger": "minecraft:player_hurt_entity",
- "conditions": {
- "damage": {
- "direct_entity": {
- "type":"arrow"
- }
- }
- }
- }
- },
- "rewards": {
- "function":"namespace:bearrowhit"
- }
- }
复制代码
※ 直接vscode复制过来竟然带特效 duangduangduang 前面的懒得重新复制了凑合着吧
bearrowhit.mcfunction
- scoreboard players operation @s lastHitter = @s possiblyArrowHit
- scoreboard players set @s fromDamaged 0 # 忽视
复制代码
是不是有一种茅塞顿开的感觉?
没错我们把沿途所有可能击中的玩家全部标上,但是只有最终击中的那个才会被正式计入击中者
然后远程攻击判定也就到这里了
不过最后我们解决几个无关紧要的问题
1.玩家自杀怎么判断?
这个我们真没办法,但是你可以用上那个fromDamaged,每tick增加1,到一定值归位并且把满值的玩家的lastHitter清零
2.偶尔判定lastHitter就是死亡者自身,怎么办?
- execute as @a run scoreboard players operation @s lastHitter -= @s id
- execute as @a[scores={lastHitter=1..}] run scoreboard players operation @s lastHitter += @s id
- execute as @a[scores={lastHitter=..-1}] run scoreboard players operation @s lastHitter += @s id
复制代码
这样解决……至于是什么意思,请自己揣摩吧。实在不懂的话我会在评论区解答,算一个小小的作业
3.怎么选择器选择击杀者?分数没法动态导入选择器里啊?
那就把击杀玩家分数特化(快记笔记)
- scoreboard players operation @a[distance=0.3..] id -= @s lastHitter
- tellraw @a [{"color":"red","selector":"@s"},{"color":"grey","text":" 被 "},{"color":"gold","selector":"@a[scores={id=0}]"},{"color":"grey","text":" 击杀了"}]
- scoreboard players operation @a[distance=0.3..] id += @s lastHitter
复制代码
像这样……我把id特化成0了。具体自己研究吧