ShapedRecipeBuilder .java

net.minecraft.data.recipes.ShapedRecipeBuilder (class): members: rows, define, shaped, showNotification, defaultId, advancementBuilder, unlockedBy, group.

net.minecraft.data.recipesclass107 linescommon3.9 KB

Implements

API Surface (11)

ShapedRecipeBuilder shaped(final HolderGetter<Item> items, final RecipeCategory category, final ItemLike item)
ShapedRecipeBuilder shaped(final HolderGetter<Item> items, final RecipeCategory category, final ItemLike item, final int count)
ShapedRecipeBuilder define(final Character symbol, final TagKey<Item> tag)
ShapedRecipeBuilder define(final Character symbol, final ItemLike item)
ShapedRecipeBuilder define(final Character symbol, final Ingredient ingredient)
ShapedRecipeBuilder pattern(final String row)
ShapedRecipeBuilder unlockedBy(final String name, final Criterion<?> criterion)
ShapedRecipeBuilder group(final @Nullable String group)
ShapedRecipeBuilder showNotification(final boolean showNotification)
ResourceKey<Recipe<?>> defaultId()
void save(final RecipeOutput output, final ResourceKey<Recipe<?>> id)

Fields & Constants (8)

items
category
result
rows
key
advancementBuilder
group
showNotification

String literals (3)

"Symbol '"
"' is already defined!"
"Pattern must be the same width on every line!"

Full Source

ShapedRecipeBuilder.java107 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.data.recipes;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import java.util.List;
6
import java.util.Map;
7
import net.minecraft.advancements.triggers.Criterion;
8
import net.minecraft.core.HolderGetter;
9
import net.minecraft.resources.ResourceKey;
10
import net.minecraft.tags.TagKey;
11
import net.minecraft.world.item.Item;
12
import net.minecraft.world.item.ItemStackTemplate;
13
import net.minecraft.world.item.crafting.Ingredient;
14
import net.minecraft.world.item.crafting.Recipe;
15
import net.minecraft.world.item.crafting.ShapedRecipe;
16
import net.minecraft.world.item.crafting.ShapedRecipePattern;
17
import net.minecraft.world.level.ItemLike;
18
import org.jspecify.annotations.Nullable;
19
20
public class ShapedRecipeBuilder implements RecipeBuilder {
21
	private final HolderGetter<Item> items;
22
	private final RecipeCategory category;
23
	private final ItemStackTemplate result;
24
	private final List<String> rows = Lists.newArrayList();
25
	private final Map<Character, Ingredient> key = Maps.newLinkedHashMap();
26
	private final RecipeUnlockAdvancementBuilder advancementBuilder = new RecipeUnlockAdvancementBuilder();
27
	private @Nullable String group;
28
	private boolean showNotification = true;
29
30
	private ShapedRecipeBuilder(final HolderGetter<Item> items, final RecipeCategory category, final ItemStackTemplate result) {
31
		this.items = items;
32
		this.category = category;
33
		this.result = result;
34
	}
35
36
	private ShapedRecipeBuilder(final HolderGetter<Item> items, final RecipeCategory category, final ItemLike result, final int count) {
37
		this(items, category, new ItemStackTemplate(result.asItem(), count));
38
	}
39
40
	public static ShapedRecipeBuilder shaped(final HolderGetter<Item> items, final RecipeCategory category, final ItemLike item) {
41
		return shaped(items, category, item, 1);
42
	}
43
44
	public static ShapedRecipeBuilder shaped(final HolderGetter<Item> items, final RecipeCategory category, final ItemLike item, final int count) {
45
		return new ShapedRecipeBuilder(items, category, item, count);
46
	}
47
48
	public ShapedRecipeBuilder define(final Character symbol, final TagKey<Item> tag) {
49
		return this.define(symbol, Ingredient.of(this.items.getOrThrow(tag)));
50
	}
51
52
	public ShapedRecipeBuilder define(final Character symbol, final ItemLike item) {
53
		return this.define(symbol, Ingredient.of(item));
54
	}
55
56
	public ShapedRecipeBuilder define(final Character symbol, final Ingredient ingredient) {
57
		if (this.key.containsKey(symbol)) {
58
			throw new IllegalArgumentException("Symbol '" + symbol + "' is already defined!");
59
		}
60
61
		if (symbol == ' ') {
62
			throw new IllegalArgumentException("Symbol ' ' (whitespace) is reserved and cannot be defined");
63
		}
64
65
		this.key.put(symbol, ingredient);
66
		return this;
67
	}
68
69
	public ShapedRecipeBuilder pattern(final String row) {
70
		if (!this.rows.isEmpty() && row.length() != this.rows.get(0).length()) {
71
			throw new IllegalArgumentException("Pattern must be the same width on every line!");
72
		}
73
74
		this.rows.add(row);
75
		return this;
76
	}
77
78
	public ShapedRecipeBuilder unlockedBy(final String name, final Criterion<?> criterion) {
79
		this.advancementBuilder.unlockedBy(name, criterion);
80
		return this;
81
	}
82
83
	public ShapedRecipeBuilder group(final @Nullable String group) {
84
		this.group = group;
85
		return this;
86
	}
87
88
	public ShapedRecipeBuilder showNotification(final boolean showNotification) {
89
		this.showNotification = showNotification;
90
		return this;
91
	}
92
93
	@Override
94
	public ResourceKey<Recipe<?>> defaultId() {
95
		return RecipeBuilder.getDefaultRecipeId(this.result);
96
	}
97
98
	@Override
99
	public void save(final RecipeOutput output, final ResourceKey<Recipe<?>> id) {
100
		ShapedRecipePattern pattern = ShapedRecipePattern.of(this.key, this.rows);
101
		ShapedRecipe recipe = new ShapedRecipe(
102
			RecipeBuilder.createCraftingCommonInfo(this.showNotification), RecipeBuilder.createCraftingBookInfo(this.category, this.group), pattern, this.result
103
		);
104
		output.accept(id, recipe, this.advancementBuilder.build(output, id, this.category));
105
	}
106
}
107

Decompiled Minecraft 26.3 source of ShapedRecipeBuilder (net/minecraft/data/recipes/ShapedRecipeBuilder.java) with Mojang official mappings, stored verbatim on the site.