tools.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. -- Localize for performance.
  2. local math_random = math.random
  3. function mese_crystals.on_tool_use(itemstack, user, pt)
  4. if pt.type ~= "node" then return end
  5. local pos = pt.under
  6. if minetest.test_protection(pos, user:get_player_name()) then return end
  7. local node = minetest.get_node(pos)
  8. local growth_stage = 0
  9. -- Determine growth stage.
  10. if node.name == "mese_crystals:mese_crystal_ore4" then
  11. growth_stage = 4
  12. elseif node.name == "mese_crystals:mese_crystal_ore3" then
  13. growth_stage = 3
  14. elseif node.name == "mese_crystals:mese_crystal_ore2" then
  15. growth_stage = 2
  16. elseif node.name == "mese_crystals:mese_crystal_ore1" then
  17. growth_stage = 1
  18. end
  19. -- Update crystaline plant.
  20. if growth_stage == 4 then
  21. node.name = "mese_crystals:mese_crystal_ore3"
  22. minetest.swap_node(pos, node)
  23. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  24. elseif growth_stage == 3 then
  25. node.name = "mese_crystals:mese_crystal_ore2"
  26. minetest.swap_node(pos, node)
  27. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  28. elseif growth_stage == 2 then
  29. node.name = "mese_crystals:mese_crystal_ore1"
  30. minetest.swap_node(pos, node)
  31. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  32. else
  33. -- Just restart growing timer.
  34. minetest.get_node_timer(pos):start(mese_crystals.get_grow_time())
  35. end
  36. -- Give wielder a harvest.
  37. if growth_stage > 1 then
  38. ambiance.sound_play("default_break_glass", pos, 0.3, 10)
  39. itemstack:add_wear(65535 / 400)
  40. local inv = user:get_inventory()
  41. local stack
  42. if math_random(1, 40) == 1 then
  43. stack = ItemStack("default:mese_crystal")
  44. else
  45. stack = ItemStack("mese_crystals:zentamine")
  46. end
  47. if inv:room_for_item("main", stack) then
  48. inv:add_item("main", stack)
  49. else
  50. minetest.item_drop(stack, nil, pos)
  51. minetest.chat_send_player(user:get_player_name(), "# Server: Cannot obtain harvest, no room in inventory!")
  52. end
  53. end
  54. return itemstack
  55. end
  56. if not mese_crystals.tool_registered then
  57. minetest.register_tool("mese_crystals:crystaline_bell", {
  58. description = "Crystaline Bell\n\nHarvests zentamine crystals.\nCannot be repaired on anvil.\nNon-metalic.",
  59. inventory_image = "crystalline_bell.png",
  60. groups = {not_repaired_by_anvil = 1},
  61. on_use = function(...)
  62. return mese_crystals.on_tool_use(...)
  63. end,
  64. })
  65. mese_crystals.tool_registered = true
  66. end