计分板计时器
引言
本系统允许您以精确的时间间隔运行指定命令,实现自定义延迟效果。
典型应用场景:
- 每2小时发送一次聊天消息
- 每10分钟执行一次"清除延迟"函数
- 每30秒为玩家施加"速度"效果
此系统特别适合管理世界中的多重计时器。使用命令方块时,您可以通过游戏刻延迟选项控制命令执行间隔。但在使用函数时,就需要采用此类系统。
建议在使用命令方块时也采用本系统,这样可以使所有世界计时器保持同步启动。
设置步骤
在聊天栏输入以下命令:
yaml
/scoreboard objectives add wiki:ticks dummy
/scoreboard objectives add wiki:events dummy创建计分项后,下一步是使用ticks计分项定义每个循环事件的间隔。
需注意:Minecraft中1秒约等于20游戏刻。根据这个比例关系,您需要进行简单计算来转换所需的时间间隔。
yaml
# 2小时 = 20刻 × 60秒 × 60分钟 × 2小时 = 144000刻
/scoreboard players set .2h wiki:ticks 144000
# 10分钟 = 20刻 × 60秒 × 10分钟 = 12000刻
/scoreboard players set .10m wiki:ticks 12000
# 30秒 = 20刻 × 30秒 = 600刻
/scoreboard players set .30s wiki:ticks 600设置完计分板数据后,即可基于定义的时间间隔运行计时器。
系统实现
BP/functions/wiki/scoreboard/world_timer.mcfunction
yaml
## 世界计时器/时钟
### 每刻增加1点
scoreboard players add .Timer wiki:ticks 1
### 将当前刻数同步到所有事件
scoreboard players operation * wiki:events = .Timer wiki:ticks
## 聊天消息(每2小时)
scoreboard players operation .ChatMessage wiki:events %= .2h wiki:ticks
execute if score .ChatMessage wiki:events matches 0 run say Technoblade永不消逝!
## 延迟清除(每10分钟)
scoreboard players operation .LagClear wiki:events %= .10m wiki:ticks
execute if score .LagClear wiki:events matches 0 run function clear_lag
## 速度效果(每30秒)
scoreboard players operation .SpeedEffect wiki:events %= .30s wiki:ticks
execute if score .SpeedEffect wiki:events matches 0 run effect @a speed 10 2 true
这里展示了3个实现案例,您可以根据需要添加任意数量的计时器。只需确保遵循给定顺序并正确应用/execute if score条件。
原理说明
wiki:events — 此计分项标记所有需要循环执行的事件:
.ChatMessage.LagClear.SpeedEffect
wiki:ticks — 此计分项定义事件间隔并运行计时器:
.2h间隔(固定值144000).10m间隔(固定值12000).30s间隔(固定值600).Timer计时器(变量值n+1)
命令1: 每游戏刻为.Timer增加1点分数,作为基础计时器。
命令2: 使用*通配符将.Timer数值同步到所有事件计分项。
命令3: 使用%=取模运算检测事件是否达到触发间隔(余数为0时触发):
- 当
.Timer为1200时:- 聊天消息:1200/144000 → 余数1200(未触发)
- 延迟清除:1200/12000 → 余数1200(未触发)
- 速度效果:1200/600 → 余数0(触发第二次)
注意:Minecraft的计分板除法采用向下取整。
命令4: 当余数为0时执行对应命令。其他命令遵循相同结构。
限定事件执行次数
要限制事件触发次数,需创建wiki:occurances计分项:
yaml
/scoreboard objectives add wiki:occurances dummy
/scoreboard players set .ChatMessage wiki:occurances 5
/scoreboard players set .SpeedEffect wiki:occurances 10然后修改系统如下:
BP/functions/wiki/scoreboard/world_timer.mcfunction
yaml
## 世界计时器
scoreboard players add .Timer wiki:ticks 1
scoreboard players operation * wiki:events = .Timer wiki:ticks
## 聊天消息(每2小时)
scoreboard players operation .ChatMessage wiki:events %= .2h wiki:ticks
execute if score .ChatMessage wiki:events matches 0 if score .ChatMessage wiki:occurances matches 1.. run say Technoblade永不消逝!
execute if score .ChatMessage wiki:events matches 0 if score .ChatMessage wiki:occurances matches 1.. run scoreboard players remove .ChatMessage wiki:occurances 1
## 速度效果(每30秒)
scoreboard players operation .SpeedEffect wiki:events %= .30s wiki:ticks
execute if score .SpeedEffect wiki:events matches 0 if score .SpeedEffect wiki:occurances matches 1.. run effect @a speed 10 2 true
execute if score .SpeedEffect wiki:events matches 0 if score .SpeedEffect wiki:occurances matches 1.. run scoreboard players remove .SpeedEffect wiki:occurances 1间隔期持续执行命令
要在计时器运行期间持续执行命令(如粒子效果),可参考以下方案:
yaml
## 速度效果(每30秒)+ 每刻粒子效果
scoreboard players operation .SpeedEffect wiki:events %= .30s wiki:ticks
execute if score .SpeedEffect wiki:occurances matches 1.. as @a at @s run particle minecraft:shulker_bullet ~~~
execute if score .SpeedEffect wiki:events matches 0 if score .SpeedEffect wiki:occurances matches 1.. run effect @a speed 10 2 true
execute if score .SpeedEffect wiki:events matches 0 if score .SpeedEffect wiki:occurances matches 1.. run scoreboard players remove .SpeedEffect wiki:occurances 1若设置执行次数为10次,粒子效果将持续显示300秒(30秒×10次)。
实体计时器
对于需要独立计时的实体(如实体消失事件),需使用异步计时器:
BP/functions/wiki/scoreboard/players/entity_timer.mcfunction
yaml
## 启动计时器
scoreboard players add @e[name="wiki:station",scores={wiki:ticks=0..}] wiki:ticks 1
# 计时期间持续命令
execute as @e[name="wiki:station",scores={wiki:ticks=0..}] at @s run particle minecraft:shulker_bullet ~~~
# 时段内特效
execute as @e[name="wiki:station",scores={wiki:ticks=0..200}] at @s run particle minecraft:basic_flame_particle ~~~
# 精确时点音效
execute as @e[name="wiki:station",scores={wiki:ticks=3600}] at @s run playsound note.pling @a[r=10]
# 停止条件
execute as @e[name="wiki:station"] at @s if entity @e[family=pacified,r=10,c=1] run scoreboard players set @s ticks -1
# 循环条件
execute as @e[name="wiki:station",scores={wiki:ticks=6000}] at @s if entity @e[family=monster,r=10,c=1] run scoreboard players set @s ticks 0
# 终止处理
kill @e[name="wiki:station",scores={wiki:ticks=6000}]
将分数设为0可循环计时器,设为-1则停止计时。需要时仍可通过设为0重新激活。

