bags.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. -- Bags for Minetest
  2. -- Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
  3. -- License: GPLv3
  4. local S = unified_inventory.gettext
  5. unified_inventory.register_page("bags", {
  6. get_formspec = function(player)
  7. local player_name = player:get_player_name()
  8. local formspec = "background[0.06,0.99;7.92,7.52;ui_bags_main_form.png]"
  9. formspec = formspec.."label[0,0;"..S("Bags").."]"
  10. formspec = formspec.."button[0,2;2,0.5;bag1;Bag 1]"
  11. formspec = formspec.."button[2,2;2,0.5;bag2;Bag 2]"
  12. formspec = formspec.."button[4,2;2,0.5;bag3;Bag 3]"
  13. formspec = formspec.."button[6,2;2,0.5;bag4;Bag 4]"
  14. formspec = formspec.."listcolors[#00000000;#00000000]"
  15. formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."_bags;bag1;0.5,1;1,1;]"
  16. formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."_bags;bag2;2.5,1;1,1;]"
  17. formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."_bags;bag3;4.5,1;1,1;]"
  18. formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."_bags;bag4;6.5,1;1,1;]"
  19. return {formspec=formspec}
  20. end,
  21. })
  22. unified_inventory.register_button("bags", {
  23. type = "image",
  24. image = "ui_bags_icon.png",
  25. tooltip = S("Bags"),
  26. hide_lite=true
  27. })
  28. unified_inventory.register_page("bag1", {
  29. get_formspec = function(player)
  30. local stack = player:get_inventory():get_stack("bag1", 1)
  31. local image = stack:get_definition().inventory_image
  32. local formspec = "image[7,0;1,1;"..image.."]"
  33. formspec = formspec.."label[0,0;Bag 1]"
  34. formspec = formspec.."listcolors[#00000000;#00000000]"
  35. formspec = formspec.."list[current_player;bag1contents;0,1;8,3;]"
  36. formspec = formspec.."listring[current_name;bag1contents]"
  37. formspec = formspec.."listring[current_player;main]"
  38. local slots = stack:get_definition().groups.bagslots
  39. if slots == 8 then
  40. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
  41. elseif slots == 16 then
  42. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
  43. elseif slots == 24 then
  44. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
  45. end
  46. return {formspec=formspec}
  47. end,
  48. })
  49. unified_inventory.register_page("bag2", {
  50. get_formspec = function(player)
  51. local stack = player:get_inventory():get_stack("bag2", 1)
  52. local image = stack:get_definition().inventory_image
  53. local formspec = "image[7,0;1,1;"..image.."]"
  54. formspec = formspec.."label[0,0;Bag 2]"
  55. formspec = formspec.."listcolors[#00000000;#00000000]"
  56. formspec = formspec.."list[current_player;bag2contents;0,1;8,3;]"
  57. formspec = formspec.."listring[current_name;bag2contents]"
  58. formspec = formspec.."listring[current_player;main]"
  59. local slots = stack:get_definition().groups.bagslots
  60. if slots == 8 then
  61. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
  62. elseif slots == 16 then
  63. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
  64. elseif slots == 24 then
  65. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
  66. end
  67. return {formspec=formspec}
  68. end,
  69. })
  70. unified_inventory.register_page("bag3", {
  71. get_formspec = function(player)
  72. local stack = player:get_inventory():get_stack("bag3", 1)
  73. local image = stack:get_definition().inventory_image
  74. local formspec = "image[7,0;1,1;"..image.."]"
  75. formspec = formspec.."label[0,0;Bag 3]"
  76. formspec = formspec.."listcolors[#00000000;#00000000]"
  77. formspec = formspec.."list[current_player;bag3contents;0,1;8,3;]"
  78. formspec = formspec.."listring[current_name;bag3contents]"
  79. formspec = formspec.."listring[current_player;main]"
  80. local slots = stack:get_definition().groups.bagslots
  81. if slots == 8 then
  82. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
  83. elseif slots == 16 then
  84. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
  85. elseif slots == 24 then
  86. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
  87. end
  88. return {formspec=formspec}
  89. end,
  90. })
  91. unified_inventory.register_page("bag4", {
  92. get_formspec = function(player)
  93. local stack = player:get_inventory():get_stack("bag4", 1)
  94. local image = stack:get_definition().inventory_image
  95. local formspec = "image[7,0;1,1;"..image.."]"
  96. formspec = formspec.."label[0,0;Bag 4]"
  97. formspec = formspec.."listcolors[#00000000;#00000000]"
  98. formspec = formspec.."list[current_player;bag4contents;0,1;8,3;]"
  99. formspec = formspec.."listring[current_name;bag4contents]"
  100. formspec = formspec.."listring[current_player;main]"
  101. local slots = stack:get_definition().groups.bagslots
  102. if slots == 8 then
  103. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
  104. elseif slots == 16 then
  105. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
  106. elseif slots == 24 then
  107. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
  108. end
  109. return {formspec=formspec}
  110. end,
  111. })
  112. minetest.register_on_player_receive_fields(function(player, formname, fields)
  113. if formname ~= "" then
  114. return
  115. end
  116. for i = 1, 4 do
  117. if fields["bag"..i] then
  118. local stack = player:get_inventory():get_stack("bag"..i, 1)
  119. if not stack:get_definition().groups.bagslots then
  120. return
  121. end
  122. unified_inventory.set_inventory_formspec(player, "bag"..i)
  123. return
  124. end
  125. end
  126. end)
  127. minetest.register_on_joinplayer(function(player)
  128. local player_inv = player:get_inventory()
  129. local bags_inv = minetest.create_detached_inventory(player:get_player_name().."_bags",{
  130. on_put = function(inv, listname, index, stack, player)
  131. player:get_inventory():set_stack(listname, index, stack)
  132. player:get_inventory():set_size(listname.."contents",
  133. stack:get_definition().groups.bagslots)
  134. end,
  135. on_take = function(inv, listname, index, stack, player)
  136. player:get_inventory():set_stack(listname, index, nil)
  137. end,
  138. allow_put = function(inv, listname, index, stack, player)
  139. if stack:get_definition().groups.bagslots then
  140. return 1
  141. else
  142. return 0
  143. end
  144. end,
  145. allow_take = function(inv, listname, index, stack, player)
  146. if player:get_inventory():is_empty(listname.."contents") then
  147. return stack:get_count()
  148. else
  149. return 0
  150. end
  151. end,
  152. allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  153. return 0
  154. end,
  155. })
  156. for i=1,4 do
  157. local bag = "bag"..i
  158. player_inv:set_size(bag, 1)
  159. bags_inv:set_size(bag, 1)
  160. bags_inv:set_stack(bag, 1, player_inv:get_stack(bag, 1))
  161. end
  162. end)
  163. -- register bag tools
  164. minetest.register_tool("unified_inventory:bag_small", {
  165. description = S("Small Bag"),
  166. inventory_image = "bags_small.png",
  167. groups = {bagslots=8},
  168. })
  169. minetest.register_tool("unified_inventory:bag_medium", {
  170. description = S("Medium Bag"),
  171. inventory_image = "bags_medium.png",
  172. groups = {bagslots=16},
  173. })
  174. minetest.register_tool("unified_inventory:bag_large", {
  175. description = S("Large Bag"),
  176. inventory_image = "bags_large.png",
  177. groups = {bagslots=24},
  178. })
  179. -- register bag crafts
  180. minetest.register_craft({
  181. output = "unified_inventory:bag_small",
  182. recipe = {
  183. {"", "farming:cotton", ""},
  184. {"group:wool", "group:wool", "group:wool"},
  185. {"group:wool", "group:wool", "group:wool"},
  186. },
  187. })
  188. minetest.register_craft({
  189. output = "unified_inventory:bag_medium",
  190. recipe = {
  191. {"", "", ""},
  192. {"farming:cotton", "unified_inventory:bag_small", "farming:cotton"},
  193. {"farming:cotton", "unified_inventory:bag_small", "farming:cotton"},
  194. },
  195. })
  196. minetest.register_craft({
  197. output = "unified_inventory:bag_large",
  198. recipe = {
  199. {"", "", ""},
  200. {"farming:cotton", "unified_inventory:bag_medium", "farming:cotton"},
  201. {"farming:cotton", "unified_inventory:bag_medium", "farming:cotton"},
  202. },
  203. })