跳转至

游戏钩子(回调)

这些是 GameServer 在特定事件时调用的 Lua 函数。通过 ScriptCore.lua 的 BridgeFunctionAttach() 注册。

OnReadScript()

脚本加载/重载时调用。用于初始化。

function BridgeFunction_OnReadScript()
    LogAdd(LOG_BLUE, "Scripts loaded!")
end

OnShutScript()

服务器关闭时调用。用于清理。

function BridgeFunction_OnShutScript()
    LogAdd(LOG_RED, "Server shutting down...")
end

OnTimerThread()

每个定时器周期调用(约每秒一次)。用于周期性任务。

function BridgeFunction_OnTimerThread()
    -- 周期性逻辑
end

OnConnectDataBase()

数据库连接建立时调用。


OnCommandManager(aIndex, code, arg) -> number

玩家使用自定义指令时调用。返回 1 阻止默认处理,返回 0 继续传递。

function BridgeFunction_OnCommandManager(aIndex, code, arg)
    if code == 100 then
        GCNoticeSend(aIndex, 1, "Custom command executed!")
        return 1 -- 已处理
    end
    return 0 -- 未处理
end

OnCharacterEntry(aIndex)

角色进入游戏世界时调用。

function BridgeFunction_OnCharacterEntry(aIndex)
    local player = GetUser(aIndex)
    if player then
        GCNoticeSend(aIndex, 1, "Welcome, " .. player.Name .. "!")
    end
end

OnCharacterClose(aIndex)

角色断开连接时调用。

function BridgeFunction_OnCharacterClose(aIndex)
    LogAdd(LOG_BLUE, "Player disconnected: index " .. aIndex)
end

OnGremoryCaseReady(aIndex)

玩家的 Gremory Case 数据加载完成时调用。请使用此钩子代替 OnCharacterEntry 进行 Gremory 操作。

function BridgeFunction_OnGremoryCaseReady(aIndex)
    GremoryGive(aIndex, GET_ITEM(14, 13), 0, 0, 0, 0, 0, 0,
        GREMORY_CASE_CHARACTER, GREMORY_REWARD_EVENT, GREMORY_EXPIRE_7_DAYS)
end

OnNpcTalk(aIndex, bIndex) -> number

玩家与 NPC 对话时调用。返回 1 阻止默认对话框,返回 0 允许。

function BridgeFunction_OnNpcTalk(aIndex, bIndex)
    local npc = GetUser(bIndex)
    if npc and npc.Class == 257 then -- 自定义 NPC 类
        GCNoticeSend(aIndex, 1, "Hello from custom NPC!")
        return 1
    end
    return 0
end

OnMonsterDie(aIndex, bIndex) -> number

怪物死亡时调用。aIndex = 怪物,bIndex = 击杀者。返回 1 阻止默认掉落,返回 0 允许。

function BridgeFunction_OnMonsterDie(aIndex, bIndex)
    local monster = GetUser(aIndex)
    if monster and monster.Class == 275 then
        GCNoticeSendToAll(0, "The boss has been defeated!")
    end
    return 0
end

OnUserDie(aIndex, bIndex)

玩家死亡时调用。aIndex = 死者,bIndex = 击杀者。

function BridgeFunction_OnUserDie(aIndex, bIndex)
    LogAdd(LOG_RED, "Player " .. aIndex .. " killed by " .. bIndex)
end

OnUserRespawn(aIndex, killerType)

玩家死后复活时调用。

function BridgeFunction_OnUserRespawn(aIndex, killerType)
    local player = GetUser(aIndex)
    if player then
        player.Life = player.MaxLife
    end
end

OnCheckUserTarget(aIndex, bIndex) -> number

验证玩家是否可以攻击另一个玩家时调用。返回 0 阻止,返回 1 允许。

function BridgeFunction_OnCheckUserTarget(aIndex, bIndex)
    local attacker = GetUser(aIndex)
    local target = GetUser(bIndex)
    if attacker and target and attacker.Map == 0 then
        return 0 -- Lorencia 禁止 PvP
    end
    return 1
end

OnCheckUserKiller(aIndex, bIndex) -> number

验证 PK 惩罚时调用。返回 0 跳过 PK 处理,返回 1 执行。


OnItemGet(aIndex, itemObjIndex, pItem) -> number

玩家拾取物品时调用。返回 0 阻止,返回 1 允许。

function BridgeFunction_OnItemGet(aIndex, itemObjIndex, pItem)
    if pItem and pItem:IsExcItem() then
        LogAdd(LOG_GREEN, "Player picked up an Excellent item!")
    end
    return 1
end

OnItemDrop(aIndex, slot, x, y, pItem) -> number

玩家丢弃物品时调用。返回 0 阻止,返回 1 允许。

function BridgeFunction_OnItemDrop(aIndex, slot, x, y, pItem)
    if pItem and pItem:IsSetItem() then
        GCNoticeSend(aIndex, 1, "You cannot drop Ancient items!")
        return 0
    end
    return 1
end

OnPacketRecv(aIndex, lpMsg, size) -> number

接收自定义数据包(headcode 0xFE)时调用。已处理则返回 1。

function BridgeFunction_OnPacketRecv(aIndex, lpMsg, size)
    -- 在此解析自定义数据包
    return 0
end

OnMapChange(aIndex, oldMap, newMap)

玩家切换地图时调用。

function BridgeFunction_OnMapChange(aIndex, oldMap, newMap)
    LogAdd(LOG_BLUE, "Player " .. aIndex .. " moved from map " .. oldMap .. " to " .. newMap)
end

OnValidateSkillUse(aIndex, skillIndex, [targetIndex]) -> boolean

验证技能是否可以使用时调用。返回 true 允许,返回 false 阻止。有两个重载版本(有/无目标)。

function BridgeFunction_OnValidateSkillUse(aIndex, skillIndex, targetIndex)
    if skillIndex == 41 and GetUser(aIndex).Map == 6 then
        GCNoticeSend(aIndex, 1, "This skill is disabled in the Arena!")
        return false
    end
    return true
end

OnTradeStart(aIndex, bIndex)

两个玩家开始交易时调用。

function BridgeFunction_OnTradeStart(aIndex, bIndex)
    LogAdd(LOG_BLUE, "Trade started: " .. aIndex .. " <-> " .. bIndex)
end

OnTradeConfirm(aIndex, bIndex)

双方确认交易时调用。

function BridgeFunction_OnTradeConfirm(aIndex, bIndex)
    LogAdd(LOG_GREEN, "Trade confirmed: " .. aIndex .. " <-> " .. bIndex)
end