diff --git a/modules/ExpGamingCommands/admin/control.lua b/modules/ExpGamingAdmin/Commands/control.lua similarity index 88% rename from modules/ExpGamingCommands/admin/control.lua rename to modules/ExpGamingAdmin/Commands/control.lua index d243f44c..18c3b1d2 100644 --- a/modules/ExpGamingCommands/admin/control.lua +++ b/modules/ExpGamingAdmin/Commands/control.lua @@ -1,11 +1,9 @@ --- A full ranking system for factorio. --- @module ExpGamingCommands.kill +-- @module ExpGamingAdmin.Commands@4.0.0 -- @author Cooldude2606 -- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE --- this file should be replaced with a collection but idk if fsm can hanndle collections in a collection - -local Admin = require('ExpGamingAdmin.AdminLib') +local Admin = require('ExpGamingAdmin.AdminLib@^4.0.0') --- Used to clear all parts of a player, removing warnings, reports, jail and temp ban -- @command clear-all diff --git a/modules/ExpGamingCommands/admin/softmod.json b/modules/ExpGamingAdmin/Commands/softmod.json similarity index 92% rename from modules/ExpGamingCommands/admin/softmod.json rename to modules/ExpGamingAdmin/Commands/softmod.json index 2b30520f..e694e727 100644 --- a/modules/ExpGamingCommands/admin/softmod.json +++ b/modules/ExpGamingAdmin/Commands/softmod.json @@ -1,5 +1,5 @@ { - "name": "admin", + "name": "Commands", "version": "4.0.0", "type": "Submodule", "description": "Admins many of the admin featues which the script can use as in game commands.", @@ -24,5 +24,5 @@ "ExpGamingAdmin.Reports": "^?4.0.0", "ExpGamingAdmin.ClearInventory": "?^4.0.0" }, - "collection": "ExpGamingCommands_4.0.0" + "collection": "ExpGamingAdmin_4.0.0" } \ No newline at end of file diff --git a/modules/ExpGamingCommands/admin/src/clear.lua b/modules/ExpGamingAdmin/Commands/src/clear.lua similarity index 100% rename from modules/ExpGamingCommands/admin/src/clear.lua rename to modules/ExpGamingAdmin/Commands/src/clear.lua diff --git a/modules/ExpGamingCommands/admin/src/jail.lua b/modules/ExpGamingAdmin/Commands/src/jail.lua similarity index 100% rename from modules/ExpGamingCommands/admin/src/jail.lua rename to modules/ExpGamingAdmin/Commands/src/jail.lua diff --git a/modules/ExpGamingCommands/admin/src/reports.lua b/modules/ExpGamingAdmin/Commands/src/reports.lua similarity index 96% rename from modules/ExpGamingCommands/admin/src/reports.lua rename to modules/ExpGamingAdmin/Commands/src/reports.lua index e4e5aba7..f1582c0d 100644 --- a/modules/ExpGamingCommands/admin/src/reports.lua +++ b/modules/ExpGamingAdmin/Commands/src/reports.lua @@ -1,5 +1,5 @@ local Admin = Admin -local Ranking = require('ExpGamingCore.Ranking') +local Ranking = require('ExpGamingCore.Ranking@^4.0.0') --- Reports a player -- @command report diff --git a/modules/ExpGamingCommands/admin/src/tempban.lua b/modules/ExpGamingAdmin/Commands/src/tempban.lua similarity index 100% rename from modules/ExpGamingCommands/admin/src/tempban.lua rename to modules/ExpGamingAdmin/Commands/src/tempban.lua diff --git a/modules/ExpGamingCommands/admin/src/warnings.lua b/modules/ExpGamingAdmin/Commands/src/warnings.lua similarity index 95% rename from modules/ExpGamingCommands/admin/src/warnings.lua rename to modules/ExpGamingAdmin/Commands/src/warnings.lua index 2e139f4e..321c790a 100644 --- a/modules/ExpGamingCommands/admin/src/warnings.lua +++ b/modules/ExpGamingAdmin/Commands/src/warnings.lua @@ -1,5 +1,5 @@ local Admin = Admin -local Ranking = require('ExpGamingCore.Ranking') +local Ranking = require('ExpGamingCore.Ranking@^4.0.0') --- Gives a warning to a player -- @command warn diff --git a/modules/ExpGamingAdmin/softmod.json b/modules/ExpGamingAdmin/softmod.json index 729e1d0b..64267287 100644 --- a/modules/ExpGamingAdmin/softmod.json +++ b/modules/ExpGamingAdmin/softmod.json @@ -241,6 +241,34 @@ "ExpGamingCore.Sync": "?^4.0.0" }, "collection": "ExpGamingAdmin_4.0.0" + }, + "Commands": { + "name": "Commands", + "version": "4.0.0", + "type": "Submodule", + "description": "Admins many of the admin featues which the script can use as in game commands.", + "location": "https://github.com/explosivegaming/scenario/releases/download/v4.0-core/ExpGamingCommands.admin_4.0.0.zip", + "keywords": [ + "ExpGaming", + "Admin", + "Tools", + "Commands", + "Temp ban", + "Jail", + "Clear Inventory", + "Report", + "Warnings" + ], + "dependencies": { + "ExpGamingLib": "^4.0.0", + "ExpGamingCore.Commands": "^4.0.0", + "ExpGamingAdmin.TempBan": "?^4.0.0", + "ExpGamingAdmin.Jail": "?^4.0.0", + "ExpGamingAdmin.Warnings": "?^4.0.0", + "ExpGamingAdmin.Reports": "^?4.0.0", + "ExpGamingAdmin.ClearInventory": "?^4.0.0" + }, + "collection": "ExpGamingAdmin_4.0.0" } }, "dependencies": {} diff --git a/modules/ExpGamingCommands/bonus/control.lua b/modules/ExpGamingCommands/bonus/control.lua index 1b387a1b..a78624a0 100644 --- a/modules/ExpGamingCommands/bonus/control.lua +++ b/modules/ExpGamingCommands/bonus/control.lua @@ -1,39 +1,39 @@ --- A full ranking system for factorio. --- @module ExpGamingCommands.kill +-- @module ExpGamingCommands.bonus@^4.0.0 -- @author Cooldude2606 -- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE +local global = global{} +local Game = require('FactorioStdLib.Game@^0.8.0') + -- these are the settings which are changed with scale being as +100% local settings = { - {key='character_mining_speed_modifier',scale=3}, - {key='character_crafting_speed_modifier',scale=3}, - {key='character_running_speed_modifier',scale=3}, - {key='character_build_distance_bonus',scale=20}, - {key='character_reach_distance_bonus',scale=20}, - {key='character_inventory_slots_bonus',scale=200} + character_mining_speed_modifier=3, + character_crafting_speed_modifier=3, + character_running_speed_modifier=3, + character_build_distance_bonus=20, + character_reach_distance_bonus=20, + character_inventory_slots_bonus=200 } -local global = global{} -local Game = require('FactorioStdLib.Game') - --- Allows a player to set there bonus -- @command bonus -- @param bonus the amount of bonus there will get commands.add_command('bonus', 'Set your player bonus (default is 20, guest has 0)', { - ['bonus'] = {true,'number-range',-1,50} -- -1 < math.floor(bonus) <= 50 + ['bonus'] = {true,'number-range-int',-1,50} -- -1 < math.floor(bonus) <= 50 }, function(event,args) local player = Game.get_player(event) local bonus = args.bonus - for _,setting in pairs(settings) do player[setting.key] = setting.scale*math.floor(bonus)*0.01 end + for key,setting in pairs(settings) do player[key] = setting*math.floor(bonus)*0.01 end global[player.index]=bonus player_return('Bonus set to: '..math.floor(bonus)..'%') -end) +end).default_admin_only = true script.on_event(defines.events.on_player_respawned,function(event) local player = Game.get_player(event) local bonus = global[player.index] if bonus then - for _,setting in pairs(settings) do player[setting.key] = setting.scale*math.floor(bonus)*0.01 end + for _,setting in pairs(settings) do player[key] = setting*math.floor(bonus)*0.01 end end end) @@ -54,8 +54,8 @@ end) return { on_init= function(self) - if loaded_modules['ExpGamingCore.Ranking'] then - local Ranking = require('ExpGamingCore.Ranking') + if loaded_modules['ExpGamingCore.Ranking@^4.0.0'] then + local Ranking = require('ExpGamingCore.Ranking@^4.0.0') -- instant respawn script.on_event(defines.events.on_pre_player_died,function(event) local player = Game.get_player(event) @@ -74,10 +74,10 @@ return { script.on_event(defines.events.rank_change,function(event) local player = Game.get_player(event) if event.new_rank:allowed('bonus') then - for _,setting in pairs(settings) do player[setting.key] = setting.scale*0.2 end + for _,setting in pairs(settings) do player[key] = setting*0.2 end global[player.index]=20 else - for _,setting in pairs(settings) do player[setting.key] = 0 end + for _,setting in pairs(settings) do player[key] = 0 end global[player.index]=nil end end) diff --git a/modules/ExpGamingCommands/cheatMode/control.lua b/modules/ExpGamingCommands/cheatMode/control.lua index a61cb809..f64037fe 100644 --- a/modules/ExpGamingCommands/cheatMode/control.lua +++ b/modules/ExpGamingCommands/cheatMode/control.lua @@ -1,5 +1,5 @@ --- A full ranking system for factorio. --- @module ExpGamingCommands.kill +-- @module ExpGamingCommands.cheatMode@4.0.0 -- @author Cooldude2606 -- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE @@ -11,4 +11,4 @@ commands.add_command('cheat-mode', 'Toggles cheat mode for a player', { }, function(event,args) local player = args.player or game.player if player.cheat_mode == true then player.cheat_mode = false else player.cheat_mode = true end -end) +end).default_admin_only = true diff --git a/modules/ExpGamingCommands/home/control.lua b/modules/ExpGamingCommands/home/control.lua index 95238d8b..f62765a8 100644 --- a/modules/ExpGamingCommands/home/control.lua +++ b/modules/ExpGamingCommands/home/control.lua @@ -1,9 +1,9 @@ --- A full ranking system for factorio. --- @module ExpGamingCommands.kill +-- @module ExpGamingCommands.home@4.0.0 -- @author Cooldude2606 -- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE -local Game = require('FactorioStdLib.Game') +local Game = require('FactorioStdLib.Game@^0.8.0') local global = global{} --- Sets the home for a player diff --git a/modules/ExpGamingCommands/kill/control.lua b/modules/ExpGamingCommands/kill/control.lua index caaf72cb..37a6df71 100644 --- a/modules/ExpGamingCommands/kill/control.lua +++ b/modules/ExpGamingCommands/kill/control.lua @@ -1,9 +1,9 @@ --- A full ranking system for factorio. --- @module ExpGamingCommands.kill +-- @module ExpGamingCommands.kill@4.0.0 -- @author Cooldude2606 -- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE -local Game = require('FactorioStdLib.Game') +local Game = require('FactorioStdLib.Game@^4.0.0') --- Kills a player of a lower rank -- @command kill @@ -15,4 +15,4 @@ commands.add_command('kill', 'Kills a player. No player name kills yourself.', { local player = args.player if player then player.character.die() else _player.character.die() end -end) +end).default_admin_only = true diff --git a/modules/ExpGamingCommands/kill/softmod.json b/modules/ExpGamingCommands/kill/softmod.json index 87fe9fe2..2a60b24c 100644 --- a/modules/ExpGamingCommands/kill/softmod.json +++ b/modules/ExpGamingCommands/kill/softmod.json @@ -3,7 +3,7 @@ "version": "4.0.0", "type": "Submodule", "description": "Adds a command which can be used to kill a player or yourself.", - "location": "https://github.com/explosivegaming/scenario/releases/download/v4.0-core/ExpGamingCommands.kill_4.0.0.zip", + "location": "https://github.com/explosivegaming/scenario/releases/download/v4.0-core/ExpGamingAdmin.Commands_4.0.0.zip", "keywords": [ "Command", "ExpGaming", diff --git a/modules/ExpGamingCommands/repair/control.lua b/modules/ExpGamingCommands/repair/control.lua index 239536c9..6491cde2 100644 --- a/modules/ExpGamingCommands/repair/control.lua +++ b/modules/ExpGamingCommands/repair/control.lua @@ -1,11 +1,11 @@ --- A full ranking system for factorio. --- @module ExpGamingCommands.kill +-- @module ExpGamingCommands.repair@4.0.0 -- @author Cooldude2606 -- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE -local Game = require('FactorioStdLib.Game') -local Ranking = require('ExpGamingCore.Ranking') +local Game = require('FactorioStdLib.Game@^0.8.0') +local Ranking = require('ExpGamingCore.Ranking@^4.0.0') -- Set an item to true to disallow it from being repaired local disallow = { @@ -18,13 +18,22 @@ local disallow = { -- Given const = 100: admin+ has unlimited, admin has const (100), mod has const / 2 (50), member has const / 5 (20) local const = 100 +local repairDisallow + +-- Module Define +local module_verbose = false +local ThisModule = { + on_init = function(self) + if loaded_modules['ExpGamingAdmin.TempBan@^4.0.0'] then verbose('ExpGamingAdmin.TempBan is installed; Loading tempban src') repairDisallow = require(module_path..'/src/tempban') end + end +} --- Used so that the value can be overriden if tempban is present -- @local -- @function repairDisallow -- @param player the player who called the command -- @param entity the entity which was repaired -local repairDisallow = function(player,entity) +repairDisallow = function(player,entity) player_return('You have repaired: '..entity.name..' this item is not allowed.',defines.textcolor.crit,player) entity.destroy() end @@ -54,10 +63,6 @@ commands.add_command('repair', 'Repairs all destoryed and damaged entites in an for key, entity in pairs(player.surface.find_entities(area)) do if entity.force == player.force and (entity.position.x-center.x)^2+(entity.position.y-center.y)^2 < sq_range and entity.health then entity.health = 10000 end end -end) +end).default_admin_only = true -return { - on_init = function(self) - if loaded_modules['ExpGamingAdmin.TempBan'] then verbose('ExpGamingAdmin.TempBan is installed; Loading tempban src') repairDisallow = require(module_path..'/src/tempban') end - end -} +return ThisModule diff --git a/modules/ExpGamingCommands/softmod.json b/modules/ExpGamingCommands/softmod.json index 5a5d1d2d..e8b15b82 100644 --- a/modules/ExpGamingCommands/softmod.json +++ b/modules/ExpGamingCommands/softmod.json @@ -14,34 +14,6 @@ "contact": "Discord: Cooldude2606#5241", "license": "https://github.com/explosivegaming/scenario/blob/master/LICENSE", "submodules": { - "admin": { - "name": "admin", - "version": "4.0.0", - "type": "Submodule", - "description": "Admins many of the admin featues which the script can use as in game commands.", - "location": "https://github.com/explosivegaming/scenario/releases/download/v4.0-core/ExpGamingCommands.admin_4.0.0.zip", - "keywords": [ - "ExpGaming", - "Admin", - "Tools", - "Commands", - "Temp ban", - "Jail", - "Clear Inventory", - "Report", - "Warnings" - ], - "dependencies": { - "ExpGamingLib": "^4.0.0", - "ExpGamingCore.Commands": "^4.0.0", - "ExpGamingAdmin.TempBan": "?^4.0.0", - "ExpGamingAdmin.Jail": "?^4.0.0", - "ExpGamingAdmin.Warnings": "?^4.0.0", - "ExpGamingAdmin.Reports": "^?4.0.0", - "ExpGamingAdmin.ClearInventory": "?^4.0.0" - }, - "collection": "ExpGamingCommands_4.0.0" - }, "bonus": { "name": "bonus", "version": "4.0.0", @@ -108,7 +80,7 @@ "version": "4.0.0", "type": "Submodule", "description": "Adds a command which can be used to kill a player or yourself.", - "location": "https://github.com/explosivegaming/scenario/releases/download/v4.0-core/ExpGamingCommands.kill_4.0.0.zip", + "location": "https://github.com/explosivegaming/scenario/releases/download/v4.0-core/ExpGamingAdmin.Commands_4.0.0.zip", "keywords": [ "Command", "ExpGaming", @@ -160,7 +132,7 @@ ], "dependencies": { "ExpGamingLib": "^4.0.0", - "ExpGamingCore.Ranking": "^4.0.0", + "ExpGamingCore.Ranking": "?^4.0.0", "ExpGamingCore.Commands": "^4.0.0", "FactorioStdLib.Game": "^0.8.0" }, diff --git a/modules/ExpGamingCommands/tags/control.lua b/modules/ExpGamingCommands/tags/control.lua index 63d62dfc..913777e3 100644 --- a/modules/ExpGamingCommands/tags/control.lua +++ b/modules/ExpGamingCommands/tags/control.lua @@ -1,10 +1,19 @@ --- A full ranking system for factorio. --- @module ExpGamingCommands.kill +-- @module ExpGamingCommands@4.0.0 -- @author Cooldude2606 -- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE -local Game = require('FactorioStdLib.Game') -local Ranking = require('ExpGamingCore.Ranking') +local Game = require('FactorioStdLib.Game@^0.8.0') +local Ranking -- ExpGamingCore.Ranking@^4.0.0 + +-- Module Define +local module_verbose = false +local ThisModule = { + on_init=function() + if loaded_modules['ExpGamingCore.Ranking@^4.0.0'] then Ranking = require('ExpGamingCore.Ranking@^4.0.0') end + --code + end +} --- Gives you a tag -- @command tag @@ -13,8 +22,10 @@ commands.add_command('tag', 'Give yourself a custom tag. Use "" to have more tha ['tag'] = {true,'string-len',20} }, function(event,args) local player = Game.get_player(event) - local rank = Ranking.get_rank(player) - player.tag = rank.tag..' - '..args.tag + if Ranking then + local rank = Ranking.get_rank(player) + player.tag = rank.tag..' - '..args.tag + else player.tag = args.tag end player_return('Your tag has been set. Use /tag-clear to remove your tag') end) @@ -25,7 +36,10 @@ commands.add_command('tag-clear', 'Removes a custom tag. Player can be self (/ta ['player'] = {true,'player-rank'} }, function(event,args) local player = args.player - local rank = Ranking.get_rank(player) - player.tag = rank.tag + if Ranking then + local rank = Ranking.get_rank(player) + player.tag = rank.tag + else player.tag = '' end player_return('Your tag has been removed.') end) + diff --git a/modules/ExpGamingCommands/tags/softmod.json b/modules/ExpGamingCommands/tags/softmod.json index fd45cb23..4504fbc8 100644 --- a/modules/ExpGamingCommands/tags/softmod.json +++ b/modules/ExpGamingCommands/tags/softmod.json @@ -12,7 +12,7 @@ ], "dependencies": { "ExpGamingLib": "^4.0.0", - "ExpGamingCore.Ranking": "^4.0.0", + "ExpGamingCore.Ranking": "?^4.0.0", "ExpGamingCore.Commands": "^4.0.0", "FactorioStdLib.Game": "^0.8.0" }, diff --git a/modules/ExpGamingCommands/teleport/control.lua b/modules/ExpGamingCommands/teleport/control.lua index 089fb82b..00c73250 100644 --- a/modules/ExpGamingCommands/teleport/control.lua +++ b/modules/ExpGamingCommands/teleport/control.lua @@ -1,9 +1,9 @@ --- A full ranking system for factorio. --- @module ExpGamingCommands.kill +-- @module ExpGamingCommands.teleport@4.0.0 -- @author Cooldude2606 -- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE -local Admin = require('ExpGamingAdmin.Teleport') +local Admin = require('ExpGamingAdmin.Teleport@^4.0.0') --- Teleports the user to the player given -- @command go-to diff --git a/modules/ExpGamingCore/Commands/control.lua b/modules/ExpGamingCore/Commands/control.lua index 3f02439b..47f46a2c 100644 --- a/modules/ExpGamingCore/Commands/control.lua +++ b/modules/ExpGamingCore/Commands/control.lua @@ -1,12 +1,11 @@ ---- A full ranking system for factorio. --- @module ExpGamingCore.Commands --- @alias commands +--- Command system that allows middle ware and auto validation of command arguments. +-- @module ExpGamingCore.Commands@4.0.0 -- @author Cooldude2606 -- @license https://github.com/explosivegaming/scenario/blob/master/LICENSE +-- @alias commands -local Game = require('FactorioStdLib.Game') -local Color = require('FactorioStdLib.Color') -local Ranking -- this is optional and is hanndled by it being present, it is loaded on init +local Game = require('FactorioStdLib.Game@^0.8.0') +local Color = require('FactorioStdLib.Color@^0.8.0') --- Used as an error constant for validation -- @field commands.error @@ -15,6 +14,11 @@ local Ranking -- this is optional and is hanndled by it being present, it is loa commands.error = setmetatable({},{__call=function(...) return ... end}) commands._add_command = commands.add_command local data = {} +local middleware = {} + +--- Used to add middle ware to the command handler, functions should return true or false +-- @tparam function callback function(player,commandName,event) should return true to allow next middle ware to run +function commands.add_middleware(callback) if not is_type(callback,'function') then error('Callback is not a function',2) return end table.insert(middleware,callback) end --- Index of all command data -- @field commands.data @@ -50,6 +54,7 @@ setmetatable(commands,{ -- @field player_rank-online converts the input to a player if the player is a lower rank than the user and online -- @field player_rank_alive converts the input to a player if the player is a lower rank than the user and online and alive commands.validate = { + ['boolean']=function(value,event) local value = value.lower() if value == 'true' or valule == 'yes' or value == 'y' or value == '1' then return true else return false end end, ['string']=function(value,event) return tostring(value) end, ['string-inf']=function(value,event) return tostring(value) end, ['string-len']=function(value,event,max) return tostring(value) and tostring(value):len() <= max and tostring(value) or commands.error{'commands.error-string-len'} end, @@ -60,10 +65,14 @@ commands.validate = { ['player']=function(value,event) return Game.get_player(player) or commands.error{'commands.error-player'} end, ['player-online']=function(value,event) local player,err = commands.validate['player'](value) return err and commands.error(err) or player.conected and player or commands.error{'commands.error-player-online'} end, ['player-alive']=function(value,event) local player,err = commands.validate['player-online'](value) return err and commands.error(err) or player.character and player.character.health > 0 and player or commands.error{'commands.error-player-alive'} end, - ['player-rank']=function(value,event) local player,err = commands.validate['player'](value) return err and commands.error(err) or Ranking and Ranking.get_rank(player).power > Ranking.get_rank(event).power or not player.admin and Game.get_player(event).admin or commands.error{'commands.error-player-rank'} end, - ['player-rank-online']=function(value,event) local player,err = commands.validate['player-online'](value) return err and commands.error(err) or Ranking and Ranking.get_rank(player).power > Ranking.get_rank(event).power or not player.admin and Game.get_player(event).admin or commands.error{'commands.error-player-rank'} end, - ['player-rank-alive']=function(value,event) local player,err = commands.validate['player-alive'](value) return err and commands.error(err) or Ranking and Ranking.get_rank(player).power > Ranking.get_rank(event).power or not player.admin and Game.get_player(event).admin or commands.error{'commands.error-player-rank'} end + ['player-rank']=function(value,event) local player,err = commands.validate['player'](value) return err and commands.error(err) or not player.admin and Game.get_player(event).admin and player or commands.error{'commands.error-player-rank'} end, + ['player-rank-online']=function(value,event) local player,err = commands.validate['player-online'](value) if err then return commands.error(err) end local player,err = commands.validate['player-rank'](player) if err then return commands.error(err) end return player end, + ['player-rank-alive']=function(value,event) local player,err = commands.validate['player-alive'](value) if err then return commands.error(err) end local player,err = commands.validate['player-rank'](player) if err then return commands.error(err) end return player end, } +--- Adds a function to the validation list +-- @tparam string name the name of the validation +-- @tparam function callback function(value,event) which returns either the value to be used or commands.error{'error-message'} +function commands.add_validation(name,callback) if not is_type(callback,'function') then error('Callback is not a function',2) return end commands.validate[name]=callback end --- Returns the inputs of this command as a formated string -- @usage commands.format_inputs('interface') -- returns (if you have ExpGamingCore.Server) @@ -136,58 +145,61 @@ function commands.get_commands(player) local commands = {} local player = Game.get_player(player) if not player then return error('Invalid player',2) end - local rank = Ranking.get_rank(player) for name,data in pairs(data) do - if rank:allowed(name) then table.insert(commands,data) end + if #middleware > 0 then for _,callback in pairs(middleware) do + local success, err = pcall(callback,player_name,command.name,command) + if not success then error(err) + elseif err then table.insert(commands,data) end + end elseif data.default_admin_only == true and player.admin then table.insert(commands,data) end end return commands end +local function logMessage(player_name,command,message,args) + game.write_file('commands.log', + game.tick + ..' Player: "'..player_name..'"' + ..' '..message..': "'..command.name..'"' + ..' With args of: '..table.tostring(args) + ..'\n' + , true, 0) +end + --- Used to call the custom commands -- @usage You dont its an internal command -- @tparam table command the event rasied by the command local function run_custom_command(command) local data = commands.data[command.name] local player_name = Game.get_player(command) and Game.get_player(command).name or 'server' - -- is the player allowed to use this command, if no ExpGamingCore.Ranking then this is ingroned - if Ranking and Ranking.meta and Ranking.meta.rank_count > 0 and not Ranking.get_rank(player_name):allowed(command.name) - or not Ranking and data.admin_only == true and game.player and not game.player.admin then + -- runs all middle ware if any, if there is no middle where then it relyis on .default_admin_only + if #middleware > 0 then for _,callback in pairs(middleware) do + local success, err = pcall(callback,player_name,command.name,command) + if not success then error(err) + elseif not err then + player_return({'commands.unauthorized'},defines.textcolor.crit) + logMessage(player_name,command,'Failed to use command (Unauthorized)',commands.validate_args(command)) + if game.player then game.player.play_sound{path='utility/cannot_build'} end + return + end + end elseif data.default_admin_only == true and game.player and not game.player.admin then player_return({'commands.unauthorized'},defines.textcolor.crit) + logMessage(player_name,command,'Failed to use command (Unauthorized)',commands.validate_args(command)) if game.player then game.player.play_sound{path='utility/cannot_build'} end - game.write_file('commands.log', - game.tick - ..' Player: "'..player_name..'"' - ..' Failed to use command (Unauthorized): "'..command.name..'"' - ..' With args of: '..table.tostring(commands.validate_args(command)) - ..'\n' - , true, 0) return end -- gets the args for the command local args, err = commands.validate_args(command) if args == commands.error then player_return({'commands.'..err,command.name,commands.format_inputs(data)},defines.textcolor.high) + logMessage(player_name,command,'Failed to use command (Invalid Args)',args) if game.player then game.player.play_sound{path='utility/deconstruct_big'} end - game.write_file('commands.log', - game.tick - ..' Player: "'..player_name..'"' - ..' Failed to use command (Invalid Args): "'..command.name..'"' - ..' With args of: '..table.tostring(args) - ..'\n' - , true, 0) return end -- runs the command local success, err = pcall(data.callback,command,args) if not success then error(err) end if err ~= commands.error and player_name ~= 'server' then player_return({'commands.command-ran'},defines.textcolor.info) end - game.write_file('commands.log', - game.tick - ..' Player: "'..player_name..'"' - ..' Used command: "'..command.name..'"' - ..' With args of: '..table.tostring(args) - ..'\n' - , true, 0) + logMessage(player_name,command,'Used command',args) end --- Used to define commands @@ -227,19 +239,16 @@ commands.add_command = function(name, description, inputs, callback) return data[name] end -function commands:on_init() - if loaded_modules['ExpGamingCore.Ranking'] then Ranking = require('ExpGamingCore.Ranking') end -end - return commands --[[ command example - locale file + **locale file** [foo] description=__1__ this is a command + **control.lua** commands.add_command('foo',{'foo.description'},{ ['player']={true,'player'}, -- a required arg that must be a valid player ['number']={true,'number-range',0,10}, -- a required arg that must be a number 0 Ranking.get_rank(event).power and player + or commands.error{'commands.error-player-rank'} +end) + +commands.add_validation('player-rank-online',function(value,event) + local player,err = commands.validate['player-online'](value) + if err then return commands.error(err) end + local player,err = commands.validate['player-rank'](player) + if err then return commands.error(err) end + return player +end) + +commands.add_validation('player-rank-alive',function(value,event) + local player,err = commands.validate['player-alive'](value) + if err then return commands.error(err) end + local player,err = commands.validate['player-rank'](player) + if err then return commands.error(err) end + return player +end) + +commands.add_middleware(function(player_name,command_name,event) + return Ranking.get_rank(player_name):allowed(command_name) +end) \ No newline at end of file diff --git a/modules/index.lua b/modules/index.lua index a3756d66..cb950c56 100644 --- a/modules/index.lua +++ b/modules/index.lua @@ -19,7 +19,7 @@ return { ['ExpGamingCore.Server@^4.0.0']='./modules/ExpGamingCore/Server', ['ExpGamingCommands.tags@4.0.0']='./modules/ExpGamingCommands/tags', ['ExpGamingCore.Commands@^4.0.0']='./modules/ExpGamingCore/Commands', - ['ExpGamingCommands.kill@4.0.0']='./modules/ExpGamingCommands/kill', + ['ExpGamingAdmin.Commands@4.0.0']='./modules/ExpGamingCommands/kill', ['ExpGamingCommands.home@4.0.0']='./modules/ExpGamingCommands/home', ['ExpGamingCommands.cheatMode@4.0.0']='./modules/ExpGamingCommands/cheatMode', ['ExpGamingCommands.bonus@4.0.0']='./modules/ExpGamingCommands/bonus',