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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package me.Danker.features;
import me.Danker.DankersSkyblockMod;
import me.Danker.commands.ToggleCommand;
import me.Danker.utils.Utils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityArmorStand;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.client.event.RenderLivingEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ColouredNames {
public static List<String> users = new ArrayList<>();
public static Map<String, String> nametags = new HashMap<>();
public static final EnumChatFormatting[] RAINBOW_COLOURS = new EnumChatFormatting[]{EnumChatFormatting.RED, EnumChatFormatting.GOLD, EnumChatFormatting.YELLOW, EnumChatFormatting.GREEN, EnumChatFormatting.AQUA, EnumChatFormatting.BLUE, EnumChatFormatting.DARK_PURPLE};
@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)));
}
}
}
}
@SubscribeEvent
public void onRenderNametag(PlayerEvent.NameFormat event) {
if (!ToggleCommand.customColouredNames || !Utils.inSkyblock) return;
if (users.contains(event.username)) {
event.displayname = replaceName(event.displayname, event.username, getColourFromName(event.username));
}
}
@SubscribeEvent(priority = EventPriority.LOW)
public void onRenderLiving(RenderLivingEvent.Specials.Pre<EntityLivingBase> event) {
if (!ToggleCommand.customColouredNames || !Utils.inSkyblock) return;
Entity entity = event.entity;
if (entity instanceof EntityArmorStand && !entity.isDead && entity.hasCustomName()) {
String name = entity.getCustomNameTag();
if (name.charAt(name.length() - 1) == '❤') return;
String id = entity.getUniqueID().toString();
String nametag = nametags.get(id);
if (name.equals(nametag)) {
entity.setCustomNameTag(nametag);
} else {
for (String user : users) {
if (name.contains(user)) {
name = replaceName(name, user, getColourFromName(user));
}
}
nametags.put(id, name);
entity.setCustomNameTag(name);
}
}
}
@SubscribeEvent
public void onWorldChange(WorldEvent.Load event) {
nametags.clear();
}
// 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()) {
if (colour.equals("§z")) {
StringBuilder rainbowName = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
rainbowName.append(RAINBOW_COLOURS[i % 7]).append(name.charAt(i));
}
return text.replaceAll(namePattern, rainbowName + prevColourMat.group("colour"));
}
return text.replaceAll(namePattern, colour + name + prevColourMat.group("colour"));
}
if (colour.equals("§z")) {
StringBuilder rainbowName = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
rainbowName.append(RAINBOW_COLOURS[i % 7]).append(name.charAt(i));
}
return text.replaceAll(namePattern, rainbowName.toString() + EnumChatFormatting.WHITE);
}
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();
}
}
|