init.lua 9.1 KB

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