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

fix: resolve "Cleaner Game Counter" breaking when changing chat width #80

Merged
merged 1 commit into from
Jun 8, 2023
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ org.gradle.jvmargs=-Xmx2G

mod_name = Hytils Reborn
mod_id = hytils-reborn
mod_version = 1.5.1-beta4
mod_version = 1.6.0-beta4

loom.platform = forge
7 changes: 7 additions & 0 deletions src/main/java/cc/woverflow/hytils/config/HytilsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import cc.polyfrost.oneconfig.config.data.PageLocation;
import cc.polyfrost.oneconfig.config.migration.VigilanceMigrator;
import cc.woverflow.hytils.HytilsReborn;
import cc.woverflow.hytils.handlers.chat.modules.modifiers.GameStartCompactor;
import cc.woverflow.hytils.util.DarkColorUtils;
import com.google.common.collect.Lists;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -1101,6 +1102,10 @@ public HytilsConfig() {
addDependency("chattingIntegration", "chatSwapper");
addDependency("hideAllChatMessage", "chatSwapper");

addListener("cleanerGameStartAnnouncements", () -> {
if (!cleanerGameStartAnnouncements) GameStartCompactor.lastMessage = null;
});

addDependency("playerCountBeforePlayerName", "gameStatusRestyle");
addDependency("playerCountOnPlayerLeave", "gameStatusRestyle");
addDependency("padPlayerCount", "gameStatusRestyle");
Expand Down Expand Up @@ -1145,13 +1150,15 @@ public void hideTabulous() {
hidePlayerRanksInTab = false;
hidePingInTab = false;
cleanerSkyblockTabInfo = false;
hideAdsInTab = false;
save();
addDependency("hideNpcsInTab", "Tabulous", () -> false);
addDependency("keepImportantNpcsInTab", "Tabulous", () -> false);
addDependency("hideGuildTagsInTab", "Tabulous", () -> false);
addDependency("hidePlayerRanksInTab", "Tabulous", () -> false);
addDependency("hidePingInTab", "Tabulous", () -> false);
addDependency("cleanerSkyblockTabInfo", "Tabulous", () -> false);
addDependency("hideAdsInTab", "Tabulous", () -> false);
}

private void setWBMessages() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import cc.woverflow.hytils.config.HytilsConfig;
import cc.woverflow.hytils.handlers.chat.ChatReceiveModule;
import cc.woverflow.hytils.mixin.GuiNewChatAccessor;
import com.google.common.collect.Lists;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ChatLine;
import net.minecraft.client.gui.GuiNewChat;
Expand All @@ -30,6 +31,7 @@
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import org.jetbrains.annotations.NotNull;

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

Expand All @@ -40,9 +42,7 @@ public int getPriority() {
return 7;
}

// Used to determine if the property has changed. TODO: Once ModCore 2 is out, have a listener for this.
private boolean prevConfigValue = HytilsConfig.cleanerGameStartAnnouncements;
private IChatComponent lastMessage = null;
public static IChatComponent lastMessage = null;

@Override
public void onMessageReceived(@NotNull ClientChatReceivedEvent event) {
Expand All @@ -51,32 +51,37 @@ public void onMessageReceived(@NotNull ClientChatReceivedEvent event) {
if (gameStartMatcher.matches() || (HytilsConfig.gameStatusRestyle && chatRestylerMatcher.matches())) {
if (lastMessage != null) {
final GuiNewChat chat = Minecraft.getMinecraft().ingameGUI.getChatGUI();
final List<IChatComponent> oldTimerLines = GuiUtilRenderComponents.splitText(lastMessage, MathHelper.floor_float((float) chat.getChatWidth() / chat.getChatScale()), Minecraft.getMinecraft().fontRendererObj, false, false);
final GuiNewChatAccessor accessor = ((GuiNewChatAccessor) chat);
for (int i = accessor.getDrawnChatLines().size() - 1; i >= 0; i--) { // tries to find the last message in drawn chat lines, if found, it removes them
final ChatLine drawnLine = accessor.getDrawnChatLines().get(i);
for (IChatComponent oldTimerLine : oldTimerLines) {
if (drawnLine.getChatComponent().getFormattedText().startsWith(oldTimerLine.getFormattedText())) {
accessor.getDrawnChatLines().remove(i);
oldTimerLines.remove(oldTimerLine);
break;
}
}
}
final List<IChatComponent> oldTimerLines = GuiUtilRenderComponents.splitText(lastMessage, MathHelper.floor_float((float) chat.getChatWidth() / chat.getChatScale()), Minecraft.getMinecraft().fontRendererObj, false, false);
removeChatLines(accessor.getChatLines(), Lists.newArrayList(lastMessage));
removeChatLines(accessor.getDrawnChatLines(), oldTimerLines);
}

lastMessage = event.message.createCopy();
}
}

@Override
public boolean isEnabled() {
// don't affect messages sent when the config value was false (or was true and was set to false later)
if (!prevConfigValue && HytilsConfig.cleanerGameStartAnnouncements) {
prevConfigValue = true;
lastMessage = null;
private void removeChatLines(List<ChatLine> currentLines, List<IChatComponent> oldTimerLines) {
Iterator<ChatLine> iterator = currentLines.iterator();
while (iterator.hasNext()) {
final ChatLine drawnLine = iterator.next();
if (drawnLine.getChatComponent().getFormattedText().contains(lastMessage.getFormattedText())) {
iterator.remove();
return;
}
for (IChatComponent oldTimerLine : oldTimerLines) {
if (drawnLine.getChatComponent().getFormattedText().contains(oldTimerLine.getFormattedText())) {
iterator.remove();
oldTimerLines.remove(oldTimerLine);
break;
}
}
if (oldTimerLines.isEmpty()) return;
}
}

@Override
public boolean isEnabled() {
return HytilsConfig.cleanerGameStartAnnouncements;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

@Mixin(GuiNewChat.class)
public interface GuiNewChatAccessor {
@Accessor("drawnChatLines")
@Accessor
List<ChatLine> getChatLines();

@Accessor
List<ChatLine> getDrawnChatLines();
}