| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.core.component.DataComponents;
|
| 6 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 7 | import net.minecraft.network.codec.StreamCodec;
|
| 8 | import net.minecraft.world.item.BannerItem;
|
| 9 | import net.minecraft.world.item.DyeColor;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.item.ItemStackTemplate;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.block.entity.BannerPatternLayers;
|
| 14 |
|
| 15 | public class ShieldDecorationRecipe extends CustomRecipe {
|
| 16 | public static final MapCodec<ShieldDecorationRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> i.group(
|
| 18 | Ingredient.CODEC.fieldOf("banner").forGetter(o -> o.banner),
|
| 19 | Ingredient.CODEC.fieldOf("target").forGetter(o -> o.target),
|
| 20 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 21 | )
|
| 22 | .apply(i, ShieldDecorationRecipe::new)
|
| 23 | );
|
| 24 | public static final StreamCodec<RegistryFriendlyByteBuf, ShieldDecorationRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 25 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 26 | o -> o.banner,
|
| 27 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 28 | o -> o.target,
|
| 29 | ItemStackTemplate.STREAM_CODEC,
|
| 30 | o -> o.result,
|
| 31 | ShieldDecorationRecipe::new
|
| 32 | );
|
| 33 | public static final RecipeSerializer<ShieldDecorationRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 34 | private final Ingredient banner;
|
| 35 | private final Ingredient target;
|
| 36 | private final ItemStackTemplate result;
|
| 37 |
|
| 38 | public ShieldDecorationRecipe(final Ingredient banner, final Ingredient target, final ItemStackTemplate result) {
|
| 39 | this.banner = banner;
|
| 40 | this.target = target;
|
| 41 | this.result = result;
|
| 42 | }
|
| 43 |
|
| 44 | public boolean matches(final CraftingInput input, final Level level) {
|
| 45 | if (input.ingredientCount() != 2) {
|
| 46 | return false;
|
| 47 | }
|
| 48 |
|
| 49 | boolean hasClearTarget = false;
|
| 50 | boolean hasPatternBanner = false;
|
| 51 |
|
| 52 | for (int slot = 0; slot < input.size(); slot++) {
|
| 53 | ItemStack itemStack = input.getItem(slot);
|
| 54 | if (!itemStack.isEmpty()) {
|
| 55 | if (this.banner.test(itemStack) && itemStack.getItem() instanceof BannerItem) {
|
| 56 | if (hasPatternBanner) {
|
| 57 | return false;
|
| 58 | }
|
| 59 |
|
| 60 | hasPatternBanner = true;
|
| 61 | } else {
|
| 62 | if (!this.target.test(itemStack)) {
|
| 63 | return false;
|
| 64 | }
|
| 65 |
|
| 66 | if (hasClearTarget) {
|
| 67 | return false;
|
| 68 | }
|
| 69 |
|
| 70 | BannerPatternLayers patterns = itemStack.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY);
|
| 71 | if (!patterns.layers().isEmpty()) {
|
| 72 | return false;
|
| 73 | }
|
| 74 |
|
| 75 | hasClearTarget = true;
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return hasClearTarget && hasPatternBanner;
|
| 81 | }
|
| 82 |
|
| 83 | public ItemStack assemble(final CraftingInput input) {
|
| 84 | BannerPatternLayers patterns = null;
|
| 85 | DyeColor baseColor = DyeColor.WHITE;
|
| 86 | ItemStack target = ItemStack.EMPTY;
|
| 87 |
|
| 88 | for (int slot = 0; slot < input.size(); slot++) {
|
| 89 | ItemStack itemStack = input.getItem(slot);
|
| 90 | if (!itemStack.isEmpty()) {
|
| 91 | if (this.banner.test(itemStack) && itemStack.getItem() instanceof BannerItem bannerItem) {
|
| 92 | patterns = itemStack.get(DataComponents.BANNER_PATTERNS);
|
| 93 | baseColor = bannerItem.getColor();
|
| 94 | } else if (this.target.test(itemStack)) {
|
| 95 | target = itemStack;
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | ItemStack result = TransmuteRecipe.createWithOriginalComponents(this.result, target);
|
| 101 | result.set(DataComponents.BANNER_PATTERNS, patterns);
|
| 102 | result.set(DataComponents.BASE_COLOR, baseColor);
|
| 103 | return result;
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public RecipeSerializer<ShieldDecorationRecipe> getSerializer() {
|
| 108 | return SERIALIZER;
|
| 109 | }
|
| 110 | }
|
| 111 |
|