From 5ba7c54408ffe778d20a8dc30d4d7e94e6c6d575 Mon Sep 17 00:00:00 2001 From: mckaygerhard Date: Thu, 12 Jan 2023 13:49:29 -0400 Subject: [PATCH] Allow tool repairs on workbench mod coexits with game mechanish * adapt the fix from the upstream by mt-sane * becouse there is is also the anvil, * This also fixes the problem with the vanishing of the tools when using the repair craft. * closed https://github.com/Bremaweb/adventuretest/pull/26 * close https://git.minetest.io/minenux/minetest-game-adventuretest/pulls/26 * close https://codeber.org/minenux/minetest-game-adventuretest/pulls/26 --- mods/workbench/init.lua | 54 ++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/mods/workbench/init.lua b/mods/workbench/init.lua index 27999b0..f38ff93 100644 --- a/mods/workbench/init.lua +++ b/mods/workbench/init.lua @@ -89,21 +89,51 @@ local function inventory_set(player, size) end local function on_craft(itemstack,player,old_craftgrid,craft_inv) - if itemstack:get_definition().skill ~= nil then - local name = player:get_player_name() - local probability = skills.get_probability(name,SKILL_CRAFTING,itemstack:get_definition().skill) - local rangeLow = ( probability - 10 ) / 100 - probability = probability / 100 - local wear = math.floor(50000 - ( 50000 * math.random(rangeLow,probability) )) - itemstack:add_wear(wear) - local i = skills.add_skill_exp(name,SKILL_CRAFTING,1) - local ii = skills.add_skill_exp(name,itemstack:get_definition().skill,1) - if i or ii then + + local name = player:get_player_name() + if not name then return nil end + + local craftItemDef = itemstack:get_definition() + if not craftItemDef then return nil end + + local craftItemSkill = craftItemDef.skill + if not craftItemSkill then return nil end + + if craftItemDef.type == "tool" then + local isSameItem = true + local itemCount = 0 + local craftedItemName = craftItemDef.name + for _,recipeStack in ipairs(old_craftgrid) do + local recipeItemName = recipeStack:get_name() + if recipeItemName ~= "" then + itemCount = itemCount + 1 + if itemCount > 2 then break end + if recipeItemName ~= craftedItemName then + isSameItem = false + break + end + end + end + if isSameItem and itemCount == 2 then + -- Yes we can ... repair + return nil + end + end + + + local probability = skills.get_probability(name, SKILL_CRAFTING, craftItemSkill) + local rangeLow = math.max(( probability - 10 ) / 100, 0.0) + probability = probability / 100 + local wear = math.floor(50000 - ( 50000 * math.random(rangeLow,probability) )) + itemstack:add_wear(wear) + local craftItemDefw = itemstack:get_definition() + local craftItemSkillw = craftItemDefw.skill +-- local i = skills.add_skill_exp(name,SKILL_CRAFTING,1) + local ii = skills.add_skill_exp(name,craftItemSkillw,1) + if ii then minetest.chat_send_player(name,"Your skills are increasing!") end return itemstack - end - return nil end minetest.register_on_craft(on_craft)