前提
首先,请确保你已完成 Datagen 设置 。
设置
首先,我们需要提供程序。 创建一个 extends FabricRecipeProvider 的类。 我们所有的配方生成都将在提供程序的 generate 方法中进行。
java
import java.util.List;
import java.util.concurrent.CompletableFuture;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.recipes.RecipeCategory;
import net.minecraft.data.recipes.RecipeOutput;
import net.minecraft.data.recipes.RecipeProvider;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricRecipeProvider;
public class ExampleModRecipeProvider extends FabricRecipeProvider {
public ExampleModRecipeProvider(FabricDataOutput output, CompletableFuture<HolderLookup.Provider> registriesFuture) {
super(output, registriesFuture);
}
@Override
protected RecipeProvider createRecipeProvider(HolderLookup.Provider registryLookup, RecipeOutput exporter) {
return new RecipeProvider(registryLookup, exporter) {
@Override
public void buildRecipes() {
HolderLookup.RegistryLookup<Item> itemLookup = registries.lookupOrThrow(Registries.ITEM);
}
};
}
@Override
public String getName() {
return "ExampleModRecipeProvider";
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
要完成设置,将此提供程序添加到 onInitializeDataGenerator 方法中的 DataGeneratorEntrypoint。
java
pack.addProvider(ExampleModRecipeProvider::new);1
无序配方
无序配方相当的简单。 只需将它们添加到提供程序中的 generate 方法中:
java
shapeless(RecipeCategory.BUILDING_BLOCKS, Items.DIRT) // You can also specify an int to produce more than one
.requires(Items.COARSE_DIRT) // You can also specify an int to require more than one, or a tag to accept multiple things
// Create an advancement that gives you the recipe
.unlockedBy(getHasName(Items.COARSE_DIRT), has(Items.COARSE_DIRT))
.save(output);1
2
3
4
5
6
2
3
4
5
6
有序配方
对于有序配方,可以使用 String 定义有序,然后定义 String 中每个 char 代表什么。
java
shaped(RecipeCategory.MISC, Items.CRAFTING_TABLE, 4)
.pattern("ll")
.pattern("ll")
.define('l', ItemTags.LOGS) // 'l' means "any log"
.group("multi_bench") // Put it in a group called "multi_bench" - groups are shown in one slot in the recipe book
.unlockedBy(getHasName(Items.CRAFTING_TABLE), has(Items.CRAFTING_TABLE))
.save(output);
shaped(RecipeCategory.MISC, Items.LOOM, 4)
.pattern("ww")
.pattern("ll")
.define('w', ItemTags.WOOL) // 'w' means "any wool"
.define('l', ItemTags.LOGS)
.group("multi_bench")
.unlockedBy(getHasName(Items.LOOM), has(Items.LOOM))
.save(output);
doorBuilder(Items.OAK_DOOR, Ingredient.of(Items.OAK_BUTTON)) // Using a helper method!
.unlockedBy(getHasName(Items.OAK_BUTTON), has(Items.OAK_BUTTON))
.save(output);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
TIP
有很多辅助方法可用于创建普通配方。 查看 RecipeProvider 提供的内容! 在 IntelliJ 中按 Alt + 7 打开类的结构,其中包括方法列表。
其他配方
其他配方的工作原理类似,但需要一些额外的参数。 比如烧炼配方需要了解奖励多少经验。
java
oreSmelting(
List.of(Items.BREAD, Items.COOKIE, Items.HAY_BLOCK), // Inputs
RecipeCategory.FOOD, // Category
Items.WHEAT, // Output
0.1f, // Experience
300, // Cooking time
"food_to_wheat" // group
);1
2
3
4
5
6
7
8
2
3
4
5
6
7
8



