node.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. local S = minetest.get_translator("repairing_anvils")
  2. local recipes = repairing_anvils.recipes
  3. local function forge(itemstack, user)
  4. local name = string.split(itemstack:to_string(), " ")
  5. local recipe = recipes[name[1]]
  6. if recipe
  7. then
  8. local did_something = false
  9. if type(recipe) == "string"
  10. then
  11. minetest.sound_play("repairing_anvils_strikes", {pos = pos})
  12. local i = itemstack:take_item()
  13. if not i:is_empty()
  14. then
  15. if itemstack:is_empty()
  16. then
  17. itemstack:add_item(recipe)
  18. return ItemStack(nil)
  19. else
  20. return ItemStack(recipe)
  21. end
  22. end
  23. else
  24. return recipe(itemstack)
  25. end
  26. end
  27. end
  28. local function anvil_rightclick(pos, node, clicker, itemstack)
  29. local inv = clicker:get_inventory()
  30. local stackcopy = itemstack:peek_item(itemstack:get_count())
  31. local workpiece = forge(stackcopy, clicker)
  32. if workpiece
  33. then
  34. if inv:room_for_item("main", workpiece)
  35. then
  36. inv:add_item("main", workpiece)
  37. itemstack = stackcopy
  38. end
  39. end
  40. return itemstack
  41. end
  42. local tooltip = S("Anvil\n@1",
  43. minetest.colorize("#90FFFF", S("Used to repair tools")))
  44. local function anvil_set_infotext(pos)
  45. local meta = minetest.get_meta(pos)
  46. meta:set_string("infotext", tooltip)
  47. end
  48. minetest.register_node("repairing_anvils:anvil",
  49. {
  50. description = tooltip,
  51. drawtype = "mesh",
  52. mesh = "repairing_anvils_anvil.obj",
  53. tiles = {"repairing_anvils_anvil.png"},
  54. groups = {cracky = 1, falling_node = 1},
  55. sounds = generic_media.node_sound_metal_defaults(),
  56. paramtype2 = "facedir",
  57. on_rightclick = anvil_rightclick,
  58. stack_max = 1,
  59. collision_box =
  60. {
  61. type = "fixed",
  62. fixed = {
  63. {-0.5, -0.5, -1/4, 0.5, 0.5, 1/4},
  64. },
  65. },
  66. selection_box =
  67. {
  68. type = "fixed",
  69. fixed = {
  70. {-0.5, -0.5, -1/4, 0.5, 0.5, 1/4},
  71. },
  72. },
  73. on_construct = anvil_set_infotext
  74. })
  75. minetest.register_alias("anvil", "repairing_anvils:anvil")
  76. crafting.register_recipe(
  77. {
  78. output = "anvil",
  79. type = "inv",
  80. items = {"iron_ingot 8"},
  81. always_known = true,
  82. })
  83. --https://freesound.org/people/alan3809/sounds/103071/