api.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. local S = unified_inventory.gettext
  2. -- Create detached creative inventory after loading all mods
  3. minetest.after(0.01, function()
  4. local rev_aliases = {}
  5. for source, target in pairs(minetest.registered_aliases) do
  6. if not rev_aliases[target] then rev_aliases[target] = {} end
  7. table.insert(rev_aliases[target], source)
  8. end
  9. unified_inventory.items_list = {}
  10. for name, def in pairs(minetest.registered_items) do
  11. if (not def.groups.not_in_creative_inventory or
  12. def.groups.not_in_creative_inventory == 0) and
  13. def.description and def.description ~= "" then
  14. table.insert(unified_inventory.items_list, name)
  15. local all_names = rev_aliases[name] or {}
  16. table.insert(all_names, name)
  17. for _, name in ipairs(all_names) do
  18. local recipes = minetest.get_all_craft_recipes(name)
  19. if recipes then
  20. for _, recipe in ipairs(recipes) do
  21. local unknowns
  22. for _,chk in pairs(recipe.items) do
  23. local groupchk = string.find(chk, "group:")
  24. if (not groupchk and not minetest.registered_items[chk])
  25. or (groupchk and not unified_inventory.get_group_item(string.gsub(chk, "group:", "")).item) then
  26. unknowns = true
  27. end
  28. end
  29. if not unknowns then
  30. unified_inventory.register_craft(recipe)
  31. end
  32. end
  33. end
  34. end
  35. end
  36. end
  37. table.sort(unified_inventory.items_list)
  38. unified_inventory.items_list_size = #unified_inventory.items_list
  39. print("Unified Inventory. inventory size: "..unified_inventory.items_list_size)
  40. for _, name in ipairs(unified_inventory.items_list) do
  41. local def = minetest.registered_items[name]
  42. if type(def.drop) == "string" then
  43. local dstack = ItemStack(def.drop)
  44. if not dstack:is_empty() and dstack:get_name() ~= name then
  45. unified_inventory.register_craft({
  46. type = "digging",
  47. items = {name},
  48. output = def.drop,
  49. width = 0,
  50. })
  51. end
  52. end
  53. end
  54. for _, recipes in pairs(unified_inventory.crafts_for.recipe) do
  55. for _, recipe in ipairs(recipes) do
  56. local ingredient_items = {}
  57. for _, spec in ipairs(recipe.items) do
  58. local matches_spec = unified_inventory.canonical_item_spec_matcher(spec)
  59. for _, name in ipairs(unified_inventory.items_list) do
  60. if matches_spec(name) then
  61. ingredient_items[name] = true
  62. end
  63. end
  64. end
  65. for name, _ in pairs(ingredient_items) do
  66. if unified_inventory.crafts_for.usage[name] == nil then
  67. unified_inventory.crafts_for.usage[name] = {}
  68. end
  69. table.insert(unified_inventory.crafts_for.usage[name], recipe)
  70. end
  71. end
  72. end
  73. end)
  74. -- load_home
  75. local function load_home()
  76. local input = io.open(unified_inventory.home_filename, "r")
  77. if not input then
  78. unified_inventory.home_pos = {}
  79. return
  80. end
  81. while true do
  82. local x = input:read("*n")
  83. if not x then break end
  84. local y = input:read("*n")
  85. local z = input:read("*n")
  86. local name = input:read("*l")
  87. unified_inventory.home_pos[name:sub(2)] = {x = x, y = y, z = z}
  88. end
  89. io.close(input)
  90. end
  91. load_home()
  92. function unified_inventory.set_home(player, pos)
  93. local player_name = player:get_player_name()
  94. unified_inventory.home_pos[player_name] = vector.round(pos)
  95. -- save the home data from the table to the file
  96. local output = io.open(unified_inventory.home_filename, "w")
  97. for k, v in pairs(unified_inventory.home_pos) do
  98. output:write(v.x.." "..v.y.." "..v.z.." "..k.."\n")
  99. end
  100. io.close(output)
  101. end
  102. function unified_inventory.go_home(player)
  103. local pos = unified_inventory.home_pos[player:get_player_name()]
  104. if pos then
  105. player:setpos(pos)
  106. end
  107. end
  108. -- register_craft
  109. function unified_inventory.register_craft(options)
  110. if not options.output then
  111. return
  112. end
  113. local itemstack = ItemStack(options.output)
  114. if itemstack:is_empty() then
  115. return
  116. end
  117. if options.type == "normal" and options.width == 0 then
  118. options = { type = "shapeless", items = options.items, output = options.output, width = 0 }
  119. end
  120. if not unified_inventory.crafts_for.recipe[itemstack:get_name()] then
  121. unified_inventory.crafts_for.recipe[itemstack:get_name()] = {}
  122. end
  123. table.insert(unified_inventory.crafts_for.recipe[itemstack:get_name()],options)
  124. end
  125. local craft_type_defaults = {
  126. width = 3,
  127. height = 3,
  128. uses_crafting_grid = false,
  129. }
  130. function unified_inventory.craft_type_defaults(name, options)
  131. if not options.description then
  132. options.description = name
  133. end
  134. setmetatable(options, {__index = craft_type_defaults})
  135. return options
  136. end
  137. function unified_inventory.register_craft_type(name, options)
  138. unified_inventory.registered_craft_types[name] =
  139. unified_inventory.craft_type_defaults(name, options)
  140. end
  141. unified_inventory.register_craft_type("normal", {
  142. description = "Crafting",
  143. icon = "ui_craftgrid_icon.png",
  144. width = 3,
  145. height = 3,
  146. get_shaped_craft_width = function (craft) return craft.width end,
  147. dynamic_display_size = function (craft)
  148. local w = craft.width
  149. local h = math.ceil(table.maxn(craft.items) / craft.width)
  150. local g = w < h and h or w
  151. return { width = g, height = g }
  152. end,
  153. uses_crafting_grid = true,
  154. })
  155. unified_inventory.register_craft_type("shapeless", {
  156. description = "Mixing",
  157. icon = "ui_craftgrid_icon.png",
  158. width = 3,
  159. height = 3,
  160. dynamic_display_size = function (craft)
  161. local maxn = table.maxn(craft.items)
  162. local g = 1
  163. while g*g < maxn do g = g + 1 end
  164. return { width = g, height = g }
  165. end,
  166. uses_crafting_grid = true,
  167. })
  168. unified_inventory.register_craft_type("cooking", {
  169. description = "Cooking",
  170. icon = "default_furnace_front.png",
  171. width = 1,
  172. height = 1,
  173. })
  174. unified_inventory.register_craft_type("digging", {
  175. description = "Digging",
  176. icon = "default_tool_steelpick.png",
  177. width = 1,
  178. height = 1,
  179. })
  180. function unified_inventory.register_page(name, def)
  181. unified_inventory.pages[name] = def
  182. end
  183. function unified_inventory.register_button(name, def)
  184. if not def.action then
  185. def.action = function(player)
  186. unified_inventory.set_inventory_formspec(player, name)
  187. end
  188. end
  189. def.name = name
  190. table.insert(unified_inventory.buttons, def)
  191. end
  192. function unified_inventory.is_creative(playername)
  193. return minetest.check_player_privs(playername, {creative=true})
  194. or minetest.setting_getbool("creative_mode")
  195. end