From bb3b20d271aef881a90eadd00aec78656435bf15 Mon Sep 17 00:00:00 2001 From: dutchie031 Date: Wed, 23 Oct 2024 15:49:17 +0200 Subject: [PATCH] fixed nil pointer issue --- classes/spearhead_events.lua | 40 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/classes/spearhead_events.lua b/classes/spearhead_events.lua index bebb601..3f248fd 100644 --- a/classes/spearhead_events.lua +++ b/classes/spearhead_events.lua @@ -1,9 +1,6 @@ local SpearheadEvents = {} do - local SpearheadLogger = Spearhead.LoggerTemplate:new("Spearhead Events", - Spearhead.LoggerTemplate.LogLevelOptions.INFO) - do -- STAGE NUMBER CHANGED local OnStageNumberChangedListeners = {} local OnStageNumberChangedHandlers = {} @@ -48,18 +45,24 @@ do 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 = {} ---Add an event listener to a specific unit ---@param unitName string to call when the unit lands ---@param landListener table table with function OnUnitLanded(self, initiatorUnit, airbase) SpearheadEvents.addOnUnitLandEventListener = function(unitName, landListener) if type(landListener) ~= "table" then - SpearheadLogger:warn("Event handler not of type table/object") + warn("Event handler not of type table/object") return end - SpearheadLogger:debug("Added Land event handler for unit: " .. unitName) - if onLandEventListeners[unitName] == nil then onLandEventListeners[unitName] = {} end @@ -92,7 +95,7 @@ do ---@param handlingObject table object with OnGroupRTB(self, groupName) SpearheadEvents.addOnGroupRTBListener = function(groupName, handlingObject) if type(handlingObject) ~= "table" then - SpearheadLogger:warn("Event handler not of type table/object") + warn("Event handler not of type table/object") return end @@ -106,7 +109,6 @@ do ---Publish the Group to RTB ---@param groupName string SpearheadEvents.PublishRTB = function(groupName) - SpearheadLogger:debug("Publishing RTB event for group " .. groupName) if groupName ~= nil then if OnGroupRTBListeners[groupName] then for _, callable in pairs(OnGroupRTBListeners[groupName]) do @@ -114,7 +116,7 @@ do callable:OnGroupRTB(groupName) end) if err then - SpearheadLogger:error(err) + error(err) end end end @@ -128,7 +130,7 @@ do ---@param handlingObject table object with OnGroupRTBInTen(self, groupName) SpearheadEvents.addOnGroupRTBInTenListener = function(groupName, handlingObject) if type(handlingObject) ~= "table" then - SpearheadLogger:warn("Event handler not of type table/object") + warn("Event handler not of type table/object") return end @@ -142,7 +144,6 @@ do ---Publish the Group is RTB ---@param groupName string SpearheadEvents.PublishRTBInTen = function(groupName) - SpearheadLogger:debug("Publishing RTB in TEN event for group " .. groupName) if groupName ~= nil then if OnGroupRTBInTenListeners[groupName] then for _, callable in pairs(OnGroupRTBInTenListeners[groupName]) do @@ -150,7 +151,7 @@ do callable:OnGroupRTBInTen(groupName) end) if err then - SpearheadLogger:error(err) + error(err) end end end @@ -165,7 +166,7 @@ do ---@param groupName string the groupname to expect SpearheadEvents.addOnGroupOnStationListener = function(groupName, handlingObject) if type(handlingObject) ~= "table" then - SpearheadLogger:warn("Event handler not of type table/object") + warn("Event handler not of type table/object") return end @@ -179,7 +180,6 @@ do ---Publish the Group to RTB ---@param groupName string SpearheadEvents.PublishOnStation = function(groupName) - SpearheadLogger:debug("Publishing onStation event for group " .. groupName) if groupName ~= nil then if OnGroupOnStationListeners[groupName] then for _, callable in pairs(OnGroupOnStationListeners[groupName]) do @@ -187,7 +187,7 @@ do callable:OnGroupOnStation(groupName) end) if err then - SpearheadLogger:error(err) + error(err) end end end @@ -202,7 +202,7 @@ do ---@param listener table object with OnStatusRequestReceived(self, groupId) SpearheadEvents.AddOnStatusRequestReceivedListener = function(listener) 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 end @@ -234,7 +234,7 @@ do ---@param listener table object with OnPlayerEnterUnit(self, unit) SpearheadEvents.AddOnPlayerEnterUnitListener = function(listener) 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 end @@ -249,7 +249,7 @@ do callable:OnPlayerEnterUnit(unit) end) if err then - SpearheadLogger:error(err) + error(err) end end end @@ -270,7 +270,7 @@ do callable:OnUnitLanded(unit, airbase) end) if err then - SpearheadLogger:error(err) + error(err) end end end @@ -289,7 +289,7 @@ do end) if err then - SpearheadLogger:error(err) + error(err) end end end