Skip to content

Commit

Permalink
feat(ench): blessing magic resistance tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Feb 21, 2024
1 parent 11879ed commit 0b31a8b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <b>祝福 Blessing</b>
Expand All @@ -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",
Expand All @@ -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()
);
}
Expand All @@ -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<String> 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<Component> 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);
}
}
}

}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/harmonicench/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/harmonicench/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -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的背叛",

Expand Down

0 comments on commit 0b31a8b

Please sign in to comment.