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