2
0

craftitems.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. -- Load support for MT game translation.
  2. local S = minetest.get_translator("keys")
  3. --
  4. -- Craftitems
  5. --
  6. minetest.register_craftitem("keys:skeleton_key", {
  7. description = S("Skeleton Key"),
  8. inventory_image = "keys_key_skeleton.png",
  9. on_use = function(itemstack, user, pointed_thing)
  10. if pointed_thing.type ~= "node" then
  11. return itemstack
  12. end
  13. local pos = pointed_thing.under
  14. local node = minetest.get_node(pos)
  15. if not node then
  16. return itemstack
  17. end
  18. local node_reg = minetest.registered_nodes[node.name]
  19. local on_skeleton_key_use = node_reg and node_reg.on_skeleton_key_use
  20. if not on_skeleton_key_use then
  21. return itemstack
  22. end
  23. -- make a new key secret in case the node callback needs it
  24. local random = math.random
  25. local newsecret = string.format(
  26. "%04x%04x%04x%04x",
  27. random(2^16) - 1, random(2^16) - 1,
  28. random(2^16) - 1, random(2^16) - 1)
  29. local secret, _, _ = on_skeleton_key_use(pos, user, newsecret)
  30. if secret then
  31. local inv = minetest.get_inventory({type="player", name=user:get_player_name()})
  32. -- update original itemstack
  33. itemstack:take_item()
  34. -- finish and return the new key
  35. local new_stack = ItemStack("keys:key")
  36. local meta = new_stack:get_meta()
  37. meta:set_string("secret", secret)
  38. meta:set_string("description", S("Key to @1's @2", user:get_player_name(),
  39. minetest.registered_nodes[node.name].description))
  40. if itemstack:get_count() == 0 then
  41. itemstack = new_stack
  42. else
  43. if inv:add_item("main", new_stack):get_count() > 0 then
  44. minetest.add_item(user:get_pos(), new_stack)
  45. end -- else: added to inventory successfully
  46. end
  47. return itemstack
  48. end
  49. end
  50. })
  51. minetest.register_craftitem("keys:key", {
  52. description = S("Key"),
  53. inventory_image = "keys_key.png",
  54. groups = {key = 1, not_in_creative_inventory = 1},
  55. on_place = function(itemstack, placer, pointed_thing)
  56. local under = pointed_thing.under
  57. local node = minetest.get_node(under)
  58. local def = minetest.registered_nodes[node.name]
  59. if def and def.on_rightclick and
  60. not (placer and placer:is_player() and
  61. placer:get_player_control().sneak) then
  62. return def.on_rightclick(under, node, placer, itemstack,
  63. pointed_thing) or itemstack
  64. end
  65. if pointed_thing.type ~= "node" then
  66. return itemstack
  67. end
  68. local pos = pointed_thing.under
  69. node = minetest.get_node(pos)
  70. if not node or node.name == "ignore" then
  71. return itemstack
  72. end
  73. local ndef = minetest.registered_nodes[node.name]
  74. if not ndef then
  75. return itemstack
  76. end
  77. local on_key_use = ndef.on_key_use
  78. if on_key_use then
  79. on_key_use(pos, placer)
  80. end
  81. return nil
  82. end
  83. })