From 3f11b3951a48b7d8868c3a7e84e8689642e4e8e2 Mon Sep 17 00:00:00 2001 From: random-geek <35757396+random-geek@users.noreply.github.com> Date: Sat, 19 Feb 2022 23:53:59 -0800 Subject: [PATCH] Fix README, minor code fixes/improvements --- README.md | 4 ++-- init.lua | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 36d7870..3b5a5fb 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A Minetest client-side mod (CSM) which adds custom chat message coloring based o ## Features -* Colors regular, /me, and join/leave messages +* Colors chat messages, DMs, /me messages, and join/leave messages * Colors names in server status messages * Set any color for any name * Set default colors for each type of message @@ -30,7 +30,7 @@ Displays a GUI for modifying colors. ### `.setcolor ` -Color messages from player `` as ``. To set a default for a certain type of message, use the names `default_chat`, `default_me`, or `default_join`. +Color messages from player `` as ``. To set a default for a certain type of message, use the names `default_chat`, `default_dm`, etc. (see `.colors` for a full list). Examples: diff --git a/init.lua b/init.lua index b5bce03..0d20d17 100644 --- a/init.lua +++ b/init.lua @@ -63,8 +63,18 @@ local function message_info(richMsg) -- Unrecognized message types will return nil. end +local function table_contains(tab, target) + for _, val in ipairs(tab) do + if val == target then + return true + end + end + + return false +end + -- Set player/default color. --- name: player name or default_whatever +-- name: player name or default_something -- color: HTML string, hex color ('#' will be prepended if necessary), or nil to delete entry. local function set_color(name, color) if not name or name == "" then @@ -80,6 +90,11 @@ local function set_color(name, color) local key if string.sub(name, 1, 8) == "default_" then + if not table_contains(MESSAGE_TYPES, string.sub(name, 9)) then + minetest.display_chat_message(string.format("No setting called '%s'.", name)) + return + end + if not color then minetest.display_chat_message("Cannot delete defaults!") return @@ -206,7 +221,7 @@ minetest.register_on_formspec_input(function(formname, fields) minetest.show_formspec("chatcolor:modify", get_formspec(true, row[1], row[2])) elseif fields.main_add then minetest.show_formspec("chatcolor:modify", get_formspec(true, "", "")) - elseif fields.mod_set and fields.mod_player and fields.mod_color then + elseif (fields.mod_set or fields.key_enter) and fields.mod_player and fields.mod_color then set_color(fields.mod_player, fields.mod_color) minetest.show_formspec("chatcolor:maingui", get_formspec()) elseif fields.mod_cancel then