需要Addon帮助修复聊天中的名称检测

时间:2014-07-21 01:26:33

标签: lua add-on world-of-warcraft

以下代码假设在任何文本示例中更改用户名称Player:'[Player2]:Hi Player'into'[Player2]:Hi> Player<'玩家为红色且><黄色的符号。问题是它没有检测到其他玩家说出来,而且只是在我说出我的名字的时候,它会通过课堂和警告对它们进行简单的着色。

我知道可能存在Global Leaks,例如这是一个beta阶段的插件,我一直在努力重写功能以使它们变得更好但是现在我试图让一切都先工作。如果您可以简化名称检测并更改它而不会弄乱魔兽世界中的链接,我会接受建议,我将在代码中添加注释行以显示您的信用名称和主要插件页面。否则我只需要修复就可以找出它无法按预期工作的原因。

以下是代码:

    local _PlayerName2 = CM2.StripSpecial(UnitName("player"), true)
    _PlayerName2 = _PlayerName2:lower()
    local _PlayerName, _ = UnitName("player")

    --Detect if the current character said their own name. Block Warnings.
    local throwWarn = true

    if ChatAuthor == curPlayer then
        --Player said something.
        throwWarn = false
        --CM2.Print("Disabled Warnings")
    end

    if text == nil then
        print("Text Nil, Check URL Detection Code")
    end

    --Color own name in your chat. Really only sender name because all other references will
    --be tied to the alert.     
    if throwWarn == false and text:find(" "..curPlayer.." ") ~= nil then
        text = CM2.far(text, curPlayer, CM2.ColorName(CM2_Nick[plainPlayerName][2], plainPlayerName))
    end
    if throwWarn == false and text:find(case_insensitive_pattern(plainPlayerName)) ~= nil then
        text = CM2.far(text, plainPlayerName, CM2.ColorName(CM2_Nick[plainPlayerName][2], plainPlayerName))
    end

    --Detect and alert when playername is spoken
    local pos = 0

    if throwWarn == true and text:find(" ".._PlayerName.." ") ~= nil then
        --Detect if the Original Player name was said.
        text = text:gsub(" ".._PlayerName.." ", " "..CHATMOD_COLOR["YELLOW"]..">"..CHATMOD_COLOR["RED"].._PlayerName..CHATMOD_COLOR["YELLOW"].."<\124r ")           
        UIErrorsFrame:AddMessage(text, red, green, blue, nil, UIERRORS_HOLD_TIME)
        PlaySound("FriendJoinGame")
    elseif throwWarn == true and text:find(" "..case_insensitive_pattern(plainPlayerName).." ") ~= nil then
        --Detect if the stripped down Player Name was said.
        text = text:gsub(" ".._PlayerName2.." ", " "..CHATMOD_COLOR["YELLOW"]..">"..CHATMOD_COLOR["RED"].._PlayerName2..CHATMOD_COLOR["YELLOW"].."<\124r ")         
        UIErrorsFrame:AddMessage(text, red, green, blue, nil, UIERRORS_HOLD_TIME)
        PlaySound("FriendJoinGame")
    end



    if text == nil then
        print("Text Nil, Check playername highlighter Code")
    end


    -- Color Nicks
    if CM2_Options["ColorNicks"] then
        -- Hold Original unmodified words for later use
        temp2 = text
        temp2 = CM2.StripSpecial(temp2)
        temp = CM2.StripSpecial(text)
        temp = string.gsub(temp, "%]%[", " ")
        temp = string.gsub(temp, "[^a-zA-Z0-9%s]", "")
        words = CM2.GetWords(temp)
        words2 = CM2.GetWords(temp:lower())

        for word = 1, #words do
            -- Cant be the player name or it locks up the client... Go figure...
            if words[word] ~= UnitName("player") and words[word] ~= plainPlayerName then
                --print(words[word]:lower())
                if CM2_Nick[words[word]:lower()] ~= nil then
                    --{level, class, guild, realm, name} Nick Layout. Name is the unfiltered name.
                    local newWord = CM2.ColorName(CM2_Nick[words[word]:lower()][2], words[word]:lower())

                    word2find = words[word]
                    pos = temp2:find(word2find) or temp:find(words2[word])

                    if newWord ~= nil then
                        --replace with find code.
                        text = CM2.far(text, word2find, newWord)
                    end
                end
            end
        end
    end   

功能CM2.far:

    function CM2.far(str, fstr, rstr)
    if (str and type(str) == "string") and (fstr and type(fstr) == "string") and (rstr and type(rstr) == "string") then
        --And space at the end so gfind will find till the end.
        --In case a match is there.
        str = str .. " "
        local pos = nil
        for x in str:gmatch("[^%:]"..case_insensitive_pattern(fstr).."[^%:]") do

            if pos ~= nil and pos ~= str:find("[^%:]"..case_insensitive_pattern(fstr).."[^%:]", pos+1) then
                str = str:sub(1, pos) .. rstr .. str:sub(pos + #fstr + 1)
                --print(str)
                pos = str:find("[^%:]"..case_insensitive_pattern(fstr).."[^%:]", pos+1)
            elseif pos == nil then
                pos = str:find("[^%:]"..case_insensitive_pattern(fstr).."[^%:]")
                str = str:sub(1, pos) .. rstr .. str:sub(pos + #fstr + 1)
                pos = str:find("[^%:]"..case_insensitive_pattern(fstr).."[^%:]", pos+1)
                --print(str)
            end
        end
        str = str:sub(1, #str - 1)
        return str
    end
end

功能CM2.StripSpecial:

    function CM2.StripSpecial(msg, player)
    PlayerStripped = 0

    --Strips out all special characters such as Ö and the like.
    --Should only be used for being returned to a temp string unless replacement is required.
    if msg ~= nil and type(msg) == "string" then
        for x=1, #msg do
            local CharVal = string.byte(string.sub(msg,x,x+1), -1)
            --local StrTab = {}
            --for a=1, #msg do
              --  StrTab:Insert(
            --print("Debug: "..string.byte(string.sub(msg,x,x+1)))
            --print(CharVal)
            if CharVal ~= nil then
                if 146 <= CharVal and CharVal <= 150 then
                    msg = StringReplace(msg, x, "O")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 178 <= CharVal and CharVal <= 182 then
                    msg = StringReplace(msg, x, "o")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 128 <= CharVal and CharVal <= 134 then
                    msg = StringReplace(msg, x, "A")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 160 <= CharVal and CharVal <= 166 then
                    msg = StringReplace(msg, x, "a")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 136 <= CharVal and CharVal <= 139 then
                    msg = StringReplace(msg, x, "E")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 168 <= CharVal and CharVal <= 171 then
                    msg = StringReplace(msg, x, "e")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 153 <= CharVal and CharVal <= 156 then
                    msg = StringReplace(msg, x, "U")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 185 <= CharVal and CharVal <= 188 then
                    msg = StringReplace(msg, x, "u")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 140 <= CharVal and CharVal <= 143 then
                    msg = StringReplace(msg, x, "I")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 172 <= CharVal and CharVal <= 175 then
                    msg = StringReplace(msg, x, "i")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 135 == CharVal then
                    msg = StringReplace(msg, x, "C")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 167 == CharVal then
                    msg = StringReplace(msg, x, "c")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 144 == CharVal then
                    msg = StringReplace(msg, x, "D")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 176 == CharVal then
                    msg = StringReplace(msg, x, "o")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 152 == CharVal then
                    msg = StringReplace(msg, x, "O")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                elseif 184 == CharVal then
                    msg = StringReplace(msg, x, "o")
                    if player then
                        PlayerStripped = PlayerStripped + 1
                    end
                end
            end
        end
    end

    return msg
end

功能CM2.GetWords:

    function CM2.GetWords(str)
    if type(str) == "string" then
        local results = {}

        for word in string.gmatch(str, "%S+", 9) do
            table.insert(results, word)
        end

        return results
    end
end

功能case_insensitive_pattern:

    function case_insensitive_pattern(pattern)

  -- find an optional '%' (group 1) followed by any character (group 2)
  local p = pattern:gsub("(%%?)(.)", function(percent, letter)
    --print(percent, letter)
    if percent ~= "" or (not letter:match("%a")) then
      if letter ~= "-" then
          -- if the '%' matched, or `letter` is not a letter, return "as is"
          return percent .. letter
      elseif letter == "-" then
          return "%-"
      end
    else
        if letter == "[" then
          return "%["
        elseif letter == "]" then
          --print("hi")
          return "%]"
        end
      -- else, return a case-insensitive character class of the matched letter
      return string.format("[%s%s]", letter:lower(), letter:upper())
    end

  end)

  return p
end

功能CM2.ColorName:

    function CM2.ColorName(str, word)
    --Using the class and word provided precolor it for chat.
    if str == "MONK" then
        word = "\124cff00ff96" .. CM2_Nick[word][5] .. "|r"
    elseif str == "DEATH KNIGHT" then
        word = "\124cffc41f3b" .. CM2_Nick[word][5] .. "|r"
    elseif str == "DRUID" then
        word = "\124cffff7d0a" .. CM2_Nick[word][5] .. "|r"
    elseif str == "HUNTER" then
        word = "\124cffabd473" .. CM2_Nick[word][5] .. "|r"
    elseif str == "MAGE" then
        word = "\124cff69ccf0" .. CM2_Nick[word][5] .. "|r"
    elseif str == "PALADIN" then
        word = "\124cfff58cba" .. CM2_Nick[word][5] .. "|r"
    elseif str == "PRIEST" then
        word = "\124cffffffff" .. CM2_Nick[word][5] .. "|r"
    elseif str == "ROGUE" then
        word = "\124cfffff569" .. CM2_Nick[word][5] .. "|r"
    elseif str == "SHAMAN" then
        word = "\124cff0070de" .. CM2_Nick[word][5] .. "|r"
    elseif str == "WARLOCK" then
        word = "\124cff9482c9" .. CM2_Nick[word][5] .. "|r"
    elseif str == "WARRIOR" then
        word = "\124cffc79c6e" .. CM2_Nick[word][5] .. "|r"
    end

    return word
end

编辑1:管理以修复自我姓名警告但注意到在聊天中说明您的完全匹配的玩家名称没有被着色。我自己可以解决这个问题。我们还没有做出任何改变来修复其他玩家说玩家的名字警告。

0 个答案:

没有答案