From 0cbcf68c883e7f167d50ed65800596689ab4603f Mon Sep 17 00:00:00 2001 From: Crystal Spider Date: Sun, 15 Dec 2024 18:42:45 +0100 Subject: [PATCH] Fix Sonar issues. --- .../torch_hit/handler/AttackHandler.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/it/crystalnest/torch_hit/handler/AttackHandler.java b/common/src/main/java/it/crystalnest/torch_hit/handler/AttackHandler.java index f5a6297..9ffeff2 100644 --- a/common/src/main/java/it/crystalnest/torch_hit/handler/AttackHandler.java +++ b/common/src/main/java/it/crystalnest/torch_hit/handler/AttackHandler.java @@ -16,6 +16,7 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; +import net.minecraft.world.item.enchantment.Repairable; import org.jetbrains.annotations.Nullable; /** @@ -64,20 +65,22 @@ private static void attack(LivingEntity attacker, Entity target, ItemStack item, * @param directHit whether the hit is direct ({@code true}) or indirect ({@code false}). * @param seconds fire duration. */ - @SuppressWarnings("DataFlowIssue") private static void consumeItem(LivingEntity attacker, ItemStack item, boolean directHit, int seconds) { if (!(attacker instanceof Player player && player.isCreative())) { if (((isCandle(item) && ModConfig.getConsumeCandle()) || (isTorch(item) && ModConfig.getConsumeTorch())) && (directHit || ModConfig.getConsumeWithIndirectHits()) && (ModConfig.getConsumeWithoutFire() || seconds > 0)) { item.shrink(1); } else if ( ModConfig.getIndirectHitToolDamage() > 0 && - attacker.getMainHandItem().has(DataComponents.TOOL) && - attacker.getMainHandItem().has(DataComponents.REPAIRABLE) && - attacker.getMainHandItem().has(DataComponents.MAX_DAMAGE) && - attacker.getMainHandItem().get(DataComponents.REPAIRABLE).items().unwrapKey().isPresent() && - attacker.getMainHandItem().get(DataComponents.REPAIRABLE).items().unwrapKey().get().toString().equals(ItemTags.WOODEN_TOOL_MATERIALS.toString()) + attacker.getMainHandItem() instanceof ItemStack tool && + tool.has(DataComponents.TOOL) && + tool.has(DataComponents.REPAIRABLE) && + tool.has(DataComponents.MAX_DAMAGE) && + tool.get(DataComponents.MAX_DAMAGE) instanceof Integer durability && + tool.get(DataComponents.REPAIRABLE) instanceof Repairable repairable && + repairable.items().unwrapKey().isPresent() && + repairable.items().unwrapKey().get().toString().equals(ItemTags.WOODEN_TOOL_MATERIALS.toString()) ) { - attacker.getMainHandItem().hurtAndBreak((attacker.getMainHandItem().get(DataComponents.MAX_DAMAGE) * ModConfig.getIndirectHitToolDamage() + 99) / 100, attacker, EquipmentSlot.MAINHAND); + tool.hurtAndBreak((durability * ModConfig.getIndirectHitToolDamage() + 99) / 100, attacker, EquipmentSlot.MAINHAND); } } }