Repository navigation
Rules
This page documents all optimization rules provided by Datapack Optimization Helper.
- execute-run-redundant
- execute-run-redundant-nested
- execute-run-redundant-run-execute
- execute-as-s-redundant
- execute-duplicate
- execute-unnecessary
- execute-group
- execute-as-if-entity-s-merge
- execute-as-if-entity-s-convert
execute run <command> at the start of a line can be simplified to just <command>.
Example:
# Before
execute run say hello
# After
say helloAuto-fix: Yes
run execute run pattern can be simplified to just run.
Example:
# Before
execute as @a run execute run say hello
# After
execute as @a run say helloAuto-fix: Yes
run execute in the middle of an execute chain is redundant and can be removed.
Example:
# Before
execute as @a run execute if entity @s[tag=foo] run say hello
# After
execute as @a if entity @s[tag=foo] run say helloAuto-fix: Yes
as @s is redundant because the executor is already the current entity.
Example:
# Before
execute as @s run say hello
# After
say helloAuto-fix: Yes
Duplicate execute subcommands can be merged.
Example:
# Before
execute at @s at @s run setblock ~ ~ ~ stone
# After
execute at @s run setblock ~ ~ ~ stoneAuto-fix: Yes
Detects unnecessary execute subcommands that have no effect.
Example:
# Before
execute positioned 0 0 0 at @s run setblock ~ ~ ~ stone
# After
setblock ~ ~ ~ stoneAuto-fix: Yes
Grouping execute commands into a function improves performance.
Example:
# Before
execute as @a run say 1
execute as @a run say 2
# After
execute as @a run function namespace:grouped_tasks
# grouped_tasks.mcfunction
say 1
say 2Auto-fix: No
as <selector> if entity @s pattern can be merged into as.
Example:
# Before
execute as @a if entity @s[tag=foo] run say hello
# After
execute as @a[tag=foo] run say helloAuto-fix: Yes
Suggests merging when as <selector> if entity @s pattern has arguments in selector.
Example:
# Before
execute as @a[scores={foo=1}] if entity @s[scores={bar=2}] run say hello
# After
execute as @a[scores={foo=1,bar=2}] run say helloAuto-fix: Yes
Using @e without a type argument selects all entities, causing performance degradation.
This rule does not support quick fix.
Example:
# Before
execute as @e run say hello
# Recommended
execute as @e[type=zombie] run say helloAuto-fix: No
Selectors without dimension constraints will search across all dimensions, causing performance degradation.
Having x, y, z, dx, dy, dz, or distance limits the search dimension, improving speed.
[!NOTE] >
dx,dy,dz,distancealso limit chunks, providing slightly better performance.
Example:
# Before
execute as @a[tag=foo] run say hello
# After
execute as @a[distance=0..,tag=foo] run say helloAuto-fix: Yes
Placing the type argument at the end is slightly faster.
Note
When checking selectors, the game first filters entities by type, then checks the remaining arguments against those entities. If type is placed at the end, redundant checks can be minimized.
For more details, see here.
Example:
# Before
execute as @e[type=pig,tag=foo] run say hello
# After
execute as @e[tag=foo,type=pig] run say helloAuto-fix: Yes
Code below an always-executed return will never run.
Example:
return 1
say this code will never runAuto-fix: No
Detects conditions that can never be true based on previously set score values.
Example:
# Score is set to 5
scoreboard players set #foo obj 5
# The condition below can never be true (5 != 10)
execute if score #foo obj matches 10 run say hello# Score is reset
scoreboard players reset #foo obj
# Reset scores don't exist, so if condition always fails
execute if score #foo obj matches 1 run say helloAuto-fix: No
Detects conditions that are always true based on previously set score values.
Example:
# Score is set to 5
scoreboard players set #foo obj 5
# The condition below is always true (5 matches 5)
execute if score #foo obj matches 5 run say hello# Score is set to 5
scoreboard players set #foo obj 5
# The condition below is always true (5 is not 10)
execute unless score #foo obj matches 10 run say helloAuto-fix: No
Detects functions that may cause infinite recursion.
Example:
function namespace:selfAuto-fix: No
Adding # to scoreboard fake players improves performance as they are processed first during selector parsing.
This rule is disabled by default since the performance difference is negligible in singleplayer and you may not want to use it for debugging purposes.
Example:
# Before
scoreboard players set const 100
# After
scoreboard players set #const 100Auto-fix: No
Detects unnecessarily duplicated return run patterns.
Example:
# Before
return run return run function namespace:foo
# After
return run function namespace:fooAuto-fix: Yes
Using if items is more performant than checking items via NBT.
Example:
# Before
execute if data entity @s Inventory[{Slot:0b,id:"minecraft:diamond"}] run say hello
# After
execute if items entity @s container.0 minecraft:diamond run say helloAuto-fix: No