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

Commit

Permalink
fix: resolve white chat stripping styling when modified
Browse files Browse the repository at this point in the history
  • Loading branch information
asbyth committed Apr 19, 2021
1 parent e43792c commit 528cf7e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
package club.sk1er.hytilities.handlers.chat;

import club.sk1er.hytilities.Hytilities;
import club.sk1er.hytilities.handlers.chat.modules.blockers.*;
import club.sk1er.hytilities.handlers.chat.modules.blockers.AdBlocker;
import club.sk1er.hytilities.handlers.chat.modules.blockers.ChatCleaner;
import club.sk1er.hytilities.handlers.chat.modules.blockers.ConnectedMessage;
import club.sk1er.hytilities.handlers.chat.modules.blockers.GuildMOTD;
import club.sk1er.hytilities.handlers.chat.modules.blockers.QuestBlocker;
import club.sk1er.hytilities.handlers.chat.modules.blockers.ShoutBlocker;
import club.sk1er.hytilities.handlers.chat.modules.events.AchievementEvent;
import club.sk1er.hytilities.handlers.chat.modules.events.LevelupEvent;
import club.sk1er.hytilities.handlers.chat.modules.modifiers.DefaultChatRestyler;
Expand All @@ -31,6 +36,8 @@
import club.sk1er.hytilities.handlers.chat.modules.triggers.ThankWatchdog;
import club.sk1er.hytilities.tweaker.asm.EntityPlayerSPTransformer;
import club.sk1er.mods.core.util.MinecraftUtils;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -128,4 +135,27 @@ public String handleSentMessage(@NotNull String message) {

return message;
}

/**
* Fixes styling when modifying a message's events.
* TODO: Improve documentation.
*
* @param component The message being modified & restored
* @param siblings The message's chat component siblings
*/
public void fixStyling(IChatComponent component, List<IChatComponent> siblings) {
if (!siblings.isEmpty()) {
for (IChatComponent sibling : siblings) {
final ChatStyle chatStyle = sibling.getChatStyle();

if (chatStyle.getChatHoverEvent() != null) {
component.getChatStyle().setChatHoverEvent(chatStyle.getChatHoverEvent());
}

if (chatStyle.getChatClickEvent() != null) {
component.getChatStyle().setChatClickEvent(chatStyle.getChatClickEvent());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package club.sk1er.hytilities.handlers.chat.modules.modifiers;

import club.sk1er.hytilities.Hytilities;
import club.sk1er.hytilities.config.HytilitiesConfig;
import club.sk1er.hytilities.handlers.chat.ChatReceiveModule;
import club.sk1er.hytilities.handlers.language.LanguageData;
Expand Down Expand Up @@ -162,19 +163,6 @@ public void onMessageReceived(@NotNull ClientChatReceivedEvent event) {
}
}

// fix siblings being removed from messages when editing them
if (!siblings.isEmpty()) {
for (IChatComponent sibling : siblings) {
final ChatStyle chatStyle = sibling.getChatStyle();

if (chatStyle.getChatHoverEvent() != null) {
event.message.getChatStyle().setChatHoverEvent(chatStyle.getChatHoverEvent());
}

if (chatStyle.getChatClickEvent() != null) {
event.message.getChatStyle().setChatClickEvent(chatStyle.getChatClickEvent());
}
}
}
Hytilities.INSTANCE.getChatHandler().fixStyling(event.message, siblings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

package club.sk1er.hytilities.handlers.chat.modules.modifiers;

import club.sk1er.hytilities.Hytilities;
import club.sk1er.hytilities.config.HytilitiesConfig;
import club.sk1er.hytilities.handlers.chat.ChatReceiveModule;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import org.jetbrains.annotations.NotNull;

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

public class WhiteChat implements ChatReceiveModule {
Expand All @@ -35,20 +38,30 @@ public int getPriority() {

@Override
public void onMessageReceived(@NotNull ClientChatReceivedEvent event) {
final String message = event.message.getFormattedText();
final List<IChatComponent> siblings = event.message.getSiblings();
boolean modified = false;

if (HytilitiesConfig.whitePrivateMessages) {
final Matcher matcher = getLanguage().privateMessageWhiteChatRegex.matcher(event.message.getFormattedText());
final Matcher matcher = getLanguage().privateMessageWhiteChatRegex.matcher(message);
if (matcher.find(0)) {
event.message = new ChatComponentText(matcher.group("type") + " " + matcher.group("prefix") + ": " + matcher.group("message").replace("§7", "§f"));
event.message = new ChatComponentText(
matcher.group("type") + " " + matcher.group("prefix") + ": " +
matcher.group("message").replace("§7", "§f")
);
modified = true;
}

return;
}

if (HytilitiesConfig.whiteChat) {
final Matcher matcher = getLanguage().whiteChatNonMessageRegex.matcher(event.message.getFormattedText());
final Matcher matcher = getLanguage().whiteChatNonMessageRegex.matcher(message);
if (matcher.find(0)) {
event.message = new ChatComponentText(matcher.group("prefix") + ": " + matcher.group("message"));
modified = true;
}
}

if (!modified) return;
Hytilities.INSTANCE.getChatHandler().fixStyling(event.message, siblings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void onWorldLoad(WorldEvent.Load event) {
tick = 0;
}

@SuppressWarnings("UnusedReturnValue")
public String onMessageSend(@NotNull String message) {
if (message.startsWith("/locraw") && !this.listening) {
this.playerSentCommand = true;
Expand Down

0 comments on commit 528cf7e

Please sign in to comment.