Jump to content

NickProtection trim hexcode


h4x7o0r

Recommended Posts

Hey guys, i'm using this resource for nick protection (https://community.multitheftauto.com/index.php?p= ... ils&id=492) that's working very well as is all i need. But, there's a problem with hexcodes between the nickname's letter or so.

Just to be more explicit in the problem, i'll try to give u an example :

i have the name [A]AAA,i'm registering my account then login and protect. If someone whant's to have same nickname like me but just with a different color he can use for example #000000[A]AAA or [A]#000000AAA and everything's fine for him.

How can i restrict the nickname to disable hexcodes for this account check ?

Here's the resource code:

  
 
local root = getRootElement()
local timers = {}
 
 
--[[
-- check Nick
--
-- Checks if the nick currently used by the given player
-- is protected and if he is allowed to use it.
--
-- @param   player   player: The player whose nick should be checked
-- @return
-- ]]
function checkNick(player)
    -- player changed nick, just joined or timer ran out (possible running timer is no longer necessary)
    killTimerForPlayer(player)
   
    -- fetch information about the player's nick
    local nick = getPlayerName(player)
   
    if isPlayerAllowedToUseNick(player,nick) then
        return
    end
 
    -- # nick is protected and player has (currently) no right to use it
   
    --outputDebugString("tell player "..getPlayerName(player).." to login or change nick")
 
    local secondsForLogin = tonumber(get("timeForLogin"))
 
    if toboolean(get("useTextDisplays")) then
        warnPlayer(player,"Your nick '"..nick.."' is protected. Please login or change nick within "..secondsForLogin.." seconds.")
    end
    outputMessage("Your nick '"..nick.."' is protected. Please login or change nick within "..secondsForLogin.." seconds.",player,0)
   
    timers[player] = setTimer(checkNickAgain,secondsForLogin*1000,1,player)
 
end
 
--[[
-- checkNickAgain
--
-- Called by the timer to check if the player is now allowed to use the nick,
-- or otherwise take action.
--
-- @param   player   player: The Player
-- @return
-- ]]
function checkNickAgain(player)
    if isPlayerAllowedToUseNick(player,getPlayerName(player)) then
        return
    end
 
    if toboolean(get("changeNickInsteadOfKicking")) then
        changeNickToRandom(player)
        if toboolean(get("useTextDisplays")) then
            warnPlayer(player,"Your nick has been changed because you used a protected nick.")
        end
        outputMessage("Your nick has been changed because you used a protected nick.",player,0)
    else
        kickPlayer(player,"Used protected nick.")
    end
 
    outputDebugString("take action on "..getPlayerName(player).." (change nick or kick)")
end
 
 
 
--[[
-- changeNickToRandom
--
-- Creates a random nick which is not currently used and sets it as
-- new nick for the given player.
--
-- @param   player   player: The player who's nick should be changed.
-- @return
-- ]]
function changeNickToRandom(player)
    local randomNick = "Player"..math.random(100,999)
    while getPlayerFromName(randomNick) do
        randomNick = "Player"..math.random(100,999)
    end
    setPlayerName(player,randomNick)
end
 
 
 
 
 
 
---------------------------------------------------
-- ### Getting and modifying data from the database
---------------------------------------------------
 
--[[
-- getProtectedNickData
--
-- Gets the data for a protected nick from the database
--
-- @param   string   nick: The nick you want to get the data from
-- @return  mixed    false/result: Returns either false if no result was found or a table with the desired data
-- ]]
function getProtectedNickData(nick)
    local result = executeSQLQuery("SELECT *, ((JULIANDAY('now') - JULIANDAY(lastUsed))) AS lastUsedDaysPassed FROM nickProtection WHERE protectedNick = ?",nick)
 
    if #result == 0 then
        return false
    end
 
    return result[1]
end
 
--[[
-- removeExpiredNicks
--
-- Removes expired nicks (depending on the mode set in the settings)
-- ]]
function removeExpiredNicks()
    local expireMode = tonumber(get("protectedNicksExpireMode"))
    if expireMode == 0 then
        return
    end
    local expiresAfterDays = tonumber(get("protectedNicksExpireAfterDays"))
    local expiredNicks = executeSQLQuery("SELECT rowid, * FROM nickProtection WHERE (JULIANDAY('now') - JULIANDAY(lastUsed)) > ?",expiresAfterDays)
    for k,v in ipairs(expiredNicks) do
        local accountString = "user."..v.accountName
        if expireMode == 2 or not hasObjectPermissionTo(accountString,"resource.nickProtection.extended",false) then
            outputDebugString("Removing expired nick: "..tostring(v.protectedNick).." (last used "..tostring(v.lastUsed)..")")
            executeSQLQuery("DELETE FROM nickProtection WHERE rowid = ?",v.rowid)
        end
    end
end
 
--[[
-- updateLastUsed
--
-- Updates the lastUsed field when the nick is used.
--
-- @param   string   nick: The nick.
-- ]]
function updateLastUsed(nick)
    executeSQLQuery("UPDATE nickProtection SET lastUsed = DATETIME('now') WHERE protectedNick = ?",nick)
end
 
--[[
-- isNickProtected
--
-- Checks if the given nick is protected by someone.
--
-- @param   string   nick: The nick that should be checked.
-- @return  boolean  true/false
-- ]]
function isNickProtected(nick)
    local data = getProtectedNickData(nick)
    if not data then
        return false
    end
    return true
end
 
--[[
-- getProtectedNicksByAccountName
--
-- Returns a table with protected nicks associated with the given account name.
--
-- @param   string   accountName: The name of the account, e.g. "Adnan"
-- @return  table    protectedNicks: A table of protected nicks (could also contain no elements at all)
-- ]]
function getProtectedNicksByAccountName(accountName)
    local protectedNicks = executeSQLQuery("SELECT *, ((JULIANDAY('now') - JULIANDAY(lastUsed))) AS lastUsedDaysPassed FROM nickProtection WHERE accountName = ?",accountName)
    return protectedNicks
end
 
--[[
-- isPlayerAllowedToUseNick
--
-- Checks if the player is allowed to use the given nick,
-- so if it is protected at all and if the player is the one
-- who protected it.
--
-- @param   player   player: The player who claims ownership of the nick
-- @param   string   nick: The nick who should be checked
-- @return  boolean  true/false
-- ]]
function isPlayerAllowedToUseNick(player,nick)
    local data = getProtectedNickData(nick)
    if data == false then
        return true
    end
 
    -- if player is logged in to the required account, return
    local playerAccountName = getAccountName(getPlayerAccount(player))
    if data.accountName ~= nil and data.accountName == playerAccountName then
        -- consider nick used, when the rightful owner tried to take it
        updateLastUsed(nick)
        return true
    end
 
    return false
   
end
 
 
 
 
--------------------
-- ### Player Events
--------------------
 
--[[
-- playerJoined
--
-- Simply calls checkNick() as soon as a player joins.
-- ]]
function playerJoined()
    checkNick(source)   
end
addEventHandler("onPlayerJoin",root,playerJoined)
 
--[[
-- playerLeft
--
-- Clears up when a player leaves the server. For now it only kills
-- the timer (if necessary).
-- ]]
function playerLeft()
    killTimerForPlayer(source)
end
addEventHandler("onPlayerQuit",root,playerLeft)
 
--[[
-- playerChangedNick
--
-- Checks on change of nick if the player is allowed to use the new nick.
-- If so, nickChangeSpamProtection is also called if enabled.
--
-- Event Handler for onPlayerChangeNick.
--
-- @param   string   oldNick: The nick previously used
-- @param   string   newNick: The nick the player wants to change to
-- @return
-- ]]
function playerChangedNick(oldNick,newNick)
    if isPlayerAllowedToUseNick(source,newNick) then
        killTimerForPlayer(source)
        -- check for nick spam if enabled
        if toboolean(get("enableNickChangeSpamProtection")) then
            nickChangeSpamProtection(source)
        end
   
Link to comment

This will remove HEX codes from the name.

  
 
local root = getRootElement()
local timers = {}
 
 
--[[
-- check Nick
--
-- Checks if the nick currently used by the given player
-- is protected and if he is allowed to use it.
--
-- @param   player   player: The player whose nick should be checked
-- @return
-- ]]
function checkNick(player)
    -- player changed nick, just joined or timer ran out (possible running timer is no longer necessary)
    killTimerForPlayer(player)
   
    -- fetch information about the player's nick
    local nick = getPlayerName(player)
   
    if isPlayerAllowedToUseNick(player,nick) then
        return
    end
 
    -- # nick is protected and player has (currently) no right to use it
   
    --outputDebugString("tell player "..getPlayerName(player).." to login or change nick")
 
    local secondsForLogin = tonumber(get("timeForLogin"))
 
    if toboolean(get("useTextDisplays")) then
        warnPlayer(player,"Your nick '"..nick.."' is protected. Please login or change nick within "..secondsForLogin.." seconds.")
    end
    outputMessage("Your nick '"..nick.."' is protected. Please login or change nick within "..secondsForLogin.." seconds.",player,0)
   
    timers[player] = setTimer(checkNickAgain,secondsForLogin*1000,1,player)
 
end
 
--[[
-- checkNickAgain
--
-- Called by the timer to check if the player is now allowed to use the nick,
-- or otherwise take action.
--
-- @param   player   player: The Player
-- @return
-- ]]
function checkNickAgain(player)
    if isPlayerAllowedToUseNick(player,getPlayerName(player)) then
        return
    end
 
    if toboolean(get("changeNickInsteadOfKicking")) then
        changeNickToRandom(player)
        if toboolean(get("useTextDisplays")) then
            warnPlayer(player,"Your nick has been changed because you used a protected nick.")
        end
        outputMessage("Your nick has been changed because you used a protected nick.",player,0)
    else
        kickPlayer(player,"Used protected nick.")
    end
 
    outputDebugString("take action on "..getPlayerName(player).." (change nick or kick)")
end
 
 
 
--[[
-- changeNickToRandom
--
-- Creates a random nick which is not currently used and sets it as
-- new nick for the given player.
--
-- @param   player   player: The player who's nick should be changed.
-- @return
-- ]]
function changeNickToRandom(player)
    local randomNick = "Player"..math.random(100,999)
    while getPlayerFromName(randomNick) do
        randomNick = "Player"..math.random(100,999)
    end
    setPlayerName(player,randomNick)
end
 
 
 
 
 
 
---------------------------------------------------
-- ### Getting and modifying data from the database
---------------------------------------------------
 
--[[
-- getProtectedNickData
--
-- Gets the data for a protected nick from the database
--
-- @param   string   nick: The nick you want to get the data from
-- @return  mixed    false/result: Returns either false if no result was found or a table with the desired data
-- ]]
function getProtectedNickData(nick)
    local result = executeSQLQuery("SELECT *, ((JULIANDAY('now') - JULIANDAY(lastUsed))) AS lastUsedDaysPassed FROM nickProtection WHERE protectedNick = ?",nick)
 
    if #result == 0 then
        return false
    end
 
    return result[1]
end
 
--[[
-- removeExpiredNicks
--
-- Removes expired nicks (depending on the mode set in the settings)
-- ]]
function removeExpiredNicks()
    local expireMode = tonumber(get("protectedNicksExpireMode"))
    if expireMode == 0 then
        return
    end
    local expiresAfterDays = tonumber(get("protectedNicksExpireAfterDays"))
    local expiredNicks = executeSQLQuery("SELECT rowid, * FROM nickProtection WHERE (JULIANDAY('now') - JULIANDAY(lastUsed)) > ?",expiresAfterDays)
    for k,v in ipairs(expiredNicks) do
        local accountString = "user."..v.accountName
        if expireMode == 2 or not hasObjectPermissionTo(accountString,"resource.nickProtection.extended",false) then
            outputDebugString("Removing expired nick: "..tostring(v.protectedNick).." (last used "..tostring(v.lastUsed)..")")
            executeSQLQuery("DELETE FROM nickProtection WHERE rowid = ?",v.rowid)
        end
    end
end
 
--[[
-- updateLastUsed
--
-- Updates the lastUsed field when the nick is used.
--
-- @param   string   nick: The nick.
-- ]]
function updateLastUsed(nick)
    executeSQLQuery("UPDATE nickProtection SET lastUsed = DATETIME('now') WHERE protectedNick = ?",nick)
end
 
--[[
-- isNickProtected
--
-- Checks if the given nick is protected by someone.
--
-- @param   string   nick: The nick that should be checked.
-- @return  boolean  true/false
-- ]]
function isNickProtected(nick)
    local data = getProtectedNickData(nick)
    if not data then
        return false
    end
    return true
end
 
--[[
-- getProtectedNicksByAccountName
--
-- Returns a table with protected nicks associated with the given account name.
--
-- @param   string   accountName: The name of the account, e.g. "Adnan"
-- @return  table    protectedNicks: A table of protected nicks (could also contain no elements at all)
-- ]]
function getProtectedNicksByAccountName(accountName)
    local protectedNicks = executeSQLQuery("SELECT *, ((JULIANDAY('now') - JULIANDAY(lastUsed))) AS lastUsedDaysPassed FROM nickProtection WHERE accountName = ?",accountName)
    return protectedNicks
end
 
--[[
-- isPlayerAllowedToUseNick
--
-- Checks if the player is allowed to use the given nick,
-- so if it is protected at all and if the player is the one
-- who protected it.
--
-- @param   player   player: The player who claims ownership of the nick
-- @param   string   nick: The nick who should be checked
-- @return  boolean  true/false
-- ]]
function isPlayerAllowedToUseNick(player,nick)
    local data = getProtectedNickData(nick)
    if data == false then
        return true
    end
 
    -- if player is logged in to the required account, return
    local playerAccountName = getAccountName(getPlayerAccount(player))
    if data.accountName ~= nil and data.accountName == playerAccountName then
        -- consider nick used, when the rightful owner tried to take it
        updateLastUsed(nick)
        return true
    end
 
    return false
   
end
 
 
 
 
--------------------
-- ### Player Events
--------------------
 
--[[
-- playerJoined
--
-- Simply calls checkNick() as soon as a player joins.
-- ]]
function playerJoined()
    checkNick(source)  
end
addEventHandler("onPlayerJoin",root,playerJoined)
 
--[[
-- playerLeft
--
-- Clears up when a player leaves the server. For now it only kills
-- the timer (if necessary).
-- ]]
function playerLeft()
    killTimerForPlayer(source)
end
addEventHandler("onPlayerQuit",root,playerLeft)
 
--[[
-- playerChangedNick
--
-- Checks on change of nick if the player is allowed to use the new nick.
-- If so, nickChangeSpamProtection is also called if enabled.
--
-- Event Handler for onPlayerChangeNick.
--
-- @param   string   oldNick: The nick previously used
-- @param   string   newNick: The nick the player wants to change to
-- @return
-- ]]
function playerChangedNick(oldNick,newNick)
    if isPlayerAllowedToUseNick(source,newNick) then
        killTimerForPlayer(source)
        -- check for nick spam if enabled
        if toboolean(get("enableNickChangeSpamProtection")) then
            nickChangeSpamProtection(source)
        end
   
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...