| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 9 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 10 | import net.minecraft.client.gui.narration.NarrationSupplier;
|
| 11 | import net.minecraft.locale.Language;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.util.FormattedCharSequence;
|
| 15 | import net.minecraft.world.inventory.tooltip.TooltipComponent;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class Tooltip implements NarrationSupplier {
|
| 20 | private static final int MAX_WIDTH = 170;
|
| 21 | private final Component message;
|
| 22 | private @Nullable List<FormattedCharSequence> cachedTooltip;
|
| 23 | private @Nullable Language splitWithLanguage;
|
| 24 | private final @Nullable Identifier style;
|
| 25 | private final @Nullable Component narration;
|
| 26 | private final Optional<TooltipComponent> component;
|
| 27 |
|
| 28 | private Tooltip(final Component message, final @Nullable Component narration, final Optional<TooltipComponent> component, final @Nullable Identifier style) {
|
| 29 | this.message = message;
|
| 30 | this.narration = narration;
|
| 31 | this.component = component;
|
| 32 | this.style = style;
|
| 33 | }
|
| 34 |
|
| 35 | public static Tooltip create(final Component message) {
|
| 36 | return new Tooltip(message, message, Optional.empty(), null);
|
| 37 | }
|
| 38 |
|
| 39 | public static Tooltip create(final Component message, final @Nullable Component narration) {
|
| 40 | return new Tooltip(message, narration, Optional.empty(), null);
|
| 41 | }
|
| 42 |
|
| 43 | public static Tooltip create(final Component message, final Optional<TooltipComponent> component, final @Nullable Identifier style) {
|
| 44 | return new Tooltip(message, message, component, style);
|
| 45 | }
|
| 46 |
|
| 47 | public Optional<TooltipComponent> component() {
|
| 48 | return this.component;
|
| 49 | }
|
| 50 |
|
| 51 | public @Nullable Identifier style() {
|
| 52 | return this.style;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public void updateNarration(final NarrationElementOutput output) {
|
| 57 | if (this.narration != null) {
|
| 58 | output.add(NarratedElementType.HINT, this.narration);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public List<FormattedCharSequence> toCharSequence(final Minecraft minecraft) {
|
| 63 | Language currentLanguage = Language.getInstance();
|
| 64 | if (this.cachedTooltip == null || currentLanguage != this.splitWithLanguage) {
|
| 65 | this.cachedTooltip = splitTooltip(minecraft, this.message);
|
| 66 | this.splitWithLanguage = currentLanguage;
|
| 67 | }
|
| 68 |
|
| 69 | return this.cachedTooltip;
|
| 70 | }
|
| 71 |
|
| 72 | public static List<FormattedCharSequence> splitTooltip(final Minecraft minecraft, final Component message) {
|
| 73 | return minecraft.font.split(message, 170);
|
| 74 | }
|
| 75 | }
|
| 76 |
|