Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Minecraft:Predicate

From SAS Gaming Wiki

Template:For Template:Exclusive

Predicates are Minecraft:JSON structures invoked to check conditions within the world. They return a pass or fail result to the invoker, which acts differently based on the result. They can be defined and invoked by Minecraft:data packs or by Minecraft:commands. In practical terms, predicates are a flexible way for data packs to encode "if this, then that" logic without needing custom code.

Predicate files are standalone data pack files containing one or more predicates.

This article describes how predicates are defined and invoked, and also describes the JSON structure of a predicate.

Definition

A predicate file is defined with the Minecraft:JSON format.

Predicate files are part of the data pack directory structure, highlighted below:

Template:Data pack directory

The root element of a predicate file can be either an Template:Nbt following the predicate structure below, or an Template:Nbt containing multiple predicates. In the latter case all predicates must evaluate to true.

Usage

Predicates and predicate files can be invoked in several different manners from other data pack files:

Commands

Minecraft:Commands or functions can invoke predicate files in two ways:

  • Minecraft:Target selectors: The selector argument predicate= checks predicate files as a filter for entity selection. The predicate file is invoked once per entity that needs filtering, each time being at the entity's location.
  • Template:Cmd: A subcommand, /execute if predicate, can invoke a predicate file or an in-line predicate definition to either return a result or to decide whether to continue with a subcommand chain. The predicate is invoked once at the current contextual position of execution.

Other predicates

The condition type minecraft:reference invokes a predicate file and returns the result to the invoker.

Presence in other files

In addition to predicate files, predicates themselves are used in other locations within other data pack files such as advancements and loot tables.

Examples

Below are a few simple predicate examples for context.

Example 1: Check if player is holding a diamond sword

{
  "condition": "minecraft:entity_properties",
  "entity": "this",
  "predicate": {
    "equipment": {
      "mainhand": {
        "items": ["minecraft:diamond_sword"]
      }
    }
  }
}

Example 2: Only run if it’s daytime

{
  "condition": "minecraft:time_check",
  "value": { "min": 0, "max": 12000 },
  "period": 24000
}

Example 3: Nested example using any_of

{
  "condition": "minecraft:any_of",
  "terms": [
    { "condition": "minecraft:random_chance", "chance": 0.25 },
    { "condition": "minecraft:weather_check", "raining": true }
  ]
}

These snippets can be placed inside Template:Cd within a data pack, then referenced using commands such as:

/execute if predicate my_pack:daytime_check run say Good morning!

JSON format

This section describes the JSON format of a predicate.

The possible values for Template:Nbt and associated extra contents:

  • all_of—Evaluates a list of predicates and passes if all of them pass. Invokable from any context.
  • any_of—Evaluates a list of predicates and passes if any one of them passes. Invokable from any context.
  • enchantment_active_check—Checks if the enchantment has been active. Requires enchantment active status provided by Minecraft:loot context, and always fails if not provided. It is therefore only usable from the Template:Cd loot context.
  • inverted—Inverts another predicate condition. Invokable from any context.
    • Template:Nbt: The condition to be negated, following the same structure as outlined here, recursively.
  • random_chance—Generates a random number between 0.0 and 1.0, and checks if it is less than a specified value. Invokable from any context.
  • random_chance_with_enchanted_bonus—Generates a random number between 0.0 and 1.0, and checks if it is less than the value determined using the level of a given enchantment. Requires attacker entity provided by Minecraft:loot context, and if not provided, the enchantment level is regarded as 0.
  • weather_check—Checks the current game weather. Invokable from any context.
    • Template:Nbt: If true, the condition passes only if it is raining or thundering.
    • Template:Nbt: If true, the condition passes only if it is thundering.

History

Template:HistoryTable

See also

External links

Navigation

Template:Navbox Java Edition technical

Minecraft:de:Prädikate Minecraft:fr:Prédicat Minecraft:ja:プレディケート Minecraft:pt:Predicado Minecraft:zh:谓词