Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Compact Messages #381

Merged
merged 6 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public List<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ChatLine> drawnChatLines = new ArrayList<>();

@Shadow
@Final
private List<ChatLine> 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<ITextComponent> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"package": "mod.acgaming.universaltweaks.tweaks.misc.chat.mixin",
"refmap": "universaltweaks.refmap.json",
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"client": [
"UTCompactMessageMixin"
]
}
Loading