otland / forgottenserver

A free and open-source MMORPG server emulator written in C++
https://otland.net
GNU General Public License v2.0
1.57k stars 1.05k forks source link

onConnectionRestoration event #4651

Open ArturKnopik opened 4 months ago

ArturKnopik commented 4 months ago

Pull Request Prelude

Changes Proposed

new CreatureEvent onConnectionRestoration Handle switching game client without logout which is currently impossible. example: call login CreatureEvent that will set bless icon status, after switching the client the icon will not be updated

local blessStatusLogin = CreatureEvent("blessStatusLogin")
function blessStatusLogin.onLogin(player)
    local blessCount = 0
    for b = 1, 5 do
        if player:hasBlessing(b) then
            blessCount = blessCount + 1
        end
    end

    local msg = NetworkMessage()
    msg:addByte(0x9C);
    if blessCount == 0 then
        msg:addU16(0)
        msg:addByte(1)
        msg:sendToPlayer(player)
        msg:delete()
        return true
    end

    local bits = bit.bor(4, 8, 16, 32, 64)
    if blessCount == 4 then
        msg:addU16(2)
        msg:addByte(2)
    elseif blessCount == 5 then
        msg:addU16(255)
        msg:addByte(3)
    end
    msg:sendToPlayer(player)
    msg:delete()
    return true
end
blessStatusLogin:register()

-- solution

local ce = CreatureEvent("blessStatusConnectionRestoration")
function ce.onConnectionRestoration(player)
    print("blessStatusConnectionRestoration")
    local blessCount = 0
    for b = 1, 5 do
        if player:hasBlessing(b) then
            blessCount = blessCount + 1
        end
    end

    local msg = NetworkMessage()
    msg:addByte(0x9C);
    if blessCount == 0 then
        msg:addU16(0)
        msg:addByte(1)
        msg:sendToPlayer(player)
        msg:delete()
        return true
    end

    local bits = bit.bor(4, 8, 16, 32, 64)
    if blessCount == 4 then
        msg:addU16(2)
        msg:addByte(2)
    elseif blessCount == 5 then
        msg:addU16(255)
        msg:addByte(3)
    end
    msg:sendToPlayer(player)
    msg:delete()
    return true
end
ce:register()