Guest User

Untitled

a guest
Jul 27th, 2017
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. toolranks = {}
  2.  
  3. toolranks.colors = {
  4. grey = minetest.get_color_escape_sequence("#9d9d9d"),
  5. green = minetest.get_color_escape_sequence("#1eff00"),
  6. gold = minetest.get_color_escape_sequence("#ffdf00"),
  7. white = minetest.get_color_escape_sequence("#ffffff")
  8. }
  9.  
  10.  
  11. function toolranks.create_description(name, uses, level)
  12.  
  13. local newdesc = toolranks.colors.green .. name .. "\n" ..
  14. toolranks.colors.gold .. "Level " .. (level or 1) .. "\n" ..
  15. toolranks.colors.grey .. "Nodes dug: " .. (uses or 0)
  16.  
  17. return newdesc
  18. end
  19.  
  20.  
  21. function toolranks.get_level(uses)
  22.  
  23. if uses < 100 then
  24. return 1
  25. elseif uses < 200 then
  26. return 2
  27. elseif uses < 400 then
  28. return 3
  29. elseif uses < 800 then
  30. return 4
  31. elseif uses < 1600 then
  32. return 5
  33. else
  34. return 6
  35. end
  36. end
  37.  
  38.  
  39. function toolranks.new_afteruse(itemstack, user, node, digparams)
  40.  
  41. local itemmeta = itemstack:get_meta() -- Metadata
  42. local itemdef = itemstack:get_definition() -- Item Definition
  43. local itemdesc = itemdef.original_description -- Original Description
  44. local dugnodes = tonumber(itemmeta:get_string("dug")) or 0 -- Number of nodes dug
  45. local lastlevel = tonumber(itemmeta:get_string("lastlevel")) or 1 -- Level the tool had on last dig
  46.  
  47. -- Only count nodes that spend the tool
  48. if digparams.wear > 0 then
  49. dugnodes = dugnodes + 1
  50. end
  51.  
  52. -- Warn player when tool is almost broken
  53. if itemstack:get_wear() > 60135 then
  54.  
  55. minetest.chat_send_player(user:get_player_name(),
  56. toolranks.colors.gold.."Your tool is about to break!")
  57.  
  58. minetest.sound_play("default_tool_breaks", {
  59. to_player = user:get_player_name(),
  60. gain = 2.0,
  61. })
  62. end
  63.  
  64. level = toolranks.get_level(dugnodes)
  65.  
  66. -- Alert player when tool has leveled up
  67. if lastlevel < level then
  68.  
  69. minetest.chat_send_player(user:get_player_name(), "Your "
  70. .. toolranks.colors.green .. itemdesc
  71. .. toolranks.colors.white .. " just leveled up!")
  72.  
  73. minetest.sound_play("toolranks_levelup", {
  74. to_player = user:get_player_name(),
  75. gain = 1.0,
  76. })
  77. end
  78.  
  79. local newdesc = toolranks.create_description(itemdesc, dugnodes, level)
  80.  
  81. -- Set new meta
  82. itemmeta:set_string("lastlevel", level)
  83. itemmeta:set_string("dug", dugnodes)
  84. itemmeta:set_string("description", newdesc)
  85.  
  86. local wear = digparams.wear
  87.  
  88. -- Set wear level
  89. if level > 1 then
  90. wear = digparams.wear / (1 + level / 4)
  91. end
  92.  
  93. itemstack:add_wear(wear)
  94.  
  95. return itemstack
  96. end
  97.  
  98.  
  99. minetest.override_item("default:pick_diamond", {
  100. original_description = "Diamond Pickaxe",
  101. description = toolranks.create_description("Diamond Pickaxe"),
  102. after_use = toolranks.new_afteruse})
  103.  
  104. minetest.override_item("default:axe_diamond", {
  105. original_description = "Diamond Axe",
  106. description = toolranks.create_description("Diamond Axe"),
  107. after_use = toolranks.new_afteruse})
  108.  
  109. minetest.override_item("default:shovel_diamond", {
  110. original_description = "Diamond Shovel",
  111. description = toolranks.create_description("Diamond Shovel"),
  112. after_use = toolranks.new_afteruse})
  113.  
  114. minetest.override_item("default:pick_wood", {
  115. original_description = "Wooden Pickaxe",
  116. description = toolranks.create_description("Wooden Pickaxe"),
  117. after_use = toolranks.new_afteruse})
  118.  
  119. minetest.override_item("default:axe_wood", {
  120. original_description = "Wooden Axe",
  121. description = toolranks.create_description("Wooden Axe"),
  122. after_use = toolranks.new_afteruse})
  123.  
  124. minetest.override_item("default:shovel_wood", {
  125. original_description = "Wooden Shovel",
  126. description = toolranks.create_description("Wooden Shovel"),
  127. after_use = toolranks.new_afteruse})
  128.  
  129. minetest.override_item("default:pick_steel", {
  130. original_description = "Steel Pickaxe",
  131. description = toolranks.create_description("Steel Pickaxe"),
  132. after_use = toolranks.new_afteruse})
  133.  
  134. minetest.override_item("default:axe_steel", {
  135. original_description = "Steel Axe",
  136. description = toolranks.create_description("Steel Axe"),
  137. after_use = toolranks.new_afteruse})
  138.  
  139. minetest.override_item("default:shovel_steel", {
  140. original_description = "Steel Shovel",
  141. description = toolranks.create_description("Steel Shovel"),
  142. after_use = toolranks.new_afteruse})
  143.  
  144. minetest.override_item("default:pick_stone", {
  145. original_description = "Stone Pickaxe",
  146. description = toolranks.create_description("Stone Pickaxe"),
  147. after_use = toolranks.new_afteruse})
  148.  
  149. minetest.override_item("default:axe_stone", {
  150. original_description = "Stone Axe",
  151. description = toolranks.create_description("Stone Axe"),
  152. after_use = toolranks.new_afteruse})
  153.  
  154. minetest.override_item("default:shovel_stone", {
  155. original_description = "Stone Shovel",
  156. description = toolranks.create_description("Stone Shovel"),
  157. after_use = toolranks.new_afteruse})
  158.  
  159. minetest.override_item("default:pick_bronze", {
  160. original_description = "Bronze Pickaxe",
  161. description = toolranks.create_description("Bronze Pickaxe"),
  162. after_use = toolranks.new_afteruse})
  163.  
  164. minetest.override_item("default:axe_bronze", {
  165. original_description = "Bronze Axe",
  166. description = toolranks.create_description("Bronze Axe"),
  167. after_use = toolranks.new_afteruse})
  168.  
  169. minetest.override_item("default:shovel_bronze", {
  170. original_description = "Bronze Shovel",
  171. description = toolranks.create_description("Bronze Shovel"),
  172. after_use = toolranks.new_afteruse})
  173.  
  174. minetest.override_item("default:pick_mese", {
  175. original_description = "Mese Pickaxe",
  176. description = toolranks.create_description("Mese Pickaxe"),
  177. after_use = toolranks.new_afteruse})
  178.  
  179. minetest.override_item("default:axe_mese", {
  180. original_description = "Mese Axe",
  181. description = toolranks.create_description("Mese Axe"),
  182. after_use = toolranks.new_afteruse})
  183.  
  184. minetest.override_item("default:shovel_mese", {
  185. original_description = "Mese Shovel",
  186. description = toolranks.create_description("Mese Shovel"),
  187. after_use = toolranks.new_afteruse})
RAW Paste Data