local function after_dig_node(pos)
   local node = minetest.get_node(pos)
   local above_pos = {x=pos.x, y=pos.y+1, z=pos.z}
   local above_name = minetest.get_node(above_pos).name
   if above_name == 'furniture:ropebox_mid' then
      minetest.set_node(above_pos, {name = 'furniture:ropebox_end', param2 = node.param2})
   end
   local below_pos = {x=pos.x, y=pos.y-1, z=pos.z}
   local below_name = minetest.get_node(below_pos).name
   while below_name == 'furniture:ropebox_mid' do
      minetest.remove_node(below_pos)
      below_pos = {x=below_pos.x, y=below_pos.y-1, z=below_pos.z}
      below_name = minetest.get_node(below_pos).name
   end
   if below_name == 'furniture:ropebox_end' then
      minetest.remove_node(below_pos)
   end
end

minetest.register_node('furniture:ropebox_top', {
   description = 'Ropebox',
   drawtype = 'mesh',
   mesh = 'furniture_ropebox_top.obj',
   tiles = {'furniture_ropebox.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   climbable = true,
   sunlight_propagates = true,
   walkable = false,
   groups = {breakable=1},
   after_place_node = function(pos)
      local node = minetest.get_node(pos)
      local below_pos = {x=pos.x, y=pos.y-1, z=pos.z}
      local below_name = minetest.get_node(below_pos).name
      while below_name == 'air' do
         minetest.set_node(below_pos, {name = 'furniture:ropebox_mid', param2 = node.param2})
         below_pos = {x=below_pos.x, y=below_pos.y-1, z=below_pos.z}
         below_name = minetest.get_node(below_pos).name
      end
      if below_name ~= 'air' then
         local pos = {x=below_pos.x, y=below_pos.y+1, z=below_pos.z}
         minetest.set_node(pos, {name = 'furniture:ropebox_end', param2 = node.param2})
      end
   end,
   after_dig_node = after_dig_node
})

local box = {
   type = 'fixed',
      fixed = {
         {-.2, -.5, -.2, .2, .5, .2}}}

minetest.register_node('furniture:ropebox_mid', {
   description = 'Ropebox',
   drawtype = 'mesh',
   mesh = 'furniture_ropebox_mid.obj',
   tiles = {'furniture_ropebox.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   drop = '',
   climbable = true,
   sunlight_propagates = true,
   walkable = false,
   selection_box = box,
   collision_box = box,
   groups = {breakable=1, not_in_creative_inventory=1},
   after_dig_node = after_dig_node
})

minetest.register_node('furniture:ropebox_end', {
   description = 'Ropebox',
   drawtype = 'mesh',
   mesh = 'furniture_ropebox_end.obj',
   tiles = {'furniture_ropebox.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   drop = '',
   climbable = true,
   sunlight_propagates = true,
   walkable = false,
   selection_box = box,
   collision_box = box,
   groups = {breakable=1, not_in_creative_inventory=1},
   after_dig_node = after_dig_node
})
