| 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.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import net.minecraft.core.component.DataComponentPatch;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 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.item.component.Fireworks;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 |
|
| 17 | public class FireworkRocketRecipe extends CustomRecipe {
|
| 18 | public static final MapCodec<FireworkRocketRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 19 | i -> i.group(
|
| 20 | Ingredient.CODEC.fieldOf("shell").forGetter(o -> o.shell),
|
| 21 | Ingredient.CODEC.fieldOf("fuel").forGetter(o -> o.fuel),
|
| 22 | Ingredient.CODEC.fieldOf("star").forGetter(o -> o.star),
|
| 23 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 24 | )
|
| 25 | .apply(i, FireworkRocketRecipe::new)
|
| 26 | );
|
| 27 | public static final StreamCodec<RegistryFriendlyByteBuf, FireworkRocketRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 28 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 29 | o -> o.shell,
|
| 30 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 31 | o -> o.fuel,
|
| 32 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 33 | o -> o.star,
|
| 34 | ItemStackTemplate.STREAM_CODEC,
|
| 35 | o -> o.result,
|
| 36 | FireworkRocketRecipe::new
|
| 37 | );
|
| 38 | public static final RecipeSerializer<FireworkRocketRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 39 | private final Ingredient shell;
|
| 40 | private final Ingredient fuel;
|
| 41 | private final Ingredient star;
|
| 42 | private final ItemStackTemplate result;
|
| 43 |
|
| 44 | public FireworkRocketRecipe(final Ingredient shell, final Ingredient fuel, final Ingredient star, final ItemStackTemplate result) {
|
| 45 | this.shell = shell;
|
| 46 | this.fuel = fuel;
|
| 47 | this.star = star;
|
| 48 | this.result = result;
|
| 49 | }
|
| 50 |
|
| 51 | public boolean matches(final CraftingInput input, final Level level) {
|
| 52 | if (input.ingredientCount() < 2) {
|
| 53 | return false;
|
| 54 | }
|
| 55 |
|
| 56 | boolean hasShell = false;
|
| 57 | int fuelCount = 0;
|
| 58 |
|
| 59 | for (int slot = 0; slot < input.size(); slot++) {
|
| 60 | ItemStack itemStack = input.getItem(slot);
|
| 61 | if (!itemStack.isEmpty()) {
|
| 62 | if (this.shell.test(itemStack)) {
|
| 63 | if (hasShell) {
|
| 64 | return false;
|
| 65 | }
|
| 66 |
|
| 67 | hasShell = true;
|
| 68 | } else if (this.fuel.test(itemStack)) {
|
| 69 | if (++fuelCount > 3) {
|
| 70 | return false;
|
| 71 | }
|
| 72 | } else if (!this.star.test(itemStack)) {
|
| 73 | return false;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | return hasShell && fuelCount >= 1;
|
| 79 | }
|
| 80 |
|
| 81 | public ItemStack assemble(final CraftingInput input) {
|
| 82 | List<FireworkExplosion> explosions = new ArrayList<>();
|
| 83 | int fuelCount = 0;
|
| 84 |
|
| 85 | for (int slot = 0; slot < input.size(); slot++) {
|
| 86 | ItemStack itemStack = input.getItem(slot);
|
| 87 | if (!itemStack.isEmpty()) {
|
| 88 | if (this.fuel.test(itemStack)) {
|
| 89 | fuelCount++;
|
| 90 | } else if (this.star.test(itemStack)) {
|
| 91 | FireworkExplosion explosion = itemStack.get(DataComponents.FIREWORK_EXPLOSION);
|
| 92 | if (explosion != null) {
|
| 93 | explosions.add(explosion);
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | DataComponentPatch components = DataComponentPatch.builder().set(DataComponents.FIREWORKS, new Fireworks(fuelCount, explosions)).build();
|
| 100 | return this.result.apply(components);
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public RecipeSerializer<FireworkRocketRecipe> getSerializer() {
|
| 105 | return SERIALIZER;
|
| 106 | }
|
| 107 | }
|
| 108 |
|