bags.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. -- Bags for Minetest
  2. -- Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
  3. -- License: GPLv3
  4. unified_inventory.register_page("bags", {
  5. get_formspec = function(player)
  6. local player_name = player:get_player_name()
  7. local formspec = "background[0.06,0.99;7.92,7.52;ui_bags_main_form.png]"
  8. formspec = formspec.."image[0,0;1,1;ui_bags_icon.png]"
  9. formspec = formspec.."label[1,0;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:"..player_name.."_bags;bag1;0.5,1;1,1;]"
  16. formspec = formspec.."list[detached:"..player_name.."_bags;bag2;2.5,1;1,1;]"
  17. formspec = formspec.."list[detached:"..player_name.."_bags;bag3;4.5,1;1,1;]"
  18. formspec = formspec.."list[detached:"..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. })
  26. for i = 1, 4 do
  27. unified_inventory.register_page("bag"..i, {
  28. get_formspec = function(player)
  29. local stack = player:get_inventory():get_stack("bag"..i, 1)
  30. local image = stack:get_definition().inventory_image
  31. local formspec = "image[7,0;1,1;"..image.."]"
  32. formspec = formspec.."listcolors[#00000000;#00000000]"
  33. formspec = formspec.."list[current_player;bag"..i.."contents;0,1;8,3;]"
  34. local slots = stack:get_definition().groups.bagslots
  35. if slots == 8 then
  36. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
  37. elseif slots == 16 then
  38. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
  39. elseif slots == 24 then
  40. formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
  41. end
  42. return {formspec=formspec}
  43. end,
  44. })
  45. end
  46. minetest.register_on_player_receive_fields(function(player, formname, fields)
  47. if formname ~= "" then
  48. return
  49. end
  50. for i = 1, 4 do
  51. if fields["bag"..i] then
  52. local stack = player:get_inventory():get_stack("bag"..i, 1)
  53. if not stack:get_definition().groups.bagslots then
  54. return
  55. end
  56. unified_inventory.set_inventory_formspec(player, "bag"..i)
  57. return
  58. end
  59. end
  60. end)
  61. minetest.register_on_joinplayer(function(player)
  62. local player_inv = player:get_inventory()
  63. local bags_inv = minetest.create_detached_inventory(player:get_player_name().."_bags",{
  64. on_put = function(inv, listname, index, stack, player)
  65. player:get_inventory():set_stack(listname, index, stack)
  66. player:get_inventory():set_size(listname.."contents",
  67. stack:get_definition().groups.bagslots)
  68. end,
  69. on_take = function(inv, listname, index, stack, player)
  70. player:get_inventory():set_stack(listname, index, nil)
  71. end,
  72. allow_put = function(inv, listname, index, stack, player)
  73. if stack:get_definition().groups.bagslots then
  74. return 1
  75. else
  76. return 0
  77. end
  78. end,
  79. allow_take = function(inv, listname, index, stack, player)
  80. if player:get_inventory():is_empty(listname.."contents") then
  81. return stack:get_count()
  82. else
  83. return 0
  84. end
  85. end,
  86. allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  87. return 0
  88. end,
  89. })
  90. for i=1,4 do
  91. local bag = "bag"..i
  92. player_inv:set_size(bag, 1)
  93. bags_inv:set_size(bag, 1)
  94. bags_inv:set_stack(bag, 1, player_inv:get_stack(bag, 1))
  95. end
  96. end)
  97. -- register bag tools
  98. minetest.register_tool("unified_inventory:bag_small", {
  99. description = "Small Bag",
  100. inventory_image = "bags_small.png",
  101. groups = {bagslots=8},
  102. })
  103. minetest.register_tool("unified_inventory:bag_medium", {
  104. description = "Medium Bag",
  105. inventory_image = "bags_medium.png",
  106. groups = {bagslots=16},
  107. })
  108. minetest.register_tool("unified_inventory:bag_large", {
  109. description = "Large Bag",
  110. inventory_image = "bags_large.png",
  111. groups = {bagslots=24},
  112. })
  113. -- register bag crafts
  114. minetest.register_craft({
  115. output = "unified_inventory:bag_small",
  116. recipe = {
  117. {"", "default:stick", ""},
  118. {"group:wood", "group:wood", "group:wood"},
  119. {"group:wood", "group:wood", "group:wood"},
  120. },
  121. })
  122. minetest.register_craft({
  123. output = "unified_inventory:bag_medium",
  124. recipe = {
  125. {"", "", ""},
  126. {"default:stick", "unified_inventory:bag_small", "default:stick"},
  127. {"default:stick", "unified_inventory:bag_small", "default:stick"},
  128. },
  129. })
  130. minetest.register_craft({
  131. output = "unified_inventory:bag_large",
  132. recipe = {
  133. {"", "", ""},
  134. {"default:stick", "unified_inventory:bag_medium", "default:stick"},
  135. {"default:stick", "unified_inventory:bag_medium", "default:stick"},
  136. },
  137. })