133 lines
3.9 KiB
Java
133 lines
3.9 KiB
Java
package net.minecraft.data.recipes;
|
|
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import net.minecraft.advancements.Advancement;
|
|
import net.minecraft.advancements.AdvancementRequirements;
|
|
import net.minecraft.advancements.AdvancementRewards;
|
|
import net.minecraft.advancements.Criterion;
|
|
import net.minecraft.advancements.critereon.RecipeUnlockedTrigger;
|
|
import net.minecraft.core.NonNullList;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraft.tags.TagKey;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.item.crafting.Ingredient;
|
|
import net.minecraft.world.item.crafting.ShapelessRecipe;
|
|
import net.minecraft.world.level.ItemLike;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class ShapelessRecipeBuilder implements RecipeBuilder {
|
|
private final RecipeCategory category;
|
|
private final Item result;
|
|
private final int count;
|
|
private final NonNullList<Ingredient> ingredients = NonNullList.create();
|
|
private final Map<String, Criterion<?>> criteria = new LinkedHashMap();
|
|
@Nullable
|
|
private String group;
|
|
|
|
public ShapelessRecipeBuilder(RecipeCategory category, ItemLike result, int count) {
|
|
this.category = category;
|
|
this.result = result.asItem();
|
|
this.count = count;
|
|
}
|
|
|
|
/**
|
|
* Creates a new builder for a shapeless recipe.
|
|
*/
|
|
public static ShapelessRecipeBuilder shapeless(RecipeCategory category, ItemLike result) {
|
|
return new ShapelessRecipeBuilder(category, result, 1);
|
|
}
|
|
|
|
/**
|
|
* Creates a new builder for a shapeless recipe.
|
|
*/
|
|
public static ShapelessRecipeBuilder shapeless(RecipeCategory category, ItemLike result, int count) {
|
|
return new ShapelessRecipeBuilder(category, result, count);
|
|
}
|
|
|
|
/**
|
|
* Adds an ingredient that can be any item in the given tag.
|
|
*/
|
|
public ShapelessRecipeBuilder requires(TagKey<Item> tag) {
|
|
return this.requires(Ingredient.of(tag));
|
|
}
|
|
|
|
/**
|
|
* Adds an ingredient of the given item.
|
|
*/
|
|
public ShapelessRecipeBuilder requires(ItemLike item) {
|
|
return this.requires(item, 1);
|
|
}
|
|
|
|
/**
|
|
* Adds the given ingredient multiple times.
|
|
*/
|
|
public ShapelessRecipeBuilder requires(ItemLike item, int quantity) {
|
|
for (int i = 0; i < quantity; i++) {
|
|
this.requires(Ingredient.of(item));
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Adds an ingredient.
|
|
*/
|
|
public ShapelessRecipeBuilder requires(Ingredient ingredient) {
|
|
return this.requires(ingredient, 1);
|
|
}
|
|
|
|
/**
|
|
* Adds an ingredient multiple times.
|
|
*/
|
|
public ShapelessRecipeBuilder requires(Ingredient ingredient, int quantity) {
|
|
for (int i = 0; i < quantity; i++) {
|
|
this.ingredients.add(ingredient);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public ShapelessRecipeBuilder unlockedBy(String name, Criterion<?> criterion) {
|
|
this.criteria.put(name, criterion);
|
|
return this;
|
|
}
|
|
|
|
public ShapelessRecipeBuilder group(@Nullable String groupName) {
|
|
this.group = groupName;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public Item getResult() {
|
|
return this.result;
|
|
}
|
|
|
|
@Override
|
|
public void save(RecipeOutput recipeOutput, ResourceLocation id) {
|
|
this.ensureValid(id);
|
|
Advancement.Builder builder = recipeOutput.advancement()
|
|
.addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(id))
|
|
.rewards(AdvancementRewards.Builder.recipe(id))
|
|
.requirements(AdvancementRequirements.Strategy.OR);
|
|
this.criteria.forEach(builder::addCriterion);
|
|
ShapelessRecipe shapelessRecipe = new ShapelessRecipe(
|
|
(String)Objects.requireNonNullElse(this.group, ""),
|
|
RecipeBuilder.determineBookCategory(this.category),
|
|
new ItemStack(this.result, this.count),
|
|
this.ingredients
|
|
);
|
|
recipeOutput.accept(id, shapelessRecipe, builder.build(id.withPrefix("recipes/" + this.category.getFolderName() + "/")));
|
|
}
|
|
|
|
/**
|
|
* Makes sure that this recipe is valid and obtainable.
|
|
*/
|
|
private void ensureValid(ResourceLocation id) {
|
|
if (this.criteria.isEmpty()) {
|
|
throw new IllegalStateException("No way of obtaining recipe " + id);
|
|
}
|
|
}
|
|
}
|