init.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. toolranks = {
  2. colors = {
  3. grey = minetest.get_color_escape_sequence("#9d9d9d"),
  4. green = minetest.get_color_escape_sequence("#1eff00"),
  5. gold = minetest.get_color_escape_sequence("#ffdf00"),
  6. white = minetest.get_color_escape_sequence("#ffffff")
  7. }
  8. }
  9. function toolranks.create_description(name, uses, level)
  10. return toolranks.colors.green .. (name or "") .. "\n"
  11. .. toolranks.colors.gold .. "Level: " .. (level or 1) .. "\n"
  12. .. toolranks.colors.grey .. "Used: " .. (uses or 0) .. " times"
  13. end
  14. function toolranks.get_level(uses)
  15. if uses >= 3200 then
  16. return 6
  17. elseif uses >= 2000 then
  18. return 5
  19. elseif uses >= 1000 then
  20. return 4
  21. elseif uses >= 400 then
  22. return 3
  23. elseif uses >= 200 then
  24. return 2
  25. else
  26. return 1
  27. end
  28. end
  29. function toolranks.new_afteruse(itemstack, user, node, digparams)
  30. -- Get tool metadata and number of times used
  31. local itemmeta = itemstack:get_meta()
  32. local dugnodes = tonumber(itemmeta:get_string("dug")) or 0
  33. -- Only count nodes that spend the tool
  34. if digparams.wear > 0 then
  35. dugnodes = dugnodes + 1
  36. itemmeta:set_string("dug", dugnodes)
  37. else
  38. return
  39. end
  40. -- Get tool description and last level
  41. local itemdef = itemstack:get_definition()
  42. local itemdesc = itemdef.original_description or itemdef.description or "Tool"
  43. local lastlevel = tonumber(itemmeta:get_string("lastlevel")) or 1
  44. local name = user:get_player_name()
  45. -- Warn player when tool is almost broken
  46. if itemstack:get_wear() > 60100 then
  47. minetest.chat_send_player(name,
  48. toolranks.colors.gold .. "Your tool is almost broken!")
  49. minetest.sound_play("default_tool_breaks", {
  50. to_player = name,
  51. gain = 2.0
  52. })
  53. end
  54. local level = toolranks.get_level(dugnodes)
  55. -- Alert player when tool has leveled up
  56. if lastlevel < level then
  57. minetest.chat_send_player(name, "Your "
  58. .. toolranks.colors.green .. itemdesc
  59. .. toolranks.colors.white .. " just leveled up!")
  60. minetest.sound_play("toolranks_levelup", {
  61. to_player = name,
  62. gain = 1.0
  63. })
  64. itemmeta:set_string("lastlevel", level)
  65. end
  66. local newdesc = toolranks.create_description(itemdesc, dugnodes, level)
  67. -- Set new meta
  68. itemmeta:set_string("description", newdesc)
  69. local wear = digparams.wear
  70. -- Set wear level
  71. if level > 1 then
  72. wear = digparams.wear * 4 / (4 + level)
  73. end
  74. itemstack:add_wear(wear)
  75. return itemstack
  76. end
  77. -- Helper function
  78. minetest.after(0, function()
  79. for name, def in pairs(minetest.registered_tools) do
  80. minetest.override_item(name, {
  81. original_description = def.description,
  82. description = toolranks.create_description(def.description, 0, 1),
  83. after_use = toolranks.new_afteruse
  84. })
  85. end
  86. end)