| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.Collections;
|
| 8 | import java.util.List;
|
| 9 | import net.minecraft.advancements.predicates.MinMaxBounds;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 12 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 13 | import net.minecraft.network.codec.StreamCodec;
|
| 14 | import net.minecraft.world.item.Item;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.ItemStackTemplate;
|
| 17 | import net.minecraft.world.item.Items;
|
| 18 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 19 | import net.minecraft.world.item.crafting.display.ShapelessCraftingRecipeDisplay;
|
| 20 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 21 | import net.minecraft.world.level.Level;
|
| 22 |
|
| 23 | public class TransmuteRecipe extends NormalCraftingRecipe {
|
| 24 | private static final int MIN_MATERIAL_COUNT = 1;
|
| 25 | private static final int MAX_MATERIAL_COUNT = 8;
|
| 26 | public static final MinMaxBounds.Ints DEFAULT_MATERIAL_COUNT = MinMaxBounds.Ints.exactly(1);
|
| 27 | public static final MinMaxBounds.Ints FULL_RANGE_MATERIAL_COUNT = MinMaxBounds.Ints.between(1, 8);
|
| 28 | public static final Codec<MinMaxBounds.Ints> MATERIAL_COUNT_BOUNDS = MinMaxBounds.Ints.CODEC
|
| 29 | .validate(MinMaxBounds.validateContainedInRange(FULL_RANGE_MATERIAL_COUNT));
|
| 30 | public static final MapCodec<TransmuteRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 31 | i -> i.group(
|
| 32 | Recipe.CommonInfo.MAP_CODEC.forGetter(o -> o.commonInfo),
|
| 33 | CraftingRecipe.CraftingBookInfo.MAP_CODEC.forGetter(o -> o.bookInfo),
|
| 34 | Ingredient.CODEC.fieldOf("input").forGetter(o -> o.input),
|
| 35 | Ingredient.CODEC.fieldOf("material").forGetter(o -> o.material),
|
| 36 | MATERIAL_COUNT_BOUNDS.optionalFieldOf("material_count", DEFAULT_MATERIAL_COUNT).forGetter(o -> o.materialCount),
|
| 37 | TransmuteResult.CODEC.fieldOf("result").forGetter(o -> o.result),
|
| 38 | Codec.BOOL.optionalFieldOf("add_material_count_to_result", false).forGetter(o -> o.addMaterialCountToResult)
|
| 39 | )
|
| 40 | .apply(i, TransmuteRecipe::new)
|
| 41 | );
|
| 42 | public static final StreamCodec<RegistryFriendlyByteBuf, TransmuteRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 43 | Recipe.CommonInfo.STREAM_CODEC,
|
| 44 | o -> o.commonInfo,
|
| 45 | CraftingRecipe.CraftingBookInfo.STREAM_CODEC,
|
| 46 | o -> o.bookInfo,
|
| 47 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 48 | o -> o.input,
|
| 49 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 50 | o -> o.material,
|
| 51 | MinMaxBounds.Ints.STREAM_CODEC,
|
| 52 | o -> o.materialCount,
|
| 53 | TransmuteResult.STREAM_CODEC,
|
| 54 | o -> o.result,
|
| 55 | ByteBufCodecs.BOOL,
|
| 56 | o -> o.addMaterialCountToResult,
|
| 57 | TransmuteRecipe::new
|
| 58 | );
|
| 59 | public static final RecipeSerializer<TransmuteRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 60 | private final Ingredient input;
|
| 61 | private final Ingredient material;
|
| 62 | private final MinMaxBounds.Ints materialCount;
|
| 63 | private final TransmuteResult result;
|
| 64 | private final boolean addMaterialCountToResult;
|
| 65 |
|
| 66 | public TransmuteRecipe(
|
| 67 | final Recipe.CommonInfo commonInfo,
|
| 68 | final CraftingRecipe.CraftingBookInfo bookInfo,
|
| 69 | final Ingredient input,
|
| 70 | final Ingredient material,
|
| 71 | final MinMaxBounds.Ints materialCount,
|
| 72 | final TransmuteResult result,
|
| 73 | final boolean addMaterialCountToResult
|
| 74 | ) {
|
| 75 | super(commonInfo, bookInfo);
|
| 76 | this.input = input;
|
| 77 | this.material = material;
|
| 78 | this.materialCount = materialCount;
|
| 79 | this.result = result;
|
| 80 | this.addMaterialCountToResult = addMaterialCountToResult;
|
| 81 | }
|
| 82 |
|
| 83 | public static ItemStack createWithOriginalComponents(final ItemStackTemplate target, final ItemStack input) {
|
| 84 | return createWithOriginalComponents(target, input, 0);
|
| 85 | }
|
| 86 |
|
| 87 | public static ItemStack createWithOriginalComponents(final ItemStackTemplate target, final ItemStack input, final int extraCount) {
|
| 88 | return target.apply(target.count() + extraCount, input.getComponentsPatch());
|
| 89 | }
|
| 90 |
|
| 91 | private int computeResultSize(final int materialCount) {
|
| 92 | return this.addMaterialCountToResult ? materialCount + this.result.count() : this.result.count();
|
| 93 | }
|
| 94 |
|
| 95 | private ItemStack computeResult(final ItemStack inputIngredient, final int materialCount) {
|
| 96 | return createWithOriginalComponents(this.result.resolve(inputIngredient.typeHolder()), inputIngredient, materialCount);
|
| 97 | }
|
| 98 |
|
| 99 | public boolean matches(final CraftingInput input, final Level level) {
|
| 100 | int minMaterialCount = this.minMaterialCount();
|
| 101 | int maxMaterialCount = this.maxMaterialCount();
|
| 102 | if (input.ingredientCount() >= minMaterialCount + 1 && input.ingredientCount() <= maxMaterialCount + 1) {
|
| 103 | ItemStack foundInput = null;
|
| 104 | int materialCount = 0;
|
| 105 |
|
| 106 | for (int slot = 0; slot < input.size(); slot++) {
|
| 107 | ItemStack stack = input.getItem(slot);
|
| 108 | if (!stack.isEmpty()) {
|
| 109 | if (this.input.test(stack)) {
|
| 110 | if (foundInput != null) {
|
| 111 | return false;
|
| 112 | }
|
| 113 |
|
| 114 | foundInput = stack;
|
| 115 | } else {
|
| 116 | if (!this.material.test(stack)) {
|
| 117 | return false;
|
| 118 | }
|
| 119 |
|
| 120 | if (++materialCount > maxMaterialCount) {
|
| 121 | return false;
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | if (foundInput != null && !foundInput.isEmpty() && this.materialCount.matches(materialCount)) {
|
| 128 | int resultCount = this.computeResultSize(materialCount);
|
| 129 | if (resultCount != 1) {
|
| 130 | return true;
|
| 131 | }
|
| 132 |
|
| 133 | ItemStack result = this.computeResult(foundInput, 0);
|
| 134 | return result.isEmpty() ? false : !ItemStack.isSameItemSameComponents(foundInput, result);
|
| 135 | } else {
|
| 136 | return false;
|
| 137 | }
|
| 138 | } else {
|
| 139 | return false;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | public ItemStack assemble(final CraftingInput input) {
|
| 144 | if (this.addMaterialCountToResult) {
|
| 145 | int materialCount = 0;
|
| 146 | ItemStack inputIngredient = ItemStack.EMPTY;
|
| 147 |
|
| 148 | for (int slot = 0; slot < input.size(); slot++) {
|
| 149 | ItemStack itemStack = input.getItem(slot);
|
| 150 | if (!itemStack.isEmpty()) {
|
| 151 | if (this.input.test(itemStack)) {
|
| 152 | inputIngredient = itemStack;
|
| 153 | } else if (this.material.test(itemStack)) {
|
| 154 | materialCount++;
|
| 155 | }
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | return this.computeResult(inputIngredient, materialCount);
|
| 160 | } else {
|
| 161 | for (int slot = 0; slot < input.size(); slot++) {
|
| 162 | ItemStack itemStack = input.getItem(slot);
|
| 163 | if (!itemStack.isEmpty() && this.input.test(itemStack)) {
|
| 164 | return this.computeResult(itemStack, 0);
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | return ItemStack.EMPTY;
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public List<RecipeDisplay> display() {
|
| 174 | List<RecipeDisplay> displays = new ArrayList<>();
|
| 175 | List<SlotDisplay> ingredientSlots = new ArrayList<>();
|
| 176 | ingredientSlots.add(this.input.display());
|
| 177 | SlotDisplay materialDisplay = this.material.display();
|
| 178 | int maxMaterialCount = this.maxMaterialCount();
|
| 179 | int minMaterialCount = this.minMaterialCount();
|
| 180 |
|
| 181 | for (int materialCount = minMaterialCount; materialCount <= maxMaterialCount; materialCount++) {
|
| 182 | ingredientSlots.add(materialDisplay);
|
| 183 | displays.add(
|
| 184 | new ShapelessCraftingRecipeDisplay(
|
| 185 | List.copyOf(ingredientSlots), this.resultDisplay(this.computeResultSize(materialCount)), new SlotDisplay.ItemSlotDisplay(Items.CRAFTING_TABLE)
|
| 186 | )
|
| 187 | );
|
| 188 | }
|
| 189 |
|
| 190 | return displays;
|
| 191 | }
|
| 192 |
|
| 193 | private SlotDisplay resultDisplay(final int resultCount) {
|
| 194 | return this.result
|
| 195 | .item()
|
| 196 | .map(item -> this.stackDisplay((Holder<Item>)item, resultCount))
|
| 197 | .orElseGet(() -> new SlotDisplay.Composite(this.input.items().map(item -> this.stackDisplay((Holder<Item>)item, resultCount)).toList()));
|
| 198 | }
|
| 199 |
|
| 200 | private SlotDisplay stackDisplay(final Holder<Item> item, final int resultCount) {
|
| 201 | return new SlotDisplay.ItemStackSlotDisplay(this.result.resolve(item, resultCount));
|
| 202 | }
|
| 203 |
|
| 204 | private int minMaterialCount() {
|
| 205 | return this.materialCount.min().orElse(1);
|
| 206 | }
|
| 207 |
|
| 208 | private int maxMaterialCount() {
|
| 209 | return this.materialCount.max().orElse(8);
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public RecipeSerializer<TransmuteRecipe> getSerializer() {
|
| 214 | return SERIALIZER;
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | protected PlacementInfo createPlacementInfo() {
|
| 219 | int maxMaterialCount = this.maxMaterialCount();
|
| 220 | List<Ingredient> ingredients = new ArrayList<>(1 + maxMaterialCount);
|
| 221 | ingredients.add(this.input);
|
| 222 | ingredients.addAll(Collections.nCopies(maxMaterialCount, this.material));
|
| 223 | return PlacementInfo.create(ingredients);
|
| 224 | }
|
| 225 | }
|
| 226 |
|