| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.util.ExtraCodecs;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.item.component.FireworkExplosion;
|
| 12 | import net.minecraft.world.item.component.Fireworks;
|
| 13 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 14 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 15 |
|
| 16 | /**
|
| 17 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 18 | */
|
| 19 | public class SetFireworksFunction extends LootItemConditionalFunction {
|
| 20 | public static final MapCodec<SetFireworksFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 21 | i -> commonFields(i)
|
| 22 | .and(
|
| 23 | i.group(
|
| 24 | ListOperation.StandAlone.codec(FireworkExplosion.CODEC, 256).optionalFieldOf("explosions").forGetter(f -> f.explosions),
|
| 25 | ExtraCodecs.UNSIGNED_BYTE.optionalFieldOf("flight_duration").forGetter(f -> f.flightDuration)
|
| 26 | )
|
| 27 | )
|
| 28 | .apply(i, SetFireworksFunction::new)
|
| 29 | );
|
| 30 | public static final Fireworks DEFAULT_VALUE = new Fireworks(0, List.of());
|
| 31 | private final Optional<ListOperation.StandAlone<FireworkExplosion>> explosions;
|
| 32 | private final Optional<Integer> flightDuration;
|
| 33 |
|
| 34 | /**
|
| 35 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 36 | */
|
| 37 | public SetFireworksFunction(
|
| 38 | final Optional<Holder<LootItemCondition>> condition,
|
| 39 | final Optional<ListOperation.StandAlone<FireworkExplosion>> explosions,
|
| 40 | final Optional<Integer> flightDuration
|
| 41 | ) {
|
| 42 | super(condition);
|
| 43 | this.explosions = explosions;
|
| 44 | this.flightDuration = flightDuration;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 49 | itemStack.update(DataComponents.FIREWORKS, DEFAULT_VALUE, this::apply);
|
| 50 | return itemStack;
|
| 51 | }
|
| 52 |
|
| 53 | private Fireworks apply(final Fireworks old) {
|
| 54 | return new Fireworks(
|
| 55 | this.flightDuration.orElseGet(old::flightDuration),
|
| 56 | this.explosions.<List<FireworkExplosion>>map(operation -> operation.apply(old.explosions())).orElse(old.explosions())
|
| 57 | );
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public MapCodec<SetFireworksFunction> codec() {
|
| 62 | return MAP_CODEC;
|
| 63 | }
|
| 64 | }
|
| 65 |
|