| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import net.minecraft.ChatFormatting;
|
| 8 | import net.minecraft.core.component.DataComponentGetter;
|
| 9 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.network.chat.ComponentSerialization;
|
| 12 | import net.minecraft.network.chat.ComponentUtils;
|
| 13 | import net.minecraft.network.chat.Style;
|
| 14 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 15 | import net.minecraft.network.codec.StreamCodec;
|
| 16 | import net.minecraft.util.Util;
|
| 17 | import net.minecraft.world.item.Item;
|
| 18 | import net.minecraft.world.item.TooltipFlag;
|
| 19 |
|
| 20 | public record ItemLore(List<Component> lines, List<Component> styledLines) implements TooltipProvider {
|
| 21 | public static final ItemLore EMPTY = new ItemLore(List.of());
|
| 22 | public static final int MAX_LINES = 256;
|
| 23 | private static final Style LORE_STYLE = Style.EMPTY.withColor(ChatFormatting.DARK_PURPLE).withItalic(true);
|
| 24 | public static final Codec<ItemLore> CODEC = ComponentSerialization.CODEC.sizeLimitedListOf(256).xmap(ItemLore::new, ItemLore::lines);
|
| 25 | public static final StreamCodec<RegistryFriendlyByteBuf, ItemLore> STREAM_CODEC = ComponentSerialization.STREAM_CODEC
|
| 26 | .apply(ByteBufCodecs.list(256))
|
| 27 | .map(ItemLore::new, ItemLore::lines);
|
| 28 |
|
| 29 | public ItemLore(final List<Component> lines) {
|
| 30 | this(lines, Lists.transform(lines, component -> ComponentUtils.mergeStyles(component, LORE_STYLE)));
|
| 31 | }
|
| 32 |
|
| 33 | public ItemLore {
|
| 34 | if (lines.size() > 256) {
|
| 35 | throw new IllegalArgumentException("Got " + lines.size() + " lines, but maximum is 256");
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | public ItemLore withLineAdded(final Component component) {
|
| 40 | return new ItemLore(Util.copyAndAdd(this.lines, component));
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 45 | this.styledLines.forEach(consumer);
|
| 46 | }
|
| 47 | }
|
| 48 |
|