2024-04-13 16:02:22 -04:00
|
|
|
local path = minetest.get_modpath('tool_level_enchanting')
|
2024-04-13 13:35:42 -04:00
|
|
|
local S = minetest.get_translator("tool_level_enchanting")
|
2024-04-13 23:02:58 -04:00
|
|
|
|
2024-04-16 20:24:47 -04:00
|
|
|
tool_level_enchanting = {}
|
|
|
|
|
|
|
|
-- List of ores that work with fortune
|
|
|
|
tool_level_enchanting.registered_ores = {
|
|
|
|
['default:stone_with_coal'] = true,
|
|
|
|
['default:stone_with_copper'] = true,
|
|
|
|
['default:stone_with_diamond'] = true,
|
|
|
|
['default:stone_with_gold'] = true,
|
|
|
|
['default:stone_with_iron'] = true,
|
|
|
|
['default:stone_with_mese'] = true,
|
|
|
|
['default:stone_with_tin'] = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- List of enchantments and max levels
|
|
|
|
tool_level_enchanting.registered_enchantments = {
|
|
|
|
['unbreaking'] = 10,
|
|
|
|
['efficiency'] = 5,
|
|
|
|
['silk_touch'] = 10,
|
|
|
|
['fortune'] = 10,
|
|
|
|
['auto_repair'] = 10,
|
|
|
|
['sharpness'] = 5
|
2024-04-13 23:02:58 -04:00
|
|
|
}
|
2024-04-13 13:35:42 -04:00
|
|
|
|
2024-04-13 16:02:22 -04:00
|
|
|
dofile(path .. '/enchants.lua')
|
2024-04-13 13:35:42 -04:00
|
|
|
level_divide_by = tonumber(minetest.settings:get("tool_level_enchanting_level_divide_by")) or 50
|
|
|
|
|
2024-04-13 16:02:22 -04:00
|
|
|
-- function to calculate levels
|
2024-04-13 13:35:42 -04:00
|
|
|
function tool_level_enchanting.get_level(uses)
|
2024-04-13 16:02:22 -04:00
|
|
|
--local uses_number = tonumber(uses) or 0
|
2024-04-13 13:35:42 -04:00
|
|
|
if uses < 50 then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return math.floor((25 + math.sqrt(625 + 100 * uses))/level_divide_by)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-13 23:02:58 -04:00
|
|
|
function tool_level_enchanting.create_description(name, uses, itemstack)
|
2024-04-16 19:44:20 -04:00
|
|
|
local enchantment_string = 'Enchants:'
|
2024-04-13 23:02:58 -04:00
|
|
|
if itemstack then
|
|
|
|
local itemmeta = itemstack:get_meta()
|
|
|
|
local unbreaking = tonumber(itemmeta:get_string("unbreaking")) or 0
|
|
|
|
local fortune = tonumber(itemmeta:get_string("fortune")) or 0
|
|
|
|
local silk_touch = tonumber(itemmeta:get_string("silk_touch")) or 0
|
|
|
|
local auto_repair = tonumber(itemmeta:get_string("auto_repair")) or 0
|
|
|
|
if unbreaking > 0 then
|
|
|
|
enchantment_string = S(
|
|
|
|
'@1\n Unbreaking @2',
|
|
|
|
enchantment_string,
|
|
|
|
unbreaking
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if fortune > 0 then
|
|
|
|
enchantment_string = S(
|
|
|
|
'@1\n Fortune @2',
|
|
|
|
enchantment_string,
|
|
|
|
fortune
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if silk_touch > 0 then
|
|
|
|
enchantment_string = S(
|
|
|
|
'@1\n Silk Touch @2',
|
|
|
|
enchantment_string,
|
|
|
|
silk_touch
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if auto_repair > 0 then
|
|
|
|
enchantment_string = S(
|
|
|
|
'@1\n Auto Repair @2',
|
|
|
|
enchantment_string,
|
|
|
|
auto_repair
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2024-04-13 13:35:42 -04:00
|
|
|
local level = tool_level_enchanting.get_level(tonumber(uses) or 0)
|
|
|
|
local newdesc = S(
|
2024-04-13 23:02:58 -04:00
|
|
|
'@1\nLevel @2\nUses: @3\n@4',
|
2024-04-13 13:35:42 -04:00
|
|
|
name,
|
|
|
|
level,
|
2024-04-13 23:02:58 -04:00
|
|
|
(type(uses) == "number" and uses or 0),
|
|
|
|
enchantment_string
|
2024-04-13 13:35:42 -04:00
|
|
|
)
|
|
|
|
return newdesc
|
|
|
|
end
|
|
|
|
|
|
|
|
function tool_level_enchanting.on_use(itemstack, user, node, digparams)
|
|
|
|
local itemmeta = itemstack:get_meta()
|
|
|
|
local itemdef = itemstack:get_definition()
|
|
|
|
local itemdesc = itemdef.original_description or ""
|
|
|
|
local dugnodes = tonumber(itemmeta:get_string("dug")) or 0
|
2024-04-13 23:02:58 -04:00
|
|
|
local pname = user:get_player_name()
|
2024-04-13 16:02:22 -04:00
|
|
|
-- Enchant Variables
|
2024-04-13 23:02:58 -04:00
|
|
|
local unbreaking = tonumber(itemmeta:get_string("unbreaking")) or 0
|
|
|
|
local fortune = tonumber(itemmeta:get_string("fortune")) or 0
|
|
|
|
local silk_touch = tonumber(itemmeta:get_string("silk_touch")) or 0
|
|
|
|
local auto_repair = tonumber(itemmeta:get_string("auto_repair")) or 0
|
2024-04-16 19:44:20 -04:00
|
|
|
|
2024-04-13 16:02:22 -04:00
|
|
|
-- Make sure play exists
|
|
|
|
if not pname then return itemstack end
|
2024-04-13 13:35:42 -04:00
|
|
|
|
2024-04-13 16:02:22 -04:00
|
|
|
--
|
2024-04-13 13:35:42 -04:00
|
|
|
if digparams.wear > 0 then
|
2024-04-13 23:02:58 -04:00
|
|
|
if tool_level_enchanting.fortune_use_double(fortune) then
|
|
|
|
dugnodes = dugnodes + 1
|
|
|
|
else
|
|
|
|
dugnodes = dugnodes + 2
|
|
|
|
end
|
2024-04-13 16:02:22 -04:00
|
|
|
itemmeta:set_string("dug", dugnodes)
|
2024-04-13 13:35:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if itemstack:get_wear() > 60135 then --FIXME: Make this proc earlier?
|
|
|
|
minetest.chat_send_player(user:get_player_name(), S("Your tool is about to break!"))
|
|
|
|
end
|
|
|
|
|
2024-04-13 23:02:58 -04:00
|
|
|
itemmeta:set_string("description", tool_level_enchanting.create_description(itemdesc, dugnodes, itemstack))
|
2024-04-13 13:35:42 -04:00
|
|
|
--FIXME: Add Unbreaking here? idk
|
2024-04-13 16:02:22 -04:00
|
|
|
if tool_level_enchanting.unbreaking_proc(unbreaking) then
|
|
|
|
itemstack:add_wear(digparams.wear)
|
|
|
|
end
|
2024-04-13 13:35:42 -04:00
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
|
|
|
|
function tool_level_enchanting.add_tool(name)
|
|
|
|
local desc = ItemStack(name):get_definition().description
|
|
|
|
minetest.override_item(name, {
|
|
|
|
original_description = desc,
|
|
|
|
description = tool_level_enchanting.create_description(desc),
|
|
|
|
after_use = tool_level_enchanting.on_use
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
tool_level_enchanting.add_tool("default:pick_wood")
|
2024-04-16 19:44:20 -04:00
|
|
|
tool_level_enchanting.add_tool("default:pick_stone")
|
|
|
|
tool_level_enchanting.add_tool("default:pick_steel")
|
|
|
|
tool_level_enchanting.add_tool("default:pick_bronze")
|
|
|
|
tool_level_enchanting.add_tool("default:pick_mese")
|
2024-04-13 13:35:42 -04:00
|
|
|
tool_level_enchanting.add_tool("default:pick_diamond")
|