From 8bfc59a9a21c92ef2a7a855321c570ba4e76ffc3 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Wed, 8 Jun 2022 22:16:14 +0200 Subject: [PATCH] moved chat command npc_talk_debug into extra file --- command_npc_talk_debug.lua | 75 ++++++++++++++++++++++++++++++++++++++ fs_edit_general.lua | 74 ------------------------------------- init.lua | 2 + 3 files changed, 77 insertions(+), 74 deletions(-) create mode 100644 command_npc_talk_debug.lua diff --git a/command_npc_talk_debug.lua b/command_npc_talk_debug.lua new file mode 100644 index 0000000..a86899c --- /dev/null +++ b/command_npc_talk_debug.lua @@ -0,0 +1,75 @@ + + +-- store which player is monitoring the NPC (for preconditions and +-- effects) +yl_speak_up.debug_mode_set_by_player = {} + +-- for sending debug information about preconditions and effects to +-- the player who is monitoring the NPC +-- (sometimes it is not easy/obvious to see why something failed) +yl_speak_up.debug_msg = function(player, n_id, o_id, text) + local dname = yl_speak_up.debug_mode_set_by_player[ n_id ] + -- nobody cares + if(not(dname)) then + return + end + local pname = player:get_player_name() + local d_id = yl_speak_up.speak_to[pname].d_id + minetest.chat_send_player(dname, "[NPC "..tostring(n_id)..": ".. + tostring(pname).."] <"..tostring(d_id).." "..tostring(o_id).. + "> "..tostring(text)) +end + + +-- a chat command for entering and leaving debug mode; needs to be a chat command +-- because the player may have wandered off from his NPC and get too many messages +-- without a quick way to get rid of them otherwise +minetest.register_chatcommand( 'npc_talk_debug', { + description = "Sets you as debugger for the yl_speak_up-NPC with the ID .\n".. + " lists the NPC you are currently debugging.\n".. + " turns debug mode off again.", + privs = {npc_talk_owner = true}, + func = function(pname, param) + if(param and param == "off") then + local count = 0 + for k, v in pairs(yl_speak_up.debug_mode_set_by_player) do + if(v and v == pname) then + yl_speak_up.debug_mode_set_by_player[ k ] = nil + count = count + 1 + minetest.chat_send_player(pname, "You are no longer ".. + "debugging the NPC with the ID "..tostring(k)..".") + end + end + minetest.chat_send_player(pname, "Removed you as debugger of ".. + tostring(count).." NPCs.") + return + elseif(not(param) or param == "" or param == "list") then + local count = 0 + local text = "You are currently debugging the NPCs with the following IDs:\n" + for k, v in pairs(yl_speak_up.debug_mode_set_by_player) do + if(v and v == pname) then + count = count + 1 + text = text.." "..tostring(k) + end + end + if(count == 0) then + text = text.." - none -" + else + text = text.."\nTo turn debugging off, call this command with the ".. + "parameter ." + end + minetest.chat_send_player(pname, text) + return + elseif(not(yl_speak_up.may_edit_npc(minetest.get_player_by_name(pname), param))) then + minetest.chat_send_player(pname, "You do not have the necessary privs to ".. + "edit that NPC.") + return + else + yl_speak_up.debug_mode_set_by_player[ param ] = pname + minetest.chat_send_player(pname, "You are now receiving debug information ".. + "for NPC "..tostring(param)..".\nTo turn that off, type ".. + "\"/npc_talk_debug off\".") + end + end +}); + diff --git a/fs_edit_general.lua b/fs_edit_general.lua index e064939..e0b159f 100644 --- a/fs_edit_general.lua +++ b/fs_edit_general.lua @@ -1,78 +1,4 @@ --- store which player is monitoring the NPC (for preconditions and --- effects) -yl_speak_up.debug_mode_set_by_player = {} - --- for sending debug information about preconditions and effects to --- the player who is monitoring the NPC --- (sometimes it is not easy/obvious to see why something failed) -yl_speak_up.debug_msg = function(player, n_id, o_id, text) - local dname = yl_speak_up.debug_mode_set_by_player[ n_id ] - -- nobody cares - if(not(dname)) then - return - end - local pname = player:get_player_name() - local d_id = yl_speak_up.speak_to[pname].d_id - minetest.chat_send_player(dname, "[NPC "..tostring(n_id)..": ".. - tostring(pname).."] <"..tostring(d_id).." "..tostring(o_id).. - "> "..tostring(text)) -end - - --- a chat command for entering and leaving debug mode; needs to be a chat command --- because the player may have wandered off from his NPC and get too many messages --- without a quick way to get rid of them otherwise -minetest.register_chatcommand( 'npc_talk_debug', { - description = "Sets you as debugger for the yl_speak_up-NPC with the ID .\n".. - " lists the NPC you are currently debugging.\n".. - " turns debug mode off again.", - privs = {npc_talk_owner = true}, - func = function(pname, param) - if(param and param == "off") then - local count = 0 - for k, v in pairs(yl_speak_up.debug_mode_set_by_player) do - if(v and v == pname) then - yl_speak_up.debug_mode_set_by_player[ k ] = nil - count = count + 1 - minetest.chat_send_player(pname, "You are no longer ".. - "debugging the NPC with the ID "..tostring(k)..".") - end - end - minetest.chat_send_player(pname, "Removed you as debugger of ".. - tostring(count).." NPCs.") - return - elseif(not(param) or param == "" or param == "list") then - local count = 0 - local text = "You are currently debugging the NPCs with the following IDs:\n" - for k, v in pairs(yl_speak_up.debug_mode_set_by_player) do - if(v and v == pname) then - count = count + 1 - text = text.." "..tostring(k) - end - end - if(count == 0) then - text = text.." - none -" - else - text = text.."\nTo turn debugging off, call this command with the ".. - "parameter ." - end - minetest.chat_send_player(pname, text) - return - elseif(not(yl_speak_up.may_edit_npc(minetest.get_player_by_name(pname), param))) then - minetest.chat_send_player(pname, "You do not have the necessary privs to ".. - "edit that NPC.") - return - else - yl_speak_up.debug_mode_set_by_player[ param ] = pname - minetest.chat_send_player(pname, "You are now receiving debug information ".. - "for NPC "..tostring(param)..".\nTo turn that off, type ".. - "\"/npc_talk_debug off\".") - end - end -}); - - -- helper function; get a formspec with the inventory of the player (for selecting items) yl_speak_up.fs_your_inventory_select_item = function(pname, data) return "label[0.2,4.2;Name of the item(stack):]".. diff --git a/init.lua b/init.lua index 6458ea0..368d508 100644 --- a/init.lua +++ b/init.lua @@ -60,6 +60,8 @@ dofile(modpath .. "print_as_table.lua") dofile(modpath .. "formspec_helpers.lua") -- handle alternate text for dialogs dofile(modpath .. "fs_alternate_text.lua") +-- implementation of chat command npc_talk_debug: +dofile(modpath .. "command_npc_talk_debug.lua") -- common functions for editing preconditions and effects dofile(modpath .. "fs_edit_general.lua") -- edit preconditions (can be reached through edit options dialog)