Class ResourceConditions

java.lang.Object
net.fabricmc.fabric.api.resource.conditions.v1.ResourceConditions

public final class ResourceConditions extends Object
Registration and access to resource loading conditions. A resource condition is an identified Predicate<JsonObject> that can decide whether a resource should be loaded or not.

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 Details

    • CONDITIONS_KEY

      public static final String CONDITIONS_KEY
      The key ("fabric:load_conditions") Fabric uses to identify resource conditions in a JSON object.
      See Also:
    • CONDITION_ID_KEY

      public static final String 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(Identifier identifier, Predicate<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<JsonObject> get(Identifier identifier)
      Get the resource condition with the passed name, or null if none is registered (yet).
    • objectMatchesConditions

      public static boolean objectMatchesConditions(JsonObject object)
      Check if the passed JSON object either has no fabric: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(JsonArray conditions, boolean and) throws RuntimeException
      If and 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(JsonObject condition) throws RuntimeException
      Check if the passed condition object matches.
      Throws:
      RuntimeException - If some condition failed to parse.