From f76558b7fddd5f1fd05e35df744e497a98b27e2a Mon Sep 17 00:00:00 2001 From: jasonl8446 Date: Sat, 13 Apr 2024 23:02:58 -0400 Subject: [PATCH] Added auto repair --- enchants.lua | 93 +++++++++++++++++++++++++++++++++++++++++++++++++--- init.lua | 72 +++++++++++++++++++++++++++++++++++----- 2 files changed, 151 insertions(+), 14 deletions(-) diff --git a/enchants.lua b/enchants.lua index ae84769..f89fdd9 100644 --- a/enchants.lua +++ b/enchants.lua @@ -1,11 +1,16 @@ 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 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 @@ -14,28 +19,106 @@ end -- (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(1,10) - if tonumber(enchant_level) >= tonumber(rand_num) then + 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 --- Silk Touch --- Return block mined --- No idea how to implient yet +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 few seconds -- Max Level 5 -- Will work simularly to a compass/clock does -- Not sure how much per level to repair yet +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 -- TODO: -- Make sure enchantment is an enchantment diff --git a/init.lua b/init.lua index f2876aa..501d6f5 100644 --- a/init.lua +++ b/init.lua @@ -1,6 +1,17 @@ local path = minetest.get_modpath('tool_level_enchanting') local S = minetest.get_translator("tool_level_enchanting") -tool_level_enchanting = {} + +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, + } +} dofile(path .. '/enchants.lua') level_divide_by = tonumber(minetest.settings:get("tool_level_enchanting_level_divide_by")) or 50 @@ -15,13 +26,50 @@ function tool_level_enchanting.get_level(uses) end end -function tool_level_enchanting.create_description(name, uses) +function tool_level_enchanting.create_description(name, uses, itemstack) + enchantment_string = 'Enchants:' + 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 local level = tool_level_enchanting.get_level(tonumber(uses) or 0) local newdesc = S( - '@1\nLevel @2\nUses: @3', + '@1\nLevel @2\nUses: @3\n@4', name, level, - (type(uses) == "number" and uses or 0) + (type(uses) == "number" and uses or 0), + enchantment_string ) return newdesc end @@ -31,16 +79,22 @@ function tool_level_enchanting.on_use(itemstack, user, node, digparams) local itemdef = itemstack:get_definition() local itemdesc = itemdef.original_description or "" local dugnodes = tonumber(itemmeta:get_string("dug")) or 0 - local pname = user:get_player_name() + local pname = user:get_player_name() -- Enchant Variables - local unbreaking = tonumber(itemmeta:get_string("unbreaking")) or 0 - + 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 -- Make sure play exists if not pname then return itemstack end -- if digparams.wear > 0 then - dugnodes = dugnodes + 1 + if tool_level_enchanting.fortune_use_double(fortune) then + dugnodes = dugnodes + 1 + else + dugnodes = dugnodes + 2 + end itemmeta:set_string("dug", dugnodes) end @@ -48,7 +102,7 @@ function tool_level_enchanting.on_use(itemstack, user, node, digparams) minetest.chat_send_player(user:get_player_name(), S("Your tool is about to break!")) end - itemmeta:set_string("description", tool_level_enchanting.create_description(itemdesc, dugnodes)) + itemmeta:set_string("description", tool_level_enchanting.create_description(itemdesc, dugnodes, itemstack)) --FIXME: Add Unbreaking here? idk if tool_level_enchanting.unbreaking_proc(unbreaking) then itemstack:add_wear(digparams.wear)