Class GetEnchantmentLevelEvent

java.lang.Object
net.neoforged.bus.api.Event
net.neoforged.neoforge.event.enchanting.GetEnchantmentLevelEvent

public class GetEnchantmentLevelEvent extends net.neoforged.bus.api.Event
This event is fired whenever the enchantment level of a particular item is requested for gameplay purposes.
It is called from
invalid reference
IItemStackExtension#getEnchantmentLevel(Enchantment)
and
invalid reference
IItemStackExtension#getAllEnchantments()
.

It is not fired for interactions with NBT, which means these changes will not reflect in the item tooltip.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final net.minecraft.world.item.enchantment.ItemEnchantments.Mutable
     
    protected final net.minecraft.core.HolderLookup.RegistryLookup<net.minecraft.world.item.enchantment.Enchantment>
     
    protected final net.minecraft.world.item.ItemStack
     
    protected final @Nullable net.minecraft.core.Holder<net.minecraft.world.item.enchantment.Enchantment>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    GetEnchantmentLevelEvent(net.minecraft.world.item.ItemStack stack, net.minecraft.world.item.enchantment.ItemEnchantments.Mutable enchantments, @Nullable net.minecraft.core.Holder<net.minecraft.world.item.enchantment.Enchantment> targetEnchant, net.minecraft.core.HolderLookup.RegistryLookup<net.minecraft.world.item.enchantment.Enchantment> lookup)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    net.minecraft.world.item.enchantment.ItemEnchantments.Mutable
    Returns the mutable enchantment->level map.
    Optional<net.minecraft.core.Holder.Reference<net.minecraft.world.item.enchantment.Enchantment>>
    getHolder(net.minecraft.resources.ResourceKey<net.minecraft.world.item.enchantment.Enchantment> key)
    Attempts to resolve a Holder.Reference for a target enchantment.
    net.minecraft.core.HolderLookup.RegistryLookup<net.minecraft.world.item.enchantment.Enchantment>
    Returns the underlying registry lookup, which can be used to access enchantment Holders.
    net.minecraft.world.item.ItemStack
    Returns the item stack that is being queried against.
    @Nullable net.minecraft.core.Holder<net.minecraft.world.item.enchantment.Enchantment>
    This method returns the specific enchantment being queried from
    invalid reference
    IItemStackExtension#getEnchantmentLevel(Enchantment)
    .
    boolean
    isTargetting(net.minecraft.core.Holder<net.minecraft.world.item.enchantment.Enchantment> ench)
    Helper method around getTargetEnchant() that checks if the target is the specified enchantment, or if the target is null.
    boolean
    isTargetting(net.minecraft.resources.ResourceKey<net.minecraft.world.item.enchantment.Enchantment> ench)
    Helper method around getTargetEnchant() that checks if the target is the specified enchantment, or if the target is null.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • stack

      protected final net.minecraft.world.item.ItemStack stack
    • enchantments

      protected final net.minecraft.world.item.enchantment.ItemEnchantments.Mutable enchantments
    • targetEnchant

      @Nullable protected final @Nullable net.minecraft.core.Holder<net.minecraft.world.item.enchantment.Enchantment> targetEnchant
    • lookup

      protected final net.minecraft.core.HolderLookup.RegistryLookup<net.minecraft.world.item.enchantment.Enchantment> lookup
  • Constructor Details

    • GetEnchantmentLevelEvent

      public GetEnchantmentLevelEvent(net.minecraft.world.item.ItemStack stack, net.minecraft.world.item.enchantment.ItemEnchantments.Mutable enchantments, @Nullable @Nullable net.minecraft.core.Holder<net.minecraft.world.item.enchantment.Enchantment> targetEnchant, net.minecraft.core.HolderLookup.RegistryLookup<net.minecraft.world.item.enchantment.Enchantment> lookup)
  • Method Details

    • getStack

      public net.minecraft.world.item.ItemStack getStack()
      Returns the item stack that is being queried against.
    • getEnchantments

      public net.minecraft.world.item.enchantment.ItemEnchantments.Mutable getEnchantments()
      Returns the mutable enchantment->level map.
    • getTargetEnchant

      @Nullable public @Nullable net.minecraft.core.Holder<net.minecraft.world.item.enchantment.Enchantment> getTargetEnchant()
      This method returns the specific enchantment being queried from
      invalid reference
      IItemStackExtension#getEnchantmentLevel(Enchantment)
      .

      If this is value is present, you only need to adjust the level of that enchantment.

      If this value is null, then the event was fired from

      invalid reference
      IItemStackExtension#getAllEnchantments()
      and all enchantments should be populated.
      Returns:
      The specific enchantment being queried, or null, if all enchantments are being requested.
    • isTargetting

      public boolean isTargetting(net.minecraft.core.Holder<net.minecraft.world.item.enchantment.Enchantment> ench)
      Helper method around getTargetEnchant() that checks if the target is the specified enchantment, or if the target is null.
      Parameters:
      ench - The enchantment to check.
      Returns:
      If modifications to the passed enchantment are relevant for this event.
      See Also:
    • isTargetting

      public boolean isTargetting(net.minecraft.resources.ResourceKey<net.minecraft.world.item.enchantment.Enchantment> ench)
      Helper method around getTargetEnchant() that checks if the target is the specified enchantment, or if the target is null.
      Parameters:
      ench - The enchantment to check.
      Returns:
      If modifications to the passed enchantment are relevant for this event.
      See Also:
    • getHolder

      public Optional<net.minecraft.core.Holder.Reference<net.minecraft.world.item.enchantment.Enchantment>> getHolder(net.minecraft.resources.ResourceKey<net.minecraft.world.item.enchantment.Enchantment> key)
      Attempts to resolve a Holder.Reference for a target enchantment. Since enchantments are data, they are not guaranteed to exist.
      Parameters:
      key - The target resource key
      Returns:
      If the holder was available, an Optional containing it; otherwise an empty Optional.
    • getLookup

      public net.minecraft.core.HolderLookup.RegistryLookup<net.minecraft.world.item.enchantment.Enchantment> getLookup()
      Returns the underlying registry lookup, which can be used to access enchantment Holders.
      Returns:
      the underlying registry lookup, which can be used to access enchantment Holders