diff --git a/sparkmachine/lua/quarry.lua b/sparkmachine/lua/quarry.lua index 15efe93..52cfa6c 100644 --- a/sparkmachine/lua/quarry.lua +++ b/sparkmachine/lua/quarry.lua @@ -36,6 +36,23 @@ local function try_drain_energy(target_pos, amount) --- end end +local function try_remove_item(target_pos, listname, itemname, amount) + local inv = minetest.get_inventory({type="node", pos=target_pos}) + local stack = ItemStack(itemname) + local amount = amount or 1 + stack:set_count(amount) + + local result = inv:remove_item(listname, stack) + + if result:get_count() ~= amount then + inv:add_item(listname, result) + else + return true + end + + return false +end + local function on_construct(pos, player) player:get_meta():set_string(NAME .. ":quarry_pos" , minetest.pos_to_string(pos)) @@ -133,13 +150,19 @@ end local function place_strut(position, quarrypos) if minetest.get_node(position).name == "air" then - if try_drain_energy(quarrypos, ENERGYCOST.static_strut) then - minetest.set_node(position, { name = NAME .. ":static_strut"}) + minetest.debug("PLACE") + if try_remove_item(quarrypos, "quarry", NAME .. ":static_strut") then + minetest.debug("ENERGY") + if try_drain_energy(quarrypos, ENERGYCOST.static_strut) then + minetest.debug("CONFIRM") + minetest.set_node(position, { name = NAME .. ":static_strut"}) + else + -- Restore items + end end return true - else - return false end + return false end