api.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. -- Create detached creative inventory after loading all mods
  2. minetest.after(0.01, function()
  3. unified_inventory.items_list = {}
  4. for name, def in pairs(minetest.registered_items) do
  5. if (not def.groups.not_in_creative_inventory or
  6. def.groups.not_in_creative_inventory == 0) and
  7. def.description and def.description ~= "" then
  8. table.insert(unified_inventory.items_list, name)
  9. local recipes = minetest.get_all_craft_recipes(name)
  10. if recipes then
  11. unified_inventory.crafts_table[name] = recipes
  12. else
  13. unified_inventory.crafts_table[name] = {}
  14. end
  15. end
  16. end
  17. table.sort(unified_inventory.items_list)
  18. unified_inventory.items_list_size = #unified_inventory.items_list
  19. print("Unified Inventory. inventory size: "..unified_inventory.items_list_size)
  20. end)
  21. -- load_home
  22. local function load_home()
  23. local input = io.open(unified_inventory.home_filename, "r")
  24. if input then
  25. while true do
  26. local x = input:read("*n")
  27. if x == nil then
  28. break
  29. end
  30. local y = input:read("*n")
  31. local z = input:read("*n")
  32. local name = input:read("*l")
  33. unified_inventory.home_pos[name:sub(2)] = {x = x, y = y, z = z}
  34. end
  35. io.close(input)
  36. else
  37. unified_inventory.home_pos = {}
  38. end
  39. end
  40. load_home()
  41. function unified_inventory.set_home(player, pos)
  42. local player_name = player:get_player_name()
  43. unified_inventory.home_pos[player_name] = pos
  44. -- save the home data from the table to the file
  45. local output = io.open(unified_inventory.home_filename, "w")
  46. for k, v in pairs(unified_inventory.home_pos) do
  47. if v ~= nil then
  48. output:write(math.floor(v.x).." "
  49. ..math.floor(v.y).." "
  50. ..math.floor(v.z).." "
  51. ..k.."\n")
  52. end
  53. end
  54. io.close(output)
  55. end
  56. function unified_inventory.go_home(player)
  57. local pos = unified_inventory.home_pos[player:get_player_name()]
  58. if pos ~= nil then
  59. player:setpos(pos)
  60. end
  61. end
  62. -- register_craft
  63. function unified_inventory.register_craft(options)
  64. if options.output == nil then
  65. return
  66. end
  67. local itemstack = ItemStack(options.output)
  68. if itemstack:is_empty() then
  69. return
  70. end
  71. if unified_inventory.crafts_table[itemstack:get_name()] == nil then
  72. unified_inventory.crafts_table[itemstack:get_name()] = {}
  73. end
  74. table.insert(unified_inventory.crafts_table[itemstack:get_name()],options)
  75. end
  76. function unified_inventory.register_page(name, def)
  77. unified_inventory.pages[name] = def
  78. end
  79. function unified_inventory.register_button(name, def)
  80. if not def.action then
  81. def.action = function(player)
  82. unified_inventory.set_inventory_formspec(player, name)
  83. end
  84. end
  85. def.name = name
  86. table.insert(unified_inventory.buttons, def)
  87. end
  88. function unified_inventory.is_creative(playername)
  89. if minetest.check_player_privs(playername, {creative=true}) or
  90. minetest.setting_getbool("creative_mode") then
  91. return true
  92. end
  93. end