From f6b9fb97e364576785da3a0dbfd6743b17952655 Mon Sep 17 00:00:00 2001 From: FlorianMichael Date: Thu, 26 Sep 2024 00:38:36 +0200 Subject: [PATCH] Fix sharpness enchantment tooltip calculation in <= 1.20.6 Closes https://github.com/ViaVersion/ViaFabricPlus/issues/559 --- .../fixes/minecraft/item/MixinItemStack.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java index 5255fbe7..03c4ad67 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java @@ -25,7 +25,10 @@ import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; import de.florianmichael.viafabricplus.util.ItemUtil; import net.minecraft.component.ComponentType; import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.ItemEnchantmentsComponent; import net.minecraft.enchantment.Enchantment; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.enchantment.Enchantments; import net.minecraft.entity.attribute.EntityAttribute; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; @@ -35,9 +38,7 @@ import net.minecraft.item.tooltip.TooltipType; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtList; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.RegistryKeys; -import net.minecraft.registry.RegistryWrapper; +import net.minecraft.registry.*; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.text.Text; import net.minecraft.util.math.MathHelper; @@ -78,11 +79,27 @@ public abstract class MixinItemStack { } @Redirect(method = "appendAttributeModifierTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;getAttributeBaseValue(Lnet/minecraft/registry/entry/RegistryEntry;)D", ordinal = 0)) - private double removeAttackDamageValueFromCalculation(PlayerEntity instance, RegistryEntry registryEntry) { + private double fixAttackDamageCalculation(PlayerEntity instance, RegistryEntry registryEntry) { + double value = 0.0; + if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_20_5)) { + final ItemEnchantmentsComponent enchantments = EnchantmentHelper.getEnchantments((ItemStack) (Object) this); + for (RegistryEntry enchantment : enchantments.getEnchantments()) { + if (enchantment.matchesKey(Enchantments.SHARPNESS)) { + final int level = enchantments.getLevel(enchantment); + if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) { + value = level * 1.25F; + } else { + value = 1.0F + (float) Math.max(0, level - 1) * 0.5F; + } + break; + } + } + } + if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) { - return 0; + return value; } else { - return instance.getAttributeBaseValue(registryEntry); + return instance.getAttributeBaseValue(registryEntry) + value; } }