fixed nil pointer issue

This commit is contained in:
2024-10-23 15:49:17 +02:00
parent d784ab6e1d
commit bb3b20d271
+20 -20
View File
@@ -1,9 +1,6 @@
local SpearheadEvents = {} local SpearheadEvents = {}
do do
local SpearheadLogger = Spearhead.LoggerTemplate:new("Spearhead Events",
Spearhead.LoggerTemplate.LogLevelOptions.INFO)
do -- STAGE NUMBER CHANGED do -- STAGE NUMBER CHANGED
local OnStageNumberChangedListeners = {} local OnStageNumberChangedListeners = {}
local OnStageNumberChangedHandlers = {} local OnStageNumberChangedHandlers = {}
@@ -48,18 +45,24 @@ do
end end
end end
local warn = function(text)
env.warn("[Spearhead][Events] " .. (text or "nil"))
end
local error = function(text)
env.error("[Spearhead][Events] " .. (text or "nil"))
end
local onLandEventListeners = {} local onLandEventListeners = {}
---Add an event listener to a specific unit ---Add an event listener to a specific unit
---@param unitName string to call when the unit lands ---@param unitName string to call when the unit lands
---@param landListener table table with function OnUnitLanded(self, initiatorUnit, airbase) ---@param landListener table table with function OnUnitLanded(self, initiatorUnit, airbase)
SpearheadEvents.addOnUnitLandEventListener = function(unitName, landListener) SpearheadEvents.addOnUnitLandEventListener = function(unitName, landListener)
if type(landListener) ~= "table" then if type(landListener) ~= "table" then
SpearheadLogger:warn("Event handler not of type table/object") warn("Event handler not of type table/object")
return return
end end
SpearheadLogger:debug("Added Land event handler for unit: " .. unitName)
if onLandEventListeners[unitName] == nil then if onLandEventListeners[unitName] == nil then
onLandEventListeners[unitName] = {} onLandEventListeners[unitName] = {}
end end
@@ -92,7 +95,7 @@ do
---@param handlingObject table object with OnGroupRTB(self, groupName) ---@param handlingObject table object with OnGroupRTB(self, groupName)
SpearheadEvents.addOnGroupRTBListener = function(groupName, handlingObject) SpearheadEvents.addOnGroupRTBListener = function(groupName, handlingObject)
if type(handlingObject) ~= "table" then if type(handlingObject) ~= "table" then
SpearheadLogger:warn("Event handler not of type table/object") warn("Event handler not of type table/object")
return return
end end
@@ -106,7 +109,6 @@ do
---Publish the Group to RTB ---Publish the Group to RTB
---@param groupName string ---@param groupName string
SpearheadEvents.PublishRTB = function(groupName) SpearheadEvents.PublishRTB = function(groupName)
SpearheadLogger:debug("Publishing RTB event for group " .. groupName)
if groupName ~= nil then if groupName ~= nil then
if OnGroupRTBListeners[groupName] then if OnGroupRTBListeners[groupName] then
for _, callable in pairs(OnGroupRTBListeners[groupName]) do for _, callable in pairs(OnGroupRTBListeners[groupName]) do
@@ -114,7 +116,7 @@ do
callable:OnGroupRTB(groupName) callable:OnGroupRTB(groupName)
end) end)
if err then if err then
SpearheadLogger:error(err) error(err)
end end
end end
end end
@@ -128,7 +130,7 @@ do
---@param handlingObject table object with OnGroupRTBInTen(self, groupName) ---@param handlingObject table object with OnGroupRTBInTen(self, groupName)
SpearheadEvents.addOnGroupRTBInTenListener = function(groupName, handlingObject) SpearheadEvents.addOnGroupRTBInTenListener = function(groupName, handlingObject)
if type(handlingObject) ~= "table" then if type(handlingObject) ~= "table" then
SpearheadLogger:warn("Event handler not of type table/object") warn("Event handler not of type table/object")
return return
end end
@@ -142,7 +144,6 @@ do
---Publish the Group is RTB ---Publish the Group is RTB
---@param groupName string ---@param groupName string
SpearheadEvents.PublishRTBInTen = function(groupName) SpearheadEvents.PublishRTBInTen = function(groupName)
SpearheadLogger:debug("Publishing RTB in TEN event for group " .. groupName)
if groupName ~= nil then if groupName ~= nil then
if OnGroupRTBInTenListeners[groupName] then if OnGroupRTBInTenListeners[groupName] then
for _, callable in pairs(OnGroupRTBInTenListeners[groupName]) do for _, callable in pairs(OnGroupRTBInTenListeners[groupName]) do
@@ -150,7 +151,7 @@ do
callable:OnGroupRTBInTen(groupName) callable:OnGroupRTBInTen(groupName)
end) end)
if err then if err then
SpearheadLogger:error(err) error(err)
end end
end end
end end
@@ -165,7 +166,7 @@ do
---@param groupName string the groupname to expect ---@param groupName string the groupname to expect
SpearheadEvents.addOnGroupOnStationListener = function(groupName, handlingObject) SpearheadEvents.addOnGroupOnStationListener = function(groupName, handlingObject)
if type(handlingObject) ~= "table" then if type(handlingObject) ~= "table" then
SpearheadLogger:warn("Event handler not of type table/object") warn("Event handler not of type table/object")
return return
end end
@@ -179,7 +180,6 @@ do
---Publish the Group to RTB ---Publish the Group to RTB
---@param groupName string ---@param groupName string
SpearheadEvents.PublishOnStation = function(groupName) SpearheadEvents.PublishOnStation = function(groupName)
SpearheadLogger:debug("Publishing onStation event for group " .. groupName)
if groupName ~= nil then if groupName ~= nil then
if OnGroupOnStationListeners[groupName] then if OnGroupOnStationListeners[groupName] then
for _, callable in pairs(OnGroupOnStationListeners[groupName]) do for _, callable in pairs(OnGroupOnStationListeners[groupName]) do
@@ -187,7 +187,7 @@ do
callable:OnGroupOnStation(groupName) callable:OnGroupOnStation(groupName)
end) end)
if err then if err then
SpearheadLogger:error(err) error(err)
end end
end end
end end
@@ -202,7 +202,7 @@ do
---@param listener table object with OnStatusRequestReceived(self, groupId) ---@param listener table object with OnStatusRequestReceived(self, groupId)
SpearheadEvents.AddOnStatusRequestReceivedListener = function(listener) SpearheadEvents.AddOnStatusRequestReceivedListener = function(listener)
if type(listener) ~= "table" then if type(listener) ~= "table" then
SpearheadLogger:warn("Unit lost Event listener not of type table/object") warn("Unit lost Event listener not of type table/object")
return return
end end
@@ -234,7 +234,7 @@ do
---@param listener table object with OnPlayerEnterUnit(self, unit) ---@param listener table object with OnPlayerEnterUnit(self, unit)
SpearheadEvents.AddOnPlayerEnterUnitListener = function(listener) SpearheadEvents.AddOnPlayerEnterUnitListener = function(listener)
if type(listener) ~= "table" then if type(listener) ~= "table" then
SpearheadLogger:warn("Unit lost Event listener not of type table/object") warn("Unit lost Event listener not of type table/object")
return return
end end
@@ -249,7 +249,7 @@ do
callable:OnPlayerEnterUnit(unit) callable:OnPlayerEnterUnit(unit)
end) end)
if err then if err then
SpearheadLogger:error(err) error(err)
end end
end end
end end
@@ -270,7 +270,7 @@ do
callable:OnUnitLanded(unit, airbase) callable:OnUnitLanded(unit, airbase)
end) end)
if err then if err then
SpearheadLogger:error(err) error(err)
end end
end end
end end
@@ -289,7 +289,7 @@ do
end) end)
if err then if err then
SpearheadLogger:error(err) error(err)
end end
end end
end end