Translation notice
This page was translated with machine translation and may contain inaccuracies. If you can help improve it, please open an issue or submit a pull request.
Better right-click detection: Halving method
Let’s look at a performance improvement solution for right-click detection:
Right-click detection (halving method)
Before we start, let’s do an experiment: If a scoreboard value initially is
- initial:
, , , , - ...
You'll notice that the last value always seems to be
If we don't continue to add
- initial:
, , , , - ...
If we don't continue to add
Let’s look directly at the resulting numbers:
You will find that when the value is added
However, players who are familiar with command will say that right-click advancement detection is very unstable and often disconnects. In this way, during a long press, the detection will start immediately after the end due to disconnection. So this method cannot be used? No, we can add it to the process
If the value is
What if you press it longer?
What if contact is discontinued?
You will find that as long as
Why is this? In this process, only
Due to space limitations, this video does not carry out strict mathematical proofs. If you are interested you can prove it yourself.
Next, I will post the complete code without further explanation.
# example:rmb/using_item
# ...右键运行example:rmb/run的进度
# example:load
scoreboard objectives add rmb_flag dummy
scoreboard objectives add example dummy
scoreboard players set 2 example 2
# example:rmb/run
scoreboard players add @s rmb_flag 4
advancement revoke @s only example:rmb/using_item
# example:tick
execute as @a at @s run function example:rmb/player_tick
# example:rmb/player_tick
scoreboard players operation @s rmb_flag /= 2 example
execute if score @s rmb_flag matches 2 run say 开始长按右键
execute if score @s rmb_flag matches 3 run say 正在长按右键
execute if score @s rmb_flag matches 1 run say 松开右键summary
In this tutorial, we learned a high-performance right-click detection scheme-the halving method. The knowledge points involved are:
Halving method:
- Taking advantage of the properties of integer division, by
The formula of , completes the state flow within a variable - If you need touch-off protection, you only need to modify the numbers and do not need to add new logic.
- Taking advantage of the properties of integer division, by
State mapping table
| Scheme | Value | Meaning | |----------|----|----------------| |
- scoreboard operations
- Constant setting: The scoreboard operation does not support direct division by numbers. A "constant score" must be set first (such as setting
exampleThe score is2) as the divisor. - Operation instructions:
- Input signal:
scoreboard players add @s rmb_flag 4(Executed in the function triggered by advancement) - Status decay:
scoreboard players operation @s rmb_flag /= 2 example(Executed in Tickfunction)
- Input signal:
- Constant setting: The scoreboard operation does not support direct division by numbers. A "constant score" must be set first (such as setting