furniture = {}
furniture.players = {}

furniture.dyes = {
   {'grey',       'Grey',       '#808080'},
   {'dark_grey',  'Dark Grey',  '#404040'},
   {'black',      'Black',      '#1a1a1a'},
   {'violet',     'Violet',     '#480680'},
   {'blue',       'Blue',       '#00519d'},
   {'cyan',       'Cyan',       '#00959d'},
   {'dark_green', 'Dark Green', '#004d00'},
   {'green',      'Green',      '#008000'},
   {'yellow',     'Yellow',     '#ffff00'},
   {'brown',      'Brown',      '#40230f'},
   {'orange',     'Orange',     '#ff5600'},
   {'red',        'Red',        '#c91818'},
   {'magenta',    'Magenta',    '#d80481'},
   {'pink',       'Pink',       '#ff7dc8'},
   {'white',      'White',      '#ffffff'},
}

furniture.playing = function(player) -- Is player playing or building a level?
   local name = player:get_player_name()
   if minetest.check_player_privs(player, {creative = true}) then
      return false --Has creative, is building level
   else
      return true --Does not have creative, in playing level
   end
end

furniture.punch = function(pos, node, puncher, pointed_thing)
   if furniture.playing(puncher) then
      furniture.stash_punch(pos, node, puncher, pointed_thing)
   else
      local def = minetest.registered_nodes[node.name]
      local paramtype2 = def.paramtype2
      if paramtype2 == 'colorfacedir' then
         furniture.dye_more(pos, node, puncher, pointed_thing)
      end
   end
end

furniture.right_click = function(pos, node, clicker)
   if furniture.playing(clicker) then
      --nothing yet
   else
      furniture.dye_less(pos, node, clicker)
   end
end

furniture.stash_punch = function(pos, node, puncher, pointed_thing)
   local meta = minetest.get_meta(pos)
   local inv = meta:get_inventory()
   if not inv:is_empty('stash') then
      local name = puncher:get_player_name()
      local player_attributes = puncher:get_meta()
      local luck = (player_attributes:get_int('luck') or 1) * 10
      local chance = math.random(0,105)
      if luck >= chance then
         local stuff = inv:get_list('stash')
         local random = math.random(1,8)
         local item = stuff[random]
         local drop = item:get_name()
         local player_inv = puncher:get_inventory()
         if player_inv:room_for_item('main', drop) then
            player_inv:add_item('main', drop)
         else
            minetest.chat_send_player(name, 'You don\'t have room for that.')
         end
      end
   end
end

furniture.dye_more = function(pos, node, puncher, pointed_thing)
   local player = puncher:get_player_name()
   local wield = puncher:get_wielded_item()
   local wield_name = wield:get_name()
   if minetest.is_protected(pos, player) and not minetest.check_player_privs(puncher, 'protection_bypass') or wield_name == 'creative:tool_breaking' then
      return
   end
      minetest.swap_node(pos, {name = node.name, param2 = node.param2+32})
end

furniture.dye_less = function(pos, node, clicker)
   local player = clicker:get_player_name()
   if minetest.is_protected(pos, player) and not minetest.check_player_privs(clicker, 'protection_bypass') then
      return
   end
      minetest.swap_node(pos, {name = node.name, param2 = node.param2-32})
end

dofile(minetest.get_modpath('furniture')..'/bathroom.lua') --Things you'd find in a bathroom
dofile(minetest.get_modpath('furniture')..'/bedroom.lua') --Things you'd find in a bedroom.
dofile(minetest.get_modpath('furniture')..'/decor.lua')
dofile(minetest.get_modpath('furniture')..'/fences.lua')
dofile(minetest.get_modpath('furniture')..'/formspecs.lua') --Formspecs for the nodes.
dofile(minetest.get_modpath('furniture')..'/kitchen.lua') --appliances and counters
dofile(minetest.get_modpath('furniture')..'/library.lua') --Books things.
dofile(minetest.get_modpath('furniture')..'/medical.lua') --Stuff you might find in a hospital
dofile(minetest.get_modpath('furniture')..'/misc.lua') --Miscilanious things that don't particularly fit any single category
dofile(minetest.get_modpath('furniture')..'/news.lua')
dofile(minetest.get_modpath('furniture')..'/office.lua') --Things you'd find in an office.
dofile(minetest.get_modpath('furniture')..'/outdoors.lua')
dofile(minetest.get_modpath('furniture')..'/retail_shelving.lua') --Shelves like you'd find in a store.
dofile(minetest.get_modpath('furniture')..'/seating.lua') --chairs, benches, stools
dofile(minetest.get_modpath('furniture')..'/storage.lua') --Chests, boxes, etc.
dofile(minetest.get_modpath('furniture')..'/tables.lua') --Not sure if I'll ever have more than one table.
