Skip to content

Commit

Permalink
Fix sharpness enchantment tooltip calculation in <= 1.20.6
Browse files Browse the repository at this point in the history
Closes #559
  • Loading branch information
FlorianMichael committed Sep 25, 2024
1 parent 6925f86 commit f6b9fb9
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
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;
Expand All @@ -35,9 +38,7 @@
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;
Expand Down Expand Up @@ -78,11 +79,27 @@ private <T extends TooltipAppender> void replaceEnchantmentTooltip(ComponentType
}

@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<EntityAttribute> registryEntry) {
private double fixAttackDamageCalculation(PlayerEntity instance, RegistryEntry<EntityAttribute> registryEntry) {
double value = 0.0;
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_20_5)) {
final ItemEnchantmentsComponent enchantments = EnchantmentHelper.getEnchantments((ItemStack) (Object) this);
for (RegistryEntry<Enchantment> 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;
}
}

Expand Down

0 comments on commit f6b9fb9

Please sign in to comment.