| 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 it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 7 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.function.Consumer;
|
| 10 | import java.util.function.IntFunction;
|
| 11 | import net.minecraft.ChatFormatting;
|
| 12 | import net.minecraft.core.component.DataComponentGetter;
|
| 13 | import net.minecraft.network.chat.CommonComponents;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.network.chat.MutableComponent;
|
| 16 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 17 | import net.minecraft.network.codec.StreamCodec;
|
| 18 | import net.minecraft.util.ByIdMap;
|
| 19 | import net.minecraft.util.StringRepresentable;
|
| 20 | import net.minecraft.world.item.DyeColor;
|
| 21 | import net.minecraft.world.item.Item;
|
| 22 | import net.minecraft.world.item.TooltipFlag;
|
| 23 |
|
| 24 | public record FireworkExplosion(FireworkExplosion.Shape shape, IntList colors, IntList fadeColors, boolean hasTrail, boolean hasTwinkle)
|
| 25 | implements TooltipProvider {
|
| 26 | public static final FireworkExplosion DEFAULT = new FireworkExplosion(FireworkExplosion.Shape.SMALL_BALL, IntList.of(), IntList.of(), false, false);
|
| 27 | public static final Codec<IntList> COLOR_LIST_CODEC = Codec.INT.listOf().xmap(IntArrayList::new, ArrayList::new);
|
| 28 | public static final Codec<FireworkExplosion> CODEC = RecordCodecBuilder.create(
|
| 29 | i -> i.group(
|
| 30 | FireworkExplosion.Shape.CODEC.fieldOf("shape").forGetter(FireworkExplosion::shape),
|
| 31 | COLOR_LIST_CODEC.optionalFieldOf("colors", IntList.of()).forGetter(FireworkExplosion::colors),
|
| 32 | COLOR_LIST_CODEC.optionalFieldOf("fade_colors", IntList.of()).forGetter(FireworkExplosion::fadeColors),
|
| 33 | Codec.BOOL.optionalFieldOf("has_trail", false).forGetter(FireworkExplosion::hasTrail),
|
| 34 | Codec.BOOL.optionalFieldOf("has_twinkle", false).forGetter(FireworkExplosion::hasTwinkle)
|
| 35 | )
|
| 36 | .apply(i, FireworkExplosion::new)
|
| 37 | );
|
| 38 | private static final StreamCodec<ByteBuf, IntList> COLOR_LIST_STREAM_CODEC = ByteBufCodecs.INT
|
| 39 | .apply(ByteBufCodecs.list())
|
| 40 | .map(IntArrayList::new, ArrayList::new);
|
| 41 | public static final StreamCodec<ByteBuf, FireworkExplosion> STREAM_CODEC = StreamCodec.composite(
|
| 42 | FireworkExplosion.Shape.STREAM_CODEC,
|
| 43 | FireworkExplosion::shape,
|
| 44 | COLOR_LIST_STREAM_CODEC,
|
| 45 | FireworkExplosion::colors,
|
| 46 | COLOR_LIST_STREAM_CODEC,
|
| 47 | FireworkExplosion::fadeColors,
|
| 48 | ByteBufCodecs.BOOL,
|
| 49 | FireworkExplosion::hasTrail,
|
| 50 | ByteBufCodecs.BOOL,
|
| 51 | FireworkExplosion::hasTwinkle,
|
| 52 | FireworkExplosion::new
|
| 53 | );
|
| 54 | private static final Component CUSTOM_COLOR_NAME = Component.translatable("item.minecraft.firework_star.custom_color");
|
| 55 |
|
| 56 | @Override
|
| 57 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 58 | consumer.accept(this.shape.getName().withStyle(ChatFormatting.GRAY));
|
| 59 | this.addAdditionalTooltip(consumer);
|
| 60 | }
|
| 61 |
|
| 62 | public void addAdditionalTooltip(final Consumer<Component> consumer) {
|
| 63 | if (!this.colors.isEmpty()) {
|
| 64 | consumer.accept(appendColors(Component.empty().withStyle(ChatFormatting.GRAY), this.colors));
|
| 65 | }
|
| 66 |
|
| 67 | if (!this.fadeColors.isEmpty()) {
|
| 68 | consumer.accept(
|
| 69 | appendColors(Component.translatable("item.minecraft.firework_star.fade_to").append(CommonComponents.SPACE).withStyle(ChatFormatting.GRAY), this.fadeColors)
|
| 70 | );
|
| 71 | }
|
| 72 |
|
| 73 | if (this.hasTrail) {
|
| 74 | consumer.accept(Component.translatable("item.minecraft.firework_star.trail").withStyle(ChatFormatting.GRAY));
|
| 75 | }
|
| 76 |
|
| 77 | if (this.hasTwinkle) {
|
| 78 | consumer.accept(Component.translatable("item.minecraft.firework_star.flicker").withStyle(ChatFormatting.GRAY));
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | private static Component appendColors(final MutableComponent builder, final IntList colors) {
|
| 83 | for (int i = 0; i < colors.size(); i++) {
|
| 84 | if (i > 0) {
|
| 85 | builder.append(", ");
|
| 86 | }
|
| 87 |
|
| 88 | builder.append(getColorName(colors.getInt(i)));
|
| 89 | }
|
| 90 |
|
| 91 | return builder;
|
| 92 | }
|
| 93 |
|
| 94 | private static Component getColorName(final int colorIndex) {
|
| 95 | DyeColor color = DyeColor.byFireworkColor(colorIndex);
|
| 96 | return color == null ? CUSTOM_COLOR_NAME : Component.translatable("item.minecraft.firework_star." + color.getName());
|
| 97 | }
|
| 98 |
|
| 99 | public FireworkExplosion withFadeColors(final IntList fadeColors) {
|
| 100 | return new FireworkExplosion(this.shape, this.colors, new IntArrayList(fadeColors), this.hasTrail, this.hasTwinkle);
|
| 101 | }
|
| 102 |
|
| 103 | public enum Shape implements StringRepresentable {
|
| 104 | SMALL_BALL(0, "small_ball"),
|
| 105 | LARGE_BALL(1, "large_ball"),
|
| 106 | STAR(2, "star"),
|
| 107 | CREEPER(3, "creeper"),
|
| 108 | BURST(4, "burst");
|
| 109 |
|
| 110 | private static final IntFunction<FireworkExplosion.Shape> BY_ID = ByIdMap.continuous(
|
| 111 | FireworkExplosion.Shape::getId, values(), ByIdMap.OutOfBoundsStrategy.ZERO
|
| 112 | );
|
| 113 | public static final StreamCodec<ByteBuf, FireworkExplosion.Shape> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, FireworkExplosion.Shape::getId);
|
| 114 | public static final Codec<FireworkExplosion.Shape> CODEC = StringRepresentable.fromValues(FireworkExplosion.Shape::values);
|
| 115 | private final int id;
|
| 116 | private final String name;
|
| 117 |
|
| 118 | Shape(final int id, final String name) {
|
| 119 | this.id = id;
|
| 120 | this.name = name;
|
| 121 | }
|
| 122 |
|
| 123 | public MutableComponent getName() {
|
| 124 | return Component.translatable("item.minecraft.firework_star.shape." + this.name);
|
| 125 | }
|
| 126 |
|
| 127 | public int getId() {
|
| 128 | return this.id;
|
| 129 | }
|
| 130 |
|
| 131 | public static FireworkExplosion.Shape byId(final int id) {
|
| 132 | return BY_ID.apply(id);
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public String getSerializedName() {
|
| 137 | return this.name;
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|