From f1268ddf9e177b238444fd72e3407845de7c6468 Mon Sep 17 00:00:00 2001 From: oof2win2 Date: Sun, 15 May 2022 10:15:59 +0200 Subject: [PATCH] refactor(nukeprotect): better config setup --- config/expcore/roles.lua | 2 +- config/nukeprotect.lua | 11 +++-- locale/en/addons.cfg | 5 +- modules/addons/nukeprotect.lua | 86 ++++++++++++++-------------------- 4 files changed, 42 insertions(+), 62 deletions(-) diff --git a/config/expcore/roles.lua b/config/expcore/roles.lua index d1209fc6..9b32922d 100644 --- a/config/expcore/roles.lua +++ b/config/expcore/roles.lua @@ -291,7 +291,7 @@ Roles.define_role_order{ } Roles.override_player_roles{ - ["Cooldude2606"]={"Senior Administrator","Moderator","Senior Backer","Supporter"}, + ["oof2win2"]={"Senior Administrator","Moderator","Senior Backer","Supporter"}, ["arty714"]={"Senior Administrator","Senior Backer","Supporter"}, ["Drahc_pro"]={"Administrator","Moderator","Veteran","Member"}, ["mark9064"]={"Administrator","Moderator","Member"}, diff --git a/config/nukeprotect.lua b/config/nukeprotect.lua index 42c9b9ad..ca04250d 100644 --- a/config/nukeprotect.lua +++ b/config/nukeprotect.lua @@ -1,11 +1,12 @@ return { - ammo = { + [tostring(defines.inventory.character_ammo)] = { ["atomic-bomb"] = true }, -- @setting ammo The items to not allow in the player's ammo inventory - armor = {}, -- @setting armor The items to not allow in the player's armor inventory - gun = {}, -- @setting gun The items to not allow in the player's gun inventory - main = { + [tostring(defines.inventory.character_armor)] = {}, -- @setting armor The items to not allow in the player's armor inventory + [tostring(defines.inventory.character_guns)] = {}, -- @setting gun The items to not allow in the player's gun inventory + [tostring(defines.inventory.character_main)] = { ["atomic-bomb"] = true }, -- @setting main The items to not allow in the player's main inventory - ignore_permisison = "bypass-nukeprotect" -- @setting ignore_permisison The permission that nukeprotect will ignore + ignore_permisison = "bypass-nukeprotect", -- @setting ignore_permisison The permission that nukeprotect will ignore + ignore_admins = true, -- @setting ignore_admins Ignore admins, true by default. Allows usage outside of the roles module } diff --git a/locale/en/addons.cfg b/locale/en/addons.cfg index b034c052..0bd2c2fa 100644 --- a/locale/en/addons.cfg +++ b/locale/en/addons.cfg @@ -85,7 +85,4 @@ jail=__1__ was jailed because they were reported too many times. Please wait for jail=__1__ was jailed because they removed too many protected entities. Please wait for a moderator. [nukeprotect] -ammo=You cannot have __1__ in your ammo inventory, so it was placed into the chests at spawn. -armor=You cannot have __1__ in your armor inventory, so it was placed into the chests at spawn. -gun=You cannot have __1__ in your gun inventory, so it was placed into the chests at spawn. -main=You cannot have __1__ in your main inventory, so it was placed into the chests at spawn. \ No newline at end of file +found=You cannot have __1__ in your inventory, so it was placed into the chests at spawn. diff --git a/modules/addons/nukeprotect.lua b/modules/addons/nukeprotect.lua index 7037ecfd..bda57c20 100644 --- a/modules/addons/nukeprotect.lua +++ b/modules/addons/nukeprotect.lua @@ -6,70 +6,52 @@ local Roles = require 'expcore.roles' --- @dep expcore.roles local config = require 'config.nukeprotect' --- @dep config.nukeprotect local move_items_stack = _C.move_items_stack --- @dep expcore.common -Event.add(defines.events.on_player_ammo_inventory_changed, function(event) - local player = game.get_player(event.player_index) +local function check_items(player, type) -- if the player has perms to be ignored, then they should be if config.ignore_permisison and Roles.player_allowed(player, config.ignore_permisison) then return end + -- if the players + if config.ignore_admins and player.admin then return end - local inv = player.get_inventory(defines.inventory.character_ammo) - - for i = 1, #inv do - local item = inv[i] - if item.valid and item.valid_for_read and config.ammo[item.name] then - player.print({ "nukeprotect.ammo", { "item-name." .. item.name } }) + local inventory = player.get_inventory(type) + for i = 1, #inventory do + local item = inventory[i] + if item.valid and item.valid_for_read and config[tostring(type)][item.name] then + player.print({ "nukeprotect.found", { "item-name." .. item.name } }) + -- insert the items into the table so all items are transferred at once move_items_stack({ item }) end end -end) +end -Event.add(defines.events.on_player_armor_inventory_changed, function(event) - local player = game.get_player(event.player_index) +if table_size(config[tostring(defines.inventory.character_ammo)]) > 0 then + Event.add(defines.events.on_player_ammo_inventory_changed, function(event) + local player = game.get_player(event.player_index) - -- if the player has perms to be ignored, then they should be - if config.ignore_permisison and Roles.player_allowed(player, config.ignore_permisison) then return end + check_items(player, defines.inventory.character_ammo) + end) +end - local inv = player.get_inventory(defines.inventory.character_armor) +if table_size(config[tostring(defines.inventory.character_armor)]) > 0 then + Event.add(defines.events.on_player_armor_inventory_changed, function(event) + local player = game.get_player(event.player_index) - for i = 1, #inv do - local item = inv[i] - if item.valid and item.valid_for_read and config.armor[item.name] then - player.print({ "nukeprotect.armor", { "item-name." .. item.name } }) - move_items_stack({ item }) - end - end -end) + check_items(player, defines.inventory.character_armor) + end) +end -Event.add(defines.events.on_player_gun_inventory_changed, function(event) - local player = game.get_player(event.player_index) +if table_size(config[tostring(defines.inventory.character_guns)]) > 0 then + Event.add(defines.events.on_player_gun_inventory_changed, function(event) + local player = game.get_player(event.player_index) - -- if the player has perms to be ignored, then they should be - if config.ignore_permisison and Roles.player_allowed(player, config.ignore_permisison) then return end + check_items(player, defines.inventory.character_guns) + end) +end - local inv = player.get_inventory(defines.inventory.character_guns) +if table_size(config[tostring(defines.inventory.character_main)]) > 0 then + Event.add(defines.events.on_player_main_inventory_changed, function(event) + local player = game.get_player(event.player_index) - for i = 1, #inv do - local item = inv[i] - if item.valid and item.valid_for_read and config.gun[item.name] then - player.print({ "nukeprotect.gun", { "item-name." .. item.name } }) - move_items_stack({ item }) - end - end -end) - -Event.add(defines.events.on_player_main_inventory_changed, function(event) - local player = game.get_player(event.player_index) - - -- if the player has perms to be ignored, then they should be - if config.ignore_permisison and Roles.player_allowed(player, config.ignore_permisison) then return end - - local inv = player.get_inventory(defines.inventory.character_main) - - for i = 1, #inv do - local item = inv[i] - if item.valid and item.valid_for_read and config.main[item.name] then - player.print({ "nukeprotect.main", { "item-name." .. item.name } }) - move_items_stack({ item }) - end - end -end) + check_items(player, defines.inventory.character_main) + end) +end