| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Map;
|
| 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.ItemStack;
|
| 10 | import net.minecraft.world.item.MapItem;
|
| 11 | import net.minecraft.world.item.component.MapPostProcessing;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 14 |
|
| 15 | public class MapExtendingRecipe extends CustomRecipe {
|
| 16 | public static final MapCodec<MapExtendingRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> i.group(
|
| 18 | Ingredient.CODEC.fieldOf("map").forGetter(o -> o.map),
|
| 19 | Ingredient.CODEC.fieldOf("material").forGetter(o -> o.material),
|
| 20 | TransmuteResult.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 21 | )
|
| 22 | .apply(i, MapExtendingRecipe::new)
|
| 23 | );
|
| 24 | public static final StreamCodec<RegistryFriendlyByteBuf, MapExtendingRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 25 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 26 | o -> o.map,
|
| 27 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 28 | o -> o.material,
|
| 29 | TransmuteResult.STREAM_CODEC,
|
| 30 | o -> o.result,
|
| 31 | MapExtendingRecipe::new
|
| 32 | );
|
| 33 | public static final RecipeSerializer<MapExtendingRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 34 | private final ShapedRecipePattern pattern;
|
| 35 | private final Ingredient map;
|
| 36 | private final Ingredient material;
|
| 37 | private final TransmuteResult result;
|
| 38 |
|
| 39 | public MapExtendingRecipe(final Ingredient map, final Ingredient material, final TransmuteResult result) {
|
| 40 | this.map = map;
|
| 41 | this.material = material;
|
| 42 | this.result = result;
|
| 43 | this.pattern = ShapedRecipePattern.of(Map.of('#', material, 'x', map), "###", "#x#", "###");
|
| 44 | }
|
| 45 |
|
| 46 | public boolean matches(final CraftingInput input, final Level level) {
|
| 47 | if (!this.pattern.matches(input)) {
|
| 48 | return false;
|
| 49 | }
|
| 50 |
|
| 51 | ItemStack map = findFilledMap(input);
|
| 52 | if (map.isEmpty()) {
|
| 53 | return false;
|
| 54 | }
|
| 55 |
|
| 56 | MapItemSavedData data = MapItem.getSavedData(map, level);
|
| 57 | return data == null ? false : data.scale < 4;
|
| 58 | }
|
| 59 |
|
| 60 | public ItemStack assemble(final CraftingInput input) {
|
| 61 | ItemStack sourceMap = findFilledMap(input);
|
| 62 | ItemStack map = TransmuteRecipe.createWithOriginalComponents(this.result.resolve(sourceMap.typeHolder()), sourceMap);
|
| 63 | map.set(DataComponents.MAP_POST_PROCESSING, MapPostProcessing.SCALE);
|
| 64 | return map;
|
| 65 | }
|
| 66 |
|
| 67 | private static ItemStack findFilledMap(final CraftingInput input) {
|
| 68 | for (int i = 0; i < input.size(); i++) {
|
| 69 | ItemStack itemStack = input.getItem(i);
|
| 70 | if (itemStack.has(DataComponents.MAP_ID)) {
|
| 71 | return itemStack;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return ItemStack.EMPTY;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public RecipeSerializer<MapExtendingRecipe> getSerializer() {
|
| 80 | return SERIALIZER;
|
| 81 | }
|
| 82 | }
|
| 83 |
|