Search in sources :

Example 1 with Style

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

the class ChatColor method translateColors.

public static Text translateColors(String string) {
    final Pattern hex = Pattern.compile("ยง#[0-9a-fA-F]{6}");
    LiteralText text = new LiteralText("");
    Style style = Style.EMPTY;
    Matcher matcher = hex.matcher(string);
    while (matcher.find()) {
        int start = matcher.start();
        int end = matcher.end();
        text.append(new LiteralText(string.substring(0, start)).setStyle(style));
        style = style.withColor(TextColor.parse(string.substring(start + 1, end)));
        string = string.substring(end);
        matcher = hex.matcher(string);
    }
    text.append(new LiteralText(string).setStyle(style));
    return text;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Style(net.minecraft.text.Style) LiteralText(net.minecraft.text.LiteralText)

Example 2 with Style

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

the class StyleLoader method load.

@Override
public Style load(NbtCompound tag) {
    Style style = Style.EMPTY;
    style = style.withBold(tag.getBoolean("bold"));
    style = style.withObfuscated(tag.getBoolean("obfuscated"));
    style = style.withItalic(tag.getBoolean("italic"));
    style = style.withStrikethrough(tag.getBoolean("strikethrough"));
    style = style.withUnderline(tag.getBoolean("underlined"));
    if (tag.contains("rgb"))
        style = style.withColor(tag.getInt("rgb"));
    return style;
}
Also used : Style(net.minecraft.text.Style)

Example 3 with Style

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

the class ColorCodeFormatter method format.

@Override
public Optional<FluidText> format(FluidText text, @Nullable ParseResults<CommandSource> parse) {
    if (parse != null) {
        return Optional.empty();
    }
    String string = text.getString();
    if (!string.contains("&")) {
        return Optional.empty();
    }
    SearchResult search = SearchResult.searchOf(string, "(?i)&[0-9A-FK-OR]", FindType.REGEX);
    if (search.size() == 0) {
        return Optional.empty();
    }
    int index = 0;
    Style last = Style.EMPTY;
    FluidText formatted = new FluidText();
    for (StringMatch match : search.getMatches()) {
        formatted.append(text.truncate(new StringMatch("", index, match.start)).fillStyle(last));
        Formatting format = Formatting.byCode(match.match.charAt(1));
        last = last.withFormatting(format);
        index = match.start;
    }
    FluidText small = text.truncate(new StringMatch("", index, string.length()));
    if (small != null && !small.getString().isEmpty()) {
        formatted.append(small.fillStyle(last));
    }
    return Optional.of(formatted);
}
Also used : FluidText(io.github.darkkronicle.advancedchatcore.util.FluidText) Formatting(net.minecraft.util.Formatting) Style(net.minecraft.text.Style) StringMatch(io.github.darkkronicle.advancedchatcore.util.StringMatch) SearchResult(io.github.darkkronicle.advancedchatcore.util.SearchResult)

Example 4 with Style

use of net.minecraft.text.Style in project mclogs-fabric by aternosorg.

the class CommandMclogsList method register.

static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
    dispatcher.register(literal("mclogs").then(literal("list").requires(source -> source.hasPermissionLevel(2)).executes((context) -> {
        ServerCommandSource source = context.getSource();
        try {
            String[] logs = MclogsFabricLoader.getLogs(context);
            if (logs.length == 0) {
                source.sendFeedback(new LiteralText("No logs available!"), false);
                return 0;
            }
            LiteralText feedback = new LiteralText("Available Logs:");
            for (String log : logs) {
                Style s = Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/mclogs share " + log));
                LiteralText tempText = new LiteralText("\n" + log);
                tempText.setStyle(s);
                feedback.append(tempText);
            }
            source.sendFeedback(feedback, false);
            return logs.length;
        } catch (Exception e) {
            MclogsFabricLoader.logger.error("An error occurred when listing your logs.");
            MclogsFabricLoader.logger.error(e);
            LiteralText error = new LiteralText("An error occurred. Check your log for more details.");
            source.sendError(error);
            return -1;
        }
    })));
}
Also used : LiteralText(net.minecraft.text.LiteralText) Style(net.minecraft.text.Style) CommandDispatcher(com.mojang.brigadier.CommandDispatcher) ServerCommandSource(net.minecraft.server.command.ServerCommandSource) ClickEvent(net.minecraft.text.ClickEvent) CommandManager.literal(net.minecraft.server.command.CommandManager.literal) ClickEvent(net.minecraft.text.ClickEvent) Style(net.minecraft.text.Style) ServerCommandSource(net.minecraft.server.command.ServerCommandSource) LiteralText(net.minecraft.text.LiteralText)

Example 5 with Style

use of net.minecraft.text.Style in project mclogs-fabric by aternosorg.

the class MclogsFabricLoader method share.

public static int share(ServerCommandSource source, String filename) {
    MclogsAPI.mcversion = source.getMinecraftServer().getVersion();
    logger.log(Level.INFO, "Sharing " + filename);
    source.sendFeedback(new LiteralText("Sharing " + filename), false);
    try {
        Path logs = source.getMinecraftServer().getFile("logs/").toPath();
        Path log = logs.resolve(filename);
        if (!log.getParent().equals(logs)) {
            throw new FileNotFoundException();
        }
        APIResponse response = MclogsAPI.share(log);
        if (response.success) {
            LiteralText feedback = new LiteralText("Your log has been uploaded: ");
            feedback.setStyle(Style.EMPTY.withColor(Formatting.GREEN));
            LiteralText link = new LiteralText(response.url);
            Style linkStyle = Style.EMPTY.withColor(Formatting.BLUE);
            linkStyle = linkStyle.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, response.url));
            link.setStyle(linkStyle);
            source.sendFeedback(feedback.append(link), true);
            return 1;
        } else {
            logger.error("An error occurred when uploading your log: ");
            logger.error(response.error);
            LiteralText error = new LiteralText("An error occurred. Check your log for more details");
            source.sendError(error);
            return 0;
        }
    } catch (FileNotFoundException | IllegalArgumentException e) {
        LiteralText error = new LiteralText("The log file " + filename + " doesn't exist. Use '/mclogs list' to list all logs.");
        source.sendError(error);
        return -1;
    } catch (IOException e) {
        source.sendError(new LiteralText("An error occurred. Check your log for more details"));
        logger.error("Could not get log file!");
        logger.error(e);
        return 0;
    }
}
Also used : Path(java.nio.file.Path) APIResponse(gs.mclo.java.APIResponse) ClickEvent(net.minecraft.text.ClickEvent) FileNotFoundException(java.io.FileNotFoundException) Style(net.minecraft.text.Style) IOException(java.io.IOException) LiteralText(net.minecraft.text.LiteralText)

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