Skip to content

Commit 5acee0e

Browse files
committed
Revert "Various chat head bug fixes"
This reverts commit 50dec3b.
1 parent 59210b3 commit 5acee0e

File tree

9 files changed

+81
-219
lines changed

9 files changed

+81
-219
lines changed

src/main/java/org/polyfrost/chatting/hook/ChatLineHook.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@
88

99
public interface ChatLineHook {
1010
HashSet<WeakReference<ChatLine>> chatLines = new HashSet<>();
11-
boolean isDetected();
12-
void setDetected(boolean detected);
11+
boolean hasDetected();
1312
NetworkPlayerInfo getPlayerInfo();
14-
void setPlayerInfo(NetworkPlayerInfo playerInfo);
15-
NetworkPlayerInfo getDetectedPlayerInfo();
16-
void setDetectedPlayerInfo(NetworkPlayerInfo detectedPlayerInfo);
17-
boolean isFirstDetection();
18-
void setFirstDetection(boolean firstDetection);
1913

2014
void updatePlayerInfo();
2115

src/main/java/org/polyfrost/chatting/mixin/ChatLineMixin.java

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,28 @@
77

88
import org.polyfrost.chatting.config.ChattingConfig;
99
import org.polyfrost.chatting.hook.ChatLineHook;
10+
import net.minecraft.client.Minecraft;
1011
import net.minecraft.client.gui.ChatLine;
12+
import net.minecraft.client.network.NetHandlerPlayClient;
1113
import net.minecraft.client.network.NetworkPlayerInfo;
1214
import net.minecraft.util.IChatComponent;
13-
import org.polyfrost.chatting.utils.ChatHeadHooks;
15+
import org.jetbrains.annotations.Nullable;
1416
import org.spongepowered.asm.mixin.Mixin;
1517
import org.spongepowered.asm.mixin.injection.At;
1618
import org.spongepowered.asm.mixin.injection.Inject;
1719
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1820

1921
import java.lang.ref.WeakReference;
22+
import java.util.HashMap;
23+
import java.util.Map;
2024

2125
@Mixin(ChatLine.class)
2226
public class ChatLineMixin implements ChatLineHook {
2327
private boolean detected = false;
24-
private boolean firstDetection = true;
28+
private boolean first = true;
2529
private NetworkPlayerInfo playerInfo;
2630
private NetworkPlayerInfo detectedPlayerInfo;
31+
private static NetworkPlayerInfo lastPlayerInfo;
2732
private static long lastUniqueId = 0;
2833
private long uniqueId = 0;
2934

@@ -32,52 +37,70 @@ private void onInit(int i, IChatComponent iChatComponent, int j, CallbackInfo ci
3237
lastUniqueId++;
3338
uniqueId = lastUniqueId;
3439
chatLines.add(new WeakReference<>((ChatLine) (Object) this));
35-
ChatHeadHooks.INSTANCE.detect(iChatComponent.getFormattedText(), (ChatLine) (Object) this);
36-
}
37-
38-
@Override
39-
public boolean isDetected() {
40-
return detected;
41-
}
42-
43-
@Override
44-
public void setDetected(boolean detected) {
45-
this.detected = detected;
46-
}
47-
48-
@Override
49-
public NetworkPlayerInfo getPlayerInfo() {
50-
return playerInfo;
40+
NetHandlerPlayClient netHandler = Minecraft.getMinecraft().getNetHandler();
41+
if (netHandler == null) return;
42+
Map<String, NetworkPlayerInfo> nicknameCache = new HashMap<>();
43+
try {
44+
for (String word : iChatComponent.getFormattedText().split("(§.)|\\W")) {
45+
if (word.isEmpty()) continue;
46+
playerInfo = netHandler.getPlayerInfo(word);
47+
if (playerInfo == null) {
48+
playerInfo = getPlayerFromNickname(word, netHandler, nicknameCache);
49+
}
50+
if (playerInfo != null) {
51+
detectedPlayerInfo = playerInfo;
52+
detected = true;
53+
if (playerInfo == lastPlayerInfo) {
54+
first = false;
55+
if (ChattingConfig.INSTANCE.getHideChatHeadOnConsecutiveMessages()) {
56+
playerInfo = null;
57+
}
58+
} else {
59+
lastPlayerInfo = playerInfo;
60+
}
61+
break;
62+
}
63+
}
64+
} catch (Exception ignored) {
65+
}
5166
}
5267

53-
@Override
54-
public void setPlayerInfo(NetworkPlayerInfo playerInfo) {
55-
this.playerInfo = playerInfo;
56-
}
68+
@Nullable
69+
private static NetworkPlayerInfo getPlayerFromNickname(String word, NetHandlerPlayClient connection, Map<String, NetworkPlayerInfo> nicknameCache) {
70+
if (nicknameCache.isEmpty()) {
71+
for (NetworkPlayerInfo p : connection.getPlayerInfoMap()) {
72+
IChatComponent displayName = p.getDisplayName();
73+
if (displayName != null) {
74+
String nickname = displayName.getUnformattedTextForChat();
75+
if (word.equals(nickname)) {
76+
nicknameCache.clear();
77+
return p;
78+
}
5779

58-
@Override
59-
public NetworkPlayerInfo getDetectedPlayerInfo() {
60-
return detectedPlayerInfo;
61-
}
80+
nicknameCache.put(nickname, p);
81+
}
82+
}
83+
} else {
84+
// use prepared cache
85+
return nicknameCache.get(word);
86+
}
6287

63-
@Override
64-
public void setDetectedPlayerInfo(NetworkPlayerInfo detectedPlayerInfo) {
65-
this.detectedPlayerInfo = detectedPlayerInfo;
88+
return null;
6689
}
6790

6891
@Override
69-
public boolean isFirstDetection() {
70-
return firstDetection;
92+
public boolean hasDetected() {
93+
return detected;
7194
}
7295

7396
@Override
74-
public void setFirstDetection(boolean firstDetection) {
75-
this.firstDetection = firstDetection;
97+
public NetworkPlayerInfo getPlayerInfo() {
98+
return playerInfo;
7699
}
77100

78101
@Override
79102
public void updatePlayerInfo() {
80-
if (ChattingConfig.INSTANCE.getHideChatHeadOnConsecutiveMessages() && !firstDetection) {
103+
if (ChattingConfig.INSTANCE.getHideChatHeadOnConsecutiveMessages() && !first) {
81104
playerInfo = null;
82105
} else {
83106
playerInfo = detectedPlayerInfo;

src/main/java/org/polyfrost/chatting/mixin/GuiChatMixin.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.polyfrost.chatting.mixin;
22

33
import cc.polyfrost.oneconfig.libs.universal.UDesktop;
4-
import cc.polyfrost.oneconfig.libs.universal.UResolution;
54
import org.polyfrost.chatting.chat.*;
65
import org.polyfrost.chatting.config.ChattingConfig;
76
import org.polyfrost.chatting.gui.components.ClearButton;
@@ -113,15 +112,6 @@ private int modifyInputBoxColor(int color) {
113112
return ChattingConfig.INSTANCE.getInputBoxBackgroundColor().getRGB();
114113
}
115114

116-
@ModifyArg(method = "drawScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;getChatComponent(II)Lnet/minecraft/util/IChatComponent;"), index = 0)
117-
private int modifyChatComponentX(int x) {
118-
if (ChattingConfig.INSTANCE.getShowChatHeads()) {
119-
return x - (10 * (int) UResolution.getScaleFactor());
120-
} else {
121-
return x;
122-
}
123-
}
124-
125115
@Inject(method = "mouseClicked", at = @At("HEAD"))
126116
private void mouseClicked(int mouseX, int mouseY, int mouseButton, CallbackInfo ci) {
127117
GuiNewChatHook hook = ((GuiNewChatHook) Minecraft.getMinecraft().ingameGUI.getChatGUI());
@@ -146,15 +136,6 @@ private void mouseClicked(int mouseX, int mouseY, int mouseButton, CallbackInfo
146136

147137
}
148138

149-
@ModifyArg(method = "mouseClicked", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;getChatComponent(II)Lnet/minecraft/util/IChatComponent;"), index = 0)
150-
private int modifyChatComponentX2(int x) {
151-
if (ChattingConfig.INSTANCE.getShowChatHeads()) {
152-
return x - (10 * (int) UResolution.getScaleFactor());
153-
} else {
154-
return x;
155-
}
156-
}
157-
158139
@ModifyArg(method = "keyTyped", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiChat;sendChatMessage(Ljava/lang/String;)V"), index = 0)
159140
private String modifySentMessage(String original) {
160141
if (ChattingConfig.INSTANCE.getChatShortcuts()) {

src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package org.polyfrost.chatting.mixin;
22

33
import cc.polyfrost.oneconfig.libs.universal.UMouse;
4-
import cc.polyfrost.oneconfig.libs.universal.UResolution;
54
import cc.polyfrost.oneconfig.utils.Notifications;
6-
import net.minecraft.util.IChatComponent;
75
import org.polyfrost.chatting.Chatting;
86
import org.polyfrost.chatting.chat.ChatSearchingManager;
97
import org.polyfrost.chatting.config.ChattingConfig;
10-
import org.polyfrost.chatting.hook.ChatLineHook;
118
import org.polyfrost.chatting.hook.GuiNewChatHook;
129
import org.polyfrost.chatting.utils.ModCompatHooks;
1310
import org.polyfrost.chatting.utils.RenderUtils;
@@ -23,7 +20,6 @@
2320
import org.spongepowered.asm.mixin.Unique;
2421
import org.spongepowered.asm.mixin.injection.*;
2522
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
26-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2723
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;
2824

2925
import java.awt.datatransfer.StringSelection;
@@ -165,44 +161,6 @@ private void checkStuff(int j2, CallbackInfo ci) {
165161
}
166162
}
167163

168-
@Unique
169-
private boolean chatting$cancelChatComponent = false;
170-
@Unique
171-
private int chatting$lastMouseX = 0;
172-
@Unique
173-
private int chatting$lastMouseY = 0;
174-
175-
@Inject(method = "getChatComponent", at = @At(value = "INVOKE", target = "Ljava/util/List;get(I)Ljava/lang/Object;"))
176-
private void storeMouseXAndY(int mouseX, int mouseY, CallbackInfoReturnable<IChatComponent> cir) {
177-
chatting$lastMouseX = mouseX;
178-
chatting$lastMouseY = mouseY;
179-
}
180-
181-
@ModifyVariable(method = "getChatComponent", at = @At("STORE"), ordinal = 0)
182-
private ChatLine storeChatLine(ChatLine line) {
183-
if (ChattingConfig.INSTANCE.getShowChatHeads() && !((ChatLineHook) line).isDetected() && !ChattingConfig.INSTANCE.getOffsetNonPlayerMessages()) {
184-
int i = (int) UResolution.getScaleFactor();
185-
float f = this.getChatScale();
186-
int j = chatting$lastMouseX / i - 3;
187-
int k = chatting$lastMouseY / i - 27;
188-
j = MathHelper.floor_float((float)j / f);
189-
k = MathHelper.floor_float((float)k / f);
190-
int l = Math.min(this.getLineCount(), this.drawnChatLines.size());
191-
if (j > MathHelper.floor_float((float)this.getChatWidth() / this.getChatScale()) && k < this.mc.fontRendererObj.FONT_HEIGHT * l + l) {
192-
chatting$cancelChatComponent = true;
193-
}
194-
}
195-
return line;
196-
}
197-
198-
@Inject(method = "getChatComponent", at = @At(value = "INVOKE", target = "Ljava/util/List;get(I)Ljava/lang/Object;", shift = At.Shift.AFTER), cancellable = true)
199-
private void cancelChatComponent(int mouseX, int mouseY, CallbackInfoReturnable<IChatComponent> cir) {
200-
if (chatting$cancelChatComponent) {
201-
cir.setReturnValue(null);
202-
chatting$cancelChatComponent = false;
203-
}
204-
}
205-
206164
@Override
207165
public int getRight() {
208166
return chatting$right;

src/main/java/org/polyfrost/chatting/mixin/GuiUtilRenderComponentsMixin.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/kotlin/org/polyfrost/chatting/Chatting.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ object Chatting {
245245
}
246246

247247
val fr: FontRenderer = ModCompatHooks.fontRenderer
248-
val width = messages.maxOf { fr.getStringWidth(it.value) + (if (ChattingConfig.showChatHeads && ((it.key as ChatLineHook).isDetected || ChattingConfig.offsetNonPlayerMessages)) 10 else 0) } + 4
248+
val width = messages.maxOf { fr.getStringWidth(it.value) + (if (ChattingConfig.showChatHeads && ((it.key as ChatLineHook).hasDetected() || ChattingConfig.offsetNonPlayerMessages)) 10 else 0) } + 4
249249
val fb: Framebuffer = createBindFramebuffer(width * 2, (messages.size * 9) * 2)
250250
val file = File(Minecraft.getMinecraft().mcDataDir, "screenshots/chat/" + fileFormatter.format(Date()))
251251

src/main/kotlin/org/polyfrost/chatting/utils/ChatHeadHooks.kt

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/main/kotlin/org/polyfrost/chatting/utils/ModCompatHooks.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ object ModCompatHooks {
5858
var actualX = x
5959
if (showChatHeads && !screenshot) {
6060
val hook = chatLine as ChatLineHook
61-
if (hook.isDetected || offsetNonPlayerMessages) {
61+
if (hook.hasDetected() || offsetNonPlayerMessages) {
6262
actualX += 10f
6363
}
6464
val networkPlayerInfo = hook.playerInfo

0 commit comments

Comments
 (0)