An extensible inventory mod which allows searching crafting and browsing for recipes in the same dialogue. https://content.minetest.net/packages/RealBadAngel/unified_inventory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.0KB

  1. --[[
  2. Bags for Minetest
  3. Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
  4. License: GPLv3
  5. --]]
  6. local S = minetest.get_translator("unified_inventory")
  7. local F = minetest.formspec_escape
  8. unified_inventory.register_page("bags", {
  9. get_formspec = function(player)
  10. local player_name = player:get_player_name()
  11. return { formspec = table.concat({
  12. "background[0.06,0.99;7.92,7.52;ui_bags_main_form.png]",
  13. "label[0,0;" .. F(S("Bags")) .. "]",
  14. "button[0,2;2,0.5;bag1;" .. F(S("Bag @1", 1)) .. "]",
  15. "button[2,2;2,0.5;bag2;" .. F(S("Bag @1", 2)) .. "]",
  16. "button[4,2;2,0.5;bag3;" .. F(S("Bag @1", 3)) .. "]",
  17. "button[6,2;2,0.5;bag4;" .. F(S("Bag @1", 4)) .. "]",
  18. "listcolors[#00000000;#00000000]",
  19. "list[detached:" .. F(player_name) .. "_bags;bag1;0.5,1;1,1;]",
  20. "list[detached:" .. F(player_name) .. "_bags;bag2;2.5,1;1,1;]",
  21. "list[detached:" .. F(player_name) .. "_bags;bag3;4.5,1;1,1;]",
  22. "list[detached:" .. F(player_name) .. "_bags;bag4;6.5,1;1,1;]"
  23. }) }
  24. end,
  25. })
  26. unified_inventory.register_button("bags", {
  27. type = "image",
  28. image = "ui_bags_icon.png",
  29. tooltip = S("Bags"),
  30. hide_lite=true
  31. })
  32. local function get_player_bag_stack(player, i)
  33. return minetest.get_inventory({
  34. type = "detached",
  35. name = player:get_player_name() .. "_bags"
  36. }):get_stack("bag" .. i, 1)
  37. end
  38. for bag_i = 1, 4 do
  39. unified_inventory.register_page("bag" .. bag_i, {
  40. get_formspec = function(player)
  41. local stack = get_player_bag_stack(player, bag_i)
  42. local image = stack:get_definition().inventory_image
  43. local fs = {
  44. "image[7,0;1,1;" .. image .. "]",
  45. "label[0,0;" .. F(S("Bag @1", bag_i)) .. "]",
  46. "listcolors[#00000000;#00000000]",
  47. "list[current_player;bag" .. bag_i .. "contents;0,1;8,3;]",
  48. "listring[current_name;bag" .. bag_i .. "contents]",
  49. "listring[current_player;main]"
  50. }
  51. local slots = stack:get_definition().groups.bagslots
  52. if slots == 8 then
  53. fs[#fs + 1] = "background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
  54. elseif slots == 16 then
  55. fs[#fs + 1] = "background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
  56. elseif slots == 24 then
  57. fs[#fs + 1] = "background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
  58. end
  59. local player_name = player:get_player_name() -- For if statement.
  60. if unified_inventory.trash_enabled
  61. or unified_inventory.is_creative(player_name)
  62. or minetest.get_player_privs(player_name).give then
  63. fs[#fs + 1] = "background[6.06,0;0.92,0.92;ui_bags_trash.png]"
  64. .. "list[detached:trash;main;6,0.1;1,1;]"
  65. end
  66. local inv = player:get_inventory()
  67. for i = 1, 4 do
  68. local def = get_player_bag_stack(player, i):get_definition()
  69. if def.groups.bagslots then
  70. local list_name = "bag" .. i .. "contents"
  71. local size = inv:get_size(list_name)
  72. local used = 0
  73. for si = 1, size do
  74. local stk = inv:get_stack(list_name, si)
  75. if not stk:is_empty() then
  76. used = used + 1
  77. end
  78. end
  79. local img = def.inventory_image
  80. local label = F(S("Bag @1", i)) .. "\n" .. used .. "/" .. size
  81. fs[#fs + 1] = string.format("image_button[%i,0;1,1;%s;bag%i;%s]",
  82. i + 1, img, i, label)
  83. end
  84. end
  85. return { formspec = table.concat(fs) }
  86. end,
  87. })
  88. end
  89. minetest.register_on_player_receive_fields(function(player, formname, fields)
  90. if formname ~= "" then
  91. return
  92. end
  93. for i = 1, 4 do
  94. if fields["bag" .. i] then
  95. local stack = get_player_bag_stack(player, i)
  96. if not stack:get_definition().groups.bagslots then
  97. return
  98. end
  99. unified_inventory.set_inventory_formspec(player, "bag" .. i)
  100. return
  101. end
  102. end
  103. end)
  104. local function save_bags_metadata(player, bags_inv)
  105. local is_empty = true
  106. local bags = {}
  107. for i = 1, 4 do
  108. local bag = "bag" .. i
  109. if not bags_inv:is_empty(bag) then
  110. -- Stack limit is 1, otherwise use stack:to_string()
  111. bags[i] = bags_inv:get_stack(bag, 1):get_name()
  112. is_empty = false
  113. end
  114. end
  115. local meta = player:get_meta()
  116. if is_empty then
  117. meta:set_string("unified_inventory:bags", nil)
  118. else
  119. meta:set_string("unified_inventory:bags",
  120. minetest.serialize(bags))
  121. end
  122. end
  123. local function load_bags_metadata(player, bags_inv)
  124. local player_inv = player:get_inventory()
  125. local meta = player:get_meta()
  126. local bags_meta = meta:get_string("unified_inventory:bags")
  127. local bags = bags_meta and minetest.deserialize(bags_meta) or {}
  128. local dirty_meta = false
  129. if not bags_meta then
  130. -- Backwards compatiblity
  131. for i = 1, 4 do
  132. local bag = "bag" .. i
  133. if not player_inv:is_empty(bag) then
  134. -- Stack limit is 1, otherwise use stack:to_string()
  135. bags[i] = player_inv:get_stack(bag, 1):get_name()
  136. dirty_meta = true
  137. end
  138. end
  139. end
  140. -- Fill detached slots
  141. for i = 1, 4 do
  142. local bag = "bag" .. i
  143. bags_inv:set_size(bag, 1)
  144. bags_inv:set_stack(bag, 1, bags[i] or "")
  145. end
  146. if dirty_meta then
  147. -- Requires detached inventory to be set up
  148. save_bags_metadata(player, bags_inv)
  149. end
  150. -- Clean up deprecated garbage after saving
  151. for i = 1, 4 do
  152. local bag = "bag" .. i
  153. player_inv:set_size(bag, 0)
  154. end
  155. end
  156. minetest.register_on_joinplayer(function(player)
  157. local player_name = player:get_player_name()
  158. local bags_inv = minetest.create_detached_inventory(player_name .. "_bags",{
  159. on_put = function(inv, listname, index, stack, player)
  160. player:get_inventory():set_size(listname .. "contents",
  161. stack:get_definition().groups.bagslots)
  162. save_bags_metadata(player, inv)
  163. end,
  164. allow_put = function(inv, listname, index, stack, player)
  165. local new_slots = stack:get_definition().groups.bagslots
  166. if not new_slots then
  167. return 0
  168. end
  169. local player_inv = player:get_inventory()
  170. local old_slots = player_inv:get_size(listname .. "contents")
  171. if new_slots >= old_slots then
  172. return 1
  173. end
  174. -- using a smaller bag, make sure it fits
  175. local old_list = player_inv:get_list(listname .. "contents")
  176. local new_list = {}
  177. local slots_used = 0
  178. local use_new_list = false
  179. for i, v in ipairs(old_list) do
  180. if v and not v:is_empty() then
  181. slots_used = slots_used + 1
  182. use_new_list = i > new_slots
  183. new_list[slots_used] = v
  184. end
  185. end
  186. if new_slots >= slots_used then
  187. if use_new_list then
  188. player_inv:set_list(listname .. "contents", new_list)
  189. end
  190. return 1
  191. end
  192. -- New bag is smaller: Disallow inserting
  193. return 0
  194. end,
  195. allow_take = function(inv, listname, index, stack, player)
  196. if player:get_inventory():is_empty(listname .. "contents") then
  197. return stack:get_count()
  198. end
  199. return 0
  200. end,
  201. on_take = function(inv, listname, index, stack, player)
  202. player:get_inventory():set_size(listname .. "contents", 0)
  203. save_bags_metadata(player, inv)
  204. end,
  205. allow_move = function()
  206. return 0
  207. end,
  208. }, player_name)
  209. load_bags_metadata(player, bags_inv)
  210. end)
  211. -- register bag tools
  212. minetest.register_tool("unified_inventory:bag_small", {
  213. description = S("Small Bag"),
  214. inventory_image = "bags_small.png",
  215. groups = {bagslots=8},
  216. })
  217. minetest.register_tool("unified_inventory:bag_medium", {
  218. description = S("Medium Bag"),
  219. inventory_image = "bags_medium.png",
  220. groups = {bagslots=16},
  221. })
  222. minetest.register_tool("unified_inventory:bag_large", {
  223. description = S("Large Bag"),
  224. inventory_image = "bags_large.png",
  225. groups = {bagslots=24},
  226. })
  227. -- register bag crafts
  228. if minetest.get_modpath("farming") ~= nil then
  229. minetest.register_craft({
  230. output = "unified_inventory:bag_small",
  231. recipe = {
  232. {"", "farming:string", ""},
  233. {"group:wool", "group:wool", "group:wool"},
  234. {"group:wool", "group:wool", "group:wool"},
  235. },
  236. })
  237. minetest.register_craft({
  238. output = "unified_inventory:bag_medium",
  239. recipe = {
  240. {"", "", ""},
  241. {"farming:string", "unified_inventory:bag_small", "farming:string"},
  242. {"farming:string", "unified_inventory:bag_small", "farming:string"},
  243. },
  244. })
  245. minetest.register_craft({
  246. output = "unified_inventory:bag_large",
  247. recipe = {
  248. {"", "", ""},
  249. {"farming:string", "unified_inventory:bag_medium", "farming:string"},
  250. {"farming:string", "unified_inventory:bag_medium", "farming:string"},
  251. },
  252. })
  253. end