inventory.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. -- Returns nil if there's no leftovers
  2. -- Otherwise returns a countlist of leftovers
  3. local add_count_list = function(inv, listname, count_list)
  4. local leftover_list
  5. for item, count in pairs(count_list) do
  6. local stack_max = ItemStack(item):get_stack_max()
  7. while count > 0 do
  8. local to_add = math.min(count, stack_max)
  9. local leftover = inv:add_item(listname, ItemStack({name=item, count=to_add}))
  10. local leftover_count = leftover:get_count()
  11. if leftover_count > 0 then
  12. leftover_list = leftover_list or {}
  13. leftover_list[item] = (leftover_list[item] or 0) + leftover_count + count
  14. break
  15. end
  16. count = count - to_add
  17. end
  18. end
  19. return leftover_list
  20. end
  21. -- Attempts to add the items in count_list to the inventory.
  22. -- Returns a count list containing the items that couldn't be added.
  23. simplecrafting_lib.add_items = function(inv, listname, count_list)
  24. return add_count_list(inv, listname, count_list) or {}
  25. end
  26. -- Attempts to add the items in count_list to the inventory.
  27. -- If it succeeds, returns true.
  28. -- If it fails, the inventory is not modified and returns false.
  29. simplecrafting_lib.add_items_if_room = function(inv, listname, count_list)
  30. local old_list = inv:get_list(listname) -- record current inventory
  31. if not add_count_list(inv, listname, count_list) then
  32. inv:set_list(listname, old_list) -- reset inventory
  33. return false
  34. end
  35. return true
  36. end
  37. -- Returns true if there's room in the inventory for all of the items in the count list,
  38. -- false otherwise.
  39. simplecrafting_lib.room_for_items = function(inv, listname, count_list)
  40. local old_list = inv:get_list(listname) -- record current inventory
  41. local result = add_count_list(inv, listname, count_list)
  42. inv:set_list(listname, old_list) -- reset inventory
  43. return result ~= nil
  44. end
  45. -- removes the items in the count_list (formatted as per recipe standards)
  46. -- from the inventory. Returns true on success, false on failure. Does not
  47. -- affect the inventory on failure (removal is atomic)
  48. simplecrafting_lib.remove_items = function(inv, listname, count_list)
  49. local old_list = inv:get_list(listname) -- record current inventory
  50. for item, count in pairs(count_list) do
  51. while count > 0 do
  52. -- We need to do this loop because we may be wanting to remove more items than
  53. -- a single stack of that item can hold.
  54. -- https://github.com/minetest/minetest/issues/8883
  55. local stack_to_remove = ItemStack({name=item, count=count})
  56. stack_to_remove:set_count(math.min(count, stack_to_remove:get_stack_max()))
  57. local removed = inv:remove_item(listname, stack_to_remove)
  58. if removed:is_empty() then
  59. -- ran out of things to take. Reset the inventory and return false
  60. inv:set_list(listname, old_list)
  61. return false
  62. end
  63. count = count - removed:get_count()
  64. end
  65. end
  66. return true
  67. end
  68. -- Drops the contents of a count_list at the given location in the world
  69. simplecrafting_lib.drop_items = function(pos, count_list)
  70. for item, count in pairs(count_list) do
  71. local stack_max = ItemStack(item):get_stack_max()
  72. while count > 0 do
  73. local to_add = math.min(count, stack_max)
  74. minetest.add_item(pos, ItemStack({name=item, count=to_add}))
  75. count = count - to_add
  76. end
  77. end
  78. end