112 lines
3.9 KiB
Java
112 lines
3.9 KiB
Java
package net.minecraft.world.item.crafting;
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
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.item.Items;
|
|
import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
|
import net.minecraft.world.item.crafting.display.SlotDisplay;
|
|
import net.minecraft.world.item.crafting.display.SmithingRecipeDisplay;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class SmithingTransformRecipe implements SmithingRecipe {
|
|
final Optional<Ingredient> template;
|
|
final Optional<Ingredient> base;
|
|
final Optional<Ingredient> addition;
|
|
final ItemStack result;
|
|
@Nullable
|
|
private PlacementInfo placementInfo;
|
|
|
|
public SmithingTransformRecipe(Optional<Ingredient> template, Optional<Ingredient> base, Optional<Ingredient> addition, ItemStack result) {
|
|
this.template = template;
|
|
this.base = base;
|
|
this.addition = addition;
|
|
this.result = result;
|
|
}
|
|
|
|
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 Optional<Ingredient> templateIngredient() {
|
|
return this.template;
|
|
}
|
|
|
|
@Override
|
|
public Optional<Ingredient> baseIngredient() {
|
|
return this.base;
|
|
}
|
|
|
|
@Override
|
|
public Optional<Ingredient> additionIngredient() {
|
|
return this.addition;
|
|
}
|
|
|
|
@Override
|
|
public RecipeSerializer<SmithingTransformRecipe> getSerializer() {
|
|
return RecipeSerializer.SMITHING_TRANSFORM;
|
|
}
|
|
|
|
@Override
|
|
public PlacementInfo placementInfo() {
|
|
if (this.placementInfo == null) {
|
|
this.placementInfo = PlacementInfo.createFromOptionals(List.of(this.template, this.base, this.addition));
|
|
}
|
|
|
|
return this.placementInfo;
|
|
}
|
|
|
|
@Override
|
|
public List<RecipeDisplay> display() {
|
|
return List.of(
|
|
new SmithingRecipeDisplay(
|
|
Ingredient.optionalIngredientToDisplay(this.template),
|
|
Ingredient.optionalIngredientToDisplay(this.base),
|
|
Ingredient.optionalIngredientToDisplay(this.addition),
|
|
new SlotDisplay.ItemStackSlotDisplay(this.result),
|
|
new SlotDisplay.ItemSlotDisplay(Items.SMITHING_TABLE)
|
|
)
|
|
);
|
|
}
|
|
|
|
public static class Serializer implements RecipeSerializer<SmithingTransformRecipe> {
|
|
private static final MapCodec<SmithingTransformRecipe> CODEC = RecordCodecBuilder.mapCodec(
|
|
instance -> instance.group(
|
|
Ingredient.CODEC.optionalFieldOf("template").forGetter(smithingTransformRecipe -> smithingTransformRecipe.template),
|
|
Ingredient.CODEC.optionalFieldOf("base").forGetter(smithingTransformRecipe -> smithingTransformRecipe.base),
|
|
Ingredient.CODEC.optionalFieldOf("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.composite(
|
|
Ingredient.OPTIONAL_CONTENTS_STREAM_CODEC,
|
|
smithingTransformRecipe -> smithingTransformRecipe.template,
|
|
Ingredient.OPTIONAL_CONTENTS_STREAM_CODEC,
|
|
smithingTransformRecipe -> smithingTransformRecipe.base,
|
|
Ingredient.OPTIONAL_CONTENTS_STREAM_CODEC,
|
|
smithingTransformRecipe -> smithingTransformRecipe.addition,
|
|
ItemStack.STREAM_CODEC,
|
|
smithingTransformRecipe -> smithingTransformRecipe.result,
|
|
SmithingTransformRecipe::new
|
|
);
|
|
|
|
@Override
|
|
public MapCodec<SmithingTransformRecipe> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
@Override
|
|
public StreamCodec<RegistryFriendlyByteBuf, SmithingTransformRecipe> streamCodec() {
|
|
return STREAM_CODEC;
|
|
}
|
|
}
|
|
}
|