]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Added minetest.close_formspec
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 28 Nov 2020 19:13:20 +0000 (20:13 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 28 Nov 2020 19:13:20 +0000 (20:13 +0100)
builtin/client/cheats/inventory.lua
builtin/client/util.lua
doc/client_lua_api.txt
src/client/game.cpp

index b9943f50795cd1be71910a716453a9bb42aeefa5..f407308444eae0027ea276bc93941e199146d13f 100644 (file)
@@ -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
index 783d0ceb1334266d9565589e84737440909c9402..42e383c5c66e588123bdfd084c80405f59494a18 100644 (file)
@@ -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
index f14b8ecebd6de8fd1c3625da8a3652511cff1f36..9ce553ca14ac73994cfde5805dd55513958055ff 100644 (file)
@@ -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.
 
index 8537ba39aafac02c315f2af3377a37d818d346e4..f79fdba8cc576a63f33ac649eae4d891b910891b 100644 (file)
@@ -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;
 }