Predicate
A predicate (also known as a loot condition) is a data structure commonly used throughout data packs and commands, invoked to check boolean conditions either in the world or within a given loot context. Predicates do not change data or affect the world, they only check information (that they have access to) and return either a pass or a fail. They can be defined and registered in data packs, or they can be directly defined "in-line" in various places. They can be used throughout the game such as in commands, loot tables, and advancements. 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 JSON files in data packs containing the definition for a predicate. The file path indicates the namespaced identifier used to reference it.
This article describes how predicates are defined and invoked, and also describes the JSON structure of a predicate.
Definition
[edit | edit source]A predicate file is defined with the JSON format.
Predicate files are part of the data pack directory structure, highlighted below:
data pack name.zip or
data pack name
pack.mcmeta
data
namespace
predicate
<name>.json
- More directories…
The root element of a predicate file can be either an [NBT Compound / JSON Object] object following the predicate structure below, or an [NBT List / JSON Array] array containing multiple predicates. In the latter case all predicates must evaluate to true.
Usage
[edit | edit source]Predicates and predicate files can be invoked in several different manners from other data pack files:
Commands
[edit | edit source]Commands or functions can invoke predicate files in two ways:
- 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. /execute: 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
[edit | edit source]The condition type minecraft:reference invokes a predicate file and returns the result to the invoker.
Presence in other files
[edit | edit source]In addition to predicate files, predicates themselves are used in other locations within other data pack files such as advancements and loot tables.
Examples
[edit | edit source]Below are a few simple predicate examples for context.
Example 1: Check if player is holding a diamond sword
[edit | edit source]{
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"equipment": {
"mainhand": {
"items": ["minecraft:diamond_sword"]
}
}
}
}
Example 2: Only run if it’s daytime
[edit | edit source]{
"condition": "minecraft:time_check",
"value": { "min": 0, "max": 12000 },
"period": 24000
}
Example 3: Nested example using any_of
[edit | edit source]{
"condition": "minecraft:any_of",
"terms": [
{ "condition": "minecraft:random_chance", "chance": 0.25 },
{ "condition": "minecraft:weather_check", "raining": true }
]
}These snippets can be placed inside data/<namespace>/predicate/ within a data pack, then referenced using commands such as:
/execute if predicate my_pack:daytime_check run say Good morning!
JSON format
[edit | edit source]This section describes the JSON format of a predicate.
- [NBT List / JSON Array][NBT Compound / JSON Object]: The root element of the predicate.
- [String] condition[until: JE 26.3] / [String] type[upcoming: JE 26.3]: The resource location of the condition type to check.
- Other parts of the predicate, specified below. In lists each element must evaluate to true for the predicate to pass.
The possible values for [String] condition and associated extra contents:
- all_of — Evaluates a list of predicates and passes if all of them pass. Invokable from any context.
- [NBT List / JSON Array] terms: The list of predicates to evaluate.[until: JE 26.3] / an inline value, a single namespaced ID, a list of namespaced IDs, a list of inline values, or a hash-prefixed tag ID of a
minecraft:predicatetype.[upcoming: JE 26.3]- [NBT List / JSON Array][NBT Compound / JSON Object] A predicate, following this structure recursively.
- [NBT List / JSON Array] terms: The list of predicates to evaluate.[until: JE 26.3] / an inline value, a single namespaced ID, a list of namespaced IDs, a list of inline values, or a hash-prefixed tag ID of a
- any_of — Evaluates a list of predicates and passes if any one of them passes. Invokable from any context.
- [NBT List / JSON Array] terms: The list of predicates to evaluate.[until: JE 26.3] / an inline value, a single namespaced ID, a list of namespaced IDs, a list of inline values, or a hash-prefixed tag ID of a
minecraft:predicatetype.[upcoming: JE 26.3]- [NBT List / JSON Array][NBT Compound / JSON Object] A predicate, following this structure recursively. Note that any_of only applies to [NBT List / JSON Array] terms, nested lists are all_of.
- [NBT List / JSON Array] terms: The list of predicates to evaluate.[until: JE 26.3] / an inline value, a single namespaced ID, a list of namespaced IDs, a list of inline values, or a hash-prefixed tag ID of a
- block_state_property[until: JE 26.3] — Checks the mined block and its block states. Requires block state provided by loot context, and always fails if not provided.
- [String] block: A block ID. The test fails if the block doesn't match.
- [NBT Compound / JSON Object] properties: (Optional) A map of block state names to values. Errors if the block doesn't have these properties.
- [String] name: A block state and an exact value. The value is a string.
- [NBT Compound / JSON Object] name: A block state name and a ranged value to match.
- [String] min: The min value.
- [String] max: The max value.
- damage_source_properties — Checks properties of the damage source. Requires origin and damage source provided by loot context, and always fails if not provided.
- [NBT Compound / JSON Object] predicate: Predicate applied to the damage source.
- Damage type predicate see Template:Nbt inherit/conditions/damage_type/template
- [NBT Compound / JSON Object] predicate: Predicate applied to the damage source.
- enchantment_active_check — Checks if the enchantment has been active. Requires enchantment active status provided by loot context, and always fails if not provided. It is therefore only usable from the
enchanted_locationloot context.- [Boolean] active: Whether to check for an active (
true) or inactive (false) enchantment.
- [Boolean] active: Whether to check for an active (
- entity_properties — Checks properties of an entity. Invokable from any context.
- [String] entity: The entity to check. Specifies an entity from loot context. Can be
this,attacker,direct_attacker,attacking_player,target_entity, orinteracting_entity.. - [NBT Compound / JSON Object] predicate: Predicate applied to entity, uses same structure as advancements.
- entity predicate see Template:Nbt inherit/conditions/entity/template
- [String] entity: The entity to check. Specifies an entity from loot context. Can be
- entity_scores — Checks the scoreboard scores of an entity. Requires the specified entity provided by loot context, and always fails if not provided.
- [String] entity: The entity to check. Specifies an entity from loot context. Can be
this,attacker,direct_attacker,attacking_player,target_entity, orinteracting_entity.. - [NBT Compound / JSON Object] scores: Scores to check. All specified scores must pass for the condition to pass.
- [NBT Compound / JSON Object] A score: Key name is the objective while the value specifies a range of score values required for the condition to pass.
- [Int][NBT Compound / JSON Object] min: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
minecraft:number_providertype.[upcoming: JE 26.3] Minimum score. Optional. - [Int][NBT Compound / JSON Object] max: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
minecraft:number_providertype.[upcoming: JE 26.3] Maximum score. Optional.
- [Int][NBT Compound / JSON Object] min: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
- [Int] A score: Shorthand version of the other syntax above, to check the score against a single number only. Key name is the objective while the value is the required score.
- [NBT Compound / JSON Object] A score: Key name is the objective while the value specifies a range of score values required for the condition to pass.
- [String] entity: The entity to check. Specifies an entity from loot context. Can be
- inverted — Inverts another predicate condition. Invokable from any context.
- killed_by_player — Checks if there is a
attacking_playerentity provided by loot context. Requiresattacking_playerentity provided by loot context, and always fails if not provided.
- location_check — Checks the current location against location criteria. Requires origin provided by loot context, and always fails if not provided.
- [Int] offsetX: An optional x offset to the location.
- [Int] offsetY: An optional y offset to the location.
- [Int] offsetZ: An optional z offset to the location.
- [NBT Compound / JSON Object] predicate: Predicate applied to location, uses same structure as advancements.
- location predicate see Template:Nbt inherit/conditions/location/template
- match_block[upcoming: JE 26.3] — Matches block state and optionally block entity data and components (if present).
- [String][NBT List / JSON Array] blocks: Optional; Can be a single block ID, a list of namespaced block IDs, or a hash-prefixed block tag to match.
- [NBT Compound / JSON Object] state: Optional; The block state property keys to values to match.
- [NBT Compound / JSON Object] nbt: Optional; Block entity NBT to match. See Block entity format. Match will fail if the field is present, but the block entity is not available in context.
- [NBT Compound / JSON Object] components: Optional; Map of exact components to match. Match will fail if the field is present, but the block entity is not available in context.
- [NBT Compound / JSON Object] predicates: Optional; Map of component predicates to match. Match will fail if the field is present, but the block entity is not available in context.
- match_tool — Checks tool used to mine the block. Requires tool provided by loot context, and always fails if not provided.
- [NBT Compound / JSON Object] predicate: Predicate applied to item, uses same structure as advancements.
- item predicate see Template:Nbt inherit/conditions/item/template
- [NBT Compound / JSON Object] predicate: Predicate applied to item, uses same structure as advancements.
- 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.
- [Float][NBT Compound / JSON Object] chance: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
minecraft:number_providertype.[upcoming: JE 26.3] Success rate as a number 0.0 – 1.0.
- [Float][NBT Compound / JSON Object] chance: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
- 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
attackerentity provided by loot context, and if not provided, the enchantment level is regarded as 0.- [Float] unenchanted_chance: The success rate to use when the enchantment is not present; 0.0–1.0.
- [Float][NBT Compound / JSON Object] enchanted_chance: level-based value. The success rate based on the level when then enchantment is present; 0.0–1.0.
- [String] enchantment: One enchantment (an [String] ID). The enchantment whose level to use for the chance calculation. If the enchantment is not present, uses
0as level.
- reference[until: JE 26.3] — Invokes a predicate file and returns its result. Invokable from any context. Cannot be used in enchantment definitions.
- [String] name: The resource location of the predicate to invoke. A cyclic reference causes a parsing failure.
- survives_explosion — Returns success with
1 ÷ explosion radiusprobability. Requires explosion radius provided by loot context, and always success if not provided.
- table_bonus — Passes with probability picked from a list, indexed by enchantment power. Requires tool provided by loot context. If not provided, the enchantment level is regarded as 0.
- [String] enchantment: Resource location of enchantment.
- [NBT List / JSON Array] chances: List of probabilities for enchantment power, indexed from 0.
- time_check — Compares the current day time (or rather,
24000 * day count + day time) against given values. Invokable from any context.- [String] clock: One world clock (an [String] ID) to check the time of.
- [NBT Compound / JSON Object] value: The time to compare the day time against.
- [Int][NBT Compound / JSON Object] min: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
minecraft:number_providertype.[upcoming: JE 26.3] The minimum value. - [Int][NBT Compound / JSON Object] max: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
minecraft:number_providertype.[upcoming: JE 26.3] The maximum value.
- [Int][NBT Compound / JSON Object] min: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
- [Int] value: Shorthand version of [NBT Compound / JSON Object] value above, used to check for a single value only. Number providers cannot be used in this shorthand form.
- [Int] period: If present, the day time is first reduced modulo the given number before being checked against [NBT Compound / JSON Object][Int] value. For example, setting this to 24000 causes the checked time to be equal to the current daytime.
- value_check — Compares a number against another number or range of numbers. Invokable from any context.
- [Int][NBT Compound / JSON Object] value: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
minecraft:number_providertype.[upcoming: JE 26.3] The number to test. - [NBT Compound / JSON Object] range: The range of numbers to compare [Int][NBT Compound / JSON Object] value against.
- [Int][NBT Compound / JSON Object] min: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
minecraft:number_providertype.[upcoming: JE 26.3] The minimum value. - [Int][NBT Compound / JSON Object] max: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
minecraft:number_providertype.[upcoming: JE 26.3] The maximum value.
- [Int][NBT Compound / JSON Object] min: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
- [Int] range: Shorthand version of [NBT Compound / JSON Object] range above, used to compare [Int][NBT Compound / JSON Object] value against a single number only. Number providers cannot be used in this shorthand form.
- [Int][NBT Compound / JSON Object] value: A number provider.[until: JE 26.3] / an inline value or a namespaced ID of a
- weather_check — Checks the current game weather. Invokable from any context.
- [Boolean] raining: If true, the condition passes only if it is raining or thundering.
- [Boolean] thundering: If true, the condition passes only if it is thundering.
History
[edit | edit source]| Java Edition | |||||||
|---|---|---|---|---|---|---|---|
| 1.15 | 19w38a | Added predicates to data packs. | |||||
| 1.16 | 20w12a | Added fishing_hook sub-predicate into entity predicate to check properties of the fishing hook. | |||||
| 1.19 | 22w14a | player, fishing_hook, lightning_bolt, and catType fields have been replaced with type_specific. type_specific has field type (one of player, fishing_hook, lightning_bolt, or cat) and same sub-fields as removed fields. | |||||
| 1.19.3 | 23w06a | Removed is_projectile, is_explosion, bypasses_armor, bypasses_invulnerability, bypasses_magic, is_fire, is_magic, and is_lightning fields from damage_source_properties. | |||||
A new tags array has been added to damage_source_properties. Each entry has two fields:
| |||||||
| 1.20 | 23w18a | Condition alternative has been renamed to any_of. | |||||
Added new condition all_of that passes only when all sub-conditions pass, with the same syntax as any_of. | |||||||
| 1.20.5 | 24w09a | Item predicate format has been updated:
| |||||
Block predicate format has been updated:
| |||||||
Fluid predicate format has been updated:
| |||||||
Entity predicate format has been updated:
| |||||||
Location predicate format has been updated:
| |||||||
| 24w10a | Added entity sub-predicate slots, used to check single or multiple slots on any entity. | ||||||
| 24w11a | Removed entity sub-predicate any
Component-specific item predicate properties have been moved to separate field predicates | ||||||
|
Item sub-predicate format has been updated:
| |||||||
| 1.21 | 24w18a | Renamed random_chance_with_looting to random_chance_with_enchanted_bonus, changing the chance field from a float to a level-based value, removing the looting_multiplier field, and adding a enchantment field. | |||||
Changed the chance field of random_chance from a float into a number provider. | |||||||
Added a can_see_sky field to location predicates. | |||||||
Renamed enchantment to enchantments in the minecraft:enchantments item sub-predicate; now accepting multiple possible enchantments, or tags. | |||||||
|
Changes to the entity predicate:
| |||||||
Renamed loot context entities: killer renamed to attacker; direct_killer renamed to direct_attacker; killer_player renamed to attacking_player. | |||||||
| 24w19a | Added is_direct field to damage type predicates. | ||||||
| 24w21a | Added jukebox_playable item predicate. | ||||||
The gamemode field of the player predicate now accepts a list of gamemodes rather than a single string. | |||||||
| pre1 | Split chance into unenchanted_chance and enchanted_chance in the random_chance_with_enchanted_bonus condition. | ||||||
| 1.21.2 | 24w35a | New entity sub-predicate for testing sheep wool. Fields: sheared and color. | |||||
| 24w36a | New entity sub-predicate for testing player input. Available inputs to test are forward, backward left, right, jump, sneak, and sprint. | ||||||
| 24w37a | New entity sub-predicate for salmon. Fields: variant. | ||||||
| 24w39a | Removed the minecraft:boat entity sub-predicate, since boats no longer have variants. | ||||||
| 1.21.5 | 25w03a | A new optional field was added to entity predicate: components. | |||||
Several entity sub-predicates have been removed and replaced with combination of components predicate and variant entity components. | |||||||
The field color has been removed from minecraft:sheep sub-predicate. | |||||||
| 25w07a | The stepping_on entity predicate can only evaluate to true if the entity is on ground. | ||||||
| 25w10a | New optional fields have been added to block predicates: components and predicates. | ||||||
A new optional field has been added to entity predicates: predicates. | |||||||
| 1.21.11 | 25w41a | Component predicates (predicates field in block, item and entity predicates) now include predicates for checking existence of every component type. | |||||
The flags entity predicate now supports new values. | |||||||
| 26.1 | snap1 | player Sub-Predicate has a new optional field: food. | |||||
| snap3 | minecraft:time_check loot predicate: added clock field. | ||||||
| pre1 | Added the minecraft:environment_attribute_check loot predicate. | ||||||
| 26.2 | snap3 | The entity predicate format has changed from a structure with multiple optional fields to one similar to data component maps. | |||||
Added minecraft:entity_tags entity sub-predicate. | |||||||
| snap4 | The entity sub-predicate slime has been renamed to cube_mob. | ||||||
| Upcoming Java Edition | |||||||
| 26.3 | snap3 | New entries added to previously empty minecraft:predicate: minecraft:tool/can_silk_touch, minecraft:tool/can_shear. | |||||
The minecraft:potion_contents data component predicate has been changed to allow matching on specific potion effects. | |||||||
| snap4 | Added the match_block condition type.
| ||||||
The condition field has been renamed to type.
| |||||||
| Numerous fields in predicates now accept an inline value or a namespaced ID of a corresponding type. | |||||||
Removed the reference condition type.
| |||||||
Removed the block_state_property condition type. It has been fully replaced by match_block.
| |||||||
See also
[edit | edit source]- Data pack – where predicates are defined and stored
- Loot table – one of the most common predicate users
- Advancement – another system that embeds predicates
- Execute – command subcomponent that can run predicates