init.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. --[[
  2. Bags for Minetest
  3. Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
  4. Source Code: https://github.com/cornernote/minetest-bags
  5. License: BSD-3-Clause https://raw.github.com/cornernote/minetest-bags/master/LICENSE
  6. Edited by TenPlus1
  7. ]]--
  8. local get_formspec = function(player, page)
  9. if page == "bags" then
  10. return "size[8,8.5]"
  11. ..default.gui_bg..default.gui_bg_img..default.gui_slots
  12. .."list[current_player;main;0,4.25;8,1;]"
  13. .."list[current_player;main;0,5.5;8,3;8]"
  14. .."button[0,1;2,0.5;main;Back]"
  15. .."button[2.5,1;1.3,0.5;bag1;Bag 1]"
  16. .."button[2.5,2.2;1.3,0.5;bag2;Bag 2]"
  17. .."button[5.2,1;1.3,0.5;bag3;Bag 3]"
  18. .."button[5.2,2.2;1.3,0.5;bag4;Bag 4]"
  19. .."list[detached:"..player:get_player_name().."_bags;bag1;3.8,.8;1,1;]"
  20. .."list[detached:"..player:get_player_name().."_bags;bag2;3.8,2;1,1;]"
  21. .."list[detached:"..player:get_player_name().."_bags;bag3;6.5,.8;1,1;]"
  22. .."list[detached:"..player:get_player_name().."_bags;bag4;6.5,2;1,1;]"
  23. ..default.get_hotbar_bg(0, 4.25)
  24. end
  25. for i = 1, 4 do
  26. if page == "bag" .. i then
  27. local image = player:get_inventory():get_stack("bag"
  28. .. i, 1):get_definition().inventory_image
  29. --[[
  30. return "size[8,9.5]"
  31. ..default.gui_bg..default.gui_bg_img..default.gui_slots
  32. .."list[current_player;main;0,5.5;8,4;]"
  33. .."button[6,0.2;2,0.5;main;Main]"
  34. .."button[4,0.2;2,0.5;bags;Bags]"
  35. .."image[0,0;1,1;" .. image .. "]"
  36. .."list[current_player;bag" .. i .. "contents;0,1;8,4;]"
  37. --]]
  38. return "size[8,9.5]"
  39. ..default.gui_bg..default.gui_bg_img..default.gui_slots
  40. .."list[current_player;bag" .. i .. "contents;0,0;8,4;]"
  41. .."button[0,4.2.2;2,0.5;main;Main]"
  42. .."button[6,4.2.2;2,0.5;bags;Bags]"
  43. .."image[3.5,4;1,1;" .. image .. "]"
  44. .."list[current_player;main;0,5.5;8,1;]"
  45. .."list[current_player;main;0,6.75;8,3;8]"
  46. .. "listring[current_player;main]"
  47. .. "listring[current_player;bag" .. i .. "contents]"
  48. ..default.get_hotbar_bg(0, 5.5)
  49. end
  50. end
  51. end
  52. minetest.register_on_player_receive_fields(function(player, formname, fields)
  53. if fields.bags then
  54. inventory_plus.set_inventory_formspec(player, get_formspec(player, "bags"))
  55. return
  56. end
  57. for i = 1, 4 do
  58. local page = "bag" .. i
  59. if fields[page] then
  60. if player:get_inventory():get_stack(page, 1):get_definition().groups.bagslots == nil then
  61. page = "bags"
  62. end
  63. inventory_plus.set_inventory_formspec(player, get_formspec(player, page))
  64. return
  65. end
  66. end
  67. end)
  68. minetest.register_on_joinplayer(function(player)
  69. local player_inv = player:get_inventory()
  70. local bags_inv = minetest.create_detached_inventory(player:get_player_name().."_bags",{
  71. on_put = function(inv, listname, index, stack, player)
  72. player:get_inventory():set_stack(listname, index, stack)
  73. player:get_inventory():set_size(listname.."contents", stack:get_definition().groups.bagslots)
  74. end,
  75. on_take = function(inv, listname, index, stack, player)
  76. player:get_inventory():set_stack(listname, index, nil)
  77. end,
  78. allow_put = function(inv, listname, index, stack, player)
  79. if stack:get_definition().groups.bagslots then
  80. return 1
  81. else
  82. return 0
  83. end
  84. end,
  85. allow_take = function(inv, listname, index, stack, player)
  86. if player:get_inventory():is_empty(listname .. "contents") == true then
  87. return stack:get_count()
  88. else
  89. return 0
  90. end
  91. end,
  92. allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  93. return 0
  94. end,
  95. }, player:get_player_name())
  96. for i = 1, 4 do
  97. local bag = "bag" .. i
  98. player_inv:set_size(bag, 1)
  99. bags_inv:set_size(bag, 1)
  100. bags_inv:set_stack(bag, 1, player_inv:get_stack(bag, 1))
  101. end
  102. end)
  103. -- register bags items
  104. minetest.register_craftitem("bags:small", {
  105. description = "Small Bag",
  106. inventory_image = "bags_small.png",
  107. groups = {bagslots = 8},
  108. })
  109. minetest.register_craftitem("bags:medium", {
  110. description = "Medium Bag",
  111. inventory_image = "bags_medium.png",
  112. groups = {bagslots = 16},
  113. })
  114. minetest.register_craftitem("bags:large", {
  115. description = "Large Bag",
  116. inventory_image = "bags_large.png",
  117. groups = {bagslots = 24},
  118. })
  119. minetest.register_tool("bags:trolley", {
  120. description = "Trolley",
  121. inventory_image = "bags_trolley.png",
  122. groups = {bagslots = 32},
  123. })
  124. -- register bag crafts
  125. minetest.register_craft({
  126. output = "bags:small",
  127. recipe = {
  128. {"", "group:stick", ""},
  129. {"group:leather", "group:leather", "group:leather"},
  130. {"group:leather", "group:leather", "group:leather"},
  131. },
  132. })
  133. minetest.register_craft({
  134. output = "bags:medium",
  135. recipe = {
  136. {"", "group:stick", ""},
  137. {"bags:small", "bags:small", "bags:small"},
  138. },
  139. })
  140. minetest.register_craft({
  141. output = "bags:large",
  142. recipe = {
  143. {"", "group:stick", ""},
  144. {"bags:medium", "bags:medium", "bags:medium"},
  145. },
  146. })
  147. minetest.register_craft({
  148. output = "bags:trolley",
  149. recipe = {
  150. {"", "group:stick", ""},
  151. {"bags:large", "bags:large", "bags:large"},
  152. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  153. },
  154. })
  155. -- Register button once.
  156. inventory_plus.register_button("bags", "Bags")