```lua
-- 你的代码中已经有一个主函数 moshenyuanli.main,这个函数会在点击NPC时触发
-- 你只需要确保这个函数只被调用一次,并且点击其他按钮时不会触发
-- 在 moshenyuanli.main 函数中添加一个检查,确保只有在点击NPC时触发
function moshenyuanli.main(param1, param2, param3, param4)
-- 检查是否是NPC点击事件,param4通常包含NPC相关信息
if not param4 or type(param4) ~= "table" or not param4.npcID then
return -- 不是NPC点击,直接返回不执行
end
-- 添加一个标志位,确保只触发一次
if moshenyuanli.hasTriggered then
return -- 已经触发过,直接返回
end
lib:Require(param1, function(state)
if state then
if param1 then
moshenyuanli.parent = lib:openwin(param1, nil, true, param4) or {}
moshenyuanli.param1 = param1
end
if param2 then
moshenyuanli.param2 = param2
end
if param3 then
moshenyuanli.param3 = param3
end
-- 设置触发标志
moshenyuanli.hasTriggered = true
if moshenyuanli.parent.cfg and next(moshenyuanli.parent.cfg) then
moshenyuanli.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(moshenyuanli.parent._parent)
end
end
end)
end
-- 在界面关闭时重置标志位
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "moshenyuanli", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "moshenyuanli" then
-- 重置触发标志
moshenyuanli.hasTriggered = false
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "moshenyuanli")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "moshenyuanli")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "moshenyuanli")
end
end)
```
这个修改确保了:
1. 只有在点击NPC时才会触发主函数(通过检查param4中的npcID)
2. 只触发一次(通过`hasTriggered`标志位)
3. 界面关闭时重置标志位,允许下次重新打开
注意:`lib:openwin`函数的第四个参数通常是NPC信息,包含了npcID。如果你的参数结构不同,请根据实际情况调整检查条件。