tool_level_enchanting/enchants.lua

150 lines
5.2 KiB
Lua

local modname = minetest.get_current_modname()
local S = minetest.get_translator(modname)
-- TODO:
-- Make sure enchantment is an enchantment
-- Make sure level is a number
-- Make sure enchantment is possible (correct tool)
function tool_level_enchanting.set_enchantment(itemstack,enchantment_name,level)
local itemmeta = itemstack:get_meta()
local itemdef = itemstack:get_definition()
local dugnodes = tonumber(itemmeta:get_string("dug")) or 0
local itemdesc = itemdef.original_description or ""
itemmeta:set_string(enchantment_name,level)
itemmeta:set_string("description", tool_level_enchanting.create_description(itemdesc, dugnodes, itemstack))
return itemstack
end
-- Unbreaking
-- 10% chance per level of not using durability each use
-- (Level 10, tool takes no durability usage)
-- This function is used in tool_level_enchanting.on_use
function tool_level_enchanting.unbreaking_proc(enchant_level)
local rand_num = math.random(0,10)
if tonumber(enchant_level) > tonumber(rand_num) then
return false
else
return true
end
end
-- Efficiency
-- 10% speed increase each level?
-- Max Level 10
-- Fortune
-- 10% Chance per level of doubling ore
-- 5% Chance per level of doubling uses
-- Max level 10
function tool_level_enchanting.fortune_ore_double(enchant_level)
local rand_num = math.random(0,10)
if tonumber(enchant_level) > tonumber(rand_num) then
return false
else
return true
end
end
function tool_level_enchanting.fortune_use_double(enchant_level)
local rand_num = math.random(0,20)
if tonumber(enchant_level) > tonumber(rand_num) then
return false
else
return true
end
end
-- This keeps existing handling of node drops
local old_handle_node_drops = minetest.handle_node_drops
function minetest.handle_node_drops(pos, drops, digger)
if not digger or not digger:is_player() then
return old_handle_node_drops(pos, drops, digger)
end
local node = minetest.get_node(pos)
local node_name = node.name
local wield_stack = digger:get_wielded_item()
local wield_stack_meta = wield_stack:get_meta()
local fortune = tonumber(wield_stack_meta:get_string("fortune")) or 0
local silk_touch = tonumber(wield_stack_meta:get_string("silk_touch")) or 0
-- Silk Touch
-- Return block mined
if silk_touch > 0 and minetest.get_item_group(node.name, 'no_silktouch') == 0 then
return old_handle_node_drops(pos, { ItemStack(node.name) }, digger)
end
if fortune > 0 then
local new_drops = {}
for _, itemstring in ipairs(drops) do
if tool_level_enchanting.registered_ores[node.name] and tool_level_enchanting.fortune_ore_double(fortune) then
local stack = ItemStack(itemstring)
stack:set_count(stack:get_count() * 2)
table.insert(new_drops, stack)
end
end
if #new_drops > 0 then
return old_handle_node_drops(pos, new_drops, digger)
end
return old_handle_node_drops(pos, drops, digger)
end
return old_handle_node_drops(pos, drops, digger)
end
-- Auto Repair
-- Auto repairs every time ingame time changes
-- Max Level 5
local last_time = 0
local auto_repair_multiplier = tonumber(minetest.settings:get("tool_level_enchanting_auto_repair_multiplier")) or 20
minetest.register_globalstep(function(dtime)
local players = minetest.get_connected_players()
if (last_time ~= minetest.get_gametime()) then
last_time = minetest.get_gametime()
for i,player in ipairs(players) do
local has_autorepair = false
local stack_idx = 0
if player:get_inventory() then
for i,stack in ipairs(player:get_inventory():get_list("main")) do
local auto_repair_level = tonumber(stack:get_meta():get_string("auto_repair")) or 0
if auto_repair_level > 0 then
stack:add_wear(-1 * auto_repair_multiplier * auto_repair_level)
player:get_inventory():set_stack("main",i,stack)
end
end
end
end
end
end)
-- Sharpness?
-- 10% per level? idk
-- max level 10
-- TODO:
-- Make sure enchantment is an enchantment
-- Make sure level is a number
minetest.register_chatcommand("enchant", {
description = S("Enchant an item"),
params = S("<player> <enchantment> [<level>]"),
privs = {give = true},
func = function(_, param)
local sparam = param:split(" ")
local player = sparam[1]
local enchantment = sparam[2]
local level = sparam[3]
-- Makes sure target name or enchant is not null
if not player or not enchantment then
return false, S("Usage: /enchant @1", params)
end
local target = minetest.get_player_by_name(player)
-- Makes sure player exists
if not target then
return false, S("Player @1 can not be found.", player)
end
local itemstack = target:get_wielded_item()
target:set_wielded_item(tool_level_enchanting.set_enchantment(itemstack,enchantment,level or 1))
return true, S("Enchantment Successful")
end
})