127 lines
4.1 KiB
Java
127 lines
4.1 KiB
Java
package net.minecraft.data.recipes;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import net.minecraft.advancements.Criterion;
|
|
import net.minecraft.advancements.Advancement.Builder;
|
|
import net.minecraft.advancements.AdvancementRequirements.Strategy;
|
|
import net.minecraft.advancements.critereon.RecipeUnlockedTrigger;
|
|
import net.minecraft.core.HolderGetter;
|
|
import net.minecraft.resources.ResourceKey;
|
|
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.Recipe;
|
|
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 HolderGetter<Item> items;
|
|
private final RecipeCategory category;
|
|
private final ItemStack result;
|
|
private final List<Ingredient> ingredients = new ArrayList();
|
|
private final Map<String, Criterion<?>> criteria = new LinkedHashMap();
|
|
@Nullable
|
|
private String group;
|
|
|
|
private ShapelessRecipeBuilder(HolderGetter<Item> items, RecipeCategory category, ItemStack result) {
|
|
this.items = items;
|
|
this.category = category;
|
|
this.result = result;
|
|
}
|
|
|
|
public static ShapelessRecipeBuilder shapeless(HolderGetter<Item> items, RecipeCategory category, ItemStack result) {
|
|
return new ShapelessRecipeBuilder(items, category, result);
|
|
}
|
|
|
|
public static ShapelessRecipeBuilder shapeless(HolderGetter<Item> items, RecipeCategory category, ItemLike result) {
|
|
return shapeless(items, category, result, 1);
|
|
}
|
|
|
|
public static ShapelessRecipeBuilder shapeless(HolderGetter<Item> items, RecipeCategory category, ItemLike result, int count) {
|
|
return new ShapelessRecipeBuilder(items, category, result.asItem().getDefaultInstance().copyWithCount(count));
|
|
}
|
|
|
|
/**
|
|
* Adds an ingredient that can be any item in the given tag.
|
|
*/
|
|
public ShapelessRecipeBuilder requires(TagKey<Item> tag) {
|
|
return this.requires(Ingredient.of(this.items.getOrThrow(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 string, Criterion<?> criterion) {
|
|
this.criteria.put(string, criterion);
|
|
return this;
|
|
}
|
|
|
|
public ShapelessRecipeBuilder group(@Nullable String string) {
|
|
this.group = string;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public Item getResult() {
|
|
return this.result.getItem();
|
|
}
|
|
|
|
@Override
|
|
public void save(RecipeOutput output, ResourceKey<Recipe<?>> resourceKey) {
|
|
this.ensureValid(resourceKey);
|
|
Builder builder = output.advancement()
|
|
.addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(resourceKey))
|
|
.rewards(net.minecraft.advancements.AdvancementRewards.Builder.recipe(resourceKey))
|
|
.requirements(Strategy.OR);
|
|
this.criteria.forEach(builder::addCriterion);
|
|
ShapelessRecipe shapelessRecipe = new ShapelessRecipe(
|
|
(String)Objects.requireNonNullElse(this.group, ""), RecipeBuilder.determineBookCategory(this.category), this.result, this.ingredients
|
|
);
|
|
output.accept(resourceKey, shapelessRecipe, builder.build(resourceKey.location().withPrefix("recipes/" + this.category.getFolderName() + "/")));
|
|
}
|
|
|
|
private void ensureValid(ResourceKey<Recipe<?>> recipe) {
|
|
if (this.criteria.isEmpty()) {
|
|
throw new IllegalStateException("No way of obtaining recipe " + recipe.location());
|
|
}
|
|
}
|
|
}
|