init.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. give_initial_stuff = {
  2. items = {}
  3. }
  4. local volatile_positions = {}
  5. function give_initial_stuff.give(player)
  6. minetest.log("action",
  7. "Giving initial stuff to player " .. player:get_player_name())
  8. local inv = player:get_inventory()
  9. for i, stack in pairs(give_initial_stuff.items) do
  10. local s = inv:set_stack("main", i, stack)
  11. end
  12. end
  13. function give_initial_stuff.add(stack, prefered_position)
  14. local items = give_initial_stuff.items
  15. local volatile
  16. if not prefered_position
  17. then
  18. volatile = true
  19. --count to first empty slot
  20. prefered_position = 1
  21. while true
  22. do
  23. if items[prefered_position]
  24. then
  25. prefered_position = prefered_position + 1
  26. else
  27. break
  28. end
  29. end
  30. end
  31. while true
  32. do
  33. if items[prefered_position]
  34. then
  35. if volatile_positions[prefered_position] and not volatile
  36. then
  37. local s = items[prefered_position]
  38. items[prefered_position] = ItemStack(stack)
  39. stack = s
  40. volatile_positions[prefered_position] = nil
  41. volatile = true
  42. end
  43. prefered_position = prefered_position + 1
  44. else
  45. items[prefered_position] = ItemStack(stack)
  46. if volatile
  47. then
  48. volatile_positions[prefered_position] = true
  49. end
  50. break
  51. end
  52. end
  53. end
  54. function give_initial_stuff.clear()
  55. give_initial_stuff.items = {}
  56. end
  57. function give_initial_stuff.add_from_csv(str)
  58. local items = str:split(",")
  59. for _, itemname in ipairs(items) do
  60. give_initial_stuff.add(itemname)
  61. end
  62. end
  63. function give_initial_stuff.set_list(list)
  64. give_initial_stuff.items = list
  65. end
  66. function give_initial_stuff.get_list()
  67. return give_initial_stuff.items
  68. end
  69. --give_initial_stuff.add_from_csv(stuff_string)
  70. if minetest.settings:get_bool("give_initial_stuff") then
  71. minetest.register_on_newplayer(give_initial_stuff.give)
  72. end