functions.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. --minetest.chat_send_player("MustTest", fields.reject)
  61. local reject = fields.reject
  62. if reject == "false" or reject == "true" then
  63. meta:set_string("reject", fields.reject)
  64. else
  65. meta:set_string("reject", "false")
  66. end
  67. end
  68. end
  69. end
  70. return true
  71. end
  72. end
  73. mailbox.get_insert_formspec =
  74. function(pos)
  75. local meta = minetest.get_meta(pos)
  76. local owner = meta:get_string('owner')
  77. local spos = pos.x .. "," .. pos.y .. "," ..pos.z
  78. local title = "Mailbox (Owned by <" .. owner .. ">!)"
  79. local formspec = "size[8,7.5]" ..
  80. default.gui_bg ..
  81. default.gui_bg_img ..
  82. default.gui_slots ..
  83. "label[0,0;" .. minetest.formspec_escape(title) .. "]" ..
  84. "label[3.5,1;Drop Slot]" ..
  85. "list[nodemeta:" .. spos .. ";drop;3.5,1.5;1,1;]" ..
  86. "list[current_player;main;0,3.25;8,1;]" ..
  87. "list[current_player;main;0,4.5;8,3;8]" ..
  88. "listring[nodemeta:" .. spos .. ";drop]"..
  89. "listring[current_player;main]"..
  90. default.get_hotbar_bg(0, 3.25)
  91. return formspec
  92. end
  93. mailbox.on_punch =
  94. function(pos, node, puncher, pointed_thing)
  95. local meta = minetest.get_meta(pos)
  96. local inv = meta:get_inventory()
  97. inv:set_size('cfg', 2)
  98. -- Update infotext to new format.
  99. local owner = meta:get_string("owner") or ""
  100. meta:set_string("infotext", "Mailbox (Owned by <" .. owner .. ">!)")
  101. end
  102. mailbox.after_place_node =
  103. function(pos, placer, itemstack)
  104. local meta = minetest.get_meta(pos)
  105. local inv = meta:get_inventory()
  106. local owner = placer:get_player_name()
  107. local dname = rename.gpn(owner)
  108. meta:set_string("owner", owner)
  109. meta:set_string("rename", dname)
  110. meta:set_string("infotext", "Mailbox (Owned by <" .. dname .. ">!)")
  111. inv:set_size("main", 8)
  112. inv:set_size("drop", 1)
  113. inv:set_size("cfg", 2)
  114. end
  115. mailbox.on_destruct =
  116. function(pos)
  117. -- Nothing here ATM.
  118. end
  119. mailbox.on_blast =
  120. function(pos, intensity)
  121. -- Unblastable.
  122. end
  123. mailbox.on_rightclick =
  124. function(pos, node, clicker, itemstack)
  125. local meta = minetest.get_meta(pos)
  126. local pname = clicker:get_player_name()
  127. local owner = meta:get_string("owner")
  128. local meta = minetest.get_meta(pos)
  129. if owner == pname then
  130. local formspec = mailbox.get_owner_formspec(pos)
  131. local spos = minetest.pos_to_string(pos)
  132. minetest.show_formspec(pname, "mailbox:mailbox_owner:" .. spos, formspec)
  133. elseif gdac.player_is_admin(pname) and clicker:get_player_control().aux1 then
  134. minetest.chat_send_player(pname, "# Server: You are viewing <" .. rename.gpn(owner) .. ">'s mailbox.")
  135. local formspec = mailbox.get_owner_formspec(pos)
  136. local spos = minetest.pos_to_string(pos)
  137. minetest.show_formspec(pname, "mailbox:mailbox_owner:" .. spos, formspec)
  138. else
  139. local formspec = mailbox.get_insert_formspec(pos)
  140. minetest.show_formspec(pname, "mailbox:mailbox_insert", formspec)
  141. end
  142. return itemstack
  143. end
  144. mailbox.can_dig =
  145. function(pos, player)
  146. local meta = minetest.get_meta(pos);
  147. local pname = player:get_player_name()
  148. local owner = meta:get_string("owner")
  149. local inv = meta:get_inventory()
  150. return pname == owner and
  151. inv:is_empty("main") and
  152. inv:is_empty("cfg")
  153. end
  154. mailbox.on_metadata_inventory_put =
  155. function(pos, listname, index, stack, player)
  156. local meta = minetest.get_meta(pos)
  157. local inv = meta:get_inventory()
  158. if listname == "drop" and inv:room_for_item("main", stack) then
  159. inv:remove_item("drop", stack)
  160. inv:add_item("main", stack)
  161. local desc = "Unknown Item(s)"
  162. local def = minetest.registered_items[stack:get_name()]
  163. if def and def.description then
  164. desc = utility.get_short_desc(def.description)
  165. end
  166. local ownername = rename.gpn(meta:get_string("owner"))
  167. minetest.chat_send_player(player:get_player_name(),
  168. string.format("# Server: You put %d '%s' in <%s>'s mailbox.",
  169. stack:get_count(), desc, ownername))
  170. end
  171. end
  172. mailbox.allow_metadata_inventory_put =
  173. function(pos, listname, index, stack, player)
  174. local pname = player:get_player_name()
  175. if listname == "drop" then
  176. local meta = minetest.get_meta(pos)
  177. local owner = meta:get_string('owner')
  178. if owner == pname then return 0 end -- No cheating!
  179. local reject = meta:get_string("reject")
  180. if reject == "true" then
  181. for k, v in pairs(give_initial_stuff.items) do
  182. if stack:get_name() == v:get_name() then
  183. minetest.chat_send_player(pname, "# Server: This mail box does not accept starting items, sorry!")
  184. return 0
  185. end
  186. end
  187. for k, v in ipairs(mailbox.reject_items) do
  188. if stack:get_name() == ItemStack(v):get_name() then
  189. minetest.chat_send_player(pname, "# Server: This mail box does not accept noob items, sorry!")
  190. return 0
  191. end
  192. end
  193. end
  194. local inv = meta:get_inventory()
  195. if inv:room_for_item("main", stack) then
  196. local have_clu = false
  197. local cfg = inv:get_list('cfg')
  198. if cfg then -- Inventory may not exist for old mailboxes.
  199. for k, v in ipairs(cfg) do
  200. if v:get_name() == "techcrafts:control_logic_unit" and v:get_count() == 1 then
  201. have_clu = true
  202. break
  203. end
  204. end
  205. else
  206. inv:set_size('cfg', 2)
  207. end
  208. if have_clu then
  209. local desc = "Unknown Item(s)"
  210. local def = minetest.registered_items[stack:get_name()]
  211. if def and def.description then
  212. desc = utility.get_short_desc(def.description)
  213. end
  214. local from = "SERVER"
  215. local to = owner
  216. local subject = "Mailbox Delivery"
  217. local message = "Player <" .. rename.gpn(pname) .. "> put " ..
  218. stack:get_count() ..
  219. " '" .. desc .. "' in your mailbox @ " ..
  220. rc.pos_to_namestr(pos) .. "!"
  221. email.send_mail_single(from, to, subject, message)
  222. end
  223. return -1
  224. else
  225. minetest.chat_send_player(pname, "# Server: <" .. rename.gpn(owner) .. ">'s mailbox is full!")
  226. end
  227. end
  228. if listname == "cfg" and stack:get_name() == "techcrafts:control_logic_unit" then
  229. local meta = minetest.get_meta(pos)
  230. local inv = meta:get_inventory()
  231. local owner = meta:get_string('owner')
  232. if owner == pname or gdac.player_is_admin(pname) then
  233. if inv:get_stack('cfg', index):get_count() == 0 then
  234. return 1
  235. end
  236. end
  237. end
  238. return 0
  239. end
  240. mailbox.allow_metadata_inventory_move =
  241. function(pos, from_list, from_index, to_list, to_index, count, player)
  242. return 0
  243. end
  244. mailbox.allow_metadata_inventory_take =
  245. function(pos, listname, index, stack, player)
  246. local meta = minetest.get_meta(pos)
  247. local owner = meta:get_string('owner')
  248. local pname = player:get_player_name()
  249. if owner == pname or gdac.player_is_admin(pname) then
  250. return stack:get_count()
  251. end
  252. return 0
  253. end