]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - builtin/client/cheats/inventory.lua
Small AutoTool Fix
[dragonfireclient.git] / builtin / client / cheats / inventory.lua
index d12052d7ce782b7d075bc21c3d2d8254b46b4cf1..024bc5e98aef24a97b4fc965009145cd2e1efd63 100644 (file)
@@ -1,35 +1,51 @@
-local elapsed_time = 0
-local tick_time = 0.05
+local drop_action = InventoryAction("drop")
+
+local strip_move_act = InventoryAction("move")
+strip_move_act:to("current_player", "craft", 1)
+local strip_craft_act = InventoryAction("craft")
+strip_craft_act:craft("current_player")
+local strip_move_back_act = InventoryAction("move")
+strip_move_back_act:from("current_player", "craftresult", 1)
 
 core.register_globalstep(function(dtime)
+       local player = core.localplayer
+       if not player then return end
+       local item = player:get_wielded_item()
+       local itemdef = core.get_item_def(item:get_name())
+       local wieldindex = player:get_wield_index()
+       -- AutoRefill
+       if core.settings:get_bool("autorefill") and itemdef then
+               local space = item:get_free_space()
+               local i = core.find_item(item:get_name(), wieldindex + 1)
+               if i and space > 0 then
+                       local move_act = InventoryAction("move")
+                       move_act:to("current_player", "main", wieldindex)
+                       move_act:from("current_player", "main", i)
+                       move_act:set_count(space)
+                       move_act:apply()
+               end
+       end
+       -- Strip
+       if core.settings:get_bool("strip") then
+               if itemdef and itemdef.groups.tree and player:get_control().place then
+                       strip_move_act:from("current_player", "main", wieldindex)
+                       strip_move_back_act:to("current_player", "main", wieldindex)
+                       strip_move_act:apply()
+                       strip_craft_act:apply()
+                       strip_move_back_act:apply()
+               end
+       end
        -- AutoEject
        if core.settings:get_bool("autoeject") then
-               local player = core.localplayer
                local list = (core.settings:get("eject_items") or ""):split(",")
                local inventory = core.get_inventory("current_player")
                for index, stack in pairs(inventory.main) do
                        if table.indexof(list, stack:get_name()) ~= -1 then
-                               local old_index = player:get_wield_index()
-                               player:set_wield_index(index - 1)
-                               core.drop_selected_item()
-                               player:set_wield_index(old_index)
-                               return
+                               drop_action:from("current_player", "main", index)
+                               drop_action:apply()
                        end
                end
        end
-       -- NextItem
-       if core.settings:get_bool("next_item") then
-               elapsed_time = elapsed_time + dtime
-               if elapsed_time < tick_time then return end
-               local player = minetest.localplayer
-               if not player then return end
-               local item = player:get_wielded_item()
-               if item:get_count() == 0 then
-                       local index = player:get_wield_index()
-                       player:set_wield_index(index + 1)
-               end
-               elapsed_time = 0
-       end
 end)
 
 core.register_list_command("eject", "Configure AutoEject", "eject_items")
@@ -52,22 +68,49 @@ 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
-       local player = minetest.localplayer
-       local inventory = minetest.get_inventory("current_player")
-       local node_groups = minetest.get_node_def(node.name).groups
+local function find_best_tool(nodename)
+       local player = core.localplayer
+       local inventory = core.get_inventory("current_player")
+       local node_groups = core.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 - 1
+                       new_index = index
+               end
+       end
+       return new_index
+end
+
+function core.select_best_tool(nodename)
+       core.localplayer:set_wield_index(find_best_tool(nodename))
+end
+
+local new_index, old_index, pointed_pos
+
+core.register_on_punchnode(function(pos, node)
+       if core.settings:get_bool("autotool") then
+               pointed_pos = pos
+               old_index = old_index or core.localplayer:get_wield_index()
+               new_index = find_best_tool(node.name)
+       end
+end)
+
+core.register_globalstep(function()
+       local player = core.localplayer
+       if not new_index then return end
+       if core.settings:get_bool("autotool") then
+               local pt = core.get_pointed_thing()
+               if pt and pt.type == "node" and vector.equals(core.get_pointed_thing_position(pt), pointed_pos) and player:get_control().dig then
+                       player:set_wield_index(new_index)
+                       return
                end
        end
-       player:set_wield_index(new_index)
+       player:set_wield_index(old_index)
+       new_index, old_index, pointed_pos = nil
 end)
 
 -- Enderchest
@@ -113,5 +156,8 @@ local hand_formspec = "size[9,8.75]"..
        "listring[current_player;main]"
        
 function core.open_handslot()
-       minetest.show_formspec("__builtin__:hand", hand_formspec)
+       core.show_formspec("__builtin__:hand", hand_formspec)
 end
+
+
+