local EVENTS = {}; local GLOBAL_EVENTS = {}; local _addEventHandler = addEventHandler; local _removeEventHandler = removeEventHandler; local _getEventHandlers = getEventHandlers; local _addEvent = addEvent; function addEvent(sEventName, bGlobal) if type(sEventName) ~= 'string' then return false end if type(bGlobal) ~= 'boolean' then bGlobal = false end if not EVENTS[sEventName] then EVENTS[sEventName] = {} end if bGlobal then GLOBAL_EVENTS[sEventName] = true end return _addEvent(sEventName, bGlobal) end local function getEventHandlers(sEventName, pElementAttachedTo) if type(sEventName) ~= 'string' then return false end if type (pElementAttachedTo) ~= 'userdata' then return false; end if not isElement(pElementAttachedTo) then return false; end return _getEventHandlers(sEventName, pElementAttachedTo) or {} end local function isEventHandlerAdded(sEventName, pElementAttachedTo, func) if type (sEventName) ~= 'string' then return false end if type (func) ~= 'function' then return false end if type (pElementAttachedTo) ~= 'userdata' or not isElement(pElementAttachedTo) then return true end if EVENTS[sEventName] and EVENTS[sEventName][pElementAttachedTo] and EVENTS[sEventName][pElementAttachedTo][func] then return true end local aAttachedFunctions = getEventHandlers(sEventName, pElementAttachedTo) if type(aAttachedFunctions) ~= 'table' then return false end if next(aAttachedFunctions) == nil then return false; end for i, v in next, aAttachedFunctions, nil do if (v == func) then return true end end return false end local function addEventHandler(sEventName, pElementAttachedTo, func, ...) if isEventHandlerAdded(sEventName, pElementAttachedTo, func) then return false end if _addEventHandler(sEventName, pElementAttachedTo, func, ...) then if not EVENTS[sEventName] then EVENTS[sEventName] = {} end if not EVENTS[sEventName][pElementAttachedTo] then EVENTS[sEventName][pElementAttachedTo] = {} end EVENTS[sEventName][pElementAttachedTo][func] = true return true end end local function removeEventHandler(sEventName, pElementAttachedTo, func) if isEventHandlerAdded(sEventName, pElementAttachedTo, func) then if EVENTS[sEventName] and EVENTS[sEventName][pElementAttachedTo] then if EVENTS[sEventName][pElementAttachedTo][func] then EVENTS[sEventName][pElementAttachedTo][func] = nil end if next(EVENTS[sEventName][pElementAttachedTo]) == nil then EVENTS[sEventName][pElementAttachedTo] = nil end if next(EVENTS[sEventName]) == nil then EVENTS[sEventName] = nil end end return _removeEventHandler(sEventName, pElementAttachedTo, func) end return false end local function RegisterNetEvent(event, attach, func, propagate, priority) assert(type(event) == 'string', 'eventName not is a string') addEvent(event, true) if isElement(attach) and func then addEventHandler(event, attach, func, propagate, priority) end end local function RegisterCommand(commandName, func, restrict, sensivite) return addCommandHandler(commandName, func, restrict, sensivite); end _G.addEventHandler = _addEventHandler; _G.removeEventHandler = _removeEventHandler; _G.getEventHandlers = _getEventHandlers; _G.addEventHandler = addEventHandler; _G.removeEventHandler = removeEventHandler; _G.getEventHandlers = getEventHandlers; _G.RegisterNetEvent = RegisterNetEvent; _G.RegisterCommand = RegisterCommand; _G.isEventHandlerAdded = isEventHandlerAdded;