From 3a66d6b4ce972407489afdcdf478125c65280e4a Mon Sep 17 00:00:00 2001 From: Sokomine Date: Mon, 28 Jun 2021 23:35:45 +0200 Subject: [PATCH] allowed to edit and create preconditions and effects of type function for players with npc_master priv --- fs_edit_effects.lua | 3 ++- fs_edit_general.lua | 43 +++++++++++++++++++++++++++++++++++++++ fs_edit_preconditions.lua | 3 ++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/fs_edit_effects.lua b/fs_edit_effects.lua index cdba30d..8a7c4e9 100644 --- a/fs_edit_effects.lua +++ b/fs_edit_effects.lua @@ -51,12 +51,13 @@ local check_what = { "give item (created out of thin air) to player (requires npc_master priv)", -- 7 "take item from player and destroy it (requires npc_master priv)", -- 8 "move the player to a given position (requires npc_master priv)", -- 9 + "execute Lua code (requires npc_master priv)", -- 10 } -- how to store these as r_type in the precondition: local values_what = {"", "state", "block", "craft", "on_failure", "chat_all", -- the following require the npc_master priv: - "give_item", "take_item", "move"} + "give_item", "take_item", "move", "function"} -- unlike in the preconditions, the "I cannot punch it" option is -- not offered here - because the player (and later the NPC) needs diff --git a/fs_edit_general.lua b/fs_edit_general.lua index 7e8fc53..b9f07b3 100644 --- a/fs_edit_general.lua +++ b/fs_edit_general.lua @@ -355,6 +355,10 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, end end end + -- lua code + if(fields.lua_code) then + data.lua_code = fields.lua_code + end -- the save button was pressed if(fields.save_element and data and data.what and values_what[ data.what ]) then @@ -511,6 +515,28 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, v[ "r_value" ] = minetest.pos_to_string( {x = data.move_to_x, y = data.move_to_y, z = data.move_to_z}) + -- effect "execute Lua code (requires npc_master priv)", -- precondition: 7; effect: 10 + elseif((data.what and id_prefix == "p_" and data.what == 7) + or (data.what and id_prefix == "r_" and data.what == 10)) then + if(not(data.lua_code) or data.lua_code == "") then + yl_speak_up.show_fs(player, "msg", { + input_to = "yl_speak_up:"..formspec_input_to, + formspec = "size[9,2]".. + "label[0.2,0.5;Error: Please enter the Lua code you want ".. + "to execute!]".. + "button[1.5,1.5;2,0.9;back_from_error_msg;Back]"}) + return + end + if(not(minetest.check_player_privs(player, {npc_master=true}))) then + yl_speak_up.show_fs(player, "msg", { + input_to = "yl_speak_up:"..formspec_input_to, + formspec = "size[9,2]".. + "label[0.2,0.5;Error: You need the \"npc_master\" priv ".. + " in order to set this.]".. + "button[1.5,1.5;2,0.9;back_from_error_msg;Back]"}) + return + end + v[ id_prefix.."value" ] = data.lua_code -- "NPC crafts something", -- 4 -- (only for effects; not for preconditions) @@ -1076,6 +1102,13 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result, data.move_to_z = pos.z end end + + -- lua code (precondition and effect) + elseif((data.what and id_prefix == "p_" and data.what == 7) + or (data.what and id_prefix == "r_" and data.what == 10)) then + if(e[ id_prefix.."value"] and e[ id_prefix.."value"] ~= "") then + data.lua_code = e[ id_prefix.."value" ] + end end -- write that data back yl_speak_up.speak_to[pname][ tmp_data_cache ] = data @@ -1303,6 +1336,16 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result, "field[4.2,5.0;2.0,0.6;move_to_y;;"..(data.move_to_y or "").."]".. "field[7.7,5.0;2.0,0.6;move_to_z;;"..(data.move_to_z or "").."]" + -- "execute Lua code (requires npc_master priv)", -- precondition: 7; effect: 10 + elseif((data.what and id_prefix == "p_" and data.what == 7) + or (data.what and id_prefix == "r_" and data.what == 10)) then + formspec = formspec.. + "label[0.2,3.0;Execute the following Lua code (ought to return true or false):]".. + "label[0.2,3.5;Note: You can *save* this effect only if you have the ".. + "\"npc_master\" priv!]".. + "textarea[0.2,4.5;20,4.0;lua_code;;".. + minetest.formspec_escape(tostring(data.lua_code)).."]" + -- "NPC crafts something", -- 4 -- (craft - only for effects - not for preconditions) elseif(data.what and id_prefix == "r_" and data.what == 4) then diff --git a/fs_edit_preconditions.lua b/fs_edit_preconditions.lua index c49cc0f..aff957d 100644 --- a/fs_edit_preconditions.lua +++ b/fs_edit_preconditions.lua @@ -39,10 +39,11 @@ local check_what = { "a trade", -- 4 "the inventory of the player", -- 5 "the inventory of the NPC", -- 6 + "execute Lua code (requires npc_master priv)", -- 7 } -- how to store these as p_type in the precondition: -local values_what = {"", "state", "block", "trade", "player_inv", "npc_inv"} +local values_what = {"", "state", "block", "trade", "player_inv", "npc_inv", "function"} -- options for "a trade" local check_trade = {