diff --git a/mods/ITEMS/mcl_shields/init.lua b/mods/ITEMS/mcl_shields/init.lua index 519021254..0c0abf593 100644 --- a/mods/ITEMS/mcl_shields/init.lua +++ b/mods/ITEMS/mcl_shields/init.lua @@ -69,36 +69,34 @@ minetest.register_entity("mcl_shields:shield_entity", { _shield_number = 2, on_step = function(self, dtime, moveresult) local player = self.object:get_attach() - if player then - local shield_texture = "mcl_shield_base_nopattern.png" - local i = self._shield_number - local item = wielded_item(player, i) - - if item ~= "mcl_shields:shield" and item ~= "mcl_shields:shield_enchanted" then - local itemstack = player:get_wielded_item() - if i == 1 then - itemstack = player:get_inventory():get_stack("offhand", 1) - end - local meta_texture = itemstack:get_meta():get_string("mcl_shields:shield_custom_pattern_texture") - if meta_texture ~= "" then - shield_texture = meta_texture - else - local color = minetest.registered_items[item]._shield_color - if color then - shield_texture = "mcl_shield_base_nopattern.png^(mcl_shield_pattern_base.png^[colorize:" .. color .. ")" - end - - end - end - - if shield_is_enchanted(player, i) then - shield_texture = shield_texture .. overlay - end - - self.object:set_properties({textures = {shield_texture}}) - else + if not player then self.object:remove() + return end + local shield_texture = "mcl_shield_base_nopattern.png" + local i = self._shield_number + local item = wielded_item(player, i) + + if item ~= "mcl_shields:shield" and item ~= "mcl_shields:shield_enchanted" then + local itemstack = player:get_wielded_item() + if i == 1 then + itemstack = player:get_inventory():get_stack("offhand", 1) + end + local meta_texture = itemstack:get_meta():get_string("mcl_shields:shield_custom_pattern_texture") + if meta_texture ~= "" then + shield_texture = meta_texture + else + local color = minetest.registered_items[item]._shield_color + if color then + shield_texture = "mcl_shield_base_nopattern.png^(mcl_shield_pattern_base.png^[colorize:" .. color .. ")" + end + end + end + + if shield_is_enchanted(player, i) then + shield_texture = shield_texture .. overlay + end + self.object:set_properties({textures = {shield_texture}}) end, }) @@ -109,43 +107,50 @@ end function mcl_shields.is_blocking(obj) if not obj:is_player() then return end local blocking = mcl_shields.players[obj].blocking - if blocking > 0 then - local shieldstack = obj:get_wielded_item() - if blocking == 1 then - shieldstack = obj:get_inventory():get_stack("offhand", 1) - end - return blocking, shieldstack + if blocking <= 0 then + return end + + local shieldstack = obj:get_wielded_item() + if blocking == 1 then + shieldstack = obj:get_inventory():get_stack("offhand", 1) + end + return blocking, shieldstack end mcl_damage.register_modifier(function(obj, damage, reason) local type = reason.type local damager = reason.direct local blocking, shieldstack = mcl_shields.is_blocking(obj) - if obj:is_player() and blocking and mcl_shields.types[type] and damager then - local entity = damager:get_luaentity() - if entity and (type == "arrow" or type == "generic") then - damager = entity._shooter - end - if vector.dot(obj:get_look_dir(), vector.subtract(damager:get_pos(), obj:get_pos())) >= 0 then - local durability = 336 - local unbreaking = mcl_enchanting.get_enchantment(shieldstack, mcl_shields.enchantments[2]) - if unbreaking > 0 then - durability = durability * (unbreaking + 1) - end - if not minetest.is_creative_enabled(obj:get_player_name()) and damage >= 3 then - shieldstack:add_wear(65535 / durability) - if blocking == 2 then - obj:set_wielded_item(shieldstack) - else - obj:get_inventory():set_stack("offhand", 1, shieldstack) - mcl_inventory.update_inventory_formspec(obj) - end - end - minetest.sound_play({name = "mcl_block"}) - return 0 + if not (obj:is_player() and blocking and mcl_shields.types[type] and damager) then + return + end + + local entity = damager:get_luaentity() + if entity and (type == "arrow" or type == "generic") then + damager = entity._shooter + end + + if vector.dot(obj:get_look_dir(), vector.subtract(damager:get_pos(), obj:get_pos())) < 0 then + return + end + + local durability = 336 + local unbreaking = mcl_enchanting.get_enchantment(shieldstack, mcl_shields.enchantments[2]) + if unbreaking > 0 then + durability = durability * (unbreaking + 1) + end + if not minetest.is_creative_enabled(obj:get_player_name()) and damage >= 3 then + shieldstack:add_wear(65535 / durability) + if blocking == 2 then + obj:set_wielded_item(shieldstack) + else + obj:get_inventory():set_stack("offhand", 1, shieldstack) + mcl_inventory.update_inventory_formspec(obj) end end + minetest.sound_play({name = "mcl_block"}) + return 0 end) local function modify_shield(player, vpos, vrot, i) @@ -175,20 +180,23 @@ local function set_shield(player, block, i) end local shield = mcl_shields.players[player].shields[i] if not shield then return end + local luaentity = shield:get_luaentity() if not luaentity then return end + luaentity._blocking = block end local function set_interact(player, interact) local player_name = player:get_player_name() local privs = minetest.get_player_privs(player_name) - if privs.interact ~= interact then - local meta = player:get_meta() - if meta:get_int("ineract_revoked") ~= 1 then - privs.interact = interact - minetest.set_player_privs(player_name, privs) - end + if privs.interact == interact then + return + end + local meta = player:get_meta() + if meta:get_int("ineract_revoked") ~= 1 then + privs.interact = interact + minetest.set_player_privs(player_name, privs) end end @@ -229,39 +237,41 @@ end local function handle_blocking(player) local player_shield = mcl_shields.players[player] local rmb = player:get_player_control().RMB - if rmb then - 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 + if not rmb then + player_shield.blocking = 0 + return + end - local pos = player:get_pos() - if shield_in_hand then - if not_blocking then - minetest.after(0.25, function() - if (not_blocking or not shield_in_offhand) and shield_in_hand and rmb then - player_shield.blocking = 2 - set_shield(player, true, 2) - end - end) - elseif not shield_in_offhand then - player_shield.blocking = 2 - end - elseif shield_in_offhand then - local offhand_can_block = (wielded_item(player) == "" or not mcl_util.get_pointed_thing(player, true)) - if offhand_can_block then - if not_blocking then - minetest.after(0.25, function() - 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 - end) - elseif not shield_in_hand then - player_shield.blocking = 1 + 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 shield_in_hand then + if not_blocking then + minetest.after(0.25, function() + if (not_blocking or not shield_in_offhand) and shield_in_hand and rmb then + player_shield.blocking = 2 + set_shield(player, true, 2) end - end - else - player_shield.blocking = 0 + end) + elseif not shield_in_offhand then + player_shield.blocking = 2 + end + elseif shield_in_offhand then + local offhand_can_block = (wielded_item(player) == "" or not mcl_util.get_pointed_thing(player, true)) + if not offhand_can_block then + return + end + if not_blocking then + minetest.after(0.25, function() + 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 + end) + elseif not shield_in_hand then + player_shield.blocking = 1 end else player_shield.blocking = 0