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

104 lines
3.8 KiB
Java

package net.minecraft.world.item.crafting;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.stream.Stream;
import net.minecraft.core.HolderLookup;
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 SmithingTransformRecipe implements SmithingRecipe {
final Ingredient template;
final Ingredient base;
final Ingredient addition;
final ItemStack result;
public SmithingTransformRecipe(Ingredient template, Ingredient base, Ingredient addition, ItemStack result) {
this.template = template;
this.base = base;
this.addition = addition;
this.result = result;
}
public boolean matches(SmithingRecipeInput input, Level level) {
return this.template.test(input.template()) && this.base.test(input.base()) && this.addition.test(input.addition());
}
public ItemStack assemble(SmithingRecipeInput input, HolderLookup.Provider registries) {
ItemStack itemStack = input.base().transmuteCopy(this.result.getItem(), this.result.getCount());
itemStack.applyComponents(this.result.getComponentsPatch());
return itemStack;
}
@Override
public ItemStack getResultItem(HolderLookup.Provider registries) {
return this.result;
}
@Override
public boolean isTemplateIngredient(ItemStack stack) {
return this.template.test(stack);
}
@Override
public boolean isBaseIngredient(ItemStack stack) {
return this.base.test(stack);
}
@Override
public boolean isAdditionIngredient(ItemStack stack) {
return this.addition.test(stack);
}
@Override
public RecipeSerializer<?> getSerializer() {
return RecipeSerializer.SMITHING_TRANSFORM;
}
@Override
public boolean isIncomplete() {
return Stream.of(this.template, this.base, this.addition).anyMatch(Ingredient::isEmpty);
}
public static class Serializer implements RecipeSerializer<SmithingTransformRecipe> {
private static final MapCodec<SmithingTransformRecipe> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(
Ingredient.CODEC.fieldOf("template").forGetter(smithingTransformRecipe -> smithingTransformRecipe.template),
Ingredient.CODEC.fieldOf("base").forGetter(smithingTransformRecipe -> smithingTransformRecipe.base),
Ingredient.CODEC.fieldOf("addition").forGetter(smithingTransformRecipe -> smithingTransformRecipe.addition),
ItemStack.STRICT_CODEC.fieldOf("result").forGetter(smithingTransformRecipe -> smithingTransformRecipe.result)
)
.apply(instance, SmithingTransformRecipe::new)
);
public static final StreamCodec<RegistryFriendlyByteBuf, SmithingTransformRecipe> STREAM_CODEC = StreamCodec.of(
SmithingTransformRecipe.Serializer::toNetwork, SmithingTransformRecipe.Serializer::fromNetwork
);
@Override
public MapCodec<SmithingTransformRecipe> codec() {
return CODEC;
}
@Override
public StreamCodec<RegistryFriendlyByteBuf, SmithingTransformRecipe> streamCodec() {
return STREAM_CODEC;
}
private static SmithingTransformRecipe fromNetwork(RegistryFriendlyByteBuf buffer) {
Ingredient ingredient = Ingredient.CONTENTS_STREAM_CODEC.decode(buffer);
Ingredient ingredient2 = Ingredient.CONTENTS_STREAM_CODEC.decode(buffer);
Ingredient ingredient3 = Ingredient.CONTENTS_STREAM_CODEC.decode(buffer);
ItemStack itemStack = ItemStack.STREAM_CODEC.decode(buffer);
return new SmithingTransformRecipe(ingredient, ingredient2, ingredient3, itemStack);
}
private static void toNetwork(RegistryFriendlyByteBuf buffer, SmithingTransformRecipe recipe) {
Ingredient.CONTENTS_STREAM_CODEC.encode(buffer, recipe.template);
Ingredient.CONTENTS_STREAM_CODEC.encode(buffer, recipe.base);
Ingredient.CONTENTS_STREAM_CODEC.encode(buffer, recipe.addition);
ItemStack.STREAM_CODEC.encode(buffer, recipe.result);
}
}
}