Class ResourceConditions
Predicate<JsonObject>
that can decide whether a resource should be loaded or not.
- A JSON object that may contain a condition can be parsed with
objectMatchesConditions(com.google.gson.JsonObject)
. This is the preferred way of implementing conditional objects, as it handles the details of the format (see below) and catches and logs thrown exceptions. This function should only be called from the "apply" phase of aResourceReloader
, otherwise some conditions might behave in unexpected ways. - The lower-level
conditionsMatch(com.google.gson.JsonArray, boolean)
andconditionMatches(com.google.gson.JsonObject)
may be useful when implementing conditions. - Conditions are registered with
register(net.minecraft.util.Identifier, java.util.function.Predicate<com.google.gson.JsonObject>)
and queried withget(net.minecraft.util.Identifier)
.
At the moment, Fabric only recognizes conditions for resources loaded by subclasses of JsonDataLoader
.
This means: recipes, advancements, loot tables, loot functions and loot conditions.
Fabric provides some conditions, which can be generated using the helper methods in DefaultResourceConditions
.
Details of the format
A conditional JSON object must have a CONDITIONS_KEY
entry, containing an array of condition objects.
The conditions in the array must be satisfied to load the resource.
Each condition object must contain a CONDITION_ID_KEY
entry with the identifier of the condition,
and it may also contain additional data for the condition.
Here is an example of a resource that is only loaded if no mod with id a
is loaded:
{
... // normal contents of the resource
"fabric:load_conditions": [ // array of condition objects
{ // a condition object
// the identifier of the condition... the "fabric:not" condition inverts the condition in its "value" field
"condition": "fabric:not",
// additional data, for "fabric:not", the "value" field is required to be another condition object
"value": {
// the identifier of the condition
"condition": "fabric:all_mods_loaded",
// additional data, for "fabric:all_mods_loaded"
"values": [
"a"
]
}
}
]
}
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
conditionMatches
(com.google.gson.JsonObject condition) Check if the passed condition object matches.static boolean
conditionsMatch
(com.google.gson.JsonArray conditions, boolean and) Ifand
is true, check if all the passed conditions match.static @Nullable Predicate<com.google.gson.JsonObject>
get
(net.minecraft.util.Identifier identifier) Get the resource condition with the passed name, ornull
if none is registered (yet).static boolean
objectMatchesConditions
(com.google.gson.JsonObject object) Check if the passed JSON object either has nofabric:conditions
tag, or all of its conditions match.static void
Register a new resource condition.
-
Field Details
-
CONDITIONS_KEY
The key ("fabric:load_conditions") Fabric uses to identify resource conditions in a JSON object.- See Also:
-
CONDITION_ID_KEY
The key ("condition") identifying the resource condition's identifier inside a condition object.- See Also:
-
-
Method Details
-
register
public static void register(net.minecraft.util.Identifier identifier, Predicate<com.google.gson.JsonObject> condition) Register a new resource condition.- Throws:
IllegalArgumentException
- If a resource condition is already registered with the same name.
-
get
@Nullable public static @Nullable Predicate<com.google.gson.JsonObject> get(net.minecraft.util.Identifier identifier) Get the resource condition with the passed name, ornull
if none is registered (yet). -
objectMatchesConditions
public static boolean objectMatchesConditions(com.google.gson.JsonObject object) Check if the passed JSON object either has nofabric:conditions
tag, or all of its conditions match. This should be called for objects that may contain a conditions entry.This function should only be called from the "apply" phase of a
ResourceReloader
, otherwise some conditions might behave in unexpected ways.If an exception is thrown during condition testing, it will be caught and logged, and false will be returned.
-
conditionsMatch
public static boolean conditionsMatch(com.google.gson.JsonArray conditions, boolean and) throws RuntimeException Ifand
is true, check if all the passed conditions match. If it is false, check if at least one of the passed conditions matches.- Throws:
RuntimeException
- If some condition failed to parse.
-
conditionMatches
public static boolean conditionMatches(com.google.gson.JsonObject condition) throws RuntimeException Check if the passed condition object matches.- Throws:
RuntimeException
- If some condition failed to parse.
-