Thread Tools Display Modes
09-06-24, 09:14 AM   #1
dankanst
A Murloc Raider
Join Date: Aug 2024
Posts: 7
Help with updating addon code

I've been digging around these forums for an addon that removes all instances "-RealmName" attached to player names in the chat frame, and while there were a couple of codes I found to do that, none of them seem to work in the current version of the game. My theory is that Blizzard updated their code for the chat frame, so until the addon code is updated, or a new addon is written, there is no way to hide the player realm names in the chat at the moment

That being said, I did stumble upon a code from 2015 that does a part of the job right to this day! I'll take the liberty to post it here, since it was written by an addon author on these forums for a user that had the same question as I do now:

Code:
local a,g = getmetatable(DEFAULT_CHAT_FRAME).__index.AddMessage,gsub
getmetatable(DEFAULT_CHAT_FRAME).__index.AddMessage = function(s,t,...)
    if s == ChatFrame2 then return a(s,t,...) end -- combat log
    return a(s,g(t,"|Hplayer:(.-)|h(.-)|h",function(a,b)
        return "|Hplayer:"..a.."|h"..g(b,"-([^%]:]+)(.*)","%2|r").."|h"
    end),...)
end

ChatFrame_AddMessageEventFilter('CHAT_MSG_SYSTEM', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_LOOT', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_CURRENCY', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_COMBAT_HONOR_GAIN', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_COMBAT_XP_GAIN', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_BG_SYSTEM_NEUTRAL', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_BG_SYSTEM_ALLIANCE', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_BG_SYSTEM_HORDE', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_ACHIEVEMENT', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_YELL', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_SAY', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_TEXT_EMOTE', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

ChatFrame_AddMessageEventFilter('CHAT_MSG_EMOTE', function(self, event, msg, ...)
        return false, (msg:gsub('([^\1-\64\91-\96\123-191]+)%-%u%l%a+', '%1')), ...
end)

hooksecurefunc('ChatEdit_UpdateHeader', function(editBox)
    local header = _G[editBox:GetName() .. 'Header']
    if editBox:GetAttribute('chatType') == 'WHISPER' and header then
        _G[editBox:GetName() .. 'HeaderSuffix']:Hide()
        header:SetWidth(0)
        header:SetFormattedText(CHAT_WHISPER_SEND, (strsplit('-', editBox:GetAttribute('tellTarget'))))
        editBox:SetTextInsets(15 + (header:GetRight() or 0) - (header:GetLeft() or 0), 13, 0, 0)
    end
end)
I should mention that there are no errors when loading the addon on retail, but it seems only parts of the addon work. For example, the -RealmName attached to player names when displaying emotes (Player blows Player a kiss) is removed. The -RealmName from players in system messages (Player has defeated Player in a duel, Player joins the party, etc) is also removed. But when displaying general player messages like say, yell, whisper, or with player names in chat channels, the addon does nothing, so I can still see the realms of players on these messages

My question is, since I have 0 knowledge of lua code, could somebody more experienced take a look at the addon and maybe reformulate it to remove all instances of -RealmName from all player messages and interactions in the chat frame? I wanted to reach out to the person that made the code, but they haven't been online in years, hence I'm writing this here in hope that somebody can help

Last edited by dankanst : 09-06-24 at 09:18 AM.
  Reply With Quote
09-06-24, 12:19 PM   #2
Sharpedge
A Black Drake
 
Sharpedge's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2022
Posts: 82
I already sent this to you in a PM. But maybe other folks may wanna use it as well....

Code:
-- Function to remove realm names from player names in chat messages
local function RemoveRealmFromChatMessage(message)
    -- Replace any realm name pattern, i.e., "Player-Realm" with "Player"
    return message:gsub("([%a]+)%-(%a+)", "%1")
end

-- Hook into each chat frame to override the AddMessage function
local function HookChatFrames()
    for i = 1, NUM_CHAT_WINDOWS do
        local chatFrame = _G["ChatFrame"..i]
        if chatFrame and chatFrame.AddMessage then
            -- Store the original AddMessage function
            local originalAddMessage = chatFrame.AddMessage

            -- Override the AddMessage function
            chatFrame.AddMessage = function(self, text, ...)
                -- Remove realm names from the message
                text = RemoveRealmFromChatMessage(text)
                -- Call the original AddMessage function with the modified text
                return originalAddMessage(self, text, ...)
            end
        end
    end
end

-- Call the function to hook chat frames when the addon is loaded
HookChatFrames()
  Reply With Quote
09-07-24, 12:19 PM   #3
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 668
It's not good practice to replace Blizzard's functions. This may lead to UI taints, even replacing something as innocent as adding a chat line. For example, messing with ChatFrame1 can sometimes cause taints involving Edit Mode.

You can hook CircularBuffer:PushFront(), which is what ScrollingMessageFrame:AddMessage() calls, and edit the line in the chat window directly. I use this method in my personal addon for chat line manipulation. FCF_SetWindowName() happens to be the first function guaranteed to be called when creating any chat window, during UI startup or making any additional window, that includes the frame in the first argument. Hooking FCF_SetWindowName() to then hook any frame that passes through it ensures all chat frames are caught. I also ignore the Combat Log; doing anything to each line in that frame tanks fps, but you can try by removing the commented line.

Also, while %a may match character names, %a will not match all realm names. In chat context, such as player links and shift clicking, spaces are removed but dashes and apostrophes may still be there. One realm even has numbers in it. The ascii numbers in the original code was to ensure any possible character that can't be used in a character's name is not matched. I'm not going to reinvent the wheel there if the original author found a problem with %a.

Lua Code:
  1. local hook={}
  2. hooksecurefunc("FCF_SetWindowName",function(frame)
  3.     if frame==COMBATLOG then return end -- <= (PERFORMANCE WARNING) remove this line to include combat log
  4.     if hook[frame] then return end
  5.     hook[frame]=true
  6.     hooksecurefunc(frame.historyBuffer,"PushFront",function(buffer,line)
  7.         line.message=line.message:gsub("([^\1-\64\91-\96\123-\191]+)%-[%\39%\45%w]+","%1")
  8.     end)
  9. end)
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Help with updating addon code


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off