From a4d9d22bb084ee14eb347e23a9082554a7b65d01 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Mon, 21 Dec 2020 15:12:24 +0100 Subject: [PATCH] Mending --- mods/HUD/mcl_experience/init.lua | 70 +++++++++++++++++++++- mods/ITEMS/mcl_bows/bow.lua | 2 + mods/ITEMS/mcl_enchanting/enchantments.lua | 6 +- mods/ITEMS/mcl_fire/flint_and_steel.lua | 1 + mods/ITEMS/mcl_fishing/init.lua | 1 + 5 files changed, 76 insertions(+), 4 deletions(-) diff --git a/mods/HUD/mcl_experience/init.lua b/mods/HUD/mcl_experience/init.lua index 048bd43c6..28ac779fe 100644 --- a/mods/HUD/mcl_experience/init.lua +++ b/mods/HUD/mcl_experience/init.lua @@ -331,7 +331,75 @@ local function xp_step(self, dtime) acceleration = vector.new(goal.x-currentvel.x,goal.y-currentvel.y,goal.z-currentvel.z) self.object:add_velocity(vector.add(acceleration,player_velocity)) elseif distance < 0.4 then - mcl_experience.add_experience(collector, self._xp) + local xp = self._xp + local inv = collector:get_inventory() + local candidates = { + {list = "main", index = collector:get_wield_index()}, + {list = "armor", index = 2}, + {list = "armor", index = 3}, + {list = "armor", index = 4}, + {list = "armor", index = 5}, + } + local final_candidates = {} + for _, can in ipairs(candidates) do + local stack = inv:get_stack(can.list, can.index) + local wear = stack:get_wear() + if mcl_enchanting.has_enchantment(stack, "mending") and wear > 0 then + can.stack = stack + can.wear = wear + table.insert(final_candidates, can) + end + end + if #final_candidates > 0 then + local can = final_candidates[math.random(#final_candidates)] + local stack, list, index, wear = can.stack, can.list, can.index, can.wear + local unbreaking_level = mcl_enchanting.get_enchantment(stack, "unbreaking") + local uses + local armor_uses = minetest.get_item_group(stack:get_name(), "mcl_armor_uses") + if armor_uses > 0 then + uses = armor_uses + if unbreaking_level > 0 then + uses = uses / (0.6 + 0.4 / (unbreaking_level + 1)) + end + else + local def = stack:get_definition() + if def then + local fixed_uses = def._mcl_uses + if fixed_uses then + uses = fixed_uses + if unbreaking_level > 0 then + uses = uses * (unbreaking_level + 1) + end + end + end + if not uses then + local toolcaps = stack:get_tool_capabilities() + local groupcaps = toolcaps.groupcaps + for _, v in pairs(groupcaps) do + uses = v.uses + break + end + end + end + uses = uses or 0 + local repair = (65536 / uses) * xp * 2 + local new_wear = wear - repair + if new_wear < 0 then + xp = math.floor(-new_wear / 2) + new_wear = 0 + else + xp = 0 + end + stack:set_wear(math.floor(new_wear)) + inv:set_stack(list, index, stack) + if can.list == "armor" then + local armor_inv = minetest.get_inventory({type = "detached", name = collector:get_player_name() .. "_armor"}) + armor_inv:set_stack(list, index, stack) + end + end + if xp > 0 then + mcl_experience.add_experience(collector, xp) + end self.object:remove() end return diff --git a/mods/ITEMS/mcl_bows/bow.lua b/mods/ITEMS/mcl_bows/bow.lua index d4c5fb081..9d09b4414 100644 --- a/mods/ITEMS/mcl_bows/bow.lua +++ b/mods/ITEMS/mcl_bows/bow.lua @@ -132,6 +132,7 @@ S("The speed and damage of the arrow increases the longer you charge. The regula -- Trick to disable digging as well on_use = function() return end, groups = {weapon=1,weapon_ranged=1,bow=1,enchantability=1}, + _mcl_uses = 385, }) -- Iterates through player inventory and resets all the bows in "charging" state back to their original stage @@ -189,6 +190,7 @@ for level=0, 2 do on_place = function(itemstack) return itemstack end, + _mcl_uses = 385, }) end diff --git a/mods/ITEMS/mcl_enchanting/enchantments.lua b/mods/ITEMS/mcl_enchanting/enchantments.lua index c3bfa05df..1e09fabed 100644 --- a/mods/ITEMS/mcl_enchanting/enchantments.lua +++ b/mods/ITEMS/mcl_enchanting/enchantments.lua @@ -409,8 +409,8 @@ mcl_enchanting.enchantments.lure = { power_range_table = {{15, 61}, {24, 71}, {33, 81}}, } --- unimplemented ---[[mcl_enchanting.enchantments.mending = { +-- implemented in mcl_experience +mcl_enchanting.enchantments.mending = { name = S("Mending"), max_level = 1, primary = {}, @@ -424,7 +424,7 @@ mcl_enchanting.enchantments.lure = { requires_tool = true, treasure = true, power_range_table = {{25, 75}}, -}]]-- +} -- requires missing MineClone2 feature --[[mcl_enchanting.enchantments.multishot = { diff --git a/mods/ITEMS/mcl_fire/flint_and_steel.lua b/mods/ITEMS/mcl_fire/flint_and_steel.lua index 7b585f348..39ead4557 100644 --- a/mods/ITEMS/mcl_fire/flint_and_steel.lua +++ b/mods/ITEMS/mcl_fire/flint_and_steel.lua @@ -70,6 +70,7 @@ minetest.register_tool("mcl_fire:flint_and_steel", { return stack end, sound = { breaks = "default_tool_breaks" }, + _mcl_uses = 65, }) minetest.register_craft({ diff --git a/mods/ITEMS/mcl_fishing/init.lua b/mods/ITEMS/mcl_fishing/init.lua index bf63a3c5c..4fbc1d0b2 100644 --- a/mods/ITEMS/mcl_fishing/init.lua +++ b/mods/ITEMS/mcl_fishing/init.lua @@ -330,6 +330,7 @@ minetest.register_tool("mcl_fishing:fishing_rod", { on_place = fish, on_secondary_use = fish, sound = { breaks = "default_tool_breaks" }, + _mcl_uses = 65, }) minetest.register_craft({