mojira.dev
MC-306988

Data Driven Villager Trades are not able to specify the level of a sold enchantment while maintaining use of the random cost function

Currently, the data driven trades for Librarians selling enchanted books use function ‘minecraft:enchant_randomly’ . This applies the enchantment, level, and emerald cost.

minecraft/villager_trade/librarian/1/
#emerald_and_book_enchanted_book.json

{
  "additional_wants": {
    "id": "minecraft:book"
  },
  "double_trade_price_enchantments": "#minecraft:double_trade_price",
  "given_item_modifiers": [
    {
      "function": "minecraft:enchant_randomly",
      "include_additional_cost_component": true,
      "only_compatible": false,
      "options": "#minecraft:tradeable"
    },
    {
      "function": "minecraft:filtered",
      "item_filter": {
        "items": "minecraft:enchanted_book",
        "predicates": {
          "minecraft:stored_enchantments": [
            {}
          ]
        }
      },
      "on_fail": {
        "function": "minecraft:discard"
      }
    }
  ],
  "gives": {
    "id": "minecraft:enchanted_book"
  },
  "max_uses": 12.0,
  "reputation_discount": 0.2,
  "wants": {
    "count": 0.0,
    "id": "minecraft:emerald"
  }
}

The enchantment type can be forced by modifying the above file into the following:
(Specifically the line ' “options”: “#minecraft:tradeable” ' is changed to ' “options”:”minecraft:smite” '

{
  "additional_wants": {
    "id": "minecraft:book"
  },
  "double_trade_price_enchantments": "#minecraft:double_trade_price",
  "given_item_modifiers": [
    {
      "function": "minecraft:enchant_randomly",
      "include_additional_cost_component": true,
      "only_compatible": false,
      "options": "minecraft:smite"
    },
    {
      "function": "minecraft:filtered",
      "item_filter": {
        "items": "minecraft:enchanted_book",
        "predicates": {
          "minecraft:stored_enchantments": [
            {}
          ]
        }
      }
    }
  ],
  "gives": {
    "id": "minecraft:enchanted_book"
  },
  "max_uses": 12.0,
  "reputation_discount": 0.2,
  "wants": {
    "count": 0.0,
    "id": "minecraft:emerald"
  }
}

Using the above the enchantment will always be Smite, but it’s level will be random.

Additional edits to the trade result in the same forced enchantment, but with a random level (as seen below):

{
  "additional_wants": {
    "id": "minecraft:book"
  },
  "double_trade_price_enchantments": "#minecraft:double_trade_price",
  "given_item_modifiers": [
    {
      "function": "minecraft:enchant_randomly",
      "include_additional_cost_component": true,
      "only_compatible": false,
      "options": "minecraft:smite"
    },
    {
      "function": "minecraft:filtered",
      "item_filter": {
        "items": "minecraft:enchanted_book",
        "predicates": {
          "minecraft:stored_enchantments": [
            {"minecraft:smite": 5}
          ]
        }
      },
      "on_fail": {
        "function": "minecraft:discard"
      }
    }
  ],
  "gives": {
    "id": "minecraft:enchanted_book"
  },
  "max_uses": 12.0,
  "reputation_discount": 0.2,
  "wants": {
    "count": 0.0,
    "id": "minecraft:emerald"
  }
}
{
  "additional_wants": {
    "id": "minecraft:book"
  },
  "double_trade_price_enchantments": "#minecraft:double_trade_price",
  "given_item_modifiers": [
    {
      "function": "minecraft:enchant_randomly",
      "include_additional_cost_component": true,
      "only_compatible": false,
      "options": "minecraft:smite"
    },
    {
      "function": "minecraft:filtered",
      "item_filter": {
        "items": "minecraft:enchanted_book",
        "predicates": {
          "minecraft:stored_enchantments": [
            {
              "enchantments": "minecraft:smite",
              "levels": 5
            }
          ]
        }
      }
    }
  ],
  "gives": {
    "id": "minecraft:enchanted_book"
  },
  "max_uses": 12.0,
  "reputation_discount": 0.2,
  "wants": {
    "count": 0.0,
    "id": "minecraft:emerald"
  }
}
{
  "additional_wants": {
    "id": "minecraft:book"
  },
  "double_trade_price_enchantments": "#minecraft:double_trade_price",
  "given_item_modifiers": [
    {
      "function": "minecraft:enchant_randomly",
      "include_additional_cost_component": true,
      "only_compatible": false,
      "options": "minecraft:smite"
    },
    {
      "function": "minecraft:filtered",
      "item_filter": {
        "items": "minecraft:enchanted_book",
        "predicates": {
          "minecraft:stored_enchantments": [
            {
              "enchantments": "minecraft:smite",
              "levels": 5
            }
          ]
        }
      }
    }
  ],
  "gives": {
    "id": "minecraft:enchanted_book",
    "minecraft:stored_enchantments": {
        "minecraft:smite": 5
      }
  },
  "max_uses": 12.0,
  "reputation_discount": 0.2,
  "wants": {
    "count": 0.0,
    "id": "minecraft:emerald"
  }
}
{
  "additional_wants": {
    "id": "minecraft:book"
  },
  "double_trade_price_enchantments": "#minecraft:double_trade_price",
  "given_item_modifiers": [
    {
      "function": "minecraft:enchant_randomly",
      "include_additional_cost_component": true,
      "only_compatible": false,
      "options": "minecraft:smite",
      "level":  5      
    },
    {
      "function": "minecraft:filtered",
      "item_filter": {
        "items": "minecraft:enchanted_book",
        "predicates": {
          "minecraft:stored_enchantments": [
            {}
          ]
        }
      }
    }
  ],
  "gives": {
    "id": "minecraft:enchanted_book"
  },
  "max_uses": 12.0,
  "reputation_discount": 0.2,
  "wants": {
    "count": 0.0,
    "id": "minecraft:emerald"
  }
}

Etc. If there’s a specific field for where the enchantment level should be placed, I’ve failed to stumble upon it. As well, there’s been no ideas suggested from the supporting communities, thus the bug report.

An datapack, demonstrating this has been attached. Please try with multiple librarians, as the random selection of level may produce 'level V' several times in a row.

Attachments

Comments 4

Thank you for helping us improve Minecraft! We saved your files:

[media]

Thank you for your report!
However, this issue is Invalid.

This report does not describe a bug.

The enchant_randomly loot function enchants an item with a random level, by definition. You need to use the set_enchantments loot function to set specific levels, or to specify them directly in the item stack object stored in the gives field.

Quick Links:
📓 Bug Tracker Guidelines – 💬 Community Support – 📧 Mojang Support (Technical Issues) – 📧 Microsoft Support (Account Issues)
📓 Project Summary – ✍️ Feedback and Suggestions – 📖 Game Wiki

ManosSef,

Thank you for reviewing this tick. I’d like to draw your attention to an incorrect assertion:

or to specify them directly in the item stack object stored in the gives field.

In the examples listed, this specific action was taken, documented, and showed no such result:

  "gives": {
    "id": "minecraft:enchanted_book",
    "minecraft:stored_enchantments": {
        "minecraft:smite": 5
      }

minecraft:stored_enchantments is a component. It needs to be specified in the components field.

Dumb Bunny

(Unassigned)

Unconfirmed

(Unassigned)

26.1 Release Candidate 2

Retrieved