战利品和交易表函数

战利品表和交易表共享一个相似的函数集。 虽然大多数都适用于两者,但有些仅适用于特定类型。 战利品/交易表通常有一些限制:例如,无法通过战利品或交易表将 can_place_oncan_destroy 添加到物品上。 有关可用的不同函数以及如何使用它们的示例,请参见下文。

在本教程中,您将学习以下内容:

  • 各种战利品和交易表函数以及它们如何修改物品。

要求

在开始本教程之前,建议完成以下内容。

附魔函数

这些不同的附魔函数允许您设置独特的附魔参数。

enchant_book_for_trading(仅限交易表)

该函数使用物品附魔算法为村民出售的书附魔。 它需要四个参数:base_costbase_random_costper_level_random_costper_level_cost

                {
                    "item": "minecraft:book",
                    "quantity": 1,
                    "functions": [
                        {
                            "function": "enchant_book_for_trading",
                            "base_cost": 2,
                            "base_random_cost": 5,
                            "per_level_random_cost": 10,
                            "per_level_cost": 3
                        }
                    ]
                }

现在,定义这些参数不会影响收到的附魔。 目前,上面的默认值是硬编码的。 将来您可以调整这些值,以帮助缩小此函数导致的附魔和附魔等级的范围。

enchant_random_gear

使用与附魔生成香草生物的装备相同的算法为一件物品附魔。 使用 chance 修饰符来操作算法。 请注意,chance 修饰符为 1.0 并不意味着装备有 100% 的几率会被附魔。 而是根据难度修改机会。 在和平与简单的难度下,无论如何几率永远是 0%。 在困难难度下,1.0 chance 为 100%,但几率大约是一般难度的 2/3。

                {
                    "type": "item",
                    "name": "minecraft:chainmail_boots",
                    "weight": 1,
                    "functions": [
                      {
                        "function": "enchant_random_gear",
                        "chance": 0.25
                      }
                    ]
                }

chance 增加到高于 1.0 的数字有助于绕过一般难度下的机会下降。 例如,将 chance 设置为 2.0 将始终为一般和困难难度的物品附魔。

enchant_randomly

生成与物品兼容的随机附魔。 支持可选的宝藏 boolean (true/false) 以允许开启和关闭宝藏附魔。 宝物附魔是无法通过附魔台获得的附魔,包括冰霜行者、治愈、灵魂疾行、绑定诅咒和消失诅咒。

                {
                    "type": "item",
                    "name": "minecraft:leather_helmet",
                    "weight": 1,
                    "functions": [
                      {
                        "function": "enchant_randomly",
                        "treasure": true
                      }
                    ]
                }

enchant_with_levels

应用附魔,如同是通过附魔台附魔一样,使用通过 levels 参数定义的最小和最大经验级别。 treasure 布尔值 (true/false) 将允许使用仅限宝藏的附魔。 宝物附魔是无法通过附魔台获得的附魔,包括冰霜行者、治愈、灵魂疾行、绑定诅咒和消失诅咒。

                {
                    "type": "item",
                    "name": "minecraft:diamond_sword",
                    "weight": 1,
                    "functions": [
                      {
                        "function": "enchant_with_levels",
                        "treasure": true,
                        "levels": {
                          "min": 20,
                          "max": 39
                        }
                      }
                    ]
                }

specific_enchants

此函数允许您对一个物品设置特定附魔的列表。 它还允许您对通常不会在游戏中附魔的物品应用附魔。

                {
                    "type": "item",
                    "name": "minecraft:stick",
                    "weight": 1,
                    "functions": [
                        {
                          "function": "specific_enchants",
                          "enchants": [
                             "knockback",
                             "fire_aspect"
                          ]
                       }
                    ]
                 }

您还可以通过将附魔定义为对象来专门定义附魔级别。 最大附魔等级是硬编码的,不能被覆盖。

                {
                    "type": "item",
                    "name": "minecraft:stick",
                    "weight": 1,
                    "functions": [
                        {
                          "function": "specific_enchants",
                          "enchants": [
                             {
                                "id": "knockback",
                                "level": 1
                             },
                             {
                                "id": "unbreaking",
                                "level": 3
                             }
                          ]
                       }
                    ]
                }

修改物品

下面这组函数允许您以不同的方式修改结果,比如设置一个物品返回的数量,设置它的数据值,甚至设置一个物品的名称和知识。

looting_enchant(仅限战利品表)

此函数允许您修改在实体被具有抢夺附魔的物品杀死时返回的物品数量。 因此,它仅适用于战利品表,并且仅当该战利品表因实体死亡而被调用时使用。

                {
                    "type": "item",
                    "name": "minecraft:stick",
                    "weight": 1,
                    "functions": [
                        {
                            "function": "looting_enchant",
                            "count": {
                                "min": 0,
                                "max": 1
                            }
                        }
                    ]
                }

random_block_state

用于随机化返回物品的方块状态。 例如,以下示例代码可以掉落石头 (0)、花岗岩 (1)、磨制花岗岩 (2)、闪长岩 (3)、磨制闪长岩 (4) 或安山岩 (5)。

                {
                    "type": "item",
                    "name": "minecraft:stone",
                    "weight": 1,
                    "functions": [
                        {
                          "function": "random_block_state",
                          "block_state": "stone_type",
                          "values": {
                            "min": 0,
                            "max": 5
                          }
                        }
                      ]
                }

random_aux_value

random_block_state 类似,允许您为物品选择随机辅助值。 以下示例将产生随机着色的染料。

                {
                    "type": "item",
                    "name": "minecraft:dye",
                    "weight": 1,
                    "functions": [
                        {
                          "function": "random_aux_value",
                          "values": {
                            "min": 0,
                            "max": 15
                          }
                        }
                    ]
                }

set_actor_id

此函数仅适用于刷怪蛋,用于设置刷怪蛋的实体 ID。

                {
                    "type": "item",
                    "name": "minecraft:stone",
                    "weight": 1,
                    "functions": [
                        {
                            "function": "set_actor_id",
                            "id": "compass:cool_entity"
                        }
                    ]
                }

您还可以通过省略 id 来继承与该战利品表关联的实体的实体 ID(即,让兔子掉落兔子刷怪蛋)。

                {
                    "type": "item",
                    "name": "minecraft:spawn_egg",
                    "weight": 1,
                    "functions": [
                        {
                            "function": "set_actor_id"
                        }
                    ]
                }

请注意,如果您在箱子的战利品表上使用 set_actor_id 并省略了 id 值,它将生成打开箱子的任何(玩家)的刷怪蛋。 玩家刷怪蛋会生成一个“玩家”,它只是站在周围并抛出 Molang 错误(因为它试图使用玩家的行为文件以及玩家的控制器)。 如果您打破了一个包含战利品表的箱子,该战利品表使用了从 set_actor_id 继承的 ID,它会掉落一个不会产生任何东西的默认刷怪蛋。

set_banner_details

此函数仅适用于旗帜,目前仅支持的旗帜 type1。 旗帜 type1 会生成灾厄村民旗帜。

                {
                    "type": "item",
                    "name": "minecraft:banner",
                    "weight": 1,
                    "functions": [
                        {
                            "function": "set_banner_details",
                            "type": 1
                        }
                    ]
                }

set_book_contents

此函数允许您设置书籍的内容。

                {
                    "type": "item",
                    "name": "minecraft:written_book",
                    "functions": [
                        {
                            "function": "set_book_contents",
                            "author": "Steve",
                            "title": "Creator Woes",
                            "pages": [
                                "Once upon a time there were some marketplace partners that had to edit items using NBT. And it made them sad.",
                                "Then the Bedrock team added the ability to customize items from JSON and everyone rejoiced."
                            ]
                        }
                    ]
                }

您还可以使用 rawtext 来本地化书籍的内容。 使用 rawtext 时,务必对特殊字符(如 "\)使用转义 (\)。

                {
                    "type": "item",
                    "name": "minecraft:written_book",
                    "functions": [
                        {
                            "function": "set_book_contents",
                            "author": "Steve",
                            "title": "Creator Woes",
                            "pages": [
                                "{\"rawtext\":[ {\"translate\":\"custom.book.page.1\"}]}",
                                "{\"rawtext\":[ {\"translate\":\"custom.book.page.2\"}]}"
                            ]
                        }
                    ]
                }

请注意,在撰写本文时,您只能在书的内容(pages)上使用 rawtextrawtext 适用于 authortitle 标签。

set_count

通过设置 count 值来设置返回的物品数量。 它可以是一个确切的数字,如这个示例:

                {
                    "type": "item",
                    "name": "minecraft:stone",
                    "weight": 1,
                    "functions": [
                        {
                          "function": "set_count",
                          "count": 5
                        }
                    ]
                }

或者可以设置最小/最大范围:

                {
                    "type": "item",
                    "name": "minecraft:stone",
                    "weight": 1,
                    "functions": [
                        {
                          "function": "set_count",
                          "count": {
                            "min": 1,
                            "max": 6
                          }
                        }
                    ]
                }

set_damage

通过设置 damage 值为具有耐久度的物品设置剩余耐久度百分比。 1.0 是剩余 100% 的耐久度(未损坏),而 0.0 是没有耐久度剩余。

您可以将其设置为精确的耐久度,例如在以下示例中,我们将物品设置为剩余 50% 的耐久度。

                {
                    "type": "item",
                    "name": "minecraft:iron_leggings",
                    "weight": 1,
                    "functions": [
                      {
                        "function": "set_damage",
                        "damage": 0.5
                      }
                    ]
                }

或者您可以定义游戏将随机选择的最小/最大量。 下一个示例将耐久性设置为剩余 30% 到 90%。

                {
                    "type": "item",
                    "name": "minecraft:iron_leggings",
                    "weight": 1,
                    "functions": [
                      {
                        "function": "set_damage",
                        "damage": {
                          "min": 0.3,
                          "max": 0.9
                        }
                      }
                    ]
                }

set_data

将方块或物品的数据值设置为精确的 ID。以下示例将生成磨制闪长岩方块。

                {
                    "type": "item",
                    "name": "minecraft:stone",
                    "functions": [
                        {
                            "function": "set_data",
                            "data": 4
                        }
                    ]
                }

set_data_from_color_index

从关联实体的颜色索引继承返回物品的数据值。 游戏中的一个示例是一只粉红色的羊在死亡时掉落粉红色的羊毛。 如果关联实体没有设置颜色索引(或者它在箱子的战利品表中使用),它将始终导致数据值为 0。

                {
                    "type": "item",
                    "name": "minecraft:wool",
                    "weight": 1,
                    "functions": [
                        {
                            "function": "set_data_from_color_index"
                        }
                    ]
                }

set_lore

此函数允许您设置物品的知识。 lore 对象中的每一行代表一行文本。 目前不支持 rawtext

                {
                    "type": "item",
                    "name": "minecraft:stick",
                    "weight": 1,
                    "functions": [
                        {
                            "function": "set_lore",
                            "lore": [
                                "Big Stick has been handed down",
                                "for generations from King to Prince",
                                "until it went missing four score ago"
                            ]
                       }
                    ]
                 }

set_name

此函数允许您设置物品的名称。 目前不支持 rawtext

                {
                    "type": "item",
                    "name": "minecraft:stick",
                    "weight": 1,
                    "functions": [
                        {
                            "function": "set_name",
                            "name": "Big Stick"
                        }
                    ]
                 }

其他

以下包含无法归类的函数。

fill_container

此函数允许您定义箱子的战利品表。 当物品生成并且玩家放置该物品时,它会充满所引用的战利品表中定义的内容。

箱子的战利品表在打开或打破时生成。 将其视为薛定谔的盒子。 箱子的内容直到您往里看时才决定。

                  {
                      "type": "item",
                      "name": "minecraft:chest",
                      "functions": [
                          {
                              "function": "fill_container",
                              "loot_table": "loot_tables/chests/simple_dungeon.json"
                          }
                      ]
                  }

建议在使用 fill_container 时始终使用 set_name 为箱子命名。 否则,生成的箱子在玩家的物品栏中看起来与普通的空箱子没有任何不同。

exploration_map

将普通地图转换为标记了隐藏宝藏位置的藏宝图。 destination 值定义了他们收到的藏宝图类型。

                  {
                      "type": "item",
                      "name": "minecraft:map",
                      "weight": 1,
                      "functions": [
                          {
                              "function": "exploration_map",
                              "destination": "buriedtreasure"
                          }
                      ]
                  }

您目前可以从以下目的地中进行选择:

目的地
buriedtreasure(埋藏的宝藏)
endcity(末地城)
fortress(堡垒)
mansion(豪宅)
mineshaft(矿井)
monument(纪念碑)
pillageroutpost(掠夺者前哨)
ruins(废墟)
shipwreck(沉船)
stronghold(要塞)
temple(神庙)
village(村庄)

furnace_smelt(仅限战利品表)

如果要返回的物品具有熔炼的制作配方,并且战利品表是由被火杀死的实体(火焰附加、打火石、熔岩等)触发的,则结果将是该物品的熔炼版本。 由于这些要求,此函数在村民交易或箱子中不起作用。 此函数仅在与 minecraft:loot 行为结合使用时才有效。

以下示例将在实体被火杀死时掉落石块,否则会掉落圆石。

                {
                    "type": "item",
                    "name": "minecraft:cobblestone",
                    "weight": 1,
                    "functions": [
                        {
                            "function": "furnace_smelt"
                        }
                    ]
                }