aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/Danker/features/ColouredNames.java
blob: 478a47dd1362e9afaa74c1834c5f38083c703547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package me.Danker.features;

import me.Danker.DankersSkyblockMod;
import me.Danker.commands.ToggleCommand;
import me.Danker.utils.Utils;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ColouredNames {

    public static List<String> users = new ArrayList<>();

    @SubscribeEvent
    public void onChat(ClientChatReceivedEvent event) {
        if (!ToggleCommand.customColouredNames || !Utils.inSkyblock || event.type != 0) return;

        for (String user : users) {
            if (event.message.getFormattedText().contains(user)) {
                event.message = replaceChat(event.message, user);
            }
        }
    }

    @SubscribeEvent
    public void onTooltip(ItemTooltipEvent event) {
        if (!ToggleCommand.customColouredNames || !Utils.inSkyblock) return;

        for (String user : users) {
            for (int i = 0; i < event.toolTip.size(); i++) {
                if (event.toolTip.get(i).contains(user)) {
                    event.toolTip.set(i, replaceName(event.toolTip.get(i), user, getColourFromName(user)));
                }
            }
        }
    }

    // https://github.com/SteveKunG/SkyBlockcatia/blob/1.8.9/src/main/java/com/stevekung/skyblockcatia/utils/SupporterUtils.java#L53
    public static String replaceName(String text, String name, String colour) {
        String namePattern = "(?:(?:\\u00a7[0-9a-fbr])\\B(?:" + name + ")\\b)|(?:\\u00a7[rb]" + name + "\\u00a7r)|\\b" + name + "\\b";
        Matcher prevColourMat = Pattern.compile("(?:.*(?:(?<colour>\\u00a7[0-9a-fbr])" + name + ")\\b.*)").matcher(text);

        if (prevColourMat.matches()) {
            return text.replaceAll(namePattern, colour + name + prevColourMat.group("colour"));
        }
        return text.replaceAll(namePattern, colour + name + EnumChatFormatting.WHITE);
    }

    // https://github.com/Moulberry/Hychat/blob/master/src/main/java/io/github/moulberry/hychat/util/TextProcessing.java#L23
    public static IChatComponent replaceChat(IChatComponent component, String user) {
        IChatComponent newComponent;
        ChatComponentText text = (ChatComponentText) component;

        newComponent = new ChatComponentText(replaceName(text.getUnformattedTextForChat(), user, getColourFromName(user)));
        newComponent.setChatStyle(text.getChatStyle().createShallowCopy());

        for (IChatComponent sibling : text.getSiblings()) {
            newComponent.appendSibling(replaceChat(sibling, user));
        }

        return newComponent;
    }

    public static String getColourFromName(String name) {
        return "§" + DankersSkyblockMod.data.get("colourednames").getAsJsonObject().get(name).getAsString();
    }

}