--This is a support node, it doesn't give out XP, but rather items.
--The items can be required by other nodes and those will reward XP.

local storage_locker =
   'size[8,6]'..
   'textarea[2.25,.5;4.25,2.1;;Instructions;Place an item request in the left, and take your item out of the slot on the right.\nPlease be patient, it takes time to find the part you want.]' ..
   'list[current_name;src;.75,1;1,1;]'..
   'list[current_name;dst;6.25,1;1,1;]'..
   'list[current_player;main;0,4;8,2;]'..
   'listring[current_player;main]'..
   'listring[current_name;src]'..
   'listring[current_name;dst]'..
   'listring[current_player;main]'

local storage_on_construct = function(pos)
   local meta = minetest.get_meta(pos)
   local inv = meta:get_inventory()
   inv:set_size('src', 1)
   inv:set_size('dst', 1)
   meta:set_string('infotext', 'Storage Locker')
   meta:set_string('formspec', storage_locker)
end

local storage_allow_metadata_inventory_put = function(pos, listname, index, stack, player)
   if listname == 'src' then
      local item = string.sub(stack:get_name(), -3)
      if item == 'req' then
         return 1
      else
         return 0
      end
   else
      return 0
   end
end

local storage_on_metadata_inventory_put = function(pos, listname, index, stack, player)
   local timer = minetest.get_node_timer(pos)
   timer:start(5)
end

local storage_on_timer = function(pos)
   local meta = minetest.get_meta(pos)
   local inv = meta:get_inventory()
   local instack = inv:get_stack('src', 1)
   local inputstack = instack:get_name()
   local item = string.sub(inputstack, 0, -5)
   minetest.sound_play('tasks_storage_locker', {pos = pos, gain = 1, max_hear_distance = 10})
   inv:set_stack('src', 1, '')
   inv:set_stack('dst', 1, item)
end

local col_box = {
   type = 'fixed',
   fixed = {{-.45, -.5, -.45, .5, .275, .5},
            {-.45, .275, 0, .5, .7, .5}, --Left, Bottom, Front, Right, Top, Back
            {.5, -.5, -.45, 1.45, 1.5, .5}}
   }

minetest.register_node('tasks:storage_locker_0', {
   description = 'Red Storage Locker',
   drawtype = 'mesh',
   mesh = 'tasks_storage_locker.obj',
   tiles = {'tasks_storage_locker_0.png'},
   use_texture_alpha = 'clip',
   groups = {breakable=1},
   light_source = 2,
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = col_box,
   collision_box = col_box,
   on_construct = storage_on_construct,
   allow_metadata_inventory_put = storage_allow_metadata_inventory_put,
   on_metadata_inventory_put = storage_on_metadata_inventory_put,
   on_timer = storage_on_timer
})

minetest.register_node('tasks:storage_trunk_0', {
   description = 'Old Storage Trunk',
   drawtype = 'mesh',
   mesh = 'tasks_storage_trunk.obj',
   tiles = {'tasks_storage_trunk.png'},
   groups = {breakable=1},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, 1.5, .5, .5},
      },
   collision_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, 1.5, .5, .5},
      },
   on_construct = storage_on_construct,
   allow_metadata_inventory_put = storage_allow_metadata_inventory_put,
   on_metadata_inventory_put = storage_on_metadata_inventory_put,
   on_timer = storage_on_timer
})
