drafts:resourceconditions
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| drafts:resourceconditions [2026/03/03 13:10] – infinitychances | drafts:resourceconditions [2026/03/20 17:27] (current) – changed example again to not require build script changes infinitychances | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| Sometimes you want a recipe to only exist when a certain mod is loaded, or maybe have something happen when an empty tag actually has some items. This is where resource conditions come into play. | Sometimes you want a recipe to only exist when a certain mod is loaded, or maybe have something happen when an empty tag actually has some items. This is where resource conditions come into play. | ||
| Resource Conditions allow you to define things like these in a non-breaking way. | Resource Conditions allow you to define things like these in a non-breaking way. | ||
| + | |||
| + | ===== Usage in JSON ===== | ||
| + | Resource Conditions are placed in a list, often at the top of the file for simplicity.\\ | ||
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | }, | ||
| + | { | ||
| + | ... | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| ===== Types of Conditions ===== | ===== Types of Conditions ===== | ||
| Line 9: | Line 26: | ||
| ==== TrueResourceCondition ==== | ==== TrueResourceCondition ==== | ||
| - | The TrueResourceCondition returns true. That's all it does. | + | The TrueResourceCondition returns true. That's all it does.\\ |
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| ==== AllModsLoadedResourceCondition ==== | ==== AllModsLoadedResourceCondition ==== | ||
| - | This one returns true if all mods(defined by id) listed in it are loaded(running in the current environment). | + | This one returns true if all mods(defined by id) listed in it are loaded(running in the current environment).\\ |
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| ==== AnyModsLoadedResourceCondition ==== | ==== AnyModsLoadedResourceCondition ==== | ||
| - | Returns true if any mods listed in it are loaded. | + | Returns true if any mods listed in it are loaded.\\ |
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| ==== TagsPopulatedResourceCondition ==== | ==== TagsPopulatedResourceCondition ==== | ||
| - | Returns true if all tags listed in it have members(not empty). | + | Returns true if all tags listed in it have members(not empty).\\ |
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| ==== FeaturesEnabledResourceCondition ==== | ==== FeaturesEnabledResourceCondition ==== | ||
| - | Returns true if all features listed in it are enabled. | + | Returns true if all features listed in it are enabled.\\ |
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| ==== RegistryContainsResourceCondition ==== | ==== RegistryContainsResourceCondition ==== | ||
| - | Returns true if the registry has the identifiers listed in it as entries. | + | Returns true if the registry has the identifiers listed in it as entries.\\ |
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| ==== NotResourceCondition ==== | ==== NotResourceCondition ==== | ||
| - | Returns true if the resource condition nested inside is false. | + | Returns true if the resource condition nested inside is false.\\ |
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| ==== OrResourceCondition ==== | ==== OrResourceCondition ==== | ||
| - | Returns true if either | + | Returns true if any condition inside it is true.\\ |
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| ==== AndResourceCondition ==== | ==== AndResourceCondition ==== | ||
| - | Returns true if both conditions are true. | + | Returns true if all conditions are true.\\ |
| + | Example: | ||
| + | < | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | ], | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Usage in Datagen ===== | ||
| + | This section will assume you have already set up a basic datagen setup. It will use recipes as an example. | ||
| + | |||
| + | We will create a recipe that creates a minecart with copper if the minecart improvements experiment is enabled. | ||
| + | |||
| + | Next, we can go into our recipe provider and begin. | ||
| + | <code java [enable_line_numbers=" | ||
| + | public class ModRecipeProvider extends FabricRecipeProvider { | ||
| + | ... | ||
| + | |||
| + | @Override | ||
| + | protected RecipeProvider createRecipeProvider(HolderLookup.Provider provider, RecipeOutput output) { | ||
| + | return new RecipeProvider(provider, | ||
| + | @Override | ||
| + | public void buildRecipes() { | ||
| + | shaped(RecipeCategory.MISC, | ||
| + | .pattern(" | ||
| + | .pattern(" | ||
| + | .define(' | ||
| + | .unlockedBy(getHasName(Items.COPPER_INGOT), | ||
| + | .save(withConditions(output, | ||
| + | }; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | The important part of this is when we call the save function. We can wrap our RecipeOutput with the withConditions method provided by FabricRecipeProvider, | ||
| + | The result will be this: | ||
| + | <code > | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | } | ||
| + | ], | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }, | ||
| + | " | ||
| + | "c c", | ||
| + | " | ||
| + | ], | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ===== Making A Custom Condition ===== | ||
| + | There are two things you need to do to make a custom condition: 1. Create the class and logic for it. 2. Register it. | ||
| + | |||
| + | We are going to make a FalseResourceCondition. | ||
| + | To begin, we must have our class implement ResourceCondition. | ||
| + | We can make a codec through MapCodec.unit, | ||
| + | The test method is where all the logic happens. It passes in a RegistryInfoLookup, | ||
| + | For our use, we do not need it. We can just return false. | ||
| + | Your class should currently look something like this: | ||
| + | <code java [enable_line_numbers=" | ||
| + | public class FalseResourceCondition implements ResourceCondition { | ||
| + | public static final MapCodec< | ||
| + | |||
| + | public ResourceConditionType<?> | ||
| + | return TYPE_HERE; | ||
| + | } | ||
| + | |||
| + | public boolean test(RegistryOps.@Nullable RegistryInfoLookup registryInfo) { | ||
| + | return false; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Now, we must register the condition. You can refer to DefaultResourceConditionTypes to see how they are done. | ||
| + | A register method will look something like this: | ||
| + | <code java> | ||
| + | protected static <T extends ResourceCondition> | ||
| + | return ResourceConditionType.create(Identifier.fromNamespaceAndPath(" | ||
| + | } | ||
| + | </ | ||
| + | For name, we can pass in " | ||
| + | In your '' | ||
| + | <code java> | ||
| + | public static ResourceConditionType< | ||
| + | |||
| + | @Override | ||
| + | public void onInitialize() { | ||
| + | FALSE = createResourceConditionType(" | ||
| + | // ... | ||
| + | } | ||
| + | protected static <T extends ResourceCondition> | ||
| + | return ResourceConditionType.create(Identifier.fromNamespaceAndPath(" | ||
| + | } | ||
| + | |||
| + | // ... | ||
| + | </ | ||
| + | Now, replace TYPE_HERE with the new condition type. | ||
drafts/resourceconditions.1772543429.txt.gz · Last modified: 2026/03/03 13:10 by infinitychances