Class AttributeUtil

java.lang.Object
net.neoforged.neoforge.common.util.AttributeUtil

public class AttributeUtil extends Object
Utility code to support IAttributeExtension.
  • Field Details

    • BASE_ATTACK_DAMAGE_ID

      public static final net.minecraft.resources.ResourceLocation BASE_ATTACK_DAMAGE_ID
      ID of the base modifier for Attack Damage
    • BASE_ATTACK_SPEED_ID

      public static final net.minecraft.resources.ResourceLocation BASE_ATTACK_SPEED_ID
      ID of the base modifier for Attack Speed
    • BASE_ENTITY_REACH_ID

      public static final net.minecraft.resources.ResourceLocation BASE_ENTITY_REACH_ID
      ID of the base modifier for Attack Range
    • FAKE_MERGED_ID

      public static final net.minecraft.resources.ResourceLocation FAKE_MERGED_ID
      ID used for attribute modifiers used to hold merged values when NeoForgeMod.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_COMPARATOR
      Comparator for AttributeModifier. 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 a PotionContents.
      Parameters:
      list - The list of attribute modifiers generated by calling MobEffect.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 sorted TreeMultimap 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, mutable Multimap 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.