| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 6 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 9 | import net.minecraft.network.codec.StreamCodec;
|
| 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.item.component.FireworkExplosion;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 |
|
| 16 | public class FireworkStarFadeRecipe extends CustomRecipe {
|
| 17 | public static final MapCodec<FireworkStarFadeRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> i.group(
|
| 19 | Ingredient.CODEC.fieldOf("target").forGetter(o -> o.target),
|
| 20 | Ingredient.CODEC.fieldOf("dye").forGetter(o -> o.dye),
|
| 21 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 22 | )
|
| 23 | .apply(i, FireworkStarFadeRecipe::new)
|
| 24 | );
|
| 25 | public static final StreamCodec<RegistryFriendlyByteBuf, FireworkStarFadeRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 26 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 27 | o -> o.target,
|
| 28 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 29 | o -> o.dye,
|
| 30 | ItemStackTemplate.STREAM_CODEC,
|
| 31 | o -> o.result,
|
| 32 | FireworkStarFadeRecipe::new
|
| 33 | );
|
| 34 | public static final RecipeSerializer<FireworkStarFadeRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 35 | private final Ingredient target;
|
| 36 | private final Ingredient dye;
|
| 37 | private final ItemStackTemplate result;
|
| 38 |
|
| 39 | public FireworkStarFadeRecipe(final Ingredient target, final Ingredient dye, final ItemStackTemplate result) {
|
| 40 | this.target = target;
|
| 41 | this.dye = dye;
|
| 42 | this.result = result;
|
| 43 | }
|
| 44 |
|
| 45 | public boolean matches(final CraftingInput input, final Level level) {
|
| 46 | if (input.ingredientCount() < 2) {
|
| 47 | return false;
|
| 48 | }
|
| 49 |
|
| 50 | boolean hasDye = false;
|
| 51 | boolean hasTarget = false;
|
| 52 |
|
| 53 | for (int slot = 0; slot < input.size(); slot++) {
|
| 54 | ItemStack itemStack = input.getItem(slot);
|
| 55 | if (!itemStack.isEmpty()) {
|
| 56 | if (this.dye.test(itemStack) && itemStack.has(DataComponents.DYE)) {
|
| 57 | hasDye = true;
|
| 58 | } else {
|
| 59 | if (!this.target.test(itemStack)) {
|
| 60 | return false;
|
| 61 | }
|
| 62 |
|
| 63 | if (hasTarget) {
|
| 64 | return false;
|
| 65 | }
|
| 66 |
|
| 67 | hasTarget = true;
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return hasTarget && hasDye;
|
| 73 | }
|
| 74 |
|
| 75 | public ItemStack assemble(final CraftingInput input) {
|
| 76 | IntList colors = new IntArrayList();
|
| 77 | ItemStack targetStack = null;
|
| 78 |
|
| 79 | for (int slot = 0; slot < input.size(); slot++) {
|
| 80 | ItemStack itemStack = input.getItem(slot);
|
| 81 | if (this.dye.test(itemStack)) {
|
| 82 | DyeColor dye = itemStack.getOrDefault(DataComponents.DYE, DyeColor.WHITE);
|
| 83 | colors.add(dye.getFireworkColor());
|
| 84 | } else if (this.target.test(itemStack)) {
|
| 85 | targetStack = itemStack;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | if (targetStack != null && !colors.isEmpty()) {
|
| 90 | ItemStack result = TransmuteRecipe.createWithOriginalComponents(this.result, targetStack);
|
| 91 | result.update(DataComponents.FIREWORK_EXPLOSION, FireworkExplosion.DEFAULT, colors, FireworkExplosion::withFadeColors);
|
| 92 | return result;
|
| 93 | } else {
|
| 94 | return ItemStack.EMPTY;
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public RecipeSerializer<FireworkStarFadeRecipe> getSerializer() {
|
| 100 | return SERIALIZER;
|
| 101 | }
|
| 102 | }
|
| 103 |
|