Narrow the gui player lookups

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
bbassie
2026-08-07 15:31:43 +00:00
co-authored by Claude Opus 5
parent 3c0b5069fc
commit 231c3a9ea0
4 changed files with 14 additions and 12 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ function Search.prepare(custom_commands)
command_objects[name] = { command_objects[name] = {
name = name, name = name,
description = locale_desc, description = locale_desc,
help_text = locale_desc, help_text = locale_desc,
usage = locale_desc, usage = locale_desc,
aliases = {}, aliases = {},
} }
+5 -3
View File
@@ -46,8 +46,10 @@ end
--- @return LuaPlayer --- @return LuaPlayer
function Gui.get_player(input) function Gui.get_player(input)
if type(input) == "table" and not input.player_index then if type(input) == "table" and not input.player_index then
--- @cast input { element: LuaGuiElement }
return assert(game.get_player(input.element.player_index)) return assert(game.get_player(input.element.player_index))
end end
--- @cast input LuaGuiElement | { player_index: uint }
return assert(game.get_player(input.player_index)) return assert(game.get_player(input.player_index))
end end
@@ -114,7 +116,7 @@ end
--- @param player LuaPlayer --- @param player LuaPlayer
--- @return LuaGuiElement --- @return LuaGuiElement
function Gui.get_top_element(define, player) function Gui.get_top_element(define, player)
return assert(player_elements[player.index].top[define.name], "Element is not on the top flow") return assert(assert(player_elements[player.index]).top[define.name], "Element is not on the top flow")
end end
--- Register a element define to be drawn to the left flow on join --- Register a element define to be drawn to the left flow on join
@@ -122,7 +124,7 @@ end
--- @param player LuaPlayer --- @param player LuaPlayer
--- @return LuaGuiElement --- @return LuaGuiElement
function Gui.get_left_element(define, player) function Gui.get_left_element(define, player)
return assert(player_elements[player.index].left[define.name], "Element is not on the left flow") return assert(assert(player_elements[player.index]).left[define.name], "Element is not on the left flow")
end end
--- Register a element define to be drawn to the relative flow on join --- Register a element define to be drawn to the relative flow on join
@@ -130,7 +132,7 @@ end
--- @param player LuaPlayer --- @param player LuaPlayer
--- @return LuaGuiElement --- @return LuaGuiElement
function Gui.get_relative_element(define, player) function Gui.get_relative_element(define, player)
return assert(player_elements[player.index].relative[define.name], "Element is not on the relative flow") return assert(assert(player_elements[player.index]).relative[define.name], "Element is not on the relative flow")
end end
--- Ensure all the correct elements are visible and exist --- Ensure all the correct elements are visible and exist
+3 -3
View File
@@ -61,7 +61,7 @@ local GuiData = {
--- @field element_data table<uint, table<uint, any>> --- @field element_data table<uint, table<uint, any>>
--- @field player_data table<uint, any> --- @field player_data table<uint, any>
--- @field force_data table<uint, any> --- @field force_data table<uint, any>
--- @field global_data table --- @field global_data table
--- @field [DataKey] any --- @field [DataKey] any
-- This class has no prototype methods -- This class has no prototype methods
-- Same as raw but __index ensures the values exist -- Same as raw but __index ensures the values exist
@@ -99,7 +99,7 @@ function GuiData._metatable.__index(self, key)
local data = self._raw.player_data local data = self._raw.player_data
return data and data[key.index] return data and data[key.index]
elseif object_name == "LuaForce" then elseif object_name == "LuaForce" then
--- @cast key LuaForce --- @cast key LuaForce
local data = self._raw.force_data local data = self._raw.force_data
return data and data[key.index] return data and data[key.index]
else else
@@ -172,7 +172,7 @@ local function on_object_destroyed(event)
for _, scope in pairs(registered_scopes) do for _, scope in pairs(registered_scopes) do
local data = scope._raw.element_data local data = scope._raw.element_data
local player_elements = data and data[player_index] local player_elements = data and data[player_index]
if player_elements then if data and player_elements then
player_elements[element_index] = nil player_elements[element_index] = nil
if not next(player_elements) then if not next(player_elements) then
data[player_index] = nil data[player_index] = nil
+5 -5
View File
@@ -44,7 +44,7 @@ function FlyingText.create_above_entity(options)
local entity = assert(options.target_entity, "A target entity is required") local entity = assert(options.target_entity, "A target entity is required")
local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]] local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]]
local size_y = bounding_box.left_top.y - bounding_box.right_bottom.y local size_y = bounding_box.left_top.y - bounding_box.right_bottom.y
local position = entity.position --[[@as MapPosition.struct]] local position = entity.position --[[@as MapPosition.struct]]
local offset = options.offset or { x = 0, y = 0 } local offset = options.offset or { x = 0, y = 0 }
options.position = { options.position = {
@@ -64,9 +64,9 @@ end
function FlyingText.create_above_player(options) function FlyingText.create_above_player(options)
local player = assert(options.target_player, "A target player is required") local player = assert(options.target_player, "A target player is required")
local entity = player.character; if not entity then return end local entity = player.character; if not entity then return end
local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]] local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]]
local size_y = bounding_box.left_top.y - bounding_box.right_bottom.y local size_y = bounding_box.left_top.y - bounding_box.right_bottom.y
local position = entity.position --[[@as MapPosition.struct]] local position = entity.position --[[@as MapPosition.struct]]
local offset = options.offset or { x = 0, y = 0 } local offset = options.offset or { x = 0, y = 0 }
options.position = { options.position = {
@@ -86,9 +86,9 @@ end
function FlyingText.create_as_player(options) function FlyingText.create_as_player(options)
local player = assert(options.target_player, "A target player is required") local player = assert(options.target_player, "A target player is required")
local entity = player.character; if not entity then return end local entity = player.character; if not entity then return end
local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]] local bounding_box = entity.bounding_box --[[@as ExpUtil_AABB.Box]]
local size_y = bounding_box.left_top.y - bounding_box.right_bottom.y local size_y = bounding_box.left_top.y - bounding_box.right_bottom.y
local position = entity.position --[[@as MapPosition.struct]] local position = entity.position --[[@as MapPosition.struct]]
local offset = options.offset or { x = 0, y = 0 } local offset = options.offset or { x = 0, y = 0 }
options.color = player.chat_color options.color = player.chat_color