From 9a173edea7f75307dd12deac45db23682088b96b Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Tue, 24 Dec 2024 15:21:30 +0000 Subject: [PATCH] Fix use of created_entity --- .../modules/addons/station-auto-name.lua | 23 +++++++++++-------- exp_legacy/module/modules/control/rockets.lua | 3 ++- exp_legacy/module/modules/gui/rocket-info.lua | 3 ++- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/exp_legacy/module/modules/addons/station-auto-name.lua b/exp_legacy/module/modules/addons/station-auto-name.lua index cefeeebe..dbc5ccce 100644 --- a/exp_legacy/module/modules/addons/station-auto-name.lua +++ b/exp_legacy/module/modules/addons/station-auto-name.lua @@ -15,6 +15,8 @@ local directions = { ["SW"] = 0.875, } +--- @param entity LuaEntity +--- @return string local function get_direction(entity) local angle = math.atan2(entity.position.y, entity.position.x) / math.pi for direction, required_angle in pairs(directions) do @@ -29,19 +31,20 @@ end local custom_string = " *" local custom_string_len = #custom_string +--- @param event EventData.on_built_entity | EventData.on_robot_built_entity local function station_name_changer(event) - local entity = event.created_entity + local entity = event.entity local name = entity.name if name == "entity-ghost" then if entity.ghost_name ~= "train-stop" then return end - local backername = entity.backer_name - if backername ~= "" then - entity.backer_name = backername .. custom_string + local backer_name = entity.backer_name + if backer_name ~= "" then + entity.backer_name = backer_name .. custom_string end elseif name == "train-stop" then -- only do the event if its a train stop - local backername = entity.backer_name - if backername:sub(-custom_string_len) == custom_string then - entity.backer_name = backername:sub(1, -custom_string_len - 1) + local backer_name = entity.backer_name or "" + if backer_name:sub(-custom_string_len) == custom_string then + entity.backer_name = backer_name:sub(1, -custom_string_len - 1) return end @@ -49,14 +52,14 @@ local function station_name_changer(event) -- expanded box for recourse search: local bounding2 = { { bounding_box.left_top.x - 100, bounding_box.left_top.y - 100 }, { bounding_box.right_bottom.x + 100, bounding_box.right_bottom.y + 100 } } -- gets all resources in bounding_box2: - local recourses = game.surfaces[1].find_entities_filtered{ area = bounding2, type = "resource" } - if #recourses > 0 then -- save cpu time if their are no recourses in bounding_box2 + local resources = game.surfaces[1].find_entities_filtered{ area = bounding2, type = "resource" } + if #resources > 0 then -- save cpu time if their are no resources in bounding_box2 local closest_distance local px, py = bounding_box.left_top.x, bounding_box.left_top.y local recourse_closed -- Check which recourse is closest - for i, item in ipairs(recourses) do + for i, item in ipairs(resources) do local dx, dy = px - item.bounding_box.left_top.x, py - item.bounding_box.left_top.y local distance = (dx * dx) + (dy * dy) if not closest_distance or distance < closest_distance then diff --git a/exp_legacy/module/modules/control/rockets.lua b/exp_legacy/module/modules/control/rockets.lua index 5b914235..195ac670 100644 --- a/exp_legacy/module/modules/control/rockets.lua +++ b/exp_legacy/module/modules/control/rockets.lua @@ -195,8 +195,9 @@ Event.add(defines.events.on_rocket_launch_ordered, function(event) end) --- Adds a silo to the list when it is built +--- @param event EventData.on_built_entity | EventData.on_robot_built_entity local function on_built(event) - local entity = event.created_entity + local entity = event.entity if entity.valid and entity.name == "rocket-silo" then local force = entity.force local force_name = force.name diff --git a/exp_legacy/module/modules/gui/rocket-info.lua b/exp_legacy/module/modules/gui/rocket-info.lua index 84c63fb7..9e404d15 100644 --- a/exp_legacy/module/modules/gui/rocket-info.lua +++ b/exp_legacy/module/modules/gui/rocket-info.lua @@ -574,8 +574,9 @@ Event.on_nth_tick(150, function() end) --- Adds a silo to the list when it is built +--- @param event EventData.on_built_entity | EventData.on_robot_built_entity local function on_built(event) - local entity = event.created_entity + local entity = event.entity if entity.valid and entity.name == "rocket-silo" then update_rocket_gui_progress(entity.force.name) end