tool_level_enchanting/enchants.lua

148 lines
5.0 KiB
Lua
Raw Normal View History

2024-04-13 16:30:10 -04:00
local modname = minetest.get_current_modname()
local S = minetest.get_translator(modname)
2024-04-13 23:02:58 -04:00
2024-04-13 16:02:22 -04:00
-- TODO:
-- Make sure enchantment is an enchantment
-- Make sure level is a number
function tool_level_enchanting.set_enchantment(itemstack,enchantment_name,level)
local itemmeta = itemstack:get_meta()
2024-04-13 23:02:58 -04:00
local itemdef = itemstack:get_definition()
local dugnodes = tonumber(itemmeta:get_string("dug")) or 0
local itemdesc = itemdef.original_description or ""
2024-04-13 16:02:22 -04:00
itemmeta:set_string(enchantment_name,level)
2024-04-13 23:02:58 -04:00
itemmeta:set_string("description", tool_level_enchanting.create_description(itemdesc, dugnodes, itemstack))
2024-04-13 16:02:22 -04:00
return itemstack
end
2024-04-13 16:30:10 -04:00
2024-04-13 16:02:22 -04:00
-- 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)
2024-04-13 23:02:58 -04:00
local rand_num = math.random(0,10)
if tonumber(enchant_level) > tonumber(rand_num) then
2024-04-13 16:02:22 -04:00
return false
else
return true
end
end
2024-04-13 23:02:58 -04:00
-- Efficiency
-- 10% speed increase each level?
-- Max Level 10
2024-04-13 16:02:22 -04:00
-- Fortune
-- 10% Chance per level of doubling ore
-- 5% Chance per level of doubling uses
-- Max level 10
2024-04-13 23:02:58 -04:00
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
2024-04-13 16:02:22 -04:00
2024-04-13 23:02:58 -04:00
-- 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
2024-04-13 16:02:22 -04:00
-- Auto Repair
-- Auto repairs every few seconds
-- Max Level 5
-- Will work simularly to a compass/clock does
2024-04-13 16:30:10 -04:00
-- Not sure how much per level to repair yet
2024-04-13 23:02:58 -04:00
minetest.register_globalstep(function(dtime)
local players = minetest.get_connected_players()
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 and (math.floor(minetest.get_timeofday() * 1000) % 5) == 0
then
stack:add_wear(-20 + -5 * auto_repair_level)
player:get_inventory():set_stack("main",i,stack)
end
end
end
end
end)
-- Sharpness?
-- 10% per level? idk
-- max level 10
2024-04-13 16:30:10 -04:00
-- 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
})