Commit 8f801626 authored by Fedja Beader's avatar Fedja Beader
Browse files

worldedit: if node/item is not present in hotbar, move it there. Adds setup_move_to_hotbar()

parent db9321c5
......@@ -140,12 +140,16 @@ local function compute_next_step_saved()
l:info("world-editing complete")
return
end
queue:prepend(compute_next_step_saved)
-- TODO:
local my_inv = core.get_inventory()
-- 1) dig existing other node
-- 2) find new node in inventory
-- 3) move new node to hotbar (and find/make free hotbar slot for it)
if not setup_move_to_hotbar(my_inv, node_data.node.name) then
l:err("Ran out of " .. node_data.node.name .. "?!?!")
return
end
-- 4) DONE select this node DONE
-- 5) DONE place
local anchor_pos = positions:get(1)
......@@ -156,8 +160,10 @@ local function compute_next_step_saved()
-- TODO: API is bugged
local pos = { x = x, y = y - 1, z = z }
local attach_pos = { x = x, y = y - 0, z = z }
queue:prepend(function() step_place(node_data.node.name, pos, attach_pos) end)
queue:append(function() return step_place(node_data.node.name, pos, attach_pos) end)
-- 6) rotate if needed
queue:append(compute_next_step_saved)
end
......
......@@ -18,6 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
Inventory manipulation helpers
--]]
local l = Logger:new("Library")
-- Find empty index in inventory list, or nil if all slots are occupied.
function find_empty_index(list)
......@@ -161,6 +162,45 @@ function queued_move_stack(from_stack_loc, to_stack_loc, count)
end
-- true on success, false on error
function setup_move_to_hotbar(my_inv, item_name)
local loc = find_item(my_inv, item_name)
if not loc then
l:err("Unable to move item to hotbar, " .. item_name .. " not found in inventory!")
return false
elseif loc.list_name == "main" and 1 < loc.index and loc.index < 9 then
return true -- nothing to do
end
local wanted_stack = my_inv[loc.list_name][loc.index]
local from = { inv_loc = nil, list_name = loc.list_name, list_index = loc.index }
local hotbar = { inv_loc = nil, list_name = "main", list_index = 8 }
-- move existing item out of hotbar
local stack = my_inv["main"][8]
local to = nil
if stack:get_count() > 0 then -- find empty spot to move this item to
local empty_index = find_empty_index(my_inv["main"])
if not empty_index then
l:err("Unable to move item to hotbar, no empty spot left")
return false
end
to = { inv_loc = nil, list_name = "main", list_index = empty_index }
end
queue:prepend(function() return queued_ensure_stack(hotbar, wanted_stack:get_name(), wanted_stack:get_count(), 5) end)
queue:prepend(function() return queued_move_stack(from, hotbar, wanted_stack:get_count()) end)
if stack:get_count() > 0 then
queue:prepend(function() return queued_ensure_stack(hotbar, "", 0, 5) end)
queue:prepend(function() return queued_move_stack(hotbar, to, stack:get_count()) end)
end
return true
end
-- Move all stacks from one inventory list to another
-- Returns total number of left-over items, not delay!
function setup_queued_move_list(from_inv_loc, from_list_name, to_inv_loc, to_list_name)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment