bags.lua 6.8 KB

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