init.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. key = key or {}
  2. key.modpath = minetest.get_modpath("key")
  3. key.on_craft = function(itemstack, player, old_craft_grid, craft_inv)
  4. if itemstack:get_name() == "key:key" then
  5. local pname = player:get_player_name()
  6. -- Permit copying a key.
  7. local original
  8. local index
  9. for i = 1, player:get_inventory():get_size("craft") do
  10. if old_craft_grid[i]:get_name() == "key:key" then
  11. original = old_craft_grid[i]
  12. index = i
  13. end
  14. end
  15. if not original then
  16. return
  17. end
  18. local copymeta = original:get_meta():to_table()
  19. -- copy of the key held by player's mouse cursor
  20. itemstack:get_meta():from_table(copymeta)
  21. -- put the key with metadata back in the craft grid
  22. craft_inv:set_stack("craft", index, original)
  23. minetest.chat_send_player(pname, "# Server: Key copied!")
  24. elseif itemstack:get_name() == "key:chain" then
  25. local pname = player:get_player_name()
  26. -- Special: add a key to a keychain, leave key in craft-grid and return new keychain.
  27. local key_original
  28. local key_index
  29. local chain_original
  30. local chain_index
  31. for i = 1, player:get_inventory():get_size("craft") do
  32. if old_craft_grid[i]:get_name() == "key:key" then
  33. key_original = old_craft_grid[i]
  34. key_index = i
  35. end
  36. if old_craft_grid[i]:get_name() == "key:chain" then
  37. chain_original = old_craft_grid[i]
  38. chain_index = i
  39. end
  40. end
  41. if not key_original then
  42. return
  43. end
  44. -- Add key secret to keychain and update keychain description.
  45. local key_meta = key_original:get_meta()
  46. local secret = key_meta:get_string("secret")
  47. if type(secret) == "string" and secret ~= "" then
  48. local imeta = chain_original:get_meta()
  49. local idata = minetest.deserialize(imeta:get_string("keychain"))
  50. local idesc = imeta:get_string("description")
  51. if type(idata) ~= "table" then
  52. idata = {}
  53. idesc = ""
  54. end
  55. if #idata >= 18 then -- Fit 18 keys exactly. >= determined by testing. Darn Lua arrays >:|
  56. -- Just copy data to new itemstack without adding the key secret.
  57. minetest.chat_send_player(pname, "# Server: Keychain is stuffed! No more keys can fit.")
  58. itemstack:get_meta():from_table(chain_original:get_meta():to_table())
  59. -- put the key with metadata back in the craft grid
  60. craft_inv:set_stack("craft", key_index, key_original)
  61. else
  62. if idesc == "" then
  63. idesc = key_meta:get_string("description")
  64. else
  65. idesc = idesc .. "\n" .. key_meta:get_string("description")
  66. end
  67. idata[#idata + 1] = secret
  68. idone = minetest.serialize(idata)
  69. if type(idone) == "string" then
  70. local nmeta = itemstack:get_meta()
  71. nmeta:set_string("keychain", idone)
  72. nmeta:set_string("description", idesc)
  73. minetest.chat_send_player(pname, "# Server: Key added to keychain!")
  74. end
  75. end
  76. end
  77. -- DO NOT put the key with metadata back in the craft grid
  78. -- the recipe consumes it
  79. end
  80. end
  81. key.on_skeleton_use = function(itemstack, user, pointed_thing)
  82. if pointed_thing.type ~= "node" then
  83. return itemstack
  84. end
  85. local pos = pointed_thing.under
  86. local node = minetest.get_node(pos)
  87. if not node then
  88. return itemstack
  89. end
  90. local ndef = minetest.reg_ns_nodes[node.name]
  91. if not ndef then
  92. return itemstack
  93. end
  94. local on_skeleton_key_use = ndef.on_skeleton_key_use
  95. if on_skeleton_key_use then
  96. -- make a new key secret in case the node callback needs it
  97. local random = math.random
  98. local newsecret = string.format(
  99. "%04x%04x%04x%04x",
  100. random(2^16) - 1, random(2^16) - 1,
  101. random(2^16) - 1, random(2^16) - 1)
  102. local secret, _, owner = on_skeleton_key_use(pos, user, newsecret)
  103. if secret and owner then
  104. -- finish and return the new key
  105. itemstack:take_item()
  106. itemstack:add_item("key:key")
  107. local meta = itemstack:get_meta()
  108. meta:set_string("secret", secret)
  109. meta:set_string("description", "Key to <" .. rename.gpn(owner) .. ">'s " ..
  110. utility.get_short_desc(ndef.description) .. " @ " ..
  111. rc.pos_to_namestr(vector.round(pos)))
  112. return itemstack
  113. end
  114. end
  115. return nil
  116. end
  117. key.on_forged_place = function(itemstack, placer, pointed_thing)
  118. local under = pointed_thing.under
  119. local node = minetest.get_node(under)
  120. local def = minetest.reg_ns_nodes[node.name]
  121. if def and def.on_rightclick and
  122. not (placer and placer:get_player_control().sneak) then
  123. return def.on_rightclick(under, node, placer, itemstack,
  124. pointed_thing) or itemstack
  125. end
  126. if pointed_thing.type ~= "node" then
  127. return itemstack
  128. end
  129. local pos = pointed_thing.under
  130. node = minetest.get_node(pos)
  131. if not node or node.name == "ignore" then
  132. return itemstack
  133. end
  134. local ndef = minetest.reg_ns_nodes[node.name]
  135. if not ndef then
  136. return itemstack
  137. end
  138. local on_key_use = ndef.on_key_use
  139. if on_key_use then
  140. on_key_use(pos, placer)
  141. end
  142. return nil
  143. end
  144. key.on_chain_place = function(itemstack, placer, pointed_thing)
  145. if not placer or not placer:is_player() then
  146. return itemstack
  147. end
  148. if pointed_thing.type ~= "node" then
  149. return itemstack
  150. end
  151. --minetest.chat_send_player(placer:get_player_name(), "# Server: Using keychain!")
  152. local pname = placer:get_player_name()
  153. local under = pointed_thing.under
  154. local node = minetest.get_node(under)
  155. local def = minetest.reg_ns_nodes[node.name]
  156. local meta = minetest.get_meta(under)
  157. local secret = meta:get_string("key_lock_secret")
  158. -- If node has a key secret, then check if we have a matching key on this chain.
  159. if secret ~= "" then
  160. --minetest.chat_send_player(pname, "# Server: TEST2")
  161. local imeta = itemstack:get_meta()
  162. -- Data table is expected to be an array.
  163. local idata = minetest.deserialize(imeta:get_string("keychain"))
  164. if type(idata) == "table" then
  165. for k, v in ipairs(idata) do
  166. if type(v) == "string" and v == secret then
  167. -- We have a matching secret. Set it as the "main" secret so that the general key API can use it.
  168. --minetest.chat_send_player(pname, "# Server: TEST1")
  169. imeta:set_string("secret", v)
  170. -- Hack: we actually have to set the player's wielded item.
  171. -- This is because otherwise when the node goes to check if the player is wielding the right key,
  172. -- it won't see the updated metadata secret that we just set.
  173. -- We can't pass the changed itemstack to that code directly.
  174. -- In fact, some callbacks which check key security don't accept itemstack params at ALL.
  175. -- Thus the correct data must be set directly on the player's wielded itemstack.
  176. placer:set_wielded_item(itemstack)
  177. end
  178. end
  179. end
  180. end
  181. if def and def.on_rightclick and not placer:get_player_control().sneak then
  182. return def.on_rightclick(under, node, placer, itemstack, pointed_thing) or itemstack
  183. end
  184. local pos = pointed_thing.under
  185. node = minetest.get_node(pos)
  186. if not node or node.name == "ignore" then
  187. return itemstack
  188. end
  189. local ndef = minetest.reg_ns_nodes[node.name]
  190. if not ndef then
  191. return itemstack
  192. end
  193. local on_key_use = ndef.on_key_use
  194. if on_key_use then
  195. on_key_use(pos, placer)
  196. end
  197. return nil
  198. end
  199. if not key.registered then
  200. minetest.register_craftitem("key:skeleton", {
  201. description = "Skeleton Key",
  202. inventory_image = "key_skeleton.png",
  203. groups = {key = 1},
  204. stack_max = 1,
  205. on_use = function(...)
  206. return key.on_skeleton_use(...)
  207. end
  208. })
  209. minetest.register_craftitem("key:key", {
  210. description = "Key",
  211. inventory_image = "key_key.png",
  212. groups = {key = 1, not_in_creative_inventory = 1},
  213. stack_max = 1,
  214. on_place = function(...)
  215. return key.on_forged_place(...)
  216. end,
  217. })
  218. -- keychain. idea from boxface
  219. -- all code custom written though
  220. minetest.register_craftitem("key:chain", {
  221. description = "Key Chain",
  222. inventory_image = "key_chain.png",
  223. groups = {key = 1, not_in_creative_inventory = 1},
  224. stack_max = 1,
  225. on_place = function(...)
  226. return key.on_chain_place(...)
  227. end,
  228. })
  229. minetest.register_alias("default:key", "key:key")
  230. minetest.register_craft({
  231. output = 'key:skeleton',
  232. recipe = {
  233. {'default:gold_ingot'},
  234. }
  235. })
  236. minetest.register_craft({
  237. output = 'key:chain',
  238. recipe = {
  239. {'key:skeleton', 'key:skeleton', 'key:skeleton'},
  240. {'key:skeleton', 'default:steel_ingot', 'key:skeleton'},
  241. {'key:skeleton', 'key:skeleton', 'key:skeleton'},
  242. }
  243. })
  244. minetest.register_craft({
  245. type = 'cooking',
  246. output = 'default:gold_ingot',
  247. recipe = 'key:skeleton',
  248. cooktime = 5,
  249. })
  250. minetest.register_craft({
  251. type = 'cooking',
  252. output = 'default:gold_ingot',
  253. recipe = 'key:key',
  254. cooktime = 5,
  255. })
  256. minetest.register_craft({
  257. type = "shapeless",
  258. output = "key:chain",
  259. recipe = {"key:key", "key:chain"}
  260. })
  261. -- note: there is a bug somewhere that allows two forged keys to be copied, which doesn't make sense.
  262. -- this bug exists in the official minetest_game, too, but not with books (even though the book code is almost the same).
  263. -- the bug remains regardless of whether the following code is commented out.
  264. -- update: seems this problem was caused because the items in question were registered as tools!
  265. ---[[
  266. minetest.register_craft({
  267. type = "shapeless",
  268. output = "key:key",
  269. recipe = {"key:skeleton", "key:key"}
  270. })
  271. minetest.register_on_craft(function(...) key.on_craft(...) end)
  272. --]]
  273. local c = "key:core"
  274. local f = key.modpath .. "/init.lua"
  275. reload.register_file(c, f, false)
  276. key.registered = true
  277. end