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
- unreachable-code
- unreachable-condition
- always-pass-condition
- infinite-recursion
- macro-function-without-with
- scoreboard-fake-player-missing-hash
- scoreboard-divide-by-zero
- scoreboard-overflow
- return-run-duplicate
- nbt-items-use-if-items
- naming-convention
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
Detects calls to a macro function (a function containing $-prefixed lines) without a with clause or {...} arguments.
Calling a macro function without arguments will silently skip execution of the function.
Example:
# greet.mcfunction (macro function)
$say Hello, $(name)!# Before
function namespace:greet
# After
function namespace:greet {name:"world"}
# or
function namespace:greet with storage namespace:dataAuto-fix: No
Using # when using scoreboards improves performance because it is processed first during selector parsing.
The code that processes the target part of the scoreboard in Minecraft is as follows:
if (text.equals("*")) {
return (sender, wildcard) -> {
Collection<ScoreHolder> results = (Collection<ScoreHolder>)wildcard.get();
if (results.isEmpty()) {
throw ERROR_NO_RESULTS.create();
} else {
return results;
}
};
} else {
List<ScoreHolder> nameOnlyHolder = List.of(ScoreHolder.forNameOnly(text));
if (text.startsWith("#")) {
return (sender, wildcard) -> nameOnlyHolder;
} else {
try {
UUID uuid = UUID.fromString(text);
return (sender, wildcard) -> {
MinecraftServer server = sender.getServer();
ScoreHolder firstResult = null;
List<ScoreHolder> moreResults = null;
for (ServerLevel level : server.getAllLevels()) {
Entity entity = level.getEntity(uuid);
if (entity != null) {
if (firstResult == null) {
firstResult = entity;
} else {
if (moreResults == null) {
moreResults = new ArrayList();
moreResults.add(firstResult);
}
moreResults.add(entity);
}
}
}
if (moreResults != null) {
return moreResults;
} else {
return firstResult != null ? List.of(firstResult) : nameOnlyHolder;
}
};
}
// ...Because of the if (text.startsWith("#")) part, if the target name starts with #, the check for UUIDs, target selectors, etc. is skipped and it returns immediately, making performance slightly faster.
The performance difference is not significant in singleplayer, and it is disabled by default as you may choose not to use it for debugging purposes.
Example:
# Before
scoreboard players set const 100
# After
scoreboard players set #const 100Auto-fix: No
Detects division by zero in scoreboard operations.
Example:
scoreboard players set #zero obj 0
scoreboard players operation #foo obj /= #zero objAuto-fix: No
Detects scoreboard values that exceed the 32-bit integer range (-2,147,483,648 to 2,147,483,647).
Example:
scoreboard players set #foo obj 2000000000
scoreboard players add #foo obj 2000000000
# #foo = -294967296Auto-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
Warns when scoreboard objective, tag, or team names do not match the configured pattern.
Configurable per-project via namingConvention.scoreboardObjective / namingConvention.tag / namingConvention.team in datapack.config.json.
Supported patterns
| Pattern |
|---|
camelCase |
snake_case |
PascalCase |
kebab-case |
SCREAMING_SNAKE_CASE |
| Regex |
Example:
# Before (camelCase does not match the snake_case pattern)
scoreboard objectives add myObjective dummy
tag @s add myTag
team add myTeam
# After
scoreboard objectives add my_objective dummy
tag @s add my_tag
team add my_teamBesides the patterns above, you can also specify with a regular expression:
{
"namingConvention": {
"scoreboardObjective": "^[a-z][a-z0-9_]*$",
},
}Auto-fix: No