minecraft-src/net/minecraft/world/item/crafting/ShapedRecipe.java
2025-07-04 01:41:11 +03:00

131 lines
4.3 KiB
Java

package net.minecraft.world.item.crafting;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
public class ShapedRecipe implements CraftingRecipe {
final ShapedRecipePattern pattern;
final ItemStack result;
final String group;
final CraftingBookCategory category;
final boolean showNotification;
public ShapedRecipe(String group, CraftingBookCategory category, ShapedRecipePattern pattern, ItemStack result, boolean showNotification) {
this.group = group;
this.category = category;
this.pattern = pattern;
this.result = result;
this.showNotification = showNotification;
}
public ShapedRecipe(String group, CraftingBookCategory category, ShapedRecipePattern pattern, ItemStack result) {
this(group, category, pattern, result, true);
}
@Override
public RecipeSerializer<?> getSerializer() {
return RecipeSerializer.SHAPED_RECIPE;
}
@Override
public String getGroup() {
return this.group;
}
@Override
public CraftingBookCategory category() {
return this.category;
}
@Override
public ItemStack getResultItem(HolderLookup.Provider registries) {
return this.result;
}
@Override
public NonNullList<Ingredient> getIngredients() {
return this.pattern.ingredients();
}
@Override
public boolean showNotification() {
return this.showNotification;
}
@Override
public boolean canCraftInDimensions(int width, int height) {
return width >= this.pattern.width() && height >= this.pattern.height();
}
public boolean matches(CraftingInput input, Level level) {
return this.pattern.matches(input);
}
public ItemStack assemble(CraftingInput input, HolderLookup.Provider registries) {
return this.getResultItem(registries).copy();
}
public int getWidth() {
return this.pattern.width();
}
public int getHeight() {
return this.pattern.height();
}
@Override
public boolean isIncomplete() {
NonNullList<Ingredient> nonNullList = this.getIngredients();
return nonNullList.isEmpty() || nonNullList.stream().filter(ingredient -> !ingredient.isEmpty()).anyMatch(ingredient -> ingredient.getItems().length == 0);
}
public static class Serializer implements RecipeSerializer<ShapedRecipe> {
public static final MapCodec<ShapedRecipe> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(
Codec.STRING.optionalFieldOf("group", "").forGetter(shapedRecipe -> shapedRecipe.group),
CraftingBookCategory.CODEC.fieldOf("category").orElse(CraftingBookCategory.MISC).forGetter(shapedRecipe -> shapedRecipe.category),
ShapedRecipePattern.MAP_CODEC.forGetter(shapedRecipe -> shapedRecipe.pattern),
ItemStack.STRICT_CODEC.fieldOf("result").forGetter(shapedRecipe -> shapedRecipe.result),
Codec.BOOL.optionalFieldOf("show_notification", true).forGetter(shapedRecipe -> shapedRecipe.showNotification)
)
.apply(instance, ShapedRecipe::new)
);
public static final StreamCodec<RegistryFriendlyByteBuf, ShapedRecipe> STREAM_CODEC = StreamCodec.of(
ShapedRecipe.Serializer::toNetwork, ShapedRecipe.Serializer::fromNetwork
);
@Override
public MapCodec<ShapedRecipe> codec() {
return CODEC;
}
@Override
public StreamCodec<RegistryFriendlyByteBuf, ShapedRecipe> streamCodec() {
return STREAM_CODEC;
}
private static ShapedRecipe fromNetwork(RegistryFriendlyByteBuf buffer) {
String string = buffer.readUtf();
CraftingBookCategory craftingBookCategory = buffer.readEnum(CraftingBookCategory.class);
ShapedRecipePattern shapedRecipePattern = ShapedRecipePattern.STREAM_CODEC.decode(buffer);
ItemStack itemStack = ItemStack.STREAM_CODEC.decode(buffer);
boolean bl = buffer.readBoolean();
return new ShapedRecipe(string, craftingBookCategory, shapedRecipePattern, itemStack, bl);
}
private static void toNetwork(RegistryFriendlyByteBuf buffer, ShapedRecipe recipe) {
buffer.writeUtf(recipe.group);
buffer.writeEnum(recipe.category);
ShapedRecipePattern.STREAM_CODEC.encode(buffer, recipe.pattern);
ItemStack.STREAM_CODEC.encode(buffer, recipe.result);
buffer.writeBoolean(recipe.showNotification);
}
}
}