Thread Tools Display Modes
01-07-14, 12:25 PM   #1
aapoo
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Jan 2008
Posts: 14
Removing -REALMNAME

Hello there,

I'm working on a small addon for personal use to remove the very annoying [Fansoz-Dragonblight] into [Fansoz] (like it was prior to 5.4.2).

I am, however, getting stuck at figuring out how to ensure it catches "-Realmname".

Here is my current code in it's enterity:

Code:
function huFilter(self, event, msg)
	local removestring = "-"..GetRealmName()
	if msg:find(removestring) then
		return string.gsub(msg, removestring, "")
	end
end

ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", huFilter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", huFilter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_YELL", huFilter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", huFilter)
When writing, for example, -Dragonblight in the /s it does remove the line, the whole thing. Nothing's shown instead of all but the "-Dragonblight" part.

I'm not very novice in terms of programming, but I'm not used to event based programming and lua/wow api. So, I don't want a complete answer since I really want to learn this stuff too, so a resource or an explanation of what I'm doing wrong is really all I'm asking for.

Also I am curious to learn more about the msg:find functionality, I was told by a similarily novice as myself it was useful, personal I'm pondering the usefulness of it since the gsub should simply return a nil value if there's no matches found, right?

Thanks!

/Fansoz (Best disco eu! )
  Reply With Quote
01-07-14, 02:31 PM   #2
def9
A Cobalt Mageweaver
 
def9's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2008
Posts: 219
I don't know is this will help but I've been using a simple mod called Realm Remover by Caedis since it was released to hide the realm names. Maybe you can see the answer to your question in his code.
__________________
Epiria, level 100 Ret/Holy Paladin
Simkin level 100 Combat Rogue
Feldeemus, level 100 Arcane Mage

Last edited by def9 : 01-07-14 at 02:32 PM. Reason: spelling
  Reply With Quote
01-07-14, 02:41 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Several problems with your code:

Code:
local removestring = "-"..GetRealmName()
This won't work because "-" is a "magic character" in Lua. If you want to match it literally, you need to escape it, as "%-".

Code:
return string.gsub(msg, removestring, "")
This fails because when you change the message in a chat filter (or change any of the arguments) you must return all of the arguments, in the original order, even the ones you didn't change.

Also, the player's name is not in the [i]message[i] argument. It's in the sender argument -- but if you modify it in in a chat filter, then the resulting link the chat system produces will be broken. This is one of the cases where you will still need to use the old-fashioned "hook the AddMessage method" tactic, instead of the chat filtering system.

Code:
-- Define this one time, not over and over every time a message comes in:
local SAMEREALM = "%-" .. GetRealmName()

-- Use a table to store the chat frames' original AddMessage functions:
local originals = {}

-- Define your replacement AddMessage function:
local function AddMessage(frame, message, ...)
	-- Make sure the message is a string before trying to do anything with it.
	-- Not really sure why it's not ALWAYS a string, but it's not...
	if type(message) == "string" then
		-- Find the formatted player link:
		local link, data, name = strmatch(message, "(|Hplayer:(.-)|h%[(.-)%]|h)")
		if link then
			-- Found it!
			-- Strip the server name from the display name only:
			name = gsub(name, SAMREALM, "")
			-- Make a new link:
			local newlink = "|Hplayer:" .. data .. "|h[" .. name .. "]|h"
			-- Replace the original link in the message with the new one:
			message = gsub(message, link, newlink, 1)
		end
	end
	-- Pass everything back to the frame's original AddMessage method:
	return frame[AddMessage](frame, message, ...)
end

-- Replace all the chat frames' AddMessage functions:
for i = 1, NUM_CHAT_FRAMES do
	local frame = _G["ChatFrame"..i]
	-- Store the frame's original function:
	originals[frame] = frame.AddMessage
	-- And replace it with yours:
	frame.AddMessage = AddMessage
end

-- Set up a hook to catch temporary chat windows too, such as
-- those created when you send a whisper conversation to a new window
-- or have whispers set to appear in new windows.
local orig = FCF_OpenTemporaryWindow
FCF_OpenTemporaryWindow = function(...)
	local frame = orig(...)
	originals[frame] = frame.AddMessage
	frame.AddMessage = AddMessage
	return frame
end
If you wanted to remove all realm names, just change:

Code:
name = gsub(name, SAMREALM, "")
to:

Code:
name = gsub(name, "%-[^|]+", "")
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Removing -REALMNAME


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