From 17bbb2085364dd29aa2faaa7d43e17bb7ac041b8 Mon Sep 17 00:00:00 2001 From: Irgendwer01 <67506320+Irgendwer01@users.noreply.github.com> Date: Sat, 17 Feb 2024 23:46:03 +0100 Subject: [PATCH 1/5] Compact Messages --- .../config/UTConfigTweaks.java | 5 ++ .../universaltweaks/core/UTLoadingPlugin.java | 1 + .../tweaks/misc/chat/TextComponentCount.java | 36 ++++++++ .../chat/mixin/UTCompactMessageMixin.java | 89 +++++++++++++++++++ ...ixins.tweaks.misc.chat.compactmessage.json | 9 ++ 5 files changed, 140 insertions(+) create mode 100644 src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/TextComponentCount.java create mode 100644 src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java create mode 100644 src/main/resources/mixins.tweaks.misc.chat.compactmessage.json diff --git a/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java b/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java index de5cade6..ee03f474 100644 --- a/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java +++ b/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java @@ -1195,6 +1195,11 @@ public static class MiscCategory @Config.Comment("Enhance the vanilla 'Open to LAN' GUI for listening port customization, removal of enforced authentication and more") public boolean utLANServerProperties = true; + @Config.RequiresMcRestart + @Config.Name("Compact Messages") + @Config.Comment("Removes duplicate messages and instead put a number behind the message how often it was repeated") + public boolean utCompactMessagesToggle = true; + @Config.RequiresMcRestart @Config.Name("Linear XP Amount") @Config.Comment diff --git a/src/main/java/mod/acgaming/universaltweaks/core/UTLoadingPlugin.java b/src/main/java/mod/acgaming/universaltweaks/core/UTLoadingPlugin.java index aadf2111..ea79c885 100644 --- a/src/main/java/mod/acgaming/universaltweaks/core/UTLoadingPlugin.java +++ b/src/main/java/mod/acgaming/universaltweaks/core/UTLoadingPlugin.java @@ -246,6 +246,7 @@ public List getMixinConfigs() configs.add("mixins.tweaks.world.chunks.gen.json"); configs.add("mixins.tweaks.world.loading.server.json"); configs.add("mixins.tweaks.world.sealevel.json"); + configs.add("mixins.tweaks.misc.chat.compactmessage.json"); return configs; } diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/TextComponentCount.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/TextComponentCount.java new file mode 100644 index 00000000..17d5d292 --- /dev/null +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/TextComponentCount.java @@ -0,0 +1,36 @@ +package mod.acgaming.universaltweaks.tweaks.misc.chat; + +import net.minecraft.util.text.*; + +public class TextComponentCount extends TextComponentBase { + + private final int count; + + public TextComponentCount(int count) { + this.count = count; + } + + @Override + public ITextComponent setStyle(Style style) { + style.setColor(TextFormatting.GRAY); + return super.setStyle(style); + } + + @Override + public String getUnformattedComponentText() { + return String.format(" (%s)", this.count); + } + + @Override + public TextComponentCount createCopy() { + TextComponentCount textComponentCount = new TextComponentCount(this.count); + textComponentCount.setStyle(this.getStyle().createShallowCopy()); + + for (ITextComponent itextcomponent : this.getSiblings()) + { + textComponentCount.appendSibling(itextcomponent.createCopy()); + } + + return textComponentCount; + } +} diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java new file mode 100644 index 00000000..e3ce74c2 --- /dev/null +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java @@ -0,0 +1,89 @@ +package mod.acgaming.universaltweaks.tweaks.misc.chat.mixin; + +import mod.acgaming.universaltweaks.config.UTConfigTweaks; +import mod.acgaming.universaltweaks.tweaks.misc.chat.TextComponentCount; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.ChatLine; +import net.minecraft.client.gui.GuiNewChat; +import net.minecraft.client.gui.GuiUtilRenderComponents; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.text.ITextComponent; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +@Mixin(GuiNewChat.class) +public abstract class UTCompactMessageMixin { + + @Unique + private Pattern universalTweaks$matchPattern = Pattern.compile("§7+\\s+\\(+\\d+\\)"); + + @Shadow + @Final + private List drawnChatLines = new ArrayList<>(); + + @Shadow + @Final + private List chatLines = new ArrayList<>(); + + @Shadow @Final private Minecraft mc; + + @Shadow public abstract float getChatScale(); + + @Shadow public abstract int getChatWidth(); + + @Inject(method = "printChatMessageWithOptionalDeletion", at = @At("HEAD")) + public void utCompactMessage(ITextComponent chatComponent, int chatLineId, CallbackInfo ci) { + if (!UTConfigTweaks.MISC.utCompactMessagesToggle) return; + int count = 1; + int chatSize = MathHelper.floor((float)this.getChatWidth() / this.getChatScale()); + List splittedText = GuiUtilRenderComponents.splitText(chatComponent, chatSize, this.mc.fontRenderer, false, false); + ITextComponent textComponent = splittedText.get(splittedText.size()-1); + for (int i = 0; i < drawnChatLines.size(); i++) { + ChatLine chatLine = drawnChatLines.get(i); + if (universalTweaks$isMessageEqual(chatLine.getChatComponent().createCopy(), textComponent.createCopy())) { + if (!chatLine.getChatComponent().getSiblings().isEmpty()) { + for (ITextComponent sibling : chatLine.getChatComponent().getSiblings()) { + if (universalTweaks$matchPattern.matcher(sibling.getUnformattedComponentText()).matches()) { + count += Integer.parseInt(sibling.getUnformattedComponentText().replaceAll("(?:§7)?\\D?", "")); + break; + } + } + } + this.drawnChatLines.removeIf(chatLine1 -> splittedText.contains(chatLine1.getChatComponent()) || chatLine1.equals(chatLine)); + this.chatLines.removeIf(chatLine1 -> splittedText.contains(chatLine1.getChatComponent()) || chatLine1.equals(chatLine)); + chatComponent.appendSibling(new TextComponentCount(count)); + break; + } + } + } + + @Unique + private boolean universalTweaks$isMessageEqual(ITextComponent msg1, ITextComponent msg2) { + boolean isEqual = false; + if (msg1.equals(msg2) || msg1.getUnformattedText().equals(msg2.getUnformattedText())) { + isEqual = true; + } else { + if (!msg1.getSiblings().isEmpty()) { + for (int i = 0; i < msg1.getSiblings().size(); i++) { + ITextComponent sibling = msg1.getSiblings().get(i); + if (universalTweaks$matchPattern.matcher(sibling.getUnformattedComponentText()).matches()) { + msg1.getSiblings().remove(sibling); + } + } + if (msg1.equals(msg2) || msg1.getUnformattedText().equals(msg2.getUnformattedText())) { + isEqual = true; + } + } + } + return isEqual; + } +} diff --git a/src/main/resources/mixins.tweaks.misc.chat.compactmessage.json b/src/main/resources/mixins.tweaks.misc.chat.compactmessage.json new file mode 100644 index 00000000..9d33e37f --- /dev/null +++ b/src/main/resources/mixins.tweaks.misc.chat.compactmessage.json @@ -0,0 +1,9 @@ +{ +"package": "mod.acgaming.universaltweaks.tweaks.misc.chat.mixin", +"refmap": "universaltweaks.refmap.json", +"minVersion": "0.8", +"compatibilityLevel": "JAVA_8", +"client": [ + "UTCompactMessageMixin" +] +} \ No newline at end of file From a8b38834e0fe28b2e001ff388561eda30f7bc273 Mon Sep 17 00:00:00 2001 From: Irgendwer01 <67506320+Irgendwer01@users.noreply.github.com> Date: Sun, 18 Feb 2024 03:03:04 +0100 Subject: [PATCH 2/5] Remove Redundant TextComponentCount --- .../tweaks/misc/chat/TextComponentCount.java | 36 ------------------- .../chat/mixin/UTCompactMessageMixin.java | 3 +- 2 files changed, 1 insertion(+), 38 deletions(-) delete mode 100644 src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/TextComponentCount.java diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/TextComponentCount.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/TextComponentCount.java deleted file mode 100644 index 17d5d292..00000000 --- a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/TextComponentCount.java +++ /dev/null @@ -1,36 +0,0 @@ -package mod.acgaming.universaltweaks.tweaks.misc.chat; - -import net.minecraft.util.text.*; - -public class TextComponentCount extends TextComponentBase { - - private final int count; - - public TextComponentCount(int count) { - this.count = count; - } - - @Override - public ITextComponent setStyle(Style style) { - style.setColor(TextFormatting.GRAY); - return super.setStyle(style); - } - - @Override - public String getUnformattedComponentText() { - return String.format(" (%s)", this.count); - } - - @Override - public TextComponentCount createCopy() { - TextComponentCount textComponentCount = new TextComponentCount(this.count); - textComponentCount.setStyle(this.getStyle().createShallowCopy()); - - for (ITextComponent itextcomponent : this.getSiblings()) - { - textComponentCount.appendSibling(itextcomponent.createCopy()); - } - - return textComponentCount; - } -} diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java index e3ce74c2..b3e19c27 100644 --- a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java @@ -1,7 +1,6 @@ package mod.acgaming.universaltweaks.tweaks.misc.chat.mixin; import mod.acgaming.universaltweaks.config.UTConfigTweaks; -import mod.acgaming.universaltweaks.tweaks.misc.chat.TextComponentCount; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.ChatLine; import net.minecraft.client.gui.GuiNewChat; @@ -60,7 +59,7 @@ public void utCompactMessage(ITextComponent chatComponent, int chatLineId, Callb } this.drawnChatLines.removeIf(chatLine1 -> splittedText.contains(chatLine1.getChatComponent()) || chatLine1.equals(chatLine)); this.chatLines.removeIf(chatLine1 -> splittedText.contains(chatLine1.getChatComponent()) || chatLine1.equals(chatLine)); - chatComponent.appendSibling(new TextComponentCount(count)); + chatComponent.appendSibling(new TextComponentString(" (" + count + ")").setStyle(new Style().setColor(TextFormatting.GRAY))); break; } } From f916ff7a61a434b09568ac55845e1bdd6364c289 Mon Sep 17 00:00:00 2001 From: Irgendwer01 <67506320+Irgendwer01@users.noreply.github.com> Date: Sun, 18 Feb 2024 03:03:38 +0100 Subject: [PATCH 3/5] Fix pattern not matching when colored chat is disabled --- .../tweaks/misc/chat/mixin/UTCompactMessageMixin.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java index b3e19c27..7146eee0 100644 --- a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java @@ -7,6 +7,9 @@ import net.minecraft.client.gui.GuiUtilRenderComponents; import net.minecraft.util.math.MathHelper; import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.Style; +import net.minecraft.util.text.TextComponentString; +import net.minecraft.util.text.TextFormatting; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -23,7 +26,7 @@ public abstract class UTCompactMessageMixin { @Unique - private Pattern universalTweaks$matchPattern = Pattern.compile("§7+\\s+\\(+\\d+\\)"); + private Pattern universalTweaks$matchPattern = Pattern.compile("(?:§7)?+\\s+\\(+\\d+\\)"); @Shadow @Final From 24d8295e173efeb10173939db15e1ed2ff2c89ce Mon Sep 17 00:00:00 2001 From: Irgendwer01 <67506320+Irgendwer01@users.noreply.github.com> Date: Sun, 18 Feb 2024 15:08:33 +0100 Subject: [PATCH 4/5] Change Injection target to setChatLine --- .../tweaks/misc/chat/mixin/UTCompactMessageMixin.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java index 7146eee0..d67fca49 100644 --- a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java @@ -42,8 +42,8 @@ public abstract class UTCompactMessageMixin { @Shadow public abstract int getChatWidth(); - @Inject(method = "printChatMessageWithOptionalDeletion", at = @At("HEAD")) - public void utCompactMessage(ITextComponent chatComponent, int chatLineId, CallbackInfo ci) { + @Inject(method = "setChatLine", at = @At("HEAD")) + public void utCompactMessage(ITextComponent chatComponent, int chatLineId, int updateCounter, boolean displayOnly, CallbackInfo ci) { if (!UTConfigTweaks.MISC.utCompactMessagesToggle) return; int count = 1; int chatSize = MathHelper.floor((float)this.getChatWidth() / this.getChatScale()); From 1d50ec84124250e3cdf769d0d0090a6479057c7a Mon Sep 17 00:00:00 2001 From: Irgendwer01 <67506320+Irgendwer01@users.noreply.github.com> Date: Sun, 18 Feb 2024 15:09:07 +0100 Subject: [PATCH 5/5] Fix chatLines list not being cleared properly --- .../tweaks/misc/chat/mixin/UTCompactMessageMixin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java index d67fca49..3eca7999 100644 --- a/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java @@ -61,7 +61,7 @@ public void utCompactMessage(ITextComponent chatComponent, int chatLineId, int u } } this.drawnChatLines.removeIf(chatLine1 -> splittedText.contains(chatLine1.getChatComponent()) || chatLine1.equals(chatLine)); - this.chatLines.removeIf(chatLine1 -> splittedText.contains(chatLine1.getChatComponent()) || chatLine1.equals(chatLine)); + this.chatLines.removeIf(chatLine1 -> chatLine1.getChatComponent().getUnformattedComponentText().equals(chatComponent.getUnformattedComponentText())); chatComponent.appendSibling(new TextComponentString(" (" + count + ")").setStyle(new Style().setColor(TextFormatting.GRAY))); break; }