bags.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_inv = player:get_inventory()
  158. local player_name = player:get_player_name()
  159. local bags_inv = minetest.create_detached_inventory(player_name .. "_bags",{
  160. on_put = function(inv, listname, index, stack, player)
  161. player:get_inventory():set_size(listname .. "contents",
  162. stack:get_definition().groups.bagslots)
  163. save_bags_metadata(player, inv)
  164. end,
  165. allow_put = function(inv, listname, index, stack, player)
  166. local new_slots = stack:get_definition().groups.bagslots
  167. if not new_slots then
  168. return 0
  169. end
  170. local player_inv = player:get_inventory()
  171. local old_slots = player_inv:get_size(listname .. "contents")
  172. if new_slots >= old_slots then
  173. return 1
  174. end
  175. -- using a smaller bag, make sure it fits
  176. local old_list = player_inv:get_list(listname .. "contents")
  177. local new_list = {}
  178. local slots_used = 0
  179. local use_new_list = false
  180. for i, v in ipairs(old_list) do
  181. if v and not v:is_empty() then
  182. slots_used = slots_used + 1
  183. use_new_list = i > new_slots
  184. new_list[slots_used] = v
  185. end
  186. end
  187. if new_slots >= slots_used then
  188. if use_new_list then
  189. player_inv:set_list(listname .. "contents", new_list)
  190. end
  191. return 1
  192. end
  193. -- New bag is smaller: Disallow inserting
  194. return 0
  195. end,
  196. allow_take = function(inv, listname, index, stack, player)
  197. if player:get_inventory():is_empty(listname .. "contents") then
  198. return stack:get_count()
  199. end
  200. return 0
  201. end,
  202. on_take = function(inv, listname, index, stack, player)
  203. player:get_inventory():set_size(listname .. "contents", 0)
  204. save_bags_metadata(player, inv)
  205. end,
  206. allow_move = function()
  207. return 0
  208. end,
  209. }, player_name)
  210. load_bags_metadata(player, bags_inv)
  211. end)
  212. -- register bag tools
  213. minetest.register_tool("unified_inventory:bag_small", {
  214. description = S("Small Bag"),
  215. inventory_image = "bags_small.png",
  216. groups = {bagslots=8},
  217. })
  218. minetest.register_tool("unified_inventory:bag_medium", {
  219. description = S("Medium Bag"),
  220. inventory_image = "bags_medium.png",
  221. groups = {bagslots=16},
  222. })
  223. minetest.register_tool("unified_inventory:bag_large", {
  224. description = S("Large Bag"),
  225. inventory_image = "bags_large.png",
  226. groups = {bagslots=24},
  227. })
  228. -- register bag crafts
  229. if minetest.get_modpath("farming") ~= nil then
  230. minetest.register_craft({
  231. output = "unified_inventory:bag_small",
  232. recipe = {
  233. {"", "farming:cotton", ""},
  234. {"group:wool", "group:wool", "group:wool"},
  235. {"group:wool", "group:wool", "group:wool"},
  236. },
  237. })
  238. minetest.register_craft({
  239. output = "unified_inventory:bag_medium",
  240. recipe = {
  241. {"", "", ""},
  242. {"farming:cotton", "unified_inventory:bag_small", "farming:cotton"},
  243. {"farming:cotton", "unified_inventory:bag_small", "farming:cotton"},
  244. },
  245. })
  246. minetest.register_craft({
  247. output = "unified_inventory:bag_large",
  248. recipe = {
  249. {"", "", ""},
  250. {"farming:cotton", "unified_inventory:bag_medium", "farming:cotton"},
  251. {"farming:cotton", "unified_inventory:bag_medium", "farming:cotton"},
  252. },
  253. })
  254. end