From 5bead7daaf1782ecb142a57ac57bd8ba405ba150 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sat, 28 Nov 2020 20:13:20 +0100 Subject: [PATCH] Added minetest.close_formspec --- builtin/client/cheats/inventory.lua | 13 +++++++++---- builtin/client/util.lua | 6 +++++- doc/client_lua_api.txt | 8 ++++++++ src/client/game.cpp | 20 ++++++++++++++------ 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/builtin/client/cheats/inventory.lua b/builtin/client/cheats/inventory.lua index b9943f507..f40730844 100644 --- a/builtin/client/cheats/inventory.lua +++ b/builtin/client/cheats/inventory.lua @@ -79,22 +79,27 @@ local function check_tool(stack, node_groups, old_best_time) return best_time < old_best_time, best_time end -core.register_on_punchnode(function(pos, node) - if not minetest.settings:get_bool("autotool") then return end +function core.select_best_tool(nodename) local player = minetest.localplayer local inventory = minetest.get_inventory("current_player") - local node_groups = minetest.get_node_def(node.name).groups + local node_groups = minetest.get_node_def(nodename).groups local new_index = player:get_wield_index() local is_better, best_time = false, math.huge is_better, best_time = check_tool(player:get_wielded_item(), node_groups, best_time) is_better, best_time = check_tool(inventory.hand[1], node_groups, best_time) - for index, stack in pairs(inventory.main) do + for index, stack in ipairs(inventory.main) do is_better, best_time = check_tool(stack, node_groups, best_time) if is_better then new_index = index end end player:set_wield_index(new_index) +end + +core.register_on_punchnode(function(pos, node) + if not minetest.settings:get_bool("autotool") then + core.select_best_tool(node.name) + end end) -- Enderchest diff --git a/builtin/client/util.lua b/builtin/client/util.lua index 783d0ceb1..42e383c5c 100644 --- a/builtin/client/util.lua +++ b/builtin/client/util.lua @@ -49,4 +49,8 @@ function core.get_pointed_thing() local def = core.get_item_def(item:get_name()) local ray = core.raycast(pos, pos2, true, core.settings:get_bool("point_liquids") or def and def.liquids_pointable) return ray and ray:next() -end +end + +function core.close_formspec(formname) + return core.show_formspec(formname, "") +end diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index f14b8eceb..9ce553ca1 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -1107,6 +1107,14 @@ Passed to `HTTPApiTable.fetch` callback. Returned by * Reference to the camera object. See [`Camera`](#camera) class reference for methods. * `minetest.show_formspec(formname, formspec)` : returns true on success * Shows a formspec to the player +* `minetest.close_formspec(formname)` + * `formname`: has to exactly match the one given in `show_formspec`, or the + formspec will not close. + * calling `show_formspec(formname, "")` is equal to this + expression. + * to close a formspec regardless of the formname, call + `minetest.close_formspec("")`. + **USE THIS ONLY WHEN ABSOLUTELY NECESSARY!** * `minetest.display_chat_message(message)` returns true on success * Shows a chat message to the current player. diff --git a/src/client/game.cpp b/src/client/game.cpp index 8537ba39a..f79fdba8c 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -1889,13 +1889,21 @@ void Game::handleClientEvent_ShowFormSpec(ClientEvent *event, CameraOrientation } void Game::handleClientEvent_ShowLocalFormSpec(ClientEvent *event, CameraOrientation *cam) -{ - FormspecFormSource *fs_src = new FormspecFormSource(*event->show_formspec.formspec); - LocalFormspecHandler *txt_dst = - new LocalFormspecHandler(*event->show_formspec.formname, client); - GUIFormSpecMenu::create(m_game_ui->getFormspecGUI(), client, &input->joystick, +{ + if (event->show_formspec.formspec->empty()) { + auto formspec = m_game_ui->getFormspecGUI(); + if (formspec && (event->show_formspec.formname->empty() + || *(event->show_formspec.formname) == m_game_ui->getFormspecName())) { + formspec->quitMenu(); + } + } else { + FormspecFormSource *fs_src = new FormspecFormSource(*event->show_formspec.formspec); + LocalFormspecHandler *txt_dst = + new LocalFormspecHandler(*event->show_formspec.formname, client); + GUIFormSpecMenu::create(m_game_ui->getFormspecGUI(), client, &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound); - + } + delete event->show_formspec.formspec; delete event->show_formspec.formname; } -- 2.44.0