137 lines
4.7 KiB
Java
137 lines
4.7 KiB
Java
package net.minecraft.data.recipes;
|
|
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Maps;
|
|
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.ShapedRecipe;
|
|
import net.minecraft.world.item.crafting.ShapedRecipePattern;
|
|
import net.minecraft.world.level.ItemLike;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class ShapedRecipeBuilder implements RecipeBuilder {
|
|
private final HolderGetter<Item> items;
|
|
private final RecipeCategory category;
|
|
private final Item result;
|
|
private final int count;
|
|
private final List<String> rows = Lists.<String>newArrayList();
|
|
private final Map<Character, Ingredient> key = Maps.<Character, Ingredient>newLinkedHashMap();
|
|
private final Map<String, Criterion<?>> criteria = new LinkedHashMap();
|
|
@Nullable
|
|
private String group;
|
|
private boolean showNotification = true;
|
|
|
|
private ShapedRecipeBuilder(HolderGetter<Item> items, RecipeCategory category, ItemLike result, int count) {
|
|
this.items = items;
|
|
this.category = category;
|
|
this.result = result.asItem();
|
|
this.count = count;
|
|
}
|
|
|
|
public static ShapedRecipeBuilder shaped(HolderGetter<Item> items, RecipeCategory category, ItemLike result) {
|
|
return shaped(items, category, result, 1);
|
|
}
|
|
|
|
public static ShapedRecipeBuilder shaped(HolderGetter<Item> items, RecipeCategory category, ItemLike result, int count) {
|
|
return new ShapedRecipeBuilder(items, category, result, count);
|
|
}
|
|
|
|
/**
|
|
* Adds a key to the recipe pattern.
|
|
*/
|
|
public ShapedRecipeBuilder define(Character symbol, TagKey<Item> tag) {
|
|
return this.define(symbol, Ingredient.of(this.items.getOrThrow(tag)));
|
|
}
|
|
|
|
/**
|
|
* Adds a key to the recipe pattern.
|
|
*/
|
|
public ShapedRecipeBuilder define(Character symbol, ItemLike item) {
|
|
return this.define(symbol, Ingredient.of(item));
|
|
}
|
|
|
|
/**
|
|
* Adds a key to the recipe pattern.
|
|
*/
|
|
public ShapedRecipeBuilder define(Character symbol, Ingredient ingredient) {
|
|
if (this.key.containsKey(symbol)) {
|
|
throw new IllegalArgumentException("Symbol '" + symbol + "' is already defined!");
|
|
} else if (symbol == ' ') {
|
|
throw new IllegalArgumentException("Symbol ' ' (whitespace) is reserved and cannot be defined");
|
|
} else {
|
|
this.key.put(symbol, ingredient);
|
|
return this;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds a new entry to the patterns for this recipe.
|
|
*/
|
|
public ShapedRecipeBuilder pattern(String pattern) {
|
|
if (!this.rows.isEmpty() && pattern.length() != ((String)this.rows.get(0)).length()) {
|
|
throw new IllegalArgumentException("Pattern must be the same width on every line!");
|
|
} else {
|
|
this.rows.add(pattern);
|
|
return this;
|
|
}
|
|
}
|
|
|
|
public ShapedRecipeBuilder unlockedBy(String string, Criterion<?> criterion) {
|
|
this.criteria.put(string, criterion);
|
|
return this;
|
|
}
|
|
|
|
public ShapedRecipeBuilder group(@Nullable String string) {
|
|
this.group = string;
|
|
return this;
|
|
}
|
|
|
|
public ShapedRecipeBuilder showNotification(boolean showNotification) {
|
|
this.showNotification = showNotification;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public Item getResult() {
|
|
return this.result;
|
|
}
|
|
|
|
@Override
|
|
public void save(RecipeOutput output, ResourceKey<Recipe<?>> resourceKey) {
|
|
ShapedRecipePattern shapedRecipePattern = 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);
|
|
ShapedRecipe shapedRecipe = new ShapedRecipe(
|
|
(String)Objects.requireNonNullElse(this.group, ""),
|
|
RecipeBuilder.determineBookCategory(this.category),
|
|
shapedRecipePattern,
|
|
new ItemStack(this.result, this.count),
|
|
this.showNotification
|
|
);
|
|
output.accept(resourceKey, shapedRecipe, builder.build(resourceKey.location().withPrefix("recipes/" + this.category.getFolderName() + "/")));
|
|
}
|
|
|
|
private ShapedRecipePattern ensureValid(ResourceKey<Recipe<?>> recipe) {
|
|
if (this.criteria.isEmpty()) {
|
|
throw new IllegalStateException("No way of obtaining recipe " + recipe.location());
|
|
} else {
|
|
return ShapedRecipePattern.of(this.key, this.rows);
|
|
}
|
|
}
|
|
}
|