Skip to content
This repository has been archived by the owner on Jan 2, 2022. It is now read-only.

Commit

Permalink
fix: resolve Short Channel Names overwriting all Click/Hover Events (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff authored Jul 21, 2021
1 parent adbd3f3 commit 993aa6e
Showing 1 changed file with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import club.sk1er.hytilities.handlers.language.LanguageData;
import club.sk1er.hytilities.handlers.lobby.limbo.LimboLimiter;
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.world.WorldEvent;
Expand Down Expand Up @@ -83,17 +84,17 @@ public void onMessageReceived(@NotNull ClientChatReceivedEvent event) {
Matcher friendMatcher = language.chatRestylerFriendPatternRegex.matcher(message);
Matcher officerMatcher = language.chatRestylerOfficerPatternRegex.matcher(message);
if (partyMatcher.find()) {
event.message = new ChatComponentText(message.replaceAll(language.chatRestylerPartyPatternRegex.pattern(),
partyMatcher.group(1) + "P " + partyMatcher.group(3)));
event.message = shortenChannelName(event.message, language.chatRestylerPartyPatternRegex.pattern(),
partyMatcher.group(1) + "P " + partyMatcher.group(3), false);
} else if (guildMatcher.find()) {
event.message = new ChatComponentText(message.replaceAll(language.chatRestylerGuildPatternRegex.pattern(),
guildMatcher.group(1) + "G >"));
event.message = shortenChannelName(event.message, language.chatRestylerGuildPatternRegex.pattern(),
guildMatcher.group(1) + "G >", true);
} else if (friendMatcher.find()) {
event.message = new ChatComponentText(message.replaceAll(language.chatRestylerFriendPatternRegex.pattern(),
friendMatcher.group(1) + "F >"));
event.message = shortenChannelName(event.message, language.chatRestylerFriendPatternRegex.pattern(),
friendMatcher.group(1) + "F >", true);
} else if (officerMatcher.find()) {
event.message = new ChatComponentText(message.replaceAll(language.chatRestylerOfficerPatternRegex.pattern(),
officerMatcher.group(1) + "O >"));
event.message = shortenChannelName(event.message, language.chatRestylerOfficerPatternRegex.pattern(),
officerMatcher.group(1) + "O >", false);
}
}

Expand Down Expand Up @@ -168,4 +169,25 @@ public void onMessageReceived(@NotNull ClientChatReceivedEvent event) {

Hytilities.INSTANCE.getChatHandler().fixStyling(event.message, siblings);
}

/**
* Handles the replacement of channel names
* Loops through all siblings to find a replacement
*
* @param message The message being modified
* @param pattern The regular expression to check the message and it's components against
* @param replacement The text that replaces what is matched by the regular expression
* @param checkParentComponent Whether or not to check the parent chat component
*/
private ChatComponentText shortenChannelName(IChatComponent message, String pattern, String replacement, boolean checkParentComponent) {
String originalText = message.getUnformattedTextForChat();
if (checkParentComponent && !originalText.contains("\u00a7")) {
originalText = (message.getChatStyle().getFormattingCode() + originalText + EnumChatFormatting.RESET).replaceAll(pattern, replacement);
}
ChatComponentText copy = (ChatComponentText) new ChatComponentText(originalText).setChatStyle(message.getChatStyle());
for (IChatComponent sibling : message.getSiblings()) {
copy.appendSibling(new ChatComponentText(sibling.getUnformattedTextForChat().replaceAll(pattern, replacement)).setChatStyle(sibling.getChatStyle()));
}
return copy;
}
}

0 comments on commit 993aa6e

Please sign in to comment.