diff --git a/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java b/src/main/java/mod/acgaming/universaltweaks/config/UTConfigTweaks.java index eea03a25..b582b1ad 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 4e45c991..a583ada7 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"); configs.add("mixins.tweaks.world.village.json"); return configs; } 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..3eca7999 --- /dev/null +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/misc/chat/mixin/UTCompactMessageMixin.java @@ -0,0 +1,91 @@ +package mod.acgaming.universaltweaks.tweaks.misc.chat.mixin; + +import mod.acgaming.universaltweaks.config.UTConfigTweaks; +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 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; +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 = "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()); + 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 -> chatLine1.getChatComponent().getUnformattedComponentText().equals(chatComponent.getUnformattedComponentText())); + chatComponent.appendSibling(new TextComponentString(" (" + count + ")").setStyle(new Style().setColor(TextFormatting.GRAY))); + 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