Class AttributeUtil
java.lang.Object
net.neoforged.neoforge.common.util.AttributeUtil
Utility code to support
IAttributeExtension
.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final record
Stores a single base modifier (determined byIAttributeExtension.getBaseId()
) and any other children non-base modifiers for the same attribute.private static class
State-tracking object used to merge attribute modifier tooltips inapplyTextFor(net.minecraft.world.item.ItemStack, java.util.function.Consumer<net.minecraft.network.chat.Component>, com.google.common.collect.Multimap<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>, net.minecraft.world.entity.ai.attributes.AttributeModifier>, net.neoforged.neoforge.common.util.AttributeTooltipContext)
. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Comparator
<net.minecraft.world.entity.ai.attributes.AttributeModifier> Comparator forAttributeModifier
.static final net.minecraft.resources.ResourceLocation
ID of the base modifier for Attack Damagestatic final net.minecraft.resources.ResourceLocation
ID of the base modifier for Attack Speedstatic final net.minecraft.resources.ResourceLocation
ID of the base modifier for Attack Rangestatic final net.minecraft.resources.ResourceLocation
ID used for attribute modifiers used to hold merged values whenNeoForgeMod.enableMergedAttributeTooltips()
is active.private static final org.apache.logging.log4j.Logger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addAttributeTooltips
(net.minecraft.world.item.ItemStack stack, Consumer<net.minecraft.network.chat.Component> tooltip, net.minecraft.world.item.component.TooltipDisplay tooltipDisplay, AttributeTooltipContext ctx) Checks if attribute modifier tooltips should show, and if they should, adds tooltips for all attribute modifiers present on an item stack to the stack's tooltip lines.static void
addPotionTooltip
(List<com.mojang.datafixers.util.Pair<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>, net.minecraft.world.entity.ai.attributes.AttributeModifier>> list, Consumer<net.minecraft.network.chat.Component> tooltips) Adds tooltip lines for the attribute modifiers contained in aPotionContents
.static void
applyModifierTooltips
(net.minecraft.world.item.ItemStack stack, Consumer<net.minecraft.network.chat.Component> tooltip, AttributeTooltipContext ctx) Applies the attribute modifier tooltips for all attribute modifiers present on the item stack.static void
applyTextFor
(net.minecraft.world.item.ItemStack stack, Consumer<net.minecraft.network.chat.Component> tooltip, com.google.common.collect.Multimap<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>, net.minecraft.world.entity.ai.attributes.AttributeModifier> modifierMap, AttributeTooltipContext ctx) Applies the text for the provided attribute modifiers to the tooltip for a given item stack.static com.google.common.collect.Multimap
<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>, net.minecraft.world.entity.ai.attributes.AttributeModifier> getSortedModifiers
(net.minecraft.world.item.ItemStack stack, net.minecraft.world.entity.EquipmentSlotGroup slot) Returns a sorted, mutableMultimap
containing all the attribute modifiers on an item stack for the given group.private static net.minecraft.network.chat.MutableComponent
Creates a mutable component starting with the char used to represent a drop-down list.static com.google.common.collect.Multimap
<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>, net.minecraft.world.entity.ai.attributes.AttributeModifier> Creates a sortedTreeMultimap
used to ensure a stable iteration order of item attribute modifiers.
-
Field Details
-
BASE_ATTACK_DAMAGE_ID
public static final net.minecraft.resources.ResourceLocation BASE_ATTACK_DAMAGE_IDID of the base modifier for Attack Damage -
BASE_ATTACK_SPEED_ID
public static final net.minecraft.resources.ResourceLocation BASE_ATTACK_SPEED_IDID of the base modifier for Attack Speed -
BASE_ENTITY_REACH_ID
public static final net.minecraft.resources.ResourceLocation BASE_ENTITY_REACH_IDID of the base modifier for Attack Range -
FAKE_MERGED_ID
public static final net.minecraft.resources.ResourceLocation FAKE_MERGED_IDID used for attribute modifiers used to hold merged values whenNeoForgeMod.enableMergedAttributeTooltips()
is active.Should not be used by any real attribute modifiers for gameplay purposes.
-
ATTRIBUTE_MODIFIER_COMPARATOR
public static final Comparator<net.minecraft.world.entity.ai.attributes.AttributeModifier> ATTRIBUTE_MODIFIER_COMPARATORComparator forAttributeModifier
. First compares by operation, then amount, then the ID. -
LOGGER
private static final org.apache.logging.log4j.Logger LOGGER
-
-
Constructor Details
-
AttributeUtil
public AttributeUtil()
-
-
Method Details
-
addAttributeTooltips
public static void addAttributeTooltips(net.minecraft.world.item.ItemStack stack, Consumer<net.minecraft.network.chat.Component> tooltip, net.minecraft.world.item.component.TooltipDisplay tooltipDisplay, AttributeTooltipContext ctx) Checks if attribute modifier tooltips should show, and if they should, adds tooltips for all attribute modifiers present on an item stack to the stack's tooltip lines.After the tooltip lines have been added, fires the
AddAttributeTooltipsEvent
to allow mods to add additional attribute-related lines.- Parameters:
tooltip
- A consumer to add the tooltip lines to.ctx
- The tooltip context.
-
applyModifierTooltips
public static void applyModifierTooltips(net.minecraft.world.item.ItemStack stack, Consumer<net.minecraft.network.chat.Component> tooltip, AttributeTooltipContext ctx) Applies the attribute modifier tooltips for all attribute modifiers present on the item stack.Before application, this method posts the
GatherSkippedAttributeTooltipsEvent
to determine which tooltips should be skipped.This method is also responsible for adding the modifier group category labels.
- Parameters:
tooltip
- A consumer to add the tooltip lines to.ctx
- The tooltip context.
-
applyTextFor
public static void applyTextFor(net.minecraft.world.item.ItemStack stack, Consumer<net.minecraft.network.chat.Component> tooltip, com.google.common.collect.Multimap<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>, net.minecraft.world.entity.ai.attributes.AttributeModifier> modifierMap, AttributeTooltipContext ctx) Applies the text for the provided attribute modifiers to the tooltip for a given item stack.This method will attempt to merge multiple modifiers for a single attribute into a single modifier if NeoForgeMod.enableMergedAttributeTooltips() was called.
- Parameters:
stack
- The item stack that owns the modifiers.tooltip
- The consumer to append tooltip components to.modifierMap
- A mutable map of modifiers to convert into tooltip lines.ctx
- The tooltip context.
-
addPotionTooltip
public static void addPotionTooltip(List<com.mojang.datafixers.util.Pair<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>, net.minecraft.world.entity.ai.attributes.AttributeModifier>> list, Consumer<net.minecraft.network.chat.Component> tooltips) Adds tooltip lines for the attribute modifiers contained in aPotionContents
.- Parameters:
list
- The list of attribute modifiers generated by callingMobEffect.createModifiers(int, java.util.function.BiConsumer<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>, net.minecraft.world.entity.ai.attributes.AttributeModifier>)
for each mob effect instance on the potion.tooltips
- The tooltip consumer to add lines to.
-
sortedMap
public static com.google.common.collect.Multimap<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>,net.minecraft.world.entity.ai.attributes.AttributeModifier> sortedMap()Creates a sortedTreeMultimap
used to ensure a stable iteration order of item attribute modifiers. -
getSortedModifiers
public static com.google.common.collect.Multimap<net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute>,net.minecraft.world.entity.ai.attributes.AttributeModifier> getSortedModifiers(net.minecraft.world.item.ItemStack stack, net.minecraft.world.entity.EquipmentSlotGroup slot) Returns a sorted, mutableMultimap
containing all the attribute modifiers on an item stack for the given group.This includes attribute modifiers from components (or default modifiers, if not present), enchantments, and the
ItemAttributeModifierEvent
.- Parameters:
stack
- The stack to query modifiers for.slot
- The slot group to query modifiers for.
-
listHeader
private static net.minecraft.network.chat.MutableComponent listHeader()Creates a mutable component starting with the char used to represent a drop-down list.
-