• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ Get all friend list names

List will be always up to date regardless if player has been saved or not

C++ (luascript.cpp)
C++:
//getPlayerVipList(cid)
lua_register(m_luaState, "getPlayerVipList", LuaInterface::luaGetPlayerVipList);

C++:
int32_t LuaInterface::luaGetPlayerVipList(lua_State* L)
{
    //getPlayerVipList(cid)
    ScriptEnviroment* env = getEnv();
    if (Player* player = env->getPlayerByUID(popNumber(L)))
    {
        int32_t i = 1;
        lua_newtable(L);

        for (uint32_t playerEntry : player->VIPList)
        {
            lua_pushnumber(L, i++);
            lua_pushnumber(L, playerEntry);
            pushTable(L);
        }
    }
    else
    {
        errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
        lua_pushboolean(L, false);
    }

    return 1;
}

C++ (luascript.h)
C++:
static int32_t luaGetPlayerVipList(lua_State* L);

Lua (data/libs/050-function.lua)
Lua:
function getVipListNames(cid, sort, externalQuery)

    local names = {}
    local pending = {}
    local list = getPlayerVipList(cid)

    for i, pid in ipairs(list) do
        local player = getPlayerByGUID(pid)
        if player then
            names[#names + 1] = getCreatureName(player)
        else
            -- # offline players
            if not externalQuery then
                -- # players names are cached in memory once they logged in, othewise a query is executed
                -- # still I recommend to use the external query which fetchs all the data in 1 query
                names[#names + 1] = getPlayerNameByGUID(pid)
            else
                pending[#pending + 1] = pid
            end
        end
    end

    if #pending > 0 then
        local search = db.getResult("SELECT `name` FROM `players` WHERE `id` IN (" .. table.concat(pending, ",") .. ")")
        if search:getID() ~= -1 then
            while true do
                names[#names + 1] = search:getDataString("name")

                if not search:next() then
                    break
                end
            end
            search:free()
        end
    end

    if sort then
        table.sort(names, function(a, b) return a:upper() < b:upper() end)
    end
  
    return names
end

Usage:
Lua:
    local names = getVipListNames(cid)
    for i = 1, #names do
        print(names[i])
    end
 
List will be always up to date regardless if player has been saved or not

C++ (luascript.cpp)
C++:
//getPlayerVipList(cid)
lua_register(m_luaState, "getPlayerVipList", LuaInterface::luaGetPlayerVipList);

C++:
int32_t LuaInterface::luaGetPlayerVipList(lua_State* L)
{
    //getPlayerVipList(cid)
    ScriptEnviroment* env = getEnv();
    if (Player* player = env->getPlayerByUID(popNumber(L)))
    {
        int32_t i = 1;
        lua_newtable(L);

        for (uint32_t playerEntry : player->VIPList)
        {
            lua_pushnumber(L, i++);
            lua_pushnumber(L, playerEntry);
            pushTable(L);
        }
    }
    else
    {
        errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
        lua_pushboolean(L, false);
    }

    return 1;
}

C++ (luascript.h)
C++:
static int32_t luaGetPlayerVipList(lua_State* L);

Lua (data/libs/050-function.lua)
Lua:
function getVipListNames(cid, sort, externalQuery)

    local names = {}
    local pending = {}
    local list = getPlayerVipList(cid)

    for i, pid in ipairs(list) do
        local player = getPlayerByGUID(pid)
        if player then
            names[#names + 1] = getCreatureName(player)
        else
            if not externalQuery then
                -- # players names are cached in memory once they logged in
                -- # still I recommend to use the external query
                names[#names + 1] = getPlayerNameByGUID(pid)
            else
                pending[#pending + 1] = pid
            end
        end
    end

    if #pending > 0 then
        local search = db.getResult("SELECT `name` FROM `players` WHERE `id` IN (" .. table.concat(pending, ",") .. ")")
        if search:getID() ~= -1 then
            while true do
                names[#names + 1] = search:getDataString("name")

                if not search:next() then
                    break
                end
            end
            search:free()
        end
    end

    if sort then
        table.sort(names, function(a, b) return a:upper() < b:upper() end)
    end
  
    return names
end

Usage:
Lua:
    local names = getVipListNames(cid)
    for i = 1, #names do
        print(names[i])
    end
Thats works perfectly!!!

Thank u very much
 
Back
Top