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.
A brief introduction to inline loot table (predicate, item decorator)
by CR_019
This article was also published in Void Spirit Forum and BiliBili column
1.20.5 and later, loot table, predicate and item decorators can be called inline in the function, for example:
#内联战利品表
loot give @s loot {"pools": [{"rolls": 1,"entries": [{"type": "item","name": "minecraft:stone"}]}]}
#内联物品修饰器
item modify entity @s weapon.mainhand [{"function": "minecraft:set_components","components":{"custom_data":{"dc_random":1}}}]
#内联谓词
execute if predicate {"condition":"entity_properties",entity:"this",predicate:{type:"player"}}For simple loot tables, predicates, etc. that do not require reuse, they can be directly inlined in the function without the need to create a new file to reference them. There is nothing to say about this usage, but it seems that many people don't understand it. There are only two lines on the 1.20.5 update page on the wiki, which is indeed easy to ignore. I think it still needs to be mentioned.
Comprehensive use of inline loot tables (and their components) and macros
In the development of data pack, the loot table and its components have some good features, such as weighted random extraction of the loot table, predicate can provide a lot of conditional judgments, and the item modifier can directly modify the item in the player's hand without specifically taking out the item to obtain data, etc., which can be more convenient for the data pack author. However, they do not play well with the function macro introduced in 1.20.2. As an additional json file outside of the function, the loot table component cannot add dynamic parameter content. The inline loot table can solve this problem. The inline loot table transfers the contents of the json file to the function, so that its components can be dynamically controlled using macro parameters.
Simple example: weighted random task extraction
In my dc pre-package, there is such a requirement: there are several event items in the list, among which there is a weight parameter. During execution, an event is randomly selected from among them according to the weight and executed. Using function implementation requires dealing with many additional issues, such as weight simulation, obtaining the total number of events, etc.; but this extraction logic is very similar to the extraction logic of the loot table. Therefore, we can use to build an inline loot table parameter, store it in storage, and then use the function macro to call it, generate item, and grab the event parameters we need from it. In this way, the effect of dynamic loot table is achieved.
Specific function file:
dc/function/events/random/execute.mcfuction:
#args required:events(带有weight属性作为权重,缺省默认为1)
execute if data storage dc events.temp.target.args.events run function dc:events/random/loot_table
execute if data storage dc events.temp.target.args.events run function dc:events/random/loot with storage dc:temp randomThe former instruction is used to generate a loot table, and the latter instruction is used to apply an inline loot table to generate items and extract information;
dc/function/events/random/loot_table.mcfuction:
data modify storage dc:temp random.loot_table set value {\
"pools": [\
{\
"rolls": 1,\
"entries": []\
}\
]\
}
function lay:macro/list/init {list:"storage dc events.temp.target.args.events",func:"dc:events/random/events"}First, initialize the shell of a loot table in storage, and then take out each item in the list from the specified path and add it to the entries of the loot table; lay:macro/list/initis a list operation module whose function is tolistExtract each item in the list specified in and executefuncThe instructions in operate the list item.
dc/function/events/random/events.mcfuction:
$data modify storage dc:temp random.event set value $(value)
data modify storage dc:temp random.entry set value {\
"type": "item",\
"name": "minecraft:stone",\
"functions": [\
{\
"function": "minecraft:set_components",\
"components":{\
"custom_data":{\
"dc_random":1\
}\
}\
}\
]\
}
data modify storage dc:temp random.entry.functions[0].components.custom_data.event set from storage dc:temp random.event
execute if data storage dc:temp random.event.weight run data modify storage dc:temp random.entry.weight set from storage dc:temp random.event.weight
data modify storage dc:temp random.loot_table.pools[0].entries append from storage dc:temp random.entrySet loot table items. In the previous function, we took out an item from the event list. Now we create a template for extracting items from the loot table item, and then put the event field in the extracted event under the custom_dataitem component for subsequent extraction, and put the weight field in the weight position of the table item, so that the loot table can extract the item based on this weight. Finally, put this item into the loot table framework built by the previous function.
dc/function/events/random/loot.mcfuction:
$loot spawn ~ ~ ~ loot $(loot_table)
execute as @e[type=item,distance=..5,limit=1,nbt={Item:{components:{"minecraft:custom_data":{dc_random:1}}}}] run tag @s add dc_random_temp
execute as @e[tag=dc_random_temp,limit=1] run data modify storage dc events.temp.target set from entity @s Item.components."minecraft:custom_data".event
kill @e[tag=dc_random_temp,limit=1]
function dc:events/_detect/event_execute with storage dc events.temp.targetUse the loot table path just now as a macro parameter to generate item and passdc_randomFind the item just generated in the field, extract the extracted event item, and put it into the event processing module for execution. Finally, delete the generated temporary item.
summary
Inline loot table is indeed a very simple feature, which is why few people talk about it. But it provides us with the possibility of using the dynamic loot table feature, which can make data pack development more convenient under certain circumstances. Therefore, it is still necessary to mention it.
Appendix: Related links
wiki - 1.20.5 update contentwiki - command/itemwiki - command/lootwiki - command/execute#(if|unless)_predicate