diff --git a/src/main/java/org/auioc/mcmod/harmonicench/enchantment/impl/BlessingEnchantment.java b/src/main/java/org/auioc/mcmod/harmonicench/enchantment/impl/BlessingEnchantment.java index 7aeaa8f..7fbe39f 100644 --- a/src/main/java/org/auioc/mcmod/harmonicench/enchantment/impl/BlessingEnchantment.java +++ b/src/main/java/org/auioc/mcmod/harmonicench/enchantment/impl/BlessingEnchantment.java @@ -19,20 +19,31 @@ package org.auioc.mcmod.harmonicench.enchantment.impl; +import net.minecraft.ChatFormatting; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.contents.TranslatableContents; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.damagesource.DamageTypes; import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ArmorItem; import net.minecraft.world.item.ArmorMaterials; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.item.enchantment.EnchantmentCategory; import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.item.enchantment.Enchantments; +import org.auioc.mcmod.arnicalib.base.collection.ListUtils; import org.auioc.mcmod.arnicalib.base.math.MathUtil; import org.auioc.mcmod.harmonicench.enchantment.HEEnchantments; import org.auioc.mcmod.harmoniclib.enchantment.api.HLEnchantment; import org.auioc.mcmod.harmoniclib.enchantment.api.IItemEnchantment; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.List; /** * 祝福 Blessing @@ -45,7 +56,7 @@ * @author WakelessSloth56 * @author Libellule505 */ -public class BlessingEnchantment extends HLEnchantment implements IItemEnchantment.Protection { +public class BlessingEnchantment extends HLEnchantment implements IItemEnchantment.Protection, IItemEnchantment.Tooltip { private static final EnchantmentCategory BLESSABLE_ARMOR = EnchantmentCategory.create( "BLESSABLE_ARMOR", @@ -66,11 +77,13 @@ public class BlessingEnchantment extends HLEnchantment implements IItemEnchantme } ); + private static final EquipmentSlot[] VALID_SLOTS = new EquipmentSlot[] { EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET }; + public BlessingEnchantment() { super( Enchantment.Rarity.RARE, BLESSABLE_ARMOR, - new EquipmentSlot[] { EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET }, + VALID_SLOTS, (o) -> o != Enchantments.MENDING && o != HEEnchantments.FORGING.get() ); } @@ -85,15 +98,40 @@ public int getMaxCost(int lvl) { return 51; } + private static int calcMagicProtection(int lvl, ItemStack itemStack) { + int N = EnchantmentHelper.getEnchantments(itemStack).values().stream().mapToInt((i) -> i).sum(); + double l = MathUtil.sigma(N, 1, (double i) -> 6.0D / i); + double r = MathUtil.sigma(lvl, 1, (double j) -> 1.0D / (5.0D * j - 4.0D)); + return (int) (l * r); + } + @Override public int getDamageProtection(int lvl, ItemStack itemStack, DamageSource source) { if (source.is(DamageTypes.MAGIC)) { - int N = EnchantmentHelper.getEnchantments(itemStack).values().stream().mapToInt((i) -> i).sum(); - double l = MathUtil.sigma(N, 1, (double i) -> 6.0D / i); - double r = MathUtil.sigma(lvl, 1, (double j) -> 1.0D / (5.0D * j - 4.0D)); - return (int) (l * r); + return calcMagicProtection(lvl, itemStack); } return 0; } + private static final List TOOLTIP_MODIFIER_KEYS = Arrays.stream(VALID_SLOTS).map((slot) -> "item.modifiers." + slot.getName()).toList(); + + @Override + public void onItemTooltip(int lvl, @NotNull ItemStack itemStack, @Nullable Player player, List lines, TooltipFlag flags) { + if (ItemStack.shouldShowInTooltip(itemStack.getHideFlags(), ItemStack.TooltipPart.MODIFIERS)) { // TODO arnicalib + int i = ListUtils.indexOf( + lines, + (l) -> l.getContents() instanceof TranslatableContents t && TOOLTIP_MODIFIER_KEYS.contains(t.getKey()) + ); + if (i >= 0) { + var text = Component.translatable( + "attribute.modifier.plus.0", + calcMagicProtection(lvl, itemStack), + Component.translatable("enchantment.harmonicench.blessing.tooltip") + ) + .withStyle(ChatFormatting.BLUE); + lines.add(i + 1, text); + } + } + } + } diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 0000000..be7169f --- /dev/null +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1,2 @@ +public net.minecraft.world.item.ItemStack getHideFlags()I +public net.minecraft.world.item.ItemStack shouldShowInTooltip(ILnet/minecraft/world/item/ItemStack$TooltipPart;)Z diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index acf527e..43b7018 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -14,6 +14,9 @@ description = '''${lib_mod_description}''' [[mixins]] config="harmoniclib.mixin.json" +[[accessTransformers]] +file="META-INF/accesstransformer.cfg" + [[dependencies.${lib_mod_id}]] modId="neoforge" type="required" diff --git a/src/main/resources/assets/harmonicench/lang/en_us.json b/src/main/resources/assets/harmonicench/lang/en_us.json index 0937324..9b20970 100644 --- a/src/main/resources/assets/harmonicench/lang/en_us.json +++ b/src/main/resources/assets/harmonicench/lang/en_us.json @@ -54,6 +54,7 @@ "enchantment.harmonicench.proficiency.proficiency": "(Proficiency: %s)", "enchantment.harmonicench.curse_of_sacrificing.vanished": "%1$s thirsty for sacrifice, then vanished after sucking the life out of %2$s", "enchantment.harmonicench.aim.looking_at": "Found monster %1$s, %3$s m from player %2$s", + "enchantment.harmonicench.blessing.tooltip": "Magic Resistance", "death.attack.curseOfRebelling": "%1$s was betrayed by %2$s", diff --git a/src/main/resources/assets/harmonicench/lang/zh_cn.json b/src/main/resources/assets/harmonicench/lang/zh_cn.json index 0ac91e3..5ca4002 100644 --- a/src/main/resources/assets/harmonicench/lang/zh_cn.json +++ b/src/main/resources/assets/harmonicench/lang/zh_cn.json @@ -54,6 +54,7 @@ "enchantment.harmonicench.proficiency.proficiency": "(熟练度:%s)", "enchantment.harmonicench.curse_of_sacrificing.vanished": "%1$s渴求祭品,在抽取了%2$s的生命之后消失了", "enchantment.harmonicench.aim.looking_at": "发现%1$s,距离%2$s %3$s 米", + "enchantment.harmonicench.blessing.tooltip": "魔法抗性", "death.attack.curseOfRebelling": "%1$s死于%2$s的背叛",