User Tools

Site Tools


drafts:resourceconditions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
drafts:resourceconditions [2026/03/04 02:52] – changed example infinitychancesdrafts:resourceconditions [2026/03/20 17:27] (current) – changed example again to not require build script changes infinitychances
Line 215: Line 215:
 This section will assume you have already set up a basic datagen setup. It will use recipes as an example. This section will assume you have already set up a basic datagen setup. It will use recipes as an example.
  
-We will create a custom shaped recipe that turns fried egg from farmer's delight refabricated into an egg with a water bucket and only when the mod is loaded. +We will create a recipe that creates minecart with copper if the minecart improvements experiment is enabled.
- +
-First, we must use modLocalRuntime and modCompileOnly on any version of Farmer's Delight Refabricated,which can be taken from Modrinth's maven.+
  
 Next, we can go into our recipe provider and begin. Next, we can go into our recipe provider and begin.
Line 229: Line 227:
  @Override  @Override
  public void buildRecipes() {  public void buildRecipes() {
- shaped(RecipeCategory.FOOD, Items.EGG+ shaped(RecipeCategory.MISC, Items.MINECART
- .pattern("b") + .pattern("c c") 
- .pattern("w") + .pattern("ccc") 
- .define('b', ModItems.FRIED_EGG ) + .define('c', Items.COPPER_INGOT
- .define('w', Items.WATER_BUCKET+ .unlockedBy(getHasName(Items.COPPER_INGOT), has(Items.COPPER_INGOT)) 
- .unlockedBy(getHasName(ModItems.FRIED_EGG ), has(ModItems.FRIED_EGG )) + .save(withConditions(output, new FeaturesEnabledResourceCondition(Identifier.withDefaultNamespace("minecart_improvements");
- .save(withConditions(output, new AllModsLoadedResourceCondition(List.of("farmersdelight"))));+
  };  };
  }  }
Line 242: Line 239:
 The important part of this is when we call the save function. We can wrap our RecipeOutput with the withConditions method provided by FabricRecipeProvider, which allows us to place as many conditions as we want. The important part of this is when we call the save function. We can wrap our RecipeOutput with the withConditions method provided by FabricRecipeProvider, which allows us to place as many conditions as we want.
 The result will be this: The result will be this:
-<code>+<code >
 { {
   "fabric:load_conditions": [   "fabric:load_conditions": [
     {     {
-      "condition": "fabric:all_mods_loaded", +      "condition": "fabric:features_enabled", 
-      "values": [ +      "features": [ 
-        "farmersdelight+        "minecraft:minecart_improvements
-      ]+      ] 
     }     }
   ],   ],
   "type": "minecraft:crafting_shaped",   "type": "minecraft:crafting_shaped",
-  "category": "food",+  "category": "misc",
   "key": {   "key": {
-    "b": "farmersdelight:fried_egg", +    "c": "minecraft:copper_ingot",
-    "w": "minecraft:water_bucket"+
   },   },
   "pattern": [   "pattern": [
-    "b", +    "c c", 
-    "w"+    "ccc"
   ],   ],
   "result": {   "result": {
     "count": 1,     "count": 1,
-    "id": "minecraft:egg"+    "id": "minecraft:minecart"
   }   }
 } }
 </code> </code>
 +===== 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, and we will return to the getType method.
 +The test method is where all the logic happens. It passes in a RegistryInfoLookup, which can get you a RegistryInfo from a ResourceKey.
 +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="true"]>
 +public class FalseResourceCondition implements ResourceCondition {
 +    public static final MapCodec<FalseResourceCondition> CODEC = MapCodec.unit(FalseResourceCondition::new);
 +
 +    public ResourceConditionType<?> getType() {
 +        return TYPE_HERE;
 +    }
 +
 +    public boolean test(RegistryOps.@Nullable RegistryInfoLookup registryInfo) {
 +        return false;
 +    }
 +}
 +</code>
 +
 +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> ResourceConditionType<T> createResourceConditionType(String name, MapCodec<T> codec) {
 + return ResourceConditionType.create(Identifier.fromNamespaceAndPath("tutorial", name), codec);
 +}
 +</code>
 +For name, we can pass in "false", and codec is our ''FalseResourceCondition.CODEC''.
 +In your ''ModInitializer'', do something like this: 
 +<code java>
 +public static ResourceConditionType<FalseResourceCondition> FALSE;
 + 
 +@Override
 +public void onInitialize() {
 + FALSE = createResourceConditionType("false", FalseResourceCondition.CODEC)
 + // ...
 +}
 +
 +protected static <T extends ResourceCondition> ResourceConditionType<T> createResourceConditionType(String name, MapCodec<T> codec) {
 + return ResourceConditionType.create(Identifier.fromNamespaceAndPath("tutorial", name), codec);
 +}
 + 
 +// ...
 +</code>
 +Now, replace TYPE_HERE with the new condition type.
drafts/resourceconditions.1772592757.txt.gz · Last modified: 2026/03/04 02:52 by infinitychances