| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 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.IntList;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.component.DataComponents;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.item.component.FireworkExplosion;
|
| 12 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 13 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 14 |
|
| 15 | public class SetFireworkExplosionFunction extends LootItemConditionalFunction {
|
| 16 | public static final MapCodec<SetFireworkExplosionFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> commonFields(i)
|
| 18 | .and(
|
| 19 | i.group(
|
| 20 | FireworkExplosion.Shape.CODEC.optionalFieldOf("shape").forGetter(f -> f.shape),
|
| 21 | FireworkExplosion.COLOR_LIST_CODEC.optionalFieldOf("colors").forGetter(f -> f.colors),
|
| 22 | FireworkExplosion.COLOR_LIST_CODEC.optionalFieldOf("fade_colors").forGetter(f -> f.fadeColors),
|
| 23 | Codec.BOOL.optionalFieldOf("trail").forGetter(f -> f.trail),
|
| 24 | Codec.BOOL.optionalFieldOf("twinkle").forGetter(f -> f.twinkle)
|
| 25 | )
|
| 26 | )
|
| 27 | .apply(i, SetFireworkExplosionFunction::new)
|
| 28 | );
|
| 29 | public static final FireworkExplosion DEFAULT_VALUE = new FireworkExplosion(FireworkExplosion.Shape.SMALL_BALL, IntList.of(), IntList.of(), false, false);
|
| 30 | private final Optional<FireworkExplosion.Shape> shape;
|
| 31 | private final Optional<IntList> colors;
|
| 32 | private final Optional<IntList> fadeColors;
|
| 33 | private final Optional<Boolean> trail;
|
| 34 | private final Optional<Boolean> twinkle;
|
| 35 |
|
| 36 | public SetFireworkExplosionFunction(
|
| 37 | final Optional<Holder<LootItemCondition>> condition,
|
| 38 | final Optional<FireworkExplosion.Shape> shape,
|
| 39 | final Optional<IntList> colors,
|
| 40 | final Optional<IntList> fadeColors,
|
| 41 | final Optional<Boolean> hasTrail,
|
| 42 | final Optional<Boolean> hasTwinkle
|
| 43 | ) {
|
| 44 | super(condition);
|
| 45 | this.shape = shape;
|
| 46 | this.colors = colors;
|
| 47 | this.fadeColors = fadeColors;
|
| 48 | this.trail = hasTrail;
|
| 49 | this.twinkle = hasTwinkle;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 54 | itemStack.update(DataComponents.FIREWORK_EXPLOSION, DEFAULT_VALUE, this::apply);
|
| 55 | return itemStack;
|
| 56 | }
|
| 57 |
|
| 58 | private FireworkExplosion apply(final FireworkExplosion original) {
|
| 59 | return new FireworkExplosion(
|
| 60 | this.shape.orElseGet(original::shape),
|
| 61 | this.colors.orElseGet(original::colors),
|
| 62 | this.fadeColors.orElseGet(original::fadeColors),
|
| 63 | this.trail.orElseGet(original::hasTrail),
|
| 64 | this.twinkle.orElseGet(original::hasTwinkle)
|
| 65 | );
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public MapCodec<SetFireworkExplosionFunction> codec() {
|
| 70 | return MAP_CODEC;
|
| 71 | }
|
| 72 | }
|
| 73 |
|