Added auto repair

This commit is contained in:
Jason Liszka 2024-04-13 23:02:58 -04:00
parent 639e5e5ae4
commit f76558b7fd
2 changed files with 151 additions and 14 deletions

View File

@ -1,11 +1,16 @@
local modname = minetest.get_current_modname() local modname = minetest.get_current_modname()
local S = minetest.get_translator(modname) local S = minetest.get_translator(modname)
-- TODO: -- TODO:
-- Make sure enchantment is an enchantment -- Make sure enchantment is an enchantment
-- Make sure level is a number -- Make sure level is a number
function tool_level_enchanting.set_enchantment(itemstack,enchantment_name,level) function tool_level_enchanting.set_enchantment(itemstack,enchantment_name,level)
local itemmeta = itemstack:get_meta() 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(enchantment_name,level)
itemmeta:set_string("description", tool_level_enchanting.create_description(itemdesc, dugnodes, itemstack))
return itemstack return itemstack
end end
@ -14,28 +19,106 @@ end
-- (Level 10, tool takes no durability usage) -- (Level 10, tool takes no durability usage)
-- This function is used in tool_level_enchanting.on_use -- This function is used in tool_level_enchanting.on_use
function tool_level_enchanting.unbreaking_proc(enchant_level) function tool_level_enchanting.unbreaking_proc(enchant_level)
local rand_num = math.random(1,10) local rand_num = math.random(0,10)
if tonumber(enchant_level) >= tonumber(rand_num) then if tonumber(enchant_level) > tonumber(rand_num) then
return false return false
else else
return true return true
end end
end end
-- Efficiency
-- 10% speed increase each level?
-- Max Level 10
-- Fortune -- Fortune
-- 10% Chance per level of doubling ore -- 10% Chance per level of doubling ore
-- 5% Chance per level of doubling uses -- 5% Chance per level of doubling uses
-- Max level 10 -- 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 -- Silk Touch
-- Return block mined -- Return block mined
-- No idea how to implient yet 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 Repair
-- Auto repairs every few seconds -- Auto repairs every few seconds
-- Max Level 5 -- Max Level 5
-- Will work simularly to a compass/clock does -- Will work simularly to a compass/clock does
-- Not sure how much per level to repair yet -- 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: -- TODO:
-- Make sure enchantment is an enchantment -- Make sure enchantment is an enchantment

View File

@ -1,6 +1,17 @@
local path = minetest.get_modpath('tool_level_enchanting') local path = minetest.get_modpath('tool_level_enchanting')
local S = minetest.get_translator("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') dofile(path .. '/enchants.lua')
level_divide_by = tonumber(minetest.settings:get("tool_level_enchanting_level_divide_by")) or 50 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
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 level = tool_level_enchanting.get_level(tonumber(uses) or 0)
local newdesc = S( local newdesc = S(
'@1\nLevel @2\nUses: @3', '@1\nLevel @2\nUses: @3\n@4',
name, name,
level, level,
(type(uses) == "number" and uses or 0) (type(uses) == "number" and uses or 0),
enchantment_string
) )
return newdesc return newdesc
end end
@ -34,13 +82,19 @@ function tool_level_enchanting.on_use(itemstack, user, node, digparams)
local pname = user:get_player_name() local pname = user:get_player_name()
-- Enchant Variables -- 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 -- Make sure play exists
if not pname then return itemstack end if not pname then return itemstack end
-- --
if digparams.wear > 0 then if digparams.wear > 0 then
if tool_level_enchanting.fortune_use_double(fortune) then
dugnodes = dugnodes + 1 dugnodes = dugnodes + 1
else
dugnodes = dugnodes + 2
end
itemmeta:set_string("dug", dugnodes) itemmeta:set_string("dug", dugnodes)
end 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!")) minetest.chat_send_player(user:get_player_name(), S("Your tool is about to break!"))
end 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 --FIXME: Add Unbreaking here? idk
if tool_level_enchanting.unbreaking_proc(unbreaking) then if tool_level_enchanting.unbreaking_proc(unbreaking) then
itemstack:add_wear(digparams.wear) itemstack:add_wear(digparams.wear)