| 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 it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 7 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 8 | import java.util.HashMap;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Map.Entry;
|
| 11 | import net.minecraft.core.component.DataComponents;
|
| 12 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 13 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 14 | import net.minecraft.network.codec.StreamCodec;
|
| 15 | import net.minecraft.util.StringRepresentable;
|
| 16 | import net.minecraft.world.item.DyeColor;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 | import net.minecraft.world.item.ItemStackTemplate;
|
| 19 | import net.minecraft.world.item.component.FireworkExplosion;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class FireworkStarRecipe extends CustomRecipe {
|
| 24 | public static final MapCodec<FireworkStarRecipe> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 25 | i -> i.group(
|
| 26 | Codec.simpleMap(FireworkExplosion.Shape.CODEC, Ingredient.CODEC, StringRepresentable.keys(FireworkExplosion.Shape.values()))
|
| 27 | .fieldOf("shapes")
|
| 28 | .forGetter(o -> o.shapes),
|
| 29 | Ingredient.CODEC.fieldOf("trail").forGetter(o -> o.trail),
|
| 30 | Ingredient.CODEC.fieldOf("twinkle").forGetter(o -> o.twinkle),
|
| 31 | Ingredient.CODEC.fieldOf("fuel").forGetter(o -> o.fuel),
|
| 32 | Ingredient.CODEC.fieldOf("dye").forGetter(o -> o.dye),
|
| 33 | ItemStackTemplate.CODEC.fieldOf("result").forGetter(o -> o.result)
|
| 34 | )
|
| 35 | .apply(i, FireworkStarRecipe::new)
|
| 36 | );
|
| 37 | public static final StreamCodec<RegistryFriendlyByteBuf, FireworkStarRecipe> STREAM_CODEC = StreamCodec.composite(
|
| 38 | ByteBufCodecs.map(HashMap::new, FireworkExplosion.Shape.STREAM_CODEC, Ingredient.CONTENTS_STREAM_CODEC),
|
| 39 | o -> o.shapes,
|
| 40 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 41 | o -> o.trail,
|
| 42 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 43 | o -> o.twinkle,
|
| 44 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 45 | o -> o.fuel,
|
| 46 | Ingredient.CONTENTS_STREAM_CODEC,
|
| 47 | o -> o.dye,
|
| 48 | ItemStackTemplate.STREAM_CODEC,
|
| 49 | o -> o.result,
|
| 50 | FireworkStarRecipe::new
|
| 51 | );
|
| 52 | public static final RecipeSerializer<FireworkStarRecipe> SERIALIZER = new RecipeSerializer<>(MAP_CODEC, STREAM_CODEC);
|
| 53 | private final Map<FireworkExplosion.Shape, Ingredient> shapes;
|
| 54 | private final Ingredient trail;
|
| 55 | private final Ingredient twinkle;
|
| 56 | private final Ingredient fuel;
|
| 57 | private final Ingredient dye;
|
| 58 | private final ItemStackTemplate result;
|
| 59 |
|
| 60 | public FireworkStarRecipe(
|
| 61 | final Map<FireworkExplosion.Shape, Ingredient> shapes,
|
| 62 | final Ingredient trail,
|
| 63 | final Ingredient twinkle,
|
| 64 | final Ingredient fuel,
|
| 65 | final Ingredient dye,
|
| 66 | final ItemStackTemplate result
|
| 67 | ) {
|
| 68 | this.shapes = shapes;
|
| 69 | this.trail = trail;
|
| 70 | this.twinkle = twinkle;
|
| 71 | this.fuel = fuel;
|
| 72 | this.dye = dye;
|
| 73 | this.result = result;
|
| 74 | }
|
| 75 |
|
| 76 | private FireworkExplosion.@Nullable Shape findShape(final ItemStack itemStack) {
|
| 77 | for (Entry<FireworkExplosion.Shape, Ingredient> e : this.shapes.entrySet()) {
|
| 78 | if (e.getValue().test(itemStack)) {
|
| 79 | return e.getKey();
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | return null;
|
| 84 | }
|
| 85 |
|
| 86 | public boolean matches(final CraftingInput input, final Level level) {
|
| 87 | if (input.ingredientCount() < 2) {
|
| 88 | return false;
|
| 89 | }
|
| 90 |
|
| 91 | boolean hasFuel = false;
|
| 92 | boolean hasDye = false;
|
| 93 | boolean hasShape = false;
|
| 94 | boolean hasTrail = false;
|
| 95 | boolean hasTwinkle = false;
|
| 96 |
|
| 97 | for (int slot = 0; slot < input.size(); slot++) {
|
| 98 | ItemStack itemStack = input.getItem(slot);
|
| 99 | if (!itemStack.isEmpty()) {
|
| 100 | if (this.twinkle.test(itemStack)) {
|
| 101 | if (hasTwinkle) {
|
| 102 | return false;
|
| 103 | }
|
| 104 |
|
| 105 | hasTwinkle = true;
|
| 106 | } else if (this.trail.test(itemStack)) {
|
| 107 | if (hasTrail) {
|
| 108 | return false;
|
| 109 | }
|
| 110 |
|
| 111 | hasTrail = true;
|
| 112 | } else if (this.fuel.test(itemStack)) {
|
| 113 | if (hasFuel) {
|
| 114 | return false;
|
| 115 | }
|
| 116 |
|
| 117 | hasFuel = true;
|
| 118 | } else if (this.dye.test(itemStack) && itemStack.has(DataComponents.DYE)) {
|
| 119 | hasDye = true;
|
| 120 | } else {
|
| 121 | FireworkExplosion.Shape shape = this.findShape(itemStack);
|
| 122 | if (shape == null) {
|
| 123 | return false;
|
| 124 | }
|
| 125 |
|
| 126 | if (hasShape) {
|
| 127 | return false;
|
| 128 | }
|
| 129 |
|
| 130 | hasShape = true;
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | return hasFuel && hasDye;
|
| 136 | }
|
| 137 |
|
| 138 | public ItemStack assemble(final CraftingInput input) {
|
| 139 | FireworkExplosion.Shape shape = FireworkExplosion.Shape.SMALL_BALL;
|
| 140 | boolean hasTwinkle = false;
|
| 141 | boolean hasTrail = false;
|
| 142 | IntList colors = new IntArrayList();
|
| 143 |
|
| 144 | for (int slot = 0; slot < input.size(); slot++) {
|
| 145 | ItemStack itemStack = input.getItem(slot);
|
| 146 | if (!itemStack.isEmpty()) {
|
| 147 | FireworkExplosion.Shape maybeShape = this.findShape(itemStack);
|
| 148 | if (maybeShape != null) {
|
| 149 | shape = maybeShape;
|
| 150 | } else if (this.twinkle.test(itemStack)) {
|
| 151 | hasTwinkle = true;
|
| 152 | } else if (this.trail.test(itemStack)) {
|
| 153 | hasTrail = true;
|
| 154 | } else if (this.dye.test(itemStack)) {
|
| 155 | DyeColor dye = itemStack.getOrDefault(DataComponents.DYE, DyeColor.WHITE);
|
| 156 | colors.add(dye.getFireworkColor());
|
| 157 | }
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | ItemStack star = this.result.create();
|
| 162 | star.set(DataComponents.FIREWORK_EXPLOSION, new FireworkExplosion(shape, colors, IntList.of(), hasTrail, hasTwinkle));
|
| 163 | return star;
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public RecipeSerializer<FireworkStarRecipe> getSerializer() {
|
| 168 | return SERIALIZER;
|
| 169 | }
|
| 170 | }
|
| 171 |
|