| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.minecraft.ChatFormatting;
|
| 9 | import net.minecraft.core.component.DataComponentGetter;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 12 | import net.minecraft.network.codec.StreamCodec;
|
| 13 | import net.minecraft.util.ExtraCodecs;
|
| 14 | import net.minecraft.world.item.Item;
|
| 15 | import net.minecraft.world.item.TooltipFlag;
|
| 16 |
|
| 17 | public record Fireworks(int flightDuration, List<FireworkExplosion> explosions) implements TooltipProvider {
|
| 18 | public static final int MAX_EXPLOSIONS = 256;
|
| 19 | public static final Codec<Fireworks> CODEC = RecordCodecBuilder.create(
|
| 20 | i -> i.group(
|
| 21 | ExtraCodecs.UNSIGNED_BYTE.optionalFieldOf("flight_duration", 0).forGetter(Fireworks::flightDuration),
|
| 22 | FireworkExplosion.CODEC.sizeLimitedListOf(256).optionalFieldOf("explosions", List.of()).forGetter(Fireworks::explosions)
|
| 23 | )
|
| 24 | .apply(i, Fireworks::new)
|
| 25 | );
|
| 26 | public static final StreamCodec<ByteBuf, Fireworks> STREAM_CODEC = StreamCodec.composite(
|
| 27 | ByteBufCodecs.VAR_INT, Fireworks::flightDuration, FireworkExplosion.STREAM_CODEC.apply(ByteBufCodecs.list(256)), Fireworks::explosions, Fireworks::new
|
| 28 | );
|
| 29 |
|
| 30 | public Fireworks {
|
| 31 | if (explosions.size() > 256) {
|
| 32 | throw new IllegalArgumentException("Got " + explosions.size() + " explosions, but maximum is 256");
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 38 | if (this.flightDuration > 0) {
|
| 39 | consumer.accept(Component.translatable("item.minecraft.firework_rocket.flight_duration", this.flightDuration).withStyle(ChatFormatting.GRAY));
|
| 40 | }
|
| 41 |
|
| 42 | FireworkExplosion current = null;
|
| 43 | int count = 0;
|
| 44 |
|
| 45 | for (FireworkExplosion explosion : this.explosions) {
|
| 46 | if (current == null) {
|
| 47 | current = explosion;
|
| 48 | count = 1;
|
| 49 | } else if (current.equals(explosion)) {
|
| 50 | count++;
|
| 51 | } else {
|
| 52 | addExplosionTooltip(consumer, current, count);
|
| 53 | current = explosion;
|
| 54 | count = 1;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | if (current != null) {
|
| 59 | addExplosionTooltip(consumer, current, count);
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | private static void addExplosionTooltip(final Consumer<Component> consumer, final FireworkExplosion explosion, final int count) {
|
| 64 | Component shapeName = explosion.shape().getName();
|
| 65 | if (count == 1) {
|
| 66 | consumer.accept(Component.translatable("item.minecraft.firework_rocket.single_star", shapeName).withStyle(ChatFormatting.GRAY));
|
| 67 | } else {
|
| 68 | consumer.accept(Component.translatable("item.minecraft.firework_rocket.multiple_stars", count, shapeName).withStyle(ChatFormatting.GRAY));
|
| 69 | }
|
| 70 |
|
| 71 | explosion.addAdditionalTooltip(component -> consumer.accept(Component.literal(" ").append(component)));
|
| 72 | }
|
| 73 | }
|
| 74 |
|