Search in sources :

Example 6 with Style

use of net.minecraft.text.Style in project FZMM-Mod by Zailer43.

the class GradientLogic method getGradient.

public static MutableText getGradient(String message, Color4f initialColor4f, Color4f finalColor4f, boolean obfuscated, boolean bold, boolean strikethrough, boolean underline, boolean italic) {
    MinecraftClient mc = MinecraftClient.getInstance();
    assert mc.player != null;
    Color initialColor = new Color(initialColor4f.intValue);
    Color finalColor = new Color(finalColor4f.intValue);
    byte red = (byte) (initialColor.getRed() + Byte.MIN_VALUE);
    byte green = (byte) (initialColor.getGreen() + Byte.MIN_VALUE);
    byte blue = (byte) (initialColor.getBlue() + Byte.MIN_VALUE);
    byte red2 = (byte) (finalColor.getRed() + Byte.MIN_VALUE);
    byte green2 = (byte) (finalColor.getGreen() + Byte.MIN_VALUE);
    byte blue2 = (byte) (finalColor.getBlue() + Byte.MIN_VALUE);
    Style style = Style.EMPTY;
    if (obfuscated)
        style = style.withObfuscated(true);
    if (bold)
        style = style.withBold(true);
    if (strikethrough)
        style = style.withStrikethrough(true);
    if (underline)
        style = style.withUnderline(true);
    if (italic)
        style = style.withItalic(true);
    else
        style = style.withItalic(false);
    return getGradient(message, red, green, blue, red2, green2, blue2, style);
}
Also used : MinecraftClient(net.minecraft.client.MinecraftClient) TextColor(net.minecraft.text.TextColor) Style(net.minecraft.text.Style)

Example 7 with Style

use of net.minecraft.text.Style in project carpet-discarpet by replaceitem.

the class DiscarpetCommand method register.

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
    dispatcher.register(literal("discarpet").requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(2)).executes(commandContext -> {
        String version = FabricLoader.getInstance().getModContainer("discarpet").get().getMetadata().getVersion().getFriendlyString();
        MutableText text = new LiteralText("Discarpet version " + version).formatted(Formatting.BLUE);
        text.append("\nFor help, see the ");
        text.append(new LiteralText("documentation").setStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/replaceitem/carpet-discarpet/blob/master/docs/Full.md")).withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Click to get to the Discarpet documentation"))).withFormatting(Formatting.UNDERLINE).withColor(Formatting.DARK_BLUE)));
        commandContext.getSource().sendFeedback(text, true);
        return 1;
    }).then(literal("list").executes(commandContext -> {
        Set<String> botIDs = Discarpet.discordBots.keySet();
        final LiteralText t;
        if (botIDs.size() == 0) {
            t = (LiteralText) new LiteralText("There are no bots active:\n").formatted(Formatting.RED);
        } else {
            t = (LiteralText) new LiteralText("There are " + botIDs.size() + " bots active\n").formatted(Formatting.GREEN);
        }
        botIDs.forEach(id -> t.append(new LiteralText(id + "\n").formatted(Formatting.BLUE)));
        commandContext.getSource().sendFeedback(t, true);
        return botIDs.size();
    })).then(literal("getInvite").then(CommandManager.argument("id", StringArgumentType.string()).suggests(BOTS).executes(commandContext -> {
        String id = StringArgumentType.getString(commandContext, "id");
        String invite = Discarpet.discordBots.get(id).getInvite();
        Text text = ((new LiteralText("Click here to get the invite link for the bot")).styled((style) -> {
            return style.withColor(Formatting.BLUE).withFormatting(Formatting.UNDERLINE).withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, invite)).withHoverEvent(new HoverEvent(net.minecraft.text.HoverEvent.Action.SHOW_TEXT, new LiteralText("Click to open the invite link"))).withInsertion(invite);
        }));
        commandContext.getSource().sendFeedback(text, false);
        return 1;
    }))).then(literal("reload").executes(commandContext -> {
        Discarpet.loadConfig(commandContext.getSource());
        return 1;
    })));
}
Also used : Discarpet(Discarpet.Discarpet) FabricLoader(net.fabricmc.loader.api.FabricLoader) LiteralText(net.minecraft.text.LiteralText) CommandDispatcher(com.mojang.brigadier.CommandDispatcher) ServerCommandSource(net.minecraft.server.command.ServerCommandSource) Set(java.util.Set) CommandManager.literal(net.minecraft.server.command.CommandManager.literal) HoverEvent(net.minecraft.text.HoverEvent) CommandManager(net.minecraft.server.command.CommandManager) CommandSource(net.minecraft.command.CommandSource) StringArgumentType(com.mojang.brigadier.arguments.StringArgumentType) Style(net.minecraft.text.Style) Formatting(net.minecraft.util.Formatting) ClickEvent(net.minecraft.text.ClickEvent) MutableText(net.minecraft.text.MutableText) Text(net.minecraft.text.Text) SuggestionProvider(com.mojang.brigadier.suggestion.SuggestionProvider) MutableText(net.minecraft.text.MutableText) HoverEvent(net.minecraft.text.HoverEvent) ClickEvent(net.minecraft.text.ClickEvent) LiteralText(net.minecraft.text.LiteralText) MutableText(net.minecraft.text.MutableText) Text(net.minecraft.text.Text) LiteralText(net.minecraft.text.LiteralText)

Example 8 with Style

use of net.minecraft.text.Style 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 9 with Style

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

the class AntiSpam method onChat.

private void onChat(AddChatMessageCallback.ChatAddEvent event) {
    if (!enabled)
        return;
    List<ChatHudLine<OrderedText>> chatLines = event.getChatLines();
    var chatText = event.getChatText();
    if (chatText == null)
        return;
    if (chatLines.isEmpty())
        return;
    class JustGiveMeTheStringVisitor implements CharacterVisitor {

        final StringBuilder sb = new StringBuilder();

        @Override
        public boolean accept(int index, Style style, int codePoint) {
            sb.appendCodePoint(codePoint);
            return true;
        }

        @Override
        public String toString() {
            return sb.toString();
        }
    }
    ChatHud chat = MC.inGameHud.getChatHud();
    int maxTextLength = MathHelper.floor(chat.getWidth() / chat.getChatScale());
    List<OrderedText> newLines = ChatMessages.breakRenderedChatMessageLines(chatText, maxTextLength, MC.textRenderer);
    int spamCounter = 1;
    int matchingLines = 0;
    for (int i = chatLines.size() - 1; i >= 0; i--) {
        JustGiveMeTheStringVisitor oldLineVS = new JustGiveMeTheStringVisitor();
        chatLines.get(i).getText().accept(oldLineVS);
        String oldLine = oldLineVS.toString();
        if (matchingLines <= newLines.size() - 1) {
            JustGiveMeTheStringVisitor newLineVS = new JustGiveMeTheStringVisitor();
            newLines.get(matchingLines).accept(newLineVS);
            String newLine = newLineVS.toString();
            if (matchingLines < newLines.size() - 1) {
                if (oldLine.equals(newLine))
                    matchingLines++;
                else
                    matchingLines = 0;
                continue;
            }
            if (!oldLine.startsWith(newLine)) {
                matchingLines = 0;
                continue;
            }
            if (i > 0 && matchingLines == newLines.size() - 1) {
                JustGiveMeTheStringVisitor nextOldLineVS = new JustGiveMeTheStringVisitor();
                chatLines.get(i - 1).getText().accept(nextOldLineVS);
                String nextOldLine = nextOldLineVS.toString();
                String twoLines = oldLine + nextOldLine;
                String addedText = twoLines.substring(newLine.length());
                if (addedText.startsWith(" [x") && addedText.endsWith("]")) {
                    String oldSpamCounter = addedText.substring(3, addedText.length() - 1);
                    if (MathUtils.isInteger(oldSpamCounter)) {
                        spamCounter += Integer.parseInt(oldSpamCounter);
                        matchingLines++;
                        continue;
                    }
                }
            }
            if (oldLine.length() == newLine.length())
                spamCounter++;
            else {
                String addedText = oldLine.substring(newLine.length());
                if (!addedText.startsWith(" [x") || !addedText.endsWith("]")) {
                    matchingLines = 0;
                    continue;
                }
                String oldSpamCounter = addedText.substring(3, addedText.length() - 1);
                if (!MathUtils.isInteger(oldSpamCounter)) {
                    matchingLines = 0;
                    continue;
                }
                spamCounter += Integer.parseInt(oldSpamCounter);
            }
        }
        if (i + matchingLines >= i) {
            chatLines.subList(i, i + matchingLines + 1).clear();
        }
        matchingLines = 0;
    }
    if (spamCounter > 1) {
        chatText = new LiteralText("").append(chatText).append(new LiteralText(" [x" + spamCounter + "]"));
    }
    event.setChatText(chatText);
}
Also used : ChatHudLine(net.minecraft.client.gui.hud.ChatHudLine) CharacterVisitor(net.minecraft.text.CharacterVisitor) OrderedText(net.minecraft.text.OrderedText) Style(net.minecraft.text.Style) ChatHud(net.minecraft.client.gui.hud.ChatHud) LiteralText(net.minecraft.text.LiteralText)

Example 10 with Style

use of net.minecraft.text.Style 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)

Aggregations

Style (net.minecraft.text.Style)11 LiteralText (net.minecraft.text.LiteralText)5 ClickEvent (net.minecraft.text.ClickEvent)3 TextColor (net.minecraft.text.TextColor)3 CommandDispatcher (com.mojang.brigadier.CommandDispatcher)2 FluidText (io.github.darkkronicle.advancedchatcore.util.FluidText)2 StringMatch (io.github.darkkronicle.advancedchatcore.util.StringMatch)2 CommandSource (net.minecraft.command.CommandSource)2 CommandManager.literal (net.minecraft.server.command.CommandManager.literal)2 ServerCommandSource (net.minecraft.server.command.ServerCommandSource)2 OrderedText (net.minecraft.text.OrderedText)2 Formatting (net.minecraft.util.Formatting)2 Discarpet (Discarpet.Discarpet)1 StringArgumentType (com.mojang.brigadier.arguments.StringArgumentType)1 StringRange (com.mojang.brigadier.context.StringRange)1 SuggestionProvider (com.mojang.brigadier.suggestion.SuggestionProvider)1 CommandNode (com.mojang.brigadier.tree.CommandNode)1 LiteralCommandNode (com.mojang.brigadier.tree.LiteralCommandNode)1 APIResponse (gs.mclo.java.APIResponse)1 ChatFormatterRegistry (io.github.darkkronicle.advancedchatbox.registry.ChatFormatterRegistry)1