From 8de4ce1e12e2a96cb600c2cd132226afe0e67c1e Mon Sep 17 00:00:00 2001 From: MrJeremyFisher <63616270+MrJeremyFisher@users.noreply.github.com> Date: Mon, 14 Aug 2023 17:50:34 -0400 Subject: [PATCH] Fix messages with links breaking --- .../chat/MessageDispatcher.java | 43 +++++++------------ 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/src/main/java/io/github/darkkronicle/advancedchatcore/chat/MessageDispatcher.java b/src/main/java/io/github/darkkronicle/advancedchatcore/chat/MessageDispatcher.java index ef98c3e..9a2c5d8 100644 --- a/src/main/java/io/github/darkkronicle/advancedchatcore/chat/MessageDispatcher.java +++ b/src/main/java/io/github/darkkronicle/advancedchatcore/chat/MessageDispatcher.java @@ -9,17 +9,7 @@ import io.github.darkkronicle.advancedchatcore.interfaces.IMessageFilter; import io.github.darkkronicle.advancedchatcore.interfaces.IMessageProcessor; -import io.github.darkkronicle.advancedchatcore.util.FindType; -import io.github.darkkronicle.advancedchatcore.util.SearchResult; -import io.github.darkkronicle.advancedchatcore.util.SearchUtils; -import io.github.darkkronicle.advancedchatcore.util.StringInsert; -import io.github.darkkronicle.advancedchatcore.util.StringMatch; -import io.github.darkkronicle.advancedchatcore.util.StyleFormatter; -import io.github.darkkronicle.advancedchatcore.util.TextUtil; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; +import io.github.darkkronicle.advancedchatcore.util.*; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.hud.MessageIndicator; @@ -30,6 +20,11 @@ import org.apache.logging.log4j.LogManager; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + /** * A class to handle chat events. * @@ -54,25 +49,18 @@ private MessageDispatcher() { registerPreFilter( text -> { String string = text.getString(); - if (string.isEmpty()) { - return Optional.empty(); - } - SearchResult search = - SearchResult.searchOf( - string, - "(http(s)?:\\/\\/.)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&\\/=]*)", - FindType.REGEX); - if (search.size() == 0) { - return Optional.empty(); - } + if (string.isEmpty()) return Optional.empty(); + + SearchResult search = SearchResult.searchOf(string, "(http(s)?:\\/\\/.)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&\\/=]*)", FindType.REGEX); + if (search.size() == 0) return Optional.empty(); + Map insert = new HashMap<>(); for (StringMatch match : search.getMatches()) { insert.put( match, (current, match1) -> { String url = match1.match; - if (!SearchUtils.isMatch( - match1.match, "(http(s)?:\\/\\/.)", FindType.REGEX)) { + if (!SearchUtils.isMatch(match1.match, "(http(s)?:\\/\\/.)", FindType.REGEX)) { url = "https://" + url; } if (current.getStyle().getClickEvent() == null) { @@ -81,7 +69,6 @@ private MessageDispatcher() { return MutableText.of(current.getContent()).fillStyle(current.getStyle()); }); } - text = TextUtil.replaceStrings(text, insert); return Optional.of(text); }, -1); @@ -145,7 +132,7 @@ private void process(Text text, @Nullable MessageSignatureData signature, @Nulla * registerProcess * * @param processor IMessageFilter to modify text - * @param index Index to add it. Supplying a negative value will put it at the end. + * @param index Index to add it. Supplying a negative value will put it at the end. */ public void registerPreFilter(IMessageFilter processor, int index) { if (index < 0) { @@ -161,8 +148,8 @@ public void registerPreFilter(IMessageFilter processor, int index) { * preprocessed. * * @param processor IMessageProcessor to get called back - * @param index Index that it will be added to. Supplying a negative value will put it at the - * end. + * @param index Index that it will be added to. Supplying a negative value will put it at the + * end. */ public void register(IMessageProcessor processor, int index) { if (index < 0) {