From f86a641dfa82039d206293a76a16fa15dd39815e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikita=20Wi=C5=9Bniewski?= Date: Tue, 17 Sep 2024 12:22:05 +0700 Subject: [PATCH] Improve shield block code and unhardcode offhand group --- mods/ITEMS/mcl_bows/bow.lua | 4 ++-- mods/ITEMS/mcl_bows/crossbow.lua | 6 +++--- mods/ITEMS/mcl_shields/init.lua | 33 ++++++++++++++++---------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/mods/ITEMS/mcl_bows/bow.lua b/mods/ITEMS/mcl_bows/bow.lua index 9f381f501..34784ab07 100644 --- a/mods/ITEMS/mcl_bows/bow.lua +++ b/mods/ITEMS/mcl_bows/bow.lua @@ -168,7 +168,7 @@ S("The speed and damage of the arrow increases the longer you charge. The regula itemstack:get_meta():set_string("active", "true") return itemstack end, - groups = {weapon=1,weapon_ranged=1,bow=1,enchantability=1}, + groups = {weapon=1,weapon_ranged=1,bow=1,cannot_block=1,enchantability=1}, _mcl_uses = 385, }) @@ -216,7 +216,7 @@ for level=0, 2 do wield_scale = mcl_vars.tool_wield_scale, stack_max = 1, range = 0, -- Pointing range to 0 to prevent punching with bow :D - groups = {not_in_creative_inventory=1, not_in_craft_guide=1, bow=1, enchantability=1}, + groups = {not_in_creative_inventory=1, not_in_craft_guide=1, bow=1, cannot_block=1, enchantability=1}, -- Trick to disable digging as well on_use = function() return end, on_drop = function(itemstack, dropper, pos) diff --git a/mods/ITEMS/mcl_bows/crossbow.lua b/mods/ITEMS/mcl_bows/crossbow.lua index c1cb7f8be..b6dc31dd0 100644 --- a/mods/ITEMS/mcl_bows/crossbow.lua +++ b/mods/ITEMS/mcl_bows/crossbow.lua @@ -158,7 +158,7 @@ S("The speed and damage of the arrow increases the longer you charge. The regula itemstack:get_meta():set_string("active", "true") return itemstack end, - groups = {weapon=1,weapon_ranged=1,crossbow=1,enchantability=1}, + groups = {weapon=1,weapon_ranged=1,crossbow=1,cannot_block=1,enchantability=1}, _mcl_uses = 326, }) @@ -193,7 +193,7 @@ S("The speed and damage of the arrow increases the longer you charge. The regula itemstack:get_meta():set_string("active", "true") return itemstack end, - groups = {weapon=1,weapon_ranged=1,crossbow=1,enchantability=1,not_in_creative_inventory=1}, + groups = {weapon=1,weapon_ranged=1,crossbow=1,cannot_block=1,enchantability=1,not_in_creative_inventory=1}, _mcl_uses = 326, }) @@ -238,7 +238,7 @@ for level=0, 2 do wield_scale = mcl_vars.tool_wield_scale, stack_max = 1, range = 0, -- Pointing range to 0 to prevent punching with bow :D - groups = {not_in_creative_inventory=1, not_in_craft_guide=1, bow=1, enchantability=1}, + groups = {not_in_creative_inventory=1, not_in_craft_guide=1, cannot_block=1, bow=1, enchantability=1}, -- Trick to disable digging as well on_use = function() return end, on_drop = function(itemstack, dropper, pos) diff --git a/mods/ITEMS/mcl_shields/init.lua b/mods/ITEMS/mcl_shields/init.lua index b3323bd3f..e1b790049 100644 --- a/mods/ITEMS/mcl_shields/init.lua +++ b/mods/ITEMS/mcl_shields/init.lua @@ -270,7 +270,7 @@ local function remove_shield_entity(player, i) end local function is_rmb_conflicting_node(nodename) - nodedef = minetest.registered_nodes[nodename] or {} + local nodedef = minetest.registered_nodes[nodename] or {} return nodedef.on_rightclick end @@ -282,11 +282,22 @@ local function handle_blocking(player) return end + local pointed_thing = mcl_util.get_pointed_thing(player, true) + local wielded_stack = player:get_wielded_item() + local shield_in_offhand = mcl_shields.wielding_shield(player, 1) local shield_in_hand = mcl_shields.wielding_shield(player) local not_blocking = player_shield.blocking == 0 - local pos = player:get_pos() + if pointed_thing and pointed_thing.type == "node" then + local pointed_node = minetest.get_node(pointed_thing.under) + if minetest.get_item_group(pointed_node.name, "container") > 1 + or is_rmb_conflicting_node(pointed_node.name) + or wielded_stack:get_definition().type == "node" then + return + end + end + if shield_in_hand then if not_blocking then minetest.after(0.05, function() @@ -299,27 +310,15 @@ local function handle_blocking(player) player_shield.blocking = 2 end elseif shield_in_offhand then - local pointed_thing = mcl_util.get_pointed_thing(player, true) - local wielded_stack = player:get_wielded_item() - local offhand_can_block = (minetest.get_item_group(wielded_item(player), "bow") ~= 1 - and minetest.get_item_group(wielded_item(player), "crossbow") ~= 1) - - if pointed_thing and pointed_thing.type == "node" then - local pointed_node = minetest.get_node(pointed_thing.under) - if minetest.get_item_group(pointed_node.name, "container") > 1 - or is_rmb_conflicting_node(pointed_node.name) - or wielded_stack:get_definition().type == "node" - then - return - end - end + local offhand_can_block = minetest.get_item_group(wielded_item(player), "cannot_block") ~= 1 if not offhand_can_block then return end if not_blocking then minetest.after(0.05, function() - if (not_blocking or not shield_in_hand) and shield_in_offhand and rmb and offhand_can_block then + if (not_blocking or not shield_in_hand) and shield_in_offhand + and rmb and offhand_can_block then player_shield.blocking = 1 set_shield(player, true, 1) end