functions.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. mailbox.get_owner_formspec =
  2. function(pos)
  3. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  4. local meta = minetest.get_meta(pos)
  5. local owner = meta:get_string("owner")
  6. local reject = meta:get_string("reject")
  7. -- Fallback in case value not defined.
  8. if reject ~= "false" and reject ~= "true" then
  9. reject = "false"
  10. end
  11. local formspec = "size[8,8.5]" ..
  12. default.gui_bg ..
  13. default.gui_bg_img ..
  14. default.gui_slots ..
  15. "label[0,0;" .. minetest.formspec_escape("<" .. owner .. ">'s Mailbox") .. "]" ..
  16. "list[nodemeta:" .. spos .. ";main;0,0.5;8,1;]" ..
  17. "label[6,1.75;Upgrades]" ..
  18. "list[nodemeta:" .. spos .. ";cfg;6,2.25;2,1;]" ..
  19. "button[0,1.75;2,1;get;Get Mail]" ..
  20. "item_image[2,1.65;1,1;mailbox:mailbox]" ..
  21. "checkbox[0,2.55;reject;Reject Noob Items;" .. reject .. "]" ..
  22. "list[current_player;main;0,4.25;8,1;]" ..
  23. "list[current_player;main;0,5.5;8,3;8]" ..
  24. "listring[nodemeta:" .. spos .. ";main]"..
  25. "listring[current_player;main]"..
  26. default.get_hotbar_bg(0, 4.25)
  27. return formspec
  28. end
  29. mailbox.on_receive_fields =
  30. function(player, formname, fields)
  31. if string.find(formname, "^mailbox:mailbox_owner:") then
  32. local key = string.sub(formname, string.len("mailbox:mailbox_owner:") + 1)
  33. local pos = minetest.string_to_pos(key)
  34. if pos then
  35. local pname = player:get_player_name()
  36. local meta = minetest.get_meta(pos)
  37. local owner = meta:get_string('owner')
  38. if pname == owner or gdac.player_is_admin(pname) then
  39. if fields.get then
  40. local minv = meta:get_inventory()
  41. local pinv = player:get_inventory()
  42. local added_map = false
  43. for i = 1, minv:get_size('main'), 1 do
  44. local stack = minv:get_stack('main', i)
  45. if pinv:room_for_item('main', stack) then
  46. pinv:add_item('main', stack)
  47. minv:set_stack('main', i, ItemStack(nil))
  48. -- Notify if a mapping kit was added.
  49. if map.is_mapping_kit(stack:get_name()) then
  50. added_map = true
  51. end
  52. end
  53. end
  54. -- Notify if a mapping kit was added.
  55. if added_map then
  56. map.update_inventory_info(pname)
  57. end
  58. end
  59. if fields.reject and type(fields.reject) == "string" then
  60. local reject = fields.reject
  61. if reject == "false" or reject == "true" then
  62. meta:set_string("reject", fields.reject)
  63. else
  64. meta:set_string("reject", "false")
  65. end
  66. end
  67. end
  68. end
  69. return true
  70. end
  71. end
  72. mailbox.get_insert_formspec =
  73. function(pos)
  74. local meta = minetest.get_meta(pos)
  75. local owner = meta:get_string('owner')
  76. local spos = pos.x .. "," .. pos.y .. "," ..pos.z
  77. local title = "Mailbox (Owned by <" .. owner .. ">!)"
  78. local formspec = "size[8,7.5]" ..
  79. default.gui_bg ..
  80. default.gui_bg_img ..
  81. default.gui_slots ..
  82. "label[0,0;" .. minetest.formspec_escape(title) .. "]" ..
  83. "label[3.5,1;Drop Slot]" ..
  84. "list[nodemeta:" .. spos .. ";drop;3.5,1.5;1,1;]" ..
  85. "list[current_player;main;0,3.25;8,1;]" ..
  86. "list[current_player;main;0,4.5;8,3;8]" ..
  87. "listring[nodemeta:" .. spos .. ";drop]"..
  88. "listring[current_player;main]"..
  89. default.get_hotbar_bg(0, 3.25)
  90. return formspec
  91. end
  92. mailbox.on_punch =
  93. function(pos, node, puncher, pointed_thing)
  94. local meta = minetest.get_meta(pos)
  95. local inv = meta:get_inventory()
  96. inv:set_size('cfg', 2)
  97. -- Update infotext to new format.
  98. local owner = meta:get_string("owner") or ""
  99. meta:set_string("infotext", "Mailbox (Owned by <" .. owner .. ">!)")
  100. end
  101. mailbox.after_place_node =
  102. function(pos, placer, itemstack)
  103. local meta = minetest.get_meta(pos)
  104. local inv = meta:get_inventory()
  105. local owner = placer:get_player_name()
  106. local dname = rename.gpn(owner)
  107. meta:set_string("owner", owner)
  108. meta:set_string("rename", dname)
  109. meta:set_string("infotext", "Mailbox (Owned by <" .. dname .. ">!)")
  110. inv:set_size("main", 8)
  111. inv:set_size("drop", 1)
  112. inv:set_size("cfg", 2)
  113. end
  114. mailbox.on_destruct =
  115. function(pos)
  116. -- Nothing here ATM.
  117. end
  118. mailbox.on_blast =
  119. function(pos, intensity)
  120. -- Unblastable.
  121. end
  122. mailbox.on_rightclick =
  123. function(pos, node, clicker, itemstack)
  124. local meta = minetest.get_meta(pos)
  125. local pname = clicker:get_player_name()
  126. local owner = meta:get_string("owner")
  127. local meta = minetest.get_meta(pos)
  128. if owner == pname then
  129. local formspec = mailbox.get_owner_formspec(pos)
  130. local spos = minetest.pos_to_string(pos)
  131. minetest.show_formspec(pname, "mailbox:mailbox_owner:" .. spos, formspec)
  132. elseif gdac.player_is_admin(pname) and clicker:get_player_control().aux1 then
  133. minetest.chat_send_player(pname, "# Server: You are viewing <" .. rename.gpn(owner) .. ">'s mailbox.")
  134. local formspec = mailbox.get_owner_formspec(pos)
  135. local spos = minetest.pos_to_string(pos)
  136. minetest.show_formspec(pname, "mailbox:mailbox_owner:" .. spos, formspec)
  137. else
  138. local formspec = mailbox.get_insert_formspec(pos)
  139. minetest.show_formspec(pname, "mailbox:mailbox_insert", formspec)
  140. end
  141. return itemstack
  142. end
  143. mailbox.can_dig =
  144. function(pos, player)
  145. local meta = minetest.get_meta(pos);
  146. local pname = player:get_player_name()
  147. local owner = meta:get_string("owner")
  148. local inv = meta:get_inventory()
  149. return pname == owner and
  150. inv:is_empty("main") and
  151. inv:is_empty("cfg")
  152. end
  153. mailbox.on_metadata_inventory_put =
  154. function(pos, listname, index, stack, player)
  155. local meta = minetest.get_meta(pos)
  156. local inv = meta:get_inventory()
  157. if listname == "drop" and inv:room_for_item("main", stack) then
  158. inv:remove_item("drop", stack)
  159. inv:add_item("main", stack)
  160. local desc = "Unknown Item(s)"
  161. local def = minetest.registered_items[stack:get_name()]
  162. if def and def.description then
  163. desc = utility.get_short_desc(def.description)
  164. end
  165. local ownername = rename.gpn(meta:get_string("owner"))
  166. minetest.chat_send_player(player:get_player_name(),
  167. string.format("# Server: You put %d '%s' in <%s>'s mailbox.",
  168. stack:get_count(), desc, ownername))
  169. end
  170. end
  171. mailbox.allow_metadata_inventory_put =
  172. function(pos, listname, index, stack, player)
  173. local pname = player:get_player_name()
  174. if listname == "drop" then
  175. local meta = minetest.get_meta(pos)
  176. local owner = meta:get_string('owner')
  177. if owner == pname then return 0 end -- No cheating!
  178. local reject = meta:get_string("reject")
  179. if reject == "true" then
  180. for k, v in pairs(give_initial_stuff.items) do
  181. if stack:get_name() == v:get_name() then
  182. minetest.chat_send_player(pname, "# Server: This mail box does not accept starting items, sorry!")
  183. return 0
  184. end
  185. end
  186. for k, v in ipairs(mailbox.reject_items) do
  187. if stack:get_name() == ItemStack(v):get_name() then
  188. minetest.chat_send_player(pname, "# Server: This mail box does not accept noob items, sorry!")
  189. return 0
  190. end
  191. end
  192. end
  193. local inv = meta:get_inventory()
  194. if inv:room_for_item("main", stack) then
  195. local have_clu = false
  196. local cfg = inv:get_list('cfg')
  197. if cfg then -- Inventory may not exist for old mailboxes.
  198. for k, v in ipairs(cfg) do
  199. if v:get_name() == "techcrafts:control_logic_unit" and v:get_count() == 1 then
  200. have_clu = true
  201. break
  202. end
  203. end
  204. else
  205. inv:set_size('cfg', 2)
  206. end
  207. if have_clu then
  208. local desc = "Unknown Item(s)"
  209. local def = minetest.registered_items[stack:get_name()]
  210. if def and def.description then
  211. desc = utility.get_short_desc(def.description)
  212. end
  213. local from = "SERVER"
  214. local to = owner
  215. local subject = "Mailbox Delivery"
  216. local message = "Player <" .. rename.gpn(pname) .. "> put " ..
  217. stack:get_count() ..
  218. " '" .. desc .. "' in your mailbox @ " ..
  219. rc.pos_to_namestr(pos) .. "!"
  220. email.send_mail_single(from, to, subject, message)
  221. end
  222. return -1
  223. else
  224. minetest.chat_send_player(pname, "# Server: <" .. rename.gpn(owner) .. ">'s mailbox is full!")
  225. end
  226. end
  227. if listname == "cfg" and stack:get_name() == "techcrafts:control_logic_unit" then
  228. local meta = minetest.get_meta(pos)
  229. local inv = meta:get_inventory()
  230. local owner = meta:get_string('owner')
  231. if owner == pname or gdac.player_is_admin(pname) then
  232. if inv:get_stack('cfg', index):get_count() == 0 then
  233. return 1
  234. end
  235. end
  236. end
  237. return 0
  238. end
  239. mailbox.allow_metadata_inventory_move =
  240. function(pos, from_list, from_index, to_list, to_index, count, player)
  241. return 0
  242. end
  243. mailbox.allow_metadata_inventory_take =
  244. function(pos, listname, index, stack, player)
  245. local meta = minetest.get_meta(pos)
  246. local owner = meta:get_string('owner')
  247. local pname = player:get_player_name()
  248. if owner == pname or gdac.player_is_admin(pname) then
  249. return stack:get_count()
  250. end
  251. return 0
  252. end