init.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. .. "label[2,4.2;" .. minetest.formspec_escape("Bag #" .. i .. "") .. "]"
  43. .. "button[6,4.2.2;2,0.5;bags;Bags]"
  44. .. "image[3.5,4;1,1;" .. image .. "]"
  45. .. "list[current_player;main;0,5.5;8,1;]"
  46. .. "list[current_player;main;0,6.75;8,3;8]"
  47. .. "listring[current_player;main]"
  48. .. "listring[current_player;bag" .. i .. "contents]"
  49. .. default.get_hotbar_bg(0, 5.5)
  50. end
  51. end
  52. end
  53. minetest.register_on_player_receive_fields(function(player, formname, fields)
  54. if fields.bags then
  55. inventory_plus.set_inventory_formspec(player, get_formspec(player, "bags"))
  56. return
  57. end
  58. for i = 1, 4 do
  59. local page = "bag" .. i
  60. if fields[page] then
  61. if player:get_inventory():get_stack(page, 1):get_definition().groups.bagslots == nil then
  62. page = "bags"
  63. end
  64. inventory_plus.set_inventory_formspec(player, get_formspec(player, page))
  65. return
  66. end
  67. end
  68. end)
  69. minetest.register_on_joinplayer(function(player)
  70. local player_inv = player:get_inventory()
  71. local bags_inv = minetest.create_detached_inventory(player:get_player_name().."_bags",{
  72. on_put = function(inv, listname, index, stack, player)
  73. player:get_inventory():set_stack(listname, index, stack)
  74. player:get_inventory():set_size(listname.."contents", stack:get_definition().groups.bagslots)
  75. end,
  76. on_take = function(inv, listname, index, stack, player)
  77. player:get_inventory():set_stack(listname, index, nil)
  78. end,
  79. allow_put = function(inv, listname, index, stack, player)
  80. if stack:get_definition().groups.bagslots then
  81. return 1
  82. else
  83. return 0
  84. end
  85. end,
  86. allow_take = function(inv, listname, index, stack, player)
  87. if player:get_inventory():is_empty(listname .. "contents") == true then
  88. return stack:get_count()
  89. else
  90. return 0
  91. end
  92. end,
  93. allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  94. return 0
  95. end,
  96. }, player:get_player_name())
  97. for i = 1, 4 do
  98. local bag = "bag" .. i
  99. player_inv:set_size(bag, 1)
  100. bags_inv:set_size(bag, 1)
  101. bags_inv:set_stack(bag, 1, player_inv:get_stack(bag, 1))
  102. end
  103. end)
  104. -- register bags items
  105. minetest.register_craftitem("bags:small", {
  106. description = "Small Bag",
  107. inventory_image = "bags_small.png",
  108. groups = {bagslots = 8},
  109. })
  110. minetest.register_craftitem("bags:medium", {
  111. description = "Medium Bag",
  112. inventory_image = "bags_medium.png",
  113. groups = {bagslots = 16},
  114. })
  115. minetest.register_craftitem("bags:large", {
  116. description = "Large Bag",
  117. inventory_image = "bags_large.png",
  118. groups = {bagslots = 24},
  119. })
  120. minetest.register_tool("bags:trolley", {
  121. description = "Trolley",
  122. inventory_image = "bags_trolley.png",
  123. groups = {bagslots = 32},
  124. })
  125. -- register bag crafts
  126. minetest.register_craft({
  127. output = "bags:small",
  128. recipe = {
  129. {"farming:string", "group:stick", "farming:string"},
  130. {"group:leather", "group:leather", "group:leather"},
  131. {"group:leather", "group:leather", "group:leather"},
  132. },
  133. })
  134. minetest.register_craft({
  135. output = "bags:medium",
  136. recipe = {
  137. {"farming:string", "group:stick", "farming:string"},
  138. {"bags:small", "bags:small", "bags:small"},
  139. },
  140. })
  141. minetest.register_craft({
  142. output = "bags:large",
  143. recipe = {
  144. {"farming:string", "group:stick", "farming:string"},
  145. {"bags:medium", "bags:medium", "bags:medium"},
  146. },
  147. })
  148. minetest.register_craft({
  149. output = "bags:trolley",
  150. recipe = {
  151. {"", "group:stick", ""},
  152. {"bags:large", "bags:large", "bags:large"},
  153. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  154. },
  155. })
  156. -- Register button once.
  157. inventory_plus.register_button("bags", "Bags")