Search in sources :

Example 6 with TextColor

use of net.minecraft.text.TextColor in project meteor-client by MeteorDevelopment.

the class TextUtils method preOrderTraverse.

/**
 * Performs a pre-order text traversal of {@link Text} components and ref-returns a sequential list
 * of {@link ColoredText}, such that one could know the text and its color by iterating over the list.
 *
 * @param text         The text to flatten
 * @param stack        An empty stack. This is used by the recursive algorithm to keep track of the parents of the current iteration
 * @param coloredTexts The list of colored text to return
 */
private static void preOrderTraverse(Text text, Stack<ColoredText> stack, List<ColoredText> coloredTexts) {
    if (text == null)
        return;
    // Do actions here
    String textString = text.asString();
    TextColor mcTextColor = text.getStyle().getColor();
    // If mcTextColor is null, the color should be inherited from its parent. In this case, the path of the recursion is stored on the stack,
    // with the current element's parent at the top, so simply peek it if possible. If not, there is no parent element,
    // and with no color, use the default of white.
    Color textColor;
    if (mcTextColor == null) {
        if (stack.empty())
            // No color defined, use default white
            textColor = new Color(255, 255, 255);
        else
            // Use parent color
            textColor = stack.peek().getColor();
    } else {
        // Has a color defined, so use that
        // Sets alpha to max. Some damn reason Color's packed ctor is in ARGB format, not RGBA
        textColor = new Color((text.getStyle().getColor().getRgb()) | 0xFF000000);
    }
    ColoredText coloredText = new ColoredText(textString, textColor);
    coloredTexts.add(coloredText);
    // For the recursion algorithm's child, the current coloredText is its parent, so add to stack
    stack.push(coloredText);
    // Recursively traverse
    for (Text child : text.getSiblings()) preOrderTraverse(child, stack, coloredTexts);
    stack.pop();
}
Also used : Color(meteordevelopment.meteorclient.utils.render.color.Color) TextColor(net.minecraft.text.TextColor) Text(net.minecraft.text.Text) TextColor(net.minecraft.text.TextColor)

Example 7 with TextColor

use of net.minecraft.text.TextColor in project EdenClient by HahaOO7.

the class StyleLoader method save.

@Override
public NbtCompound save(Object value) {
    Style s = cast(value);
    NbtCompound tag = new NbtCompound();
    tag.putBoolean("bold", s.isBold());
    tag.putBoolean("obfuscated", s.isObfuscated());
    tag.putBoolean("italic", s.isItalic());
    tag.putBoolean("strikethrough", s.isStrikethrough());
    tag.putBoolean("underlined", s.isUnderlined());
    TextColor rgb = s.getColor();
    if (rgb != null)
        tag.putInt("rgb", rgb.getRgb());
    return tag;
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) Style(net.minecraft.text.Style) TextColor(net.minecraft.text.TextColor)

Example 8 with TextColor

use of net.minecraft.text.TextColor in project AdvancedChatBox by DarkKronicle.

the class ChatFormatter method format.

/**
 * Format's the chat box contents
 *
 * @param string Contents
 * @return Formatted FluidText. If nothing is changed it will be the contents with Style.EMPTY
 */
public FluidText format(String string) {
    FluidText text = new FluidText(new RawText(string, Style.EMPTY));
    if (string.length() == 0) {
        return text;
    }
    if (suggestor.getAllSuggestions() != null) {
        HashMap<StringMatch, FluidText.StringInsert> format = new HashMap<>();
        for (AdvancedSuggestions suggestions : suggestor.getAllSuggestions()) {
            if (suggestions.getSuggestions().isEmpty()) {
                // Don't want to format if there's nothing there...
                continue;
            }
            boolean atLeastOne = false;
            for (AdvancedSuggestion suggestion : suggestions.getSuggestions()) {
                if (suggestion.getText().length() > 0) {
                    atLeastOne = true;
                    break;
                }
            }
            if (!atLeastOne) {
                continue;
            }
            StringRange range = suggestions.getRange();
            int start = range.getStart();
            int end = range.getEnd();
            if (end > string.length()) {
                end = string.length();
            }
            if (start < 0) {
                start = 0;
            }
            String matchString = string.subSequence(start, end).toString();
            format.put(new StringMatch(matchString, start, end), (current, match) -> {
                Style style = Style.EMPTY;
                style = style.withFormatting(Formatting.UNDERLINE);
                TextColor textColor = TextColor.fromRgb(ChatBoxConfigStorage.General.AVAILABLE_SUGGESTION_COLOR.config.get().color());
                style = style.withColor(textColor);
                return new FluidText(new RawText(matchString, style));
            });
        }
        text.replaceStrings(format);
    }
    for (ChatFormatterRegistry.ChatFormatterOption option : ChatFormatterRegistry.getInstance().getAll()) {
        if (!option.isActive()) {
            continue;
        }
        Optional<FluidText> otext = option.getOption().format(text, suggestor.getParse());
        if (otext.isPresent()) {
            text = otext.get();
        }
    }
    return text;
}
Also used : HashMap(java.util.HashMap) StringMatch(io.github.darkkronicle.advancedchatcore.util.StringMatch) RawText(io.github.darkkronicle.advancedchatcore.util.RawText) StringRange(com.mojang.brigadier.context.StringRange) FluidText(io.github.darkkronicle.advancedchatcore.util.FluidText) Style(net.minecraft.text.Style) ChatFormatterRegistry(io.github.darkkronicle.advancedchatbox.registry.ChatFormatterRegistry) TextColor(net.minecraft.text.TextColor)

Example 9 with TextColor

use of net.minecraft.text.TextColor in project Client by MatHax.

the class TextUtils method preOrderTraverse.

private static void preOrderTraverse(Text text, Stack<ColoredText> stack, List<ColoredText> coloredTexts) {
    if (text == null)
        return;
    String textString = text.asString();
    TextColor mcTextColor = text.getStyle().getColor();
    Color textColor;
    if (mcTextColor == null) {
        if (stack.empty())
            textColor = new Color(255, 255, 255);
        else
            textColor = stack.peek().getColor();
    } else
        textColor = new Color((text.getStyle().getColor().getRgb()) | 0xFF000000);
    ColoredText coloredText = new ColoredText(textString, textColor);
    coloredTexts.add(coloredText);
    stack.push(coloredText);
    for (Text child : text.getSiblings()) preOrderTraverse(child, stack, coloredTexts);
    stack.pop();
}
Also used : TextColor(net.minecraft.text.TextColor) Color(mathax.client.utils.render.color.Color) Text(net.minecraft.text.Text) TextColor(net.minecraft.text.TextColor)

Example 10 with TextColor

use of net.minecraft.text.TextColor in project Client by MatHax.

the class ToastSystem method setText.

public void setText(@Nullable String text) {
    this.text = text != null ? new LiteralText(text).setStyle(Style.EMPTY.withColor(new TextColor(textColor))) : null;
    justUpdated = true;
}
Also used : TextColor(net.minecraft.text.TextColor) LiteralText(net.minecraft.text.LiteralText)

Aggregations

TextColor (net.minecraft.text.TextColor)10 LiteralText (net.minecraft.text.LiteralText)6 Text (net.minecraft.text.Text)4 Color (mathax.client.utils.render.color.Color)2 Color (meteordevelopment.meteorclient.utils.render.color.Color)2 Style (net.minecraft.text.Style)2 Formatting (net.minecraft.util.Formatting)2 StringRange (com.mojang.brigadier.context.StringRange)1 ChatFormatterRegistry (io.github.darkkronicle.advancedchatbox.registry.ChatFormatterRegistry)1 FluidText (io.github.darkkronicle.advancedchatcore.util.FluidText)1 RawText (io.github.darkkronicle.advancedchatcore.util.RawText)1 StringMatch (io.github.darkkronicle.advancedchatcore.util.StringMatch)1 HashMap (java.util.HashMap)1 Enemy (mathax.client.systems.enemies.Enemy)1 Friend (mathax.client.systems.friends.Friend)1 SettingColor (mathax.client.utils.render.color.SettingColor)1 Friend (meteordevelopment.meteorclient.systems.friends.Friend)1 SettingColor (meteordevelopment.meteorclient.utils.render.color.SettingColor)1 NbtCompound (net.minecraft.nbt.NbtCompound)1