| 1 | package net.minecraft.data.recipes;
|
| 2 |
|
| 3 | import java.util.Objects;
|
| 4 | import net.minecraft.advancements.triggers.Criterion;
|
| 5 | import net.minecraft.core.registries.Registries;
|
| 6 | import net.minecraft.resources.Identifier;
|
| 7 | import net.minecraft.resources.ResourceKey;
|
| 8 | import net.minecraft.world.item.ItemInstance;
|
| 9 | import net.minecraft.world.item.crafting.CraftingBookCategory;
|
| 10 | import net.minecraft.world.item.crafting.CraftingRecipe;
|
| 11 | import net.minecraft.world.item.crafting.Recipe;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public interface RecipeBuilder {
|
| 15 | Identifier ROOT_RECIPE_ADVANCEMENT = Identifier.withDefaultNamespace("recipes/root");
|
| 16 |
|
| 17 | RecipeBuilder unlockedBy(String name, Criterion<?> criterion);
|
| 18 |
|
| 19 | RecipeBuilder group(@Nullable String group);
|
| 20 |
|
| 21 | @Nullable ResourceKey<Recipe<?>> defaultId();
|
| 22 |
|
| 23 | void save(RecipeOutput output, ResourceKey<Recipe<?>> location);
|
| 24 |
|
| 25 | default void save(final RecipeOutput output) {
|
| 26 | ResourceKey<Recipe<?>> defaultKey = this.defaultId();
|
| 27 | if (defaultKey == null) {
|
| 28 | throw new IllegalStateException("Recipe has no name to derive an id from - save it with an explicit id");
|
| 29 | }
|
| 30 |
|
| 31 | this.save(output, defaultKey);
|
| 32 | }
|
| 33 |
|
| 34 | default void save(final RecipeOutput output, final String id) {
|
| 35 | ResourceKey<Recipe<?>> defaultKey = this.defaultId();
|
| 36 | ResourceKey<Recipe<?>> overriddenKey = ResourceKey.create(Registries.RECIPE, Identifier.parse(id));
|
| 37 | if (overriddenKey == defaultKey) {
|
| 38 | throw new IllegalStateException("Recipe " + id + " should remove its 'save' argument as it is equal to default one");
|
| 39 | }
|
| 40 |
|
| 41 | this.save(output, overriddenKey);
|
| 42 | }
|
| 43 |
|
| 44 | static CraftingBookCategory determineCraftingBookCategory(final RecipeCategory category) {
|
| 45 | return switch (category) {
|
| 46 | case BUILDING_BLOCKS -> CraftingBookCategory.BUILDING;
|
| 47 | case TOOLS, COMBAT -> CraftingBookCategory.EQUIPMENT;
|
| 48 | case REDSTONE -> CraftingBookCategory.REDSTONE;
|
| 49 | default -> CraftingBookCategory.MISC;
|
| 50 | };
|
| 51 | }
|
| 52 |
|
| 53 | static Recipe.CommonInfo createCraftingCommonInfo(final boolean showNotification) {
|
| 54 | return new Recipe.CommonInfo(showNotification);
|
| 55 | }
|
| 56 |
|
| 57 | static CraftingRecipe.CraftingBookInfo createCraftingBookInfo(final RecipeCategory category, final @Nullable String group) {
|
| 58 | return new CraftingRecipe.CraftingBookInfo(determineCraftingBookCategory(category), Objects.requireNonNullElse(group, ""));
|
| 59 | }
|
| 60 |
|
| 61 | static ResourceKey<Recipe<?>> getDefaultRecipeId(final ItemInstance result) {
|
| 62 | return ResourceKey.create(Registries.RECIPE, result.typeHolder().unwrapKey().orElseThrow().identifier());
|
| 63 | }
|
| 64 | }
|
| 65 |
|