Cleaned up use of the Game module

This commit is contained in:
Cooldude2606
2020-08-14 01:54:51 +01:00
parent 86370aeada
commit e0eba193a4
14 changed files with 69 additions and 89 deletions

View File

@@ -2,12 +2,11 @@
-- @addon Advanced-Start
local Event = require 'utils.event' --- @dep utils.event
local Game = require 'utils.game' --- @dep utils.game
local config = require 'config.advanced_start' --- @dep config.advanced_start
local items = config.items
Event.add(defines.events.on_player_created, function(event)
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
-- game init settings
if event.player_index == 1 then
player.force.friendly_fire = config.friendly_fire

View File

@@ -10,7 +10,7 @@ local send_text = Game.print_player_floating_text -- (player_index, text, color)
Event.add(defines.events.on_console_chat, function(event)
if not event.player_index or event.player_index < 1 then return end
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
-- Some basic sanity checks
if not player then return end

View File

@@ -9,7 +9,7 @@ local config = require 'config.chat_reply' --- @dep config.chat_reply
Event.add(defines.events.on_console_chat, function(event)
local player_index = event.player_index
if not player_index or player_index < 1 then return end
local player = Game.get_player_by_index(player_index)
local player = game.players[player_index]
local message = event.message:lower():gsub("%s+", "")
local allowed = true
if config.command_admin_only and not player.admin then allowed = false end

View File

@@ -95,7 +95,7 @@ end
-- When the first player is created this will create all compilatrons that are resisted in the config
Event.add(defines.events.on_player_created, function(event)
if event.player_index ~= 1 then return end
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
for location in pairs(locations) do
Public.spawn_compilatron(player.surface, location)
end

View File

@@ -2,7 +2,6 @@
-- @addon Death-Logger
local Event = require 'utils.event' --- @dep utils.event
local Game = require 'utils.game' --- @dep utils.game
local Global = require 'utils.global' --- @dep utils.global
local config = require 'config.death_logger' --- @dep config.death_logger
local format_time, move_items = _C.format_time, _C.move_items --- @dep expcore.common
@@ -17,7 +16,7 @@ end)
--- Creates a new death marker and saves it to the given death
local function create_map_tag(death)
local player = Game.get_player_from_any(death.player_name)
local player = game.players[death.player_name]
local message = player.name..' died'
if config.include_time_of_death then
local time = format_time(death.time_of_death, {hours=true, minutes=true, string=true})
@@ -59,7 +58,7 @@ end
-- when a player dies a new death is added to the records and a map marker is made
Event.add(defines.events.on_player_died, function(event)
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
local corpse = player.surface.find_entity('character-corpse', player.position)
if config.use_chests_as_bodies then
local items = corpse.get_inventory(defines.inventory.character_corpse).get_contents()

View File

@@ -8,7 +8,7 @@ local write_json, format_time = _C.write_json, _C.format_time --- @dep expcore.c
local config = require 'config.discord_alerts' --- @dep config.discord_alerts
local function get_player_name(event)
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
return player.name, event.by_player_name
end
@@ -189,7 +189,7 @@ end
if config.player_bans then
Event.add(defines.events.on_player_banned, function(event)
if event.by_player then
local by_player = Game.get_player_by_index(event.by_player)
local by_player = game.players[event.by_player]
emit_event{
title='Banned',
description='A player has been banned',
@@ -202,7 +202,7 @@ if config.player_bans then
end)
Event.add(defines.events.on_player_unbanned, function(event)
if event.by_player then
local by_player = Game.get_player_by_index(event.by_player)
local by_player = game.players[event.by_player]
emit_event{
title='Un-Banned',
description='A player has been un-banned',
@@ -241,7 +241,7 @@ if config.player_kicks then
Event.add(defines.events.on_player_kicked, function(event)
if event.by_player then
local player_name = get_player_name(event)
local by_player = Game.get_player_by_index(event.by_player)
local by_player = game.players[event.by_player]
emit_event{
title='Kick',
description='A player has been kicked',

View File

@@ -2,7 +2,6 @@
-- @addon Scorched-Earth
local Event = require 'utils.event' --- @dep utils.event
local Game = require 'utils.game' --- @dep utils.game
local Global = require 'utils.global' --- @dep utils.global
local print_grid_value, clear_flying_text = _C.print_grid_value, _C.clear_flying_text --- @dep expcore.common
local config = require 'config.scorched_earth' --- @dep config.scorched_earth
@@ -94,7 +93,7 @@ end
-- When the player changes position the tile will have a chance to downgrade, debug check is here
Event.add(defines.events.on_player_changed_position, function(event)
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
local surface = player.surface
local position = player.position
local strength = get_tile_strength(surface, position)
@@ -133,7 +132,7 @@ end)
-- Used as a way to access the global table
return function(player_name, state)
local player = Game.get_player_from_any(player_name)
local player = game.players[player_name]
clear_flying_text(player.surface)
debug_players[player_name] = state
end

View File

@@ -134,7 +134,7 @@ end)
Event.add(defines.events.on_player_created, function(event)
if event.player_index ~= 1 then return end
local player = Game.get_player_by_index(event.player_index)
local player = game.players[event.player_index]
local p = {x=0, y=0}
local s = player.surface
spawn_base(s, p)

View File

@@ -19,7 +19,7 @@ Event.add(defines.events.on_marked_for_deconstruction, function(event)
-- Check which type of decon a player is allowed
local index = event.player_index
if chache[index] == nil then
local player = Game.get_player_by_index(index)
local player = game.players[index]
if Roles.player_allowed(player, 'fast-tree-decon') then chache[index] = 'fast'
elseif Roles.player_allowed(player, 'standard-decon') then chache[index] = 'standard'
else chache[index] = player.force end

View File

@@ -8,7 +8,6 @@
local Gui = require 'expcore.gui' --- @dep expcore.gui
local Roles = require 'expcore.roles' --- @dep expcore.roles
local Datastore = require 'expcore.datastore' --- @dep expcore.datastore
local Game = require 'utils.game' --- @dep utils.game
local Event = require 'utils.event' --- @dep utils.event
local config = require 'config.gui.player_list_actions' --- @dep config.gui.player_list_actions
local Colors = require 'utils.color_presets' --- @dep utils.color_presets
@@ -115,7 +114,7 @@ Gui.element(function(event_trigger, parent, player_data)
end)
:on_click(function(player, element, event)
local selected_player_name = element.caption
local selected_player = Game.get_player_from_any(selected_player_name)
local selected_player = game.players[selected_player_name]
if event.button == defines.mouse_button_type.left then
-- LMB will open the map to the selected player
local position = selected_player.position
@@ -177,7 +176,7 @@ local function update_action_bar(element)
element.visible = false
else
local selected_player = Game.get_player_from_any(selected_player_name)
local selected_player = game.players[selected_player_name]
if not selected_player.connected then
-- If the player is offline then reest stores
element.visible = false
@@ -357,7 +356,7 @@ end)
--- When a player leaves only remove they entry
Event.add(defines.events.on_player_left_game, function(event)
local remove_player = Game.get_player_by_index(event.player_index)
local remove_player = game.players[event.player_index]
for _, player in pairs(game.connected_players) do
local frame = Gui.get_left_element(player, player_list_container)
local scroll_table = frame.container.scroll.table
@@ -390,7 +389,7 @@ Event.add(Roles.events.on_role_unassigned, redraw_player_list)
--- When the action player is changed the action bar will update
SelectedPlayer:on_update(function(player_name, selected_player)
local player = Game.get_player_from_any(player_name)
local player = game.players[player_name]
local frame = Gui.get_left_element(player, player_list_container)
local scroll_table = frame.container.scroll.table
update_action_bar(frame.container.action_bar)
@@ -410,13 +409,13 @@ end)
--- When the action name is changed the reason input will update
SelectedAction:on_update(function(player_name, selected_action)
local player = Game.get_player_from_any(player_name)
local player = game.players[player_name]
local frame = Gui.get_left_element(player, player_list_container)
local element = frame.container.reason_bar
if selected_action then
-- if there is a new value then check the player is still online
local selected_player_name = SelectedPlayer:get(player_name)
local selected_player = Game.get_player_from_any(selected_player_name)
local selected_player = game.players[selected_player_name]
if selected_player.connected then
element.visible = true
else