From 7811ad2e1111a86bb5ec4b9fe6f0d7b57a1e7b29 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Mon, 1 Jun 2020 20:29:17 +0100 Subject: [PATCH] Added settings and warnings --- locale/en/commands.cfg | 3 +- locale/en/data.cfg | 7 ++- modules/commands/warnings.lua | 10 ++-- modules/control/warnings.lua | 85 +++++++++++++++++++-------------- modules/data/alt-view.lua | 5 +- modules/data/bonus.lua | 7 +++ modules/data/player-colours.lua | 7 +++ modules/data/quickbar.lua | 8 ++++ modules/gui/readme.lua | 17 +++---- modules/gui/server-ups.lua | 17 +++++++ 10 files changed, 112 insertions(+), 54 deletions(-) diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index 081b8f36..8d3213ce 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -45,7 +45,8 @@ removed=__1__ has one or more reports removed by __2__. [expcom-warnings] received=__1__ received a warning from __2__ for __3__. player=__1__ has __2__ warnings and __3__/__4__ script warnings. -list-tilte=The following player have this many warnings (and this many script warnings): +player-detail=__1__ gave warning for: __2__ +list-title=The following player have this many warnings (and this many script warnings): list=__1__: __2__ (__3__/__4__) cleared=__1__ had all they warnings cleared by __2__. diff --git a/locale/en/data.cfg b/locale/en/data.cfg index a264cebd..59f2877b 100644 --- a/locale/en/data.cfg +++ b/locale/en/data.cfg @@ -6,7 +6,9 @@ message-set=Your join message has been updated. saved=Your quickbar filters have been saved. [exp-required] - +Warnings=Warnings +Warnings-tooltip=The number of warnings that have been given to you by staff +Warnings-value-tooltip=The number of warnings that have been given to you by staff [exp-settings] Colour=Colour Colour-tooltip=Your player colour @@ -20,6 +22,9 @@ QuickbarFilters-value-tooltip=Change by using /save-quickbar UsesAlt=Alt View UsesAlt-tooltip=If you use alt view when you play UsesAlt-value-tooltip=Change by pressing __CONTROL__show-info__ +UsesServerUps=Server Ups +UsesServerUps-tooltip=If you use server ups view when you play +UsesServerUps-value-tooltip=Change by using /server-ups Tag=Player Tag Tag-tooltip=The tag that is shown after your name Tag-value-tooltip=Change by using /tag diff --git a/modules/commands/warnings.lua b/modules/commands/warnings.lua index 888c4709..ccd4d6a0 100644 --- a/modules/commands/warnings.lua +++ b/modules/commands/warnings.lua @@ -36,12 +36,14 @@ Commands.new_command('get-warnings', 'Gets the number of warnings a player has. local warnings = Warnings.get_warnings(player) local script_warnings = Warnings.get_script_warnings(player) local player_name_color = format_chat_player_name(player) - Commands.print{'expcom-warnings.player', player_name_color, warnings, script_warnings, config.temp_warning_limit} + Commands.print{'expcom-warnings.player', player_name_color, #warnings, #script_warnings, config.temp_warning_limit} + for _, warning in ipairs(warnings) do + Commands.print{'expcom-warnings.player-detail', format_chat_player_name(warning.by_player_name), warning.reason} + end else local rtn = {} - local user_warnings = Warnings.user_warnings local user_script_warnings = Warnings.user_script_warnings - for player_name, warnings in pairs(user_warnings) do + for player_name, warnings in pairs(Warnings.user_warnings:get_all()) do rtn[player_name] = {#warnings, 0} end for player_name, warnings in pairs(user_script_warnings) do @@ -50,7 +52,7 @@ Commands.new_command('get-warnings', 'Gets the number of warnings a player has. end rtn[player_name][2] = #warnings end - Commands.print{'expcom-warnings.list-tilte'} + Commands.print{'expcom-warnings.list-title'} for player_name, warnings in pairs(rtn) do local player_name_color = format_chat_player_name(player_name) Commands.print{'expcom-warnings.list', player_name_color, warnings[1], warnings[2], config.temp_warning_limit} diff --git a/modules/control/warnings.lua b/modules/control/warnings.lua index dd7bcba4..0145a01f 100644 --- a/modules/control/warnings.lua +++ b/modules/control/warnings.lua @@ -28,8 +28,20 @@ local config = require 'config.warnings' --- @dep config.warnings local valid_player = Game.get_player_from_any +--- Stores the quickbar filters for a player +local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data +local PlayerWarnings = PlayerData.Required:combine('Warnings') +PlayerWarnings:set_metadata{ + stringify = function(value) + if not value then return 'You have no warnings against you' end + local count = 0 + for _ in pairs(value) do count = count + 1 end + return 'You have '..count..' warnings against you' + end +} + local Warnings = { - user_warnings={}, + user_warnings=PlayerWarnings, user_script_warnings={}, events = { --- When a warning is added to a player @@ -52,7 +64,7 @@ local Warnings = { -- @tparam string reason the reason that the player was given a warning -- @tparam number warning_count the new number of warnings that the player has on_script_warning_added = script.generate_event_name(), - --- When a warning is remnoved from a player, by the script + --- When a warning is removed from a player, by the script -- @event on_script_warning_removed -- @tparam number player_index the index of the player who is having the warning removed -- @tparam number warning_count the new number of warnings that the player has @@ -60,30 +72,24 @@ local Warnings = { } } -local user_warnings = Warnings.user_warnings local user_script_warnings = Warnings.user_script_warnings -Global.register({ - user_warnings = user_warnings, - user_script_warnings = user_script_warnings -}, function(tbl) - Warnings.user_warnings = tbl.user_warnings - Warnings.user_script_warnings = tbl.user_script_warnings - user_warnings = Warnings.user_warnings - user_script_warnings = Warnings.user_script_warnings +Global.register(user_script_warnings, function(tbl) + Warnings.user_script_warnings = tbl + user_script_warnings = tbl end) ---- Gets an array of warnings that the player has, always returns a list even if emtpy +--- Gets an array of warnings that the player has, always returns a list even if empty -- @tparam LuaPlayer player the player to get the warning for -- @treturn table an array of all the warnings on this player, contains tick, by_player_name and reason function Warnings.get_warnings(player) - return user_warnings[player.name] or {} + return PlayerWarnings:get(player.name, {}) end --- Gets the number of warnings that a player has on them -- @tparam LuaPlayer player the player to count the warnings for -- @treturn number the number of warnings that the player has function Warnings.count_warnings(player) - local warnings = user_warnings[player.name] or {} + local warnings = PlayerWarnings:get(player.name, {}) return #warnings end @@ -99,19 +105,21 @@ function Warnings.add_warning(player, by_player_name, reason) reason = reason or 'Non given.' - local warnings = user_warnings[player.name] - if not warnings then - warnings = {} - user_warnings[player.name] = warnings - end + local warning_count + PlayerWarnings:update(player.name, function(_, warnings) + local warning = { + by_player_name = by_player_name, + reason = reason + } - table.insert(warnings, { - tick = game.tick, - by_player_name = by_player_name, - reason = reason - }) - - local warning_count = #warnings + if not warnings then + warning_count = 1 + return {warning} + else + table.insert(warnings, warning) + warning_count = #warnings + end + end) script.raise_event(Warnings.events.on_warning_added, { name = Warnings.events.on_warning_added, @@ -122,7 +130,7 @@ function Warnings.add_warning(player, by_player_name, reason) reason = reason }) - local action = config.actions[#warnings] + local action = config.actions[warning_count] if action then local _type = type(action) if _type == 'function' then @@ -156,7 +164,7 @@ local function warning_removed_event(player, warning_by_name, removed_by_name, w }) end ---- Removes a warning from a player, always removes the earlyist warning, fifo +--- Removes a warning from a player, always removes the earliest warning, fifo -- @tparam LuaPlayer player the player to remove a warning from -- @tparam string by_player_name the name of the player who is doing the action -- @treturn number the number of warnings that the player has @@ -165,14 +173,17 @@ function Warnings.remove_warning(player, by_player_name) if not player then return end if not by_player_name then return end - local warnings = user_warnings[player.name] - if not warnings then return end + local warning, warning_count + PlayerWarnings:update(player.name, function(_, warnings) + if not warnings then return end + warning = table.remove(warnings, 1) + warning_count = #warnings + end) - local warning = table.remove(warnings, 1) + if not warning then return end + warning_removed_event(player, warning.by_player_name, by_player_name, warning_count) - warning_removed_event(player, warning.by_player_name, by_player_name, #warnings) - - return #warnings + return warning_count end --- Removes all warnings from a player, will trigger remove event for each warning @@ -184,7 +195,7 @@ function Warnings.clear_warnings(player, by_player_name) if not player then return end if not by_player_name then return end - local warnings = user_warnings[player.name] + local warnings = PlayerWarnings:get(player) if not warnings then return end local warning_count = #warnings @@ -192,7 +203,7 @@ function Warnings.clear_warnings(player, by_player_name) warning_removed_event(player, warning.by_player_name, by_player_name, warning_count-n) end - user_warnings[player.name] = nil + PlayerWarnings:remove(player) return true end @@ -249,7 +260,7 @@ function Warnings.add_script_warning(player, reason) return warning_count end ---- Script warning removed event tigger due to it being looped in clear script warnings +--- Script warning removed event trigger due to it being looped in clear script warnings -- @tparam LuaPlayer player the player who is having a script warning removed -- @tparam number warning_count the number of warning that the player has local function script_warning_removed_event(player, warning_count) diff --git a/modules/data/alt-view.lua b/modules/data/alt-view.lua index 83aca25a..609851ad 100644 --- a/modules/data/alt-view.lua +++ b/modules/data/alt-view.lua @@ -3,10 +3,13 @@ local Event = require 'utils.event' ---@dep utils.event ---- Stores the join message that the player have +--- Stores the visible state of alt mode local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data local UsesAlt = PlayerData.Settings:combine('UsesAlt') UsesAlt:set_default(false) +UsesAlt:set_metadata{ + stringify = function(value) return value and 'Visible' or 'Hidden' end +} --- When your data loads apply alt view if you have it enabled UsesAlt:on_load(function(player_name, uses_alt) diff --git a/modules/data/bonus.lua b/modules/data/bonus.lua index 2275d543..1c5b7a70 100644 --- a/modules/data/bonus.lua +++ b/modules/data/bonus.lua @@ -12,6 +12,13 @@ require 'config.expcore.command_general_parse' --- Stores the bonus for the player local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data local PlayerBonus = PlayerData.Settings:combine('Bonus') +PlayerBonus:set_default(0) +PlayerBonus:set_metadata{ + stringify = function(value) + if not value or value == 0 then return 'None set' end + return (value*100)..'%' + end +} --- Apply a bonus amount to a player local function apply_bonus(player, amount) diff --git a/modules/data/player-colours.lua b/modules/data/player-colours.lua index bed8012f..aff392a8 100644 --- a/modules/data/player-colours.lua +++ b/modules/data/player-colours.lua @@ -8,6 +8,13 @@ local config = require 'config.preset_player_colours' --- @dep config.preset_pla --- Stores the colour that the player wants local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data local PlayerColours = PlayerData.Settings:combine('Colour') +PlayerColours:set_metadata{ + stringify = function(value) + if not value then return 'None set' end + local c = value[1] + return string.format('Red: %d Green: %d Blue: %d', c[1], c[2], c[3]) + end +} --- Used to compact player colours to take less space local floor = math.floor diff --git a/modules/data/quickbar.lua b/modules/data/quickbar.lua index d2352a7f..3353a8f3 100644 --- a/modules/data/quickbar.lua +++ b/modules/data/quickbar.lua @@ -9,6 +9,14 @@ local config = require 'config.preset_player_quickbar' --- @dep config.preset_pl --- Stores the quickbar filters for a player local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data local PlayerFilters = PlayerData.Settings:combine('QuickbarFilters') +PlayerFilters:set_metadata{ + stringify = function(value) + if not value then return 'No filters set' end + local count = 0 + for _ in pairs(value) do count = count + 1 end + return count..' filters set' + end +} --- Loads your quickbar preset PlayerFilters:on_load(function(player_name, filters) diff --git a/modules/gui/readme.lua b/modules/gui/readme.lua index 7b8c2ed8..00fa4742 100644 --- a/modules/gui/readme.lua +++ b/modules/gui/readme.lua @@ -291,8 +291,8 @@ Gui.element(function(_, parent) local value = child:get(player_name) if value ~= nil or metadata.show_always then if metadata.stringify then value = metadata.stringify(value) end - Gui.centered_label(settings, 150, metadata.name or {'exp-required.'..name}, metadata.tooltip or {'exp-required.'..name..'-tooltip'}) - Gui.centered_label(settings, 420, tostring(value), metadata.value_tooltip or {'exp-required.'..name..'-value-tooltip'}) + Gui.centered_label(required, 150, metadata.name or {'exp-required.'..name}, metadata.tooltip or {'exp-required.'..name..'-tooltip'}) + Gui.centered_label(required, 420, tostring(value), metadata.value_tooltip or {'exp-required.'..name..'-value-tooltip'}) end end @@ -302,11 +302,10 @@ Gui.element(function(_, parent) for name, child in pairs(PlayerData.Settings.children) do local metadata = child.metadata local value = child:get(player_name) - if value ~= nil or metadata.show_always then - if metadata.stringify then value = metadata.stringify(value) end - Gui.centered_label(settings, 150, metadata.name or {'exp-settings.'..name}, metadata.tooltip or {'exp-settings.'..name..'-tooltip'}) - Gui.centered_label(settings, 420, tostring(value), metadata.value_tooltip or {'exp-settings.'..name..'-value-tooltip'}) - end + if metadata.stringify then value = metadata.stringify(value) end + if value == nil then value = 'None set' end + Gui.centered_label(settings, 150, metadata.name or {'exp-settings.'..name}, metadata.tooltip or {'exp-settings.'..name..'-tooltip'}) + Gui.centered_label(settings, 420, tostring(value), metadata.value_tooltip or {'exp-settings.'..name..'-value-tooltip'}) end end @@ -324,9 +323,7 @@ Gui.element(function(_, parent) Gui.centered_label(statistics, 130, format_number(value or 0), metadata.value_tooltip or {'exp-statistics.'..name..'-tooltip'}) end end - if count > 0 then - for i = 1, count do Gui.centered_label(statistics, 140) end - end + if count > 0 then for i = 1, count do Gui.centered_label(statistics, 140) end end end -- Add the misc area diff --git a/modules/gui/server-ups.lua b/modules/gui/server-ups.lua index 901c0816..6ae45fce 100644 --- a/modules/gui/server-ups.lua +++ b/modules/gui/server-ups.lua @@ -8,6 +8,14 @@ local Gui = require 'expcore.gui' --- @dep expcore.gui local Event = require 'utils.event' --- @dep utils.event local Commands = require 'expcore.commands' --- @dep expcore.commands +--- Stores the visible state of server ups +local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data +local UsesServerUps = PlayerData.Settings:combine('UsesServerUps') +UsesServerUps:set_default(false) +UsesServerUps:set_metadata{ + stringify = function(value) return value and 'Visible' or 'Hidden' end +} + --- Label to show the server ups -- @element server_ups local server_ups = @@ -19,6 +27,14 @@ Gui.element{ font = 'default-game' } +--- Change the visible state when your data loads +UsesServerUps:on_load(function(player_name, visible) + local player = game.players[player_name] + local label = player.gui.screen[server_ups.name] + if not global.ext or not global.ext.server_ups then visible = false end + label.visible = visible +end) + --- Toggles if the server ups is visbile -- @command server-ups Commands.new_command('server-ups', 'Toggle the server ups display') @@ -29,6 +45,7 @@ Commands.new_command('server-ups', 'Toggle the server ups display') return Commands.error{'expcom-server-ups.no-ext'} end label.visible = not label.visible + UsesServerUps:set(player, label.visible) end) -- Set the location of the label