2020-10-14 13:30:01 +02:00
|
|
|
autotool={}
|
|
|
|
function autotool.check_tool(stack, node_groups, old_best_time)
|
2020-10-04 03:37:08 +02:00
|
|
|
local toolcaps = stack:get_tool_capabilities()
|
|
|
|
if not toolcaps then return end
|
|
|
|
local best_time = old_best_time
|
|
|
|
for group, groupdef in pairs(toolcaps.groupcaps) do
|
|
|
|
local level = node_groups[group]
|
|
|
|
if level then
|
|
|
|
local this_time = groupdef.times[level]
|
|
|
|
if this_time < best_time then
|
|
|
|
best_time = this_time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return best_time < old_best_time, best_time
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_punchnode(function(pos, node)
|
|
|
|
--minetest.display_chat_message(dump(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 new_index = player:get_wield_index()
|
|
|
|
local is_better, best_time = false, math.huge
|
2020-10-14 13:30:01 +02:00
|
|
|
is_better, best_time = autotool.check_tool(player:get_wielded_item(), node_groups, best_time)
|
2020-10-04 03:37:08 +02:00
|
|
|
for index, stack in pairs(inventory.main) do
|
2020-10-14 13:30:01 +02:00
|
|
|
is_better, best_time = autotool.check_tool(stack, node_groups, best_time)
|
2020-10-04 03:37:08 +02:00
|
|
|
if is_better then
|
|
|
|
new_index = index - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
player:set_wield_index(new_index)
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_cheat("AutoTool", "Inventory", "autotool")
|