1.8.8 BaseComponent[] messages color

Discussion in 'Spigot Plugin Development' started by WaffleMast3r, Jan 10, 2020.

  1. HI, I want to ask which is the best way to color the message, because for new, if it has more than one lines, the new line resets it's color. I was thinking about coloring every character or every word... or is there a better way of doing so? [​IMG] https://imgur.com/Lu6bZ1b
     
  2. setColor() on each BaseComponent (not specific to 1.15.1).
     
  3. This is what I do now, and that is what I've got.... and I can't choose multiple versions...
    Code (Text):

    public BaseComponent[] build() {
        Pair<ComponentBuilder, String> componentBuilderPair = LanguageManager.escapePrefix(language, contents.text());

        ComponentBuilder componentBuilder = componentBuilderPair.getKey();
        String text = componentBuilderPair.getValue();

        if (contents.replacements() == null) {
            componentBuilder.append(LanguageManager.color(language, text));
        } else {
            ComponentBuilder process;
            for (int i = 0; i < contents.replacements().size(); i++) {
                Replacement r = contents.replacements().get(i);
                if (!text.contains(r.getReplacement())) {
                    continue;
                }
                process = componentBuilder;
                String before = text.substring(0, text.indexOf(r.getReplacement()));
                String after = text.substring(text.indexOf(r.getReplacement()) + r.getReplacement().split("").length);
               
                text = text.substring(before.split("").length + r.getReplacement().split("").length);

                process.append(convert(before)).reset();

                if (r.getClickEvent() != null && r.getHoverEvent() != null) {
                    process.append(convert(r.getText())).event(
                            new ClickEvent(ClickEvent.Action.valueOf(r.getClickEvent().getAction()), convert(r.getClickEvent().getValue()))
                    ).event(
                            new HoverEvent(HoverEvent.Action.valueOf(r.getHoverEvent().getAction()), TextComponent.fromLegacyText(convert(r.getHoverEvent().getValue()), ChatColor.GRAY))
                    );
                } else if (r.getClickEvent() != null && r.getHoverEvent() == null) {
                    process.append(convert(r.getText())).event(
                            new ClickEvent(ClickEvent.Action.valueOf(r.getClickEvent().getAction()), convert(r.getClickEvent().getValue()))
                    );
                } else if (r.getClickEvent() == null && r.getHoverEvent() != null) {
                    process.append(convert(r.getText())).event(
                            new HoverEvent(HoverEvent.Action.valueOf(r.getHoverEvent().getAction()), TextComponent.fromLegacyText(convert(r.getHoverEvent().getValue()), ChatColor.GRAY))
                    );
                } else if (r.getClickEvent() == null && r.getHoverEvent() == null) {
                    process.append(convert(r.getText()));
                }

                if (i == contents.replacements().size() - 1) {
                    process.append(convert(after)).reset();
                }
                componentBuilder = process;
            }
        }
        return componentBuilder.create();
    }

    private String convert(String str) {
        for (String key : placeholders.keySet()) {
            str = str.replace(key, placeholders.get(key).getReplacer());
        }
        return LanguageManager.color(language, str);
    }