123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- toolranks = {
- colors = {
- grey = minetest.get_color_escape_sequence("#9d9d9d"),
- green = minetest.get_color_escape_sequence("#1eff00"),
- gold = minetest.get_color_escape_sequence("#ffdf00"),
- white = minetest.get_color_escape_sequence("#ffffff")
- }
- }
- function toolranks.create_description(name, uses, level)
- return toolranks.colors.green .. (name or "") .. "\n"
- .. toolranks.colors.gold .. "Level: " .. (level or 1) .. "\n"
- .. toolranks.colors.grey .. "Used: " .. (uses or 0) .. " times"
- end
- function toolranks.get_level(uses)
- if uses >= 3200 then
- return 6
- elseif uses >= 2000 then
- return 5
- elseif uses >= 1000 then
- return 4
- elseif uses >= 400 then
- return 3
- elseif uses >= 200 then
- return 2
- else
- return 1
- end
- end
- function toolranks.new_afteruse(itemstack, user, node, digparams)
- -- Get tool metadata and number of times used
- local itemmeta = itemstack:get_meta()
- local dugnodes = tonumber(itemmeta:get_string("dug")) or 0
- -- Only count nodes that spend the tool
- if digparams.wear > 0 then
- dugnodes = dugnodes + 1
- itemmeta:set_string("dug", dugnodes)
- else
- return
- end
- -- Get tool description and last level
- local itemdef = itemstack:get_definition()
- local itemdesc = itemdef.original_description or itemdef.description or "Tool"
- local lastlevel = tonumber(itemmeta:get_string("lastlevel")) or 1
- local name = user:get_player_name()
- -- Warn player when tool is almost broken
- if itemstack:get_wear() > 60100 then
- minetest.chat_send_player(name,
- toolranks.colors.gold .. "Your tool is almost broken!")
- minetest.sound_play("default_tool_breaks", {
- to_player = name,
- gain = 2.0
- })
- end
- local level = toolranks.get_level(dugnodes)
- -- Alert player when tool has leveled up
- if lastlevel < level then
- minetest.chat_send_player(name, "Your "
- .. toolranks.colors.green .. itemdesc
- .. toolranks.colors.white .. " just leveled up!")
- minetest.sound_play("toolranks_levelup", {
- to_player = name,
- gain = 1.0
- })
- itemmeta:set_string("lastlevel", level)
- end
- local newdesc = toolranks.create_description(itemdesc, dugnodes, level)
- -- Set new meta
- itemmeta:set_string("description", newdesc)
- local wear = digparams.wear
- -- Set wear level
- if level > 1 then
- wear = digparams.wear * 4 / (4 + level)
- end
- itemstack:add_wear(wear)
- return itemstack
- end
- -- Helper function
- minetest.after(0, function()
- for name, def in pairs(minetest.registered_tools) do
- minetest.override_item(name, {
- original_description = def.description,
- description = toolranks.create_description(def.description, 0, 1),
- after_use = toolranks.new_afteruse
- })
- end
- end)
|