init.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. local storage = minetest.get_mod_storage()
  2. local mailbox = {}
  3. screwdriver = screwdriver or {}
  4. function mailbox.get_formspec(pos, owner, fs_type)
  5. local selected = "false"
  6. if minetest.get_node(pos).name == "mailbox:letterbox" then
  7. selected = "true"
  8. end
  9. local xbg = default.gui_bg .. default.gui_bg_img .. default.gui_slots
  10. local spos = pos.x .. "," ..pos.y .. "," .. pos.z
  11. if fs_type == 1 then
  12. return "size[8,9.5]" .. xbg .. default.get_hotbar_bg(0, 5.5) ..
  13. "checkbox[0,0;books_only;Only allow written books;" .. selected .. "]" ..
  14. "list[nodemeta:" .. spos .. ";mailbox;0,1;8,4;]" ..
  15. "list[current_player;main;0,5.5;8,1;]" ..
  16. "list[current_player;main;0,6.75;8,3;8]" ..
  17. "listring[nodemeta:" .. spos .. ";mailbox]" ..
  18. "listring[current_player;main]" ..
  19. "button_exit[5,0;1,1;unrent;Unrent]"..
  20. "button_exit[7,0;1,1;exit;X]"
  21. else
  22. return "size[8,5.5]" .. xbg .. default.get_hotbar_bg(0, 1.5) ..
  23. "label[0,0;Send your goods\nto " .. owner .. " :]" ..
  24. "list[nodemeta:" .. spos .. ";drop;3.5,0;1,1;]" ..
  25. "list[current_player;main;0,1.5;8,1;]" ..
  26. "list[current_player;main;0,2.75;8,3;8]" ..
  27. "listring[nodemeta:" .. spos .. ";drop]" ..
  28. "listring[current_player;main]"
  29. end
  30. end
  31. function mailbox.unrent (pos, player)
  32. local meta = minetest.get_meta(pos)
  33. local node = minetest.get_node(pos)
  34. local player_name = player:get_player_name()
  35. node.name = "mailbox:mailbox_free"
  36. minetest.swap_node(pos, node) -- preserve Facedir
  37. mailbox.after_place_free(pos, player)
  38. storage:set_string(player_name, 'unclaimed')
  39. end
  40. minetest.register_on_player_receive_fields(function(player, formname, fields)
  41. if not formname:match("mailbox:mailbox_") then
  42. return
  43. end
  44. if fields.unrent then
  45. local pos = minetest.string_to_pos(formname:sub(17))
  46. local meta = minetest.get_meta(pos)
  47. local inv = meta:get_inventory()
  48. if inv:is_empty("mailbox") then
  49. mailbox.unrent(pos, player)
  50. else
  51. minetest.chat_send_player(player:get_player_name(), "Your mailbox is not empty!")
  52. end
  53. end
  54. if fields.books_only then
  55. local pos = minetest.string_to_pos(formname:sub(17))
  56. local node = minetest.get_node(pos)
  57. if node.name == "mailbox:mailbox" then
  58. node.name = "mailbox:letterbox"
  59. minetest.swap_node(pos, node)
  60. else
  61. node.name = "mailbox:mailbox"
  62. minetest.swap_node(pos, node)
  63. end
  64. end
  65. end)
  66. mailbox.after_place_node = function(pos, placer, _)
  67. local meta = minetest.get_meta(pos)
  68. local player_name = placer:get_player_name()
  69. meta:set_string("owner", player_name)
  70. meta:set_string("infotext", player_name.."'s Mailbox")
  71. local inv = meta:get_inventory()
  72. inv:set_size("mailbox", 8*4)
  73. inv:set_size("drop", 1)
  74. end
  75. mailbox.on_rightclick_free = function(pos, _, clicker, _)
  76. local player_name = clicker:get_player_name()
  77. local claimed = storage:get_string(player_name)
  78. if claimed ~= 'claimed' then
  79. local node = minetest.get_node(pos)
  80. node.name = "mailbox:mailbox"
  81. minetest.swap_node(pos, node)
  82. storage:set_string(player_name, 'claimed')
  83. mailbox.after_place_node(pos, clicker)
  84. else
  85. minetest.chat_send_player(player_name, 'You can only claim one mailbox!')
  86. end
  87. end
  88. mailbox.after_place_free = function(pos, placer, _)
  89. local meta = minetest.get_meta(pos)
  90. local player_name = placer:get_player_name()
  91. meta:set_string("owner", player_name)
  92. meta:set_string("infotext", "Free Mailbox, right-click to claim")
  93. end
  94. mailbox.on_rightclick = function(pos, _, clicker, _)
  95. local meta = minetest.get_meta(pos)
  96. local player = clicker:get_player_name()
  97. local owner = meta:get_string("owner")
  98. if clicker:get_wielded_item():get_name() == "mailbox:unrenter" then
  99. mailbox.unrent(pos, clicker)
  100. return
  101. end
  102. if player == owner then
  103. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  104. minetest.show_formspec(player, "mailbox:mailbox_" .. spos, mailbox.get_formspec(pos, owner, 1))
  105. else
  106. minetest.show_formspec(player, "mailbox:mailbox", mailbox.get_formspec(pos, owner, 0))
  107. end
  108. end
  109. mailbox.can_dig = function(pos, player)
  110. local meta = minetest.get_meta(pos)
  111. local owner = meta:get_string("owner")
  112. local player_name = player:get_player_name()
  113. local inv = meta:get_inventory()
  114. return inv:is_empty("mailbox") and player and player_name == owner
  115. end
  116. mailbox.on_metadata_inventory_put = function(pos, listname, index, stack, player)
  117. if listname == "drop" then
  118. local inv = minetest.get_meta(pos):get_inventory()
  119. if inv:room_for_item("mailbox", stack) then
  120. inv:remove_item("drop", stack)
  121. inv:add_item("mailbox", stack)
  122. end
  123. end
  124. end
  125. mailbox.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  126. if listname == "drop" then
  127. if minetest.get_node(pos).name == "mailbox:letterbox" and
  128. stack:get_name() ~= "default:book_written" then
  129. return 0
  130. end
  131. local meta = minetest.get_meta(pos)
  132. local inv = meta:get_inventory()
  133. if inv:room_for_item("mailbox", stack) then
  134. return -1
  135. else
  136. minetest.chat_send_player(player:get_player_name(), "Mailbox full.")
  137. return 0
  138. end
  139. end
  140. return 0
  141. end
  142. minetest.register_node("mailbox:mailbox", {
  143. description = "Mailbox",
  144. tiles = {
  145. "mailbox_mailbox_top.png", "mailbox_mailbox_bottom.png",
  146. "mailbox_mailbox_side.png", "mailbox_mailbox_side.png",
  147. "mailbox_mailbox.png", "mailbox_mailbox.png",
  148. },
  149. groups = {cracky = 3, oddly_breakable_by_hand = 1},
  150. on_rotate = screwdriver.rotate_simple,
  151. sounds = default.node_sound_defaults(),
  152. paramtype2 = "facedir",
  153. after_place_node = mailbox.after_place_node,
  154. on_rightclick = mailbox.on_rightclick,
  155. can_dig = mailbox.can_dig,
  156. on_metadata_inventory_put = mailbox.on_metadata_inventory_put,
  157. allow_metadata_inventory_put = mailbox.allow_metadata_inventory_put,
  158. })
  159. minetest.register_node("mailbox:mailbox_free", {
  160. description = "Mailbox for Rent",
  161. tiles = {
  162. "mailbox_mailbox_free_top.png", "mailbox_mailbox_free_bottom.png",
  163. "mailbox_mailbox_free_side.png", "mailbox_mailbox_free_side.png",
  164. "mailbox_mailbox_free.png", "mailbox_mailbox_free.png",
  165. },
  166. groups = {cracky = 3, oddly_breakable_by_hand = 1},
  167. on_rotate = screwdriver.rotate_simple,
  168. sounds = default.node_sound_defaults(),
  169. paramtype2 = "facedir",
  170. after_place_node = mailbox.after_place_free,
  171. on_rightclick = mailbox.on_rightclick_free,
  172. can_dig = mailbox.can_dig,
  173. -- on_metadata_inventory_put = mailbox.on_metadata_inventory_put,
  174. -- allow_metadata_inventory_put = mailbox.allow_metadata_inventory_put,
  175. })
  176. minetest.register_node("mailbox:letterbox", {
  177. description = "Letterbox (you hacker you!)",
  178. tiles = {
  179. "mailbox_letterbox_top.png", "mailbox_letterbox_bottom.png",
  180. "mailbox_letterbox_side.png", "mailbox_letterbox_side.png",
  181. "mailbox_letterbox.png", "mailbox_letterbox.png",
  182. },
  183. groups = {cracky = 3, oddly_breakable_by_hand = 1, not_in_creative_inventory = 1},
  184. on_rotate = screwdriver.rotate_simple,
  185. sounds = default.node_sound_defaults(),
  186. paramtype2 = "facedir",
  187. drop = "mailbox:mailbox",
  188. after_place_node = mailbox.after_place_node,
  189. on_rightclick = mailbox.on_rightclick,
  190. can_dig = mailbox.can_dig,
  191. on_metadata_inventory_put = mailbox.on_metadata_inventory_put,
  192. allow_metadata_inventory_put = mailbox.allow_metadata_inventory_put,
  193. })