5

Data Pack Optimization tutorialschool

5 emeralds2 replies127 views
GParcade started 04/08/2020 10:03 am history
and replied 04/08/2020 11:40 am

In this lesson we will look at ways to optimize our function code and examples of errors.

1. Try to reduce the number of /summon commands.
  This command is quite heavy especially if it will be executed every tick. You can see the difference in load in my datapack between versions where I summoning entities too often. 2.0.3 / 2.1.6

In the first, I very often suummon the item frames for determining 0.x 0.y 0.z of the player due to which the load was very large. In the second version, I used the shorter version without /summon

execute as @s store result score @s vac-x run data get entity @s Pos[​0] 10
execute as @s store result score @s vac-y run data get entity @s Pos[​1] 10
execute as @s store result score @s vac-z run data get entity @s Pos[​2] 10

2.Use only the necessary commands in command execute

  Have you ever wondered what is the difference between "if score @s scoreboard matches 1" and "as @s [​scores ={scoreboard=1}]"?

  In fact, they are the same in performance because they both go through all the entities in the selector, the only difference is that "if" the next "execute" command passes only "false" and "true" to the unlike "as" command, therefore it will be optimal only if you do not want to work with these entities, but if you use @s then "as" is already better because it is shorter.

  Note, if you are comparing a very large number of scoreboards, it is better to use "as" because a large number of "if score" does his job.


3. abbreviated commands

  DO NOT

scoreboard players operation result score += minus score
execute at @e[​type=minecraft:player] as @e[​type=minecraft:player,distance=..5,sort=nearest,limit=1] store result score @s score run scoreboard players get result score

  DO NOT

scoreboard players operation result score += minus score
scoreboard players operation @a score = result score

  DO

execute store result score @a score run scoreboard players operation result score += minus score

4.Use functions

  You may be surprised but it is really on this list! I saw a sufficient number of people who used one function instead of several and they make a big mistake since the functions for this are intended.

Examples of such functions:
execute as @a[​scores={scoreboard=4..9}] run tag @s add operation
execute as @a[​tag=operation] run say hello
execute as @a[​tag=operation] run effect give @s instant_heal 1 2
scoreboard players reset @a[​tag=operation] scoreboard
tag @a remove operation

Good function example:

execute as @a[​scores={scoreboard=4..9}] run function datapack:operation

On operation:

say hello
effect give @s instant_heal 1 2
scoreboard players reset @s scoreboard

5.Use block tags

"block tags" is a good way to shorten your code and optimize it, examples:
scoreboard players set value score 0
execute if block ~ ~ ~ air run scoreboard players set value score 1
execute if block ~ ~ ~ stone run scoreboard players set value score 1
execute if block ~ ~ ~ cave_air run scoreboard players set value score 1
execute if block ~ ~ ~ dirt run scoreboard players set value score 1
execute if block ~ ~ ~ grass_block run scoreboard players set value score 1
execute if block ~ ~ ~ black_wool run scoreboard players set value score 1
execute if block ~ ~ ~ anvil run scoreboard players set value score 1
execute if score value score matches 0 run say hello

most bether variant:

execute if block ~ ~ ~ #datapack:blocks run say hello

on datapacks/tags/blocks/blocks:

{"values":["minecraft:air","minecraft:stone","minecraft:cave_air","minecraft:dirt","minecraft:grass_block","minecraft:black_wool","minecraft:anvil"]}

As you can see the number of commands is much less than in the first example, isn’t it more pleasant to read this?

Successful optimization of date packers!
P.S. I translated some text in Google translator and do not bite me, just inform in PM.
Posted by avatar
GParcade
Level 18 : Journeyman Explorer
3

comment Login or register to post a reply.

2 replies

2
04/08/2020 10:59 amhistory
Level 50 : Grandmaster Cyborg
Bertiecrafter
The first tip is a bit of a personal mistake that not everyone is going to experience. Most people will be able to find the Pos tags on the player before trying to retrieve this from other entities. Perhaps you could remove the first section or rename it, as /summon should not be avoided in general, but just for this specific case?

And the fifth tip applies to all kinds of tags, so maybe adjust the title to be more generic?

All the others are very helpful, keep up the good work! =)
3
04/08/2020 11:40 am
Level 18 : Journeyman Explorer
GParcade
Thanks for the note, corrected =D
Planet Minecraft Logo

Website

© 2010 - 2020
www.planetminecraft.com

Welcome