Skip to content

Commit

Permalink
clean up mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
Wyvest committed Jun 18, 2024
1 parent 0c0dbac commit 0b12cbc
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface ChatLineHook {

long chatting$getUniqueId();

ChatLine getFullMessage();
ChatLine chatting$getFullMessage();
}
70 changes: 40 additions & 30 deletions src/main/java/org/polyfrost/chatting/mixin/ChatLineMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.util.IChatComponent;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
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;
Expand All @@ -25,41 +26,49 @@

@Mixin(ChatLine.class)
public class ChatLineMixin implements ChatLineHook {
private boolean detected = false;
private boolean first = true;
private NetworkPlayerInfo playerInfo;
private NetworkPlayerInfo detectedPlayerInfo;
private static NetworkPlayerInfo lastPlayerInfo;
private static long lastUniqueId = 0;
private long uniqueId = 0;
private ChatLine fullMessage = null;
@Unique
private boolean chatting$detected = false;
@Unique
private boolean chatting$first = true;
@Unique
private NetworkPlayerInfo chatting$playerInfo;
@Unique
private NetworkPlayerInfo chatting$detectedPlayerInfo;
@Unique
private static NetworkPlayerInfo chatting$lastPlayerInfo;
@Unique
private static long chatting$lastUniqueId = 0;
@Unique
private long chatting$uniqueId = 0;
@Unique
private ChatLine chatting$fullMessage = null;

@Inject(method = "<init>", at = @At("RETURN"))
private void onInit(int i, IChatComponent iChatComponent, int j, CallbackInfo ci) {
lastUniqueId++;
uniqueId = lastUniqueId;
fullMessage = ChatHook.currentLine;
chatting$lastUniqueId++;
chatting$uniqueId = chatting$lastUniqueId;
chatting$fullMessage = ChatHook.currentLine;
chatting$chatLines.add(new WeakReference<>((ChatLine) (Object) this));
NetHandlerPlayClient netHandler = Minecraft.getMinecraft().getNetHandler();
if (netHandler == null) return;
Map<String, NetworkPlayerInfo> nicknameCache = new HashMap<>();
try {
for (String word : iChatComponent.getFormattedText().split("(§.)|\\W")) {
if (word.isEmpty()) continue;
playerInfo = netHandler.getPlayerInfo(word);
if (playerInfo == null) {
playerInfo = getPlayerFromNickname(word, netHandler, nicknameCache);
chatting$playerInfo = netHandler.getPlayerInfo(word);
if (chatting$playerInfo == null) {
chatting$playerInfo = chatting$getPlayerFromNickname(word, netHandler, nicknameCache);
}
if (playerInfo != null) {
detectedPlayerInfo = playerInfo;
detected = true;
if (playerInfo == lastPlayerInfo) {
first = false;
if (chatting$playerInfo != null) {
chatting$detectedPlayerInfo = chatting$playerInfo;
chatting$detected = true;
if (chatting$playerInfo == chatting$lastPlayerInfo) {
chatting$first = false;
if (ChattingConfig.INSTANCE.getHideChatHeadOnConsecutiveMessages()) {
playerInfo = null;
chatting$playerInfo = null;
}
} else {
lastPlayerInfo = playerInfo;
chatting$lastPlayerInfo = chatting$playerInfo;
}
break;
}
Expand All @@ -68,8 +77,9 @@ private void onInit(int i, IChatComponent iChatComponent, int j, CallbackInfo ci
}
}

@Unique
@Nullable
private static NetworkPlayerInfo getPlayerFromNickname(String word, NetHandlerPlayClient connection, Map<String, NetworkPlayerInfo> nicknameCache) {
private static NetworkPlayerInfo chatting$getPlayerFromNickname(String word, NetHandlerPlayClient connection, Map<String, NetworkPlayerInfo> nicknameCache) {
if (nicknameCache.isEmpty()) {
for (NetworkPlayerInfo p : connection.getPlayerInfoMap()) {
IChatComponent displayName = p.getDisplayName();
Expand All @@ -93,30 +103,30 @@ private static NetworkPlayerInfo getPlayerFromNickname(String word, NetHandlerPl

@Override
public boolean chatting$hasDetected() {
return detected;
return chatting$detected;
}

@Override
public NetworkPlayerInfo chatting$getPlayerInfo() {
return playerInfo;
return chatting$playerInfo;
}

@Override
public void chatting$updatePlayerInfo() {
if (ChattingConfig.INSTANCE.getHideChatHeadOnConsecutiveMessages() && !first) {
playerInfo = null;
if (ChattingConfig.INSTANCE.getHideChatHeadOnConsecutiveMessages() && !chatting$first) {
chatting$playerInfo = null;
} else {
playerInfo = detectedPlayerInfo;
chatting$playerInfo = chatting$detectedPlayerInfo;
}
}

@Override
public long chatting$getUniqueId() {
return uniqueId;
return chatting$uniqueId;
}

@Override
public ChatLine getFullMessage() {
return fullMessage;
public ChatLine chatting$getFullMessage() {
return chatting$fullMessage;
}
}
64 changes: 34 additions & 30 deletions src/main/java/org/polyfrost/chatting/mixin/GuiChatMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,27 @@ public abstract class GuiChatMixin extends GuiScreen implements GuiChatHook {

@Unique
private static final List<String> COPY_TOOLTIP = Lists.newArrayList(
"\u00A7e\u00A7lCopy To Clipboard",
"\u00A7b\u00A7lNORMAL CLICK\u00A7r \u00A78- \u00A77Full Message",
"\u00A7b\u00A7lCTRL CLICK\u00A7r \u00A78- \u00A77Single Line",
"\u00A7b\u00A7lSHIFT CLICK\u00A7r \u00A78- \u00A77Screenshot Message",
"§e§lCopy To Clipboard",
"§b§lNORMAL CLICK§r §8- §7Full Message",
"§b§lCTRL CLICK§r §8- §7Single Line",
"§b§lSHIFT CLICK§r §8- §7Screenshot Message",
"",
"\u00A7e\u00A7lModifiers",
"\u00A7b\u00A7l" + chatting$getModifierKey() + "\u00A7r \u00A78- \u00A77Formatting Codes"
"§e§lModifiers",
"§b§l" + chatting$getModifierKey() + "§r §8- §7Formatting Codes"
);

@Unique
private static final List<String> DELETE_TOOLTIP = Lists.newArrayList(
"\u00A7b\u00A7lNORMAL CLICK\u00A7r \u00A78- \u00A77Full Message",
"\u00A7b\u00A7lCTRL CLICK\u00A7r \u00A78- \u00A77Single Line"
"§b§lNORMAL CLICK§r §8- §7Full Message",
"§b§lCTRL CLICK§r §8- §7Single Line"
);

private SearchButton searchButton;
private ScreenshotButton screenshotButton;
private ClearButton clearButton;
@Unique
private SearchButton chatting$searchButton;
@Unique
private ScreenshotButton chatting$screenshotButton;
@Unique
private ClearButton chatting$clearButton;

@Inject(method = "initGui", at = @At("TAIL"))
private void init(CallbackInfo ci) {
Expand All @@ -76,21 +79,21 @@ private void init(CallbackInfo ci) {

@Inject(method = "updateScreen", at = @At("HEAD"))
private void updateScreen(CallbackInfo ci) {
if (ChattingConfig.INSTANCE.getChatSearch() && searchButton.isEnabled()) {
searchButton.getInputField().updateCursorCounter();
if (ChattingConfig.INSTANCE.getChatSearch() && chatting$searchButton.isEnabled()) {
chatting$searchButton.getInputField().updateCursorCounter();
}
}

@Inject(method = "keyTyped", at = @At("HEAD"), cancellable = true)
private void keyTyped(char typedChar, int keyCode, CallbackInfo ci) {
if (ChattingConfig.INSTANCE.getChatSearch() && searchButton.isEnabled()) {
if (ChattingConfig.INSTANCE.getChatSearch() && chatting$searchButton.isEnabled()) {
ci.cancel();
if (keyCode == 1) {
searchButton.onMousePress();
chatting$searchButton.onMousePress();
return;
}
searchButton.getInputField().textboxKeyTyped(typedChar, keyCode);
ChatSearchingManager.INSTANCE.setLastSearch(searchButton.getInputField().getText());
chatting$searchButton.getInputField().textboxKeyTyped(typedChar, keyCode);
ChatSearchingManager.INSTANCE.setLastSearch(chatting$searchButton.getInputField().getText());
} else if ((Keyboard.isKeyDown(219) || Keyboard.isKeyDown(220) || Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157)) && keyCode == UKeyboard.KEY_TAB) { // either macos super key or ctrl key for any os
ci.cancel();
ChatHooks.INSTANCE.switchTab();
Expand All @@ -107,8 +110,8 @@ private void onDrawScreen(int mouseX, int mouseY, float partialTicks, CallbackIn
int scale = new ScaledResolution(mc).getScaleFactor();
int x = Mouse.getX();
int right = (int) ((hook.chatting$getRight() + ModCompatHooks.getXOffset() + 1 + hud.getPaddingX()) * hud.getScale() + (int) hud.position.getX());
delete = delete && hovered(hook, x, right + (int) ((copy ? 10 : 0) * hud.getScale()), scale, hud);
copy = copy && hovered(hook, x, right, scale, hud);
delete = delete && chatting$hovered(hook, x, right + (int) ((copy ? 10 : 0) * hud.getScale()), scale, hud);
copy = copy && chatting$hovered(hook, x, right, scale, hud);

if (copy || delete) {
List<String> tooltip = delete ? DELETE_TOOLTIP : COPY_TOOLTIP;
Expand All @@ -118,7 +121,7 @@ private void onDrawScreen(int mouseX, int mouseY, float partialTicks, CallbackIn
}

@Unique
private boolean hovered(GuiNewChatHook hook, int x, int right, int scale, ChatWindow hud) {
private boolean chatting$hovered(GuiNewChatHook hook, int x, int right, int scale, ChatWindow hud) {
return hook.chatting$isHovering() && x > right * scale && x < (right + 9 * hud.getScale()) * scale;
}

Expand Down Expand Up @@ -153,16 +156,17 @@ private void mouseClicked(int mouseX, int mouseY, int mouseButton, CallbackInfo
} else if (ChattingConfig.INSTANCE.getChatDelete() && x > right + (copy ? 10 : 0) * hud.getScale() * scale && x < right + ((copy ? 10 : 0) + 9) * hud.getScale() * scale) {
ChatLine chatLine = hook.chatting$getHoveredLine(Mouse.getY());
if (chatLine == null) return;
ModCompatHooks.getDrawnChatLines().removeIf(line -> remove(line, chatLine));
ModCompatHooks.getChatLines().removeIf(line -> remove(line, chatLine));
ModCompatHooks.getDrawnChatLines().removeIf(line -> chatting$remove(line, chatLine));
ModCompatHooks.getChatLines().removeIf(line -> chatting$remove(line, chatLine));
}
}
}

private boolean remove(ChatLine line, ChatLine chatLine) {
@Unique
private boolean chatting$remove(ChatLine line, ChatLine chatLine) {
return UKeyboard.isCtrlKeyDown() ?
((ChatLineHook) line).chatting$getUniqueId() == ((ChatLineHook) chatLine).chatting$getUniqueId() :
((ChatLineHook) ((ChatLineHook) line).getFullMessage()).chatting$getUniqueId() == ((ChatLineHook) ((ChatLineHook) chatLine).getFullMessage()).chatting$getUniqueId();
((ChatLineHook) ((ChatLineHook) line).chatting$getFullMessage()).chatting$getUniqueId() == ((ChatLineHook) ((ChatLineHook) chatLine).chatting$getFullMessage()).chatting$getUniqueId();
}

@ModifyArg(method = "keyTyped", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiChat;sendChatMessage(Ljava/lang/String;)V"), index = 0)
Expand Down Expand Up @@ -202,17 +206,17 @@ private void handleMouseInput(CallbackInfo ci) {

@Unique
private void chatting$initButtons() {
searchButton = new SearchButton();
chatting$searchButton = new SearchButton();
if (ChattingConfig.INSTANCE.getChatSearch()) {
buttonList.add(searchButton);
buttonList.add(chatting$searchButton);
}
screenshotButton = new ScreenshotButton();
chatting$screenshotButton = new ScreenshotButton();
if (ChattingConfig.INSTANCE.getChatScreenshot()) {
buttonList.add(screenshotButton);
buttonList.add(chatting$screenshotButton);
}
clearButton = new ClearButton();
chatting$clearButton = new ClearButton();
if (ChattingConfig.INSTANCE.getChatDeleteHistory()) {
buttonList.add(clearButton);
buttonList.add(chatting$clearButton);
}
if (ChattingConfig.INSTANCE.getChatTabs()) {
for (ChatTab chatTab : ChatTabs.INSTANCE.getTabs()) {
Expand Down
Loading

0 comments on commit 0b12cbc

Please sign in to comment.