Class ModifyDefaultComponentsEvent

java.lang.Object
net.neoforged.bus.api.Event
net.neoforged.neoforge.event.ModifyDefaultComponentsEvent
All Implemented Interfaces:
net.neoforged.fml.event.IModBusEvent

public final class ModifyDefaultComponentsEvent extends net.neoforged.bus.api.Event implements net.neoforged.fml.event.IModBusEvent
The event used to modify the default components of an item.
This event is fired on the mod event bus.

Example usage:

import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.Items;

public void modifyComponents(ModifyDefaultComponentsEvent event) {
   event.modify(Items.MELON_SEEDS, builder -> builder
        .set(DataComponents.MAX_STACK_SIZE, 16)); // Stack melon seeds to at most 16 items

   event.modify(Items.APPLE, builder -> builder
        .remove(DataComponents.FOOD)); // Remove the ability of eating apples
}

// Lowest priority listener
public void modifyComponentsLow(ModifyDefaultComponentsEvent event) {
   event.modifyMatching(item -> item.components().has(DataComponents.FIRE_RESISTANT), builder -> builder
        .set(DataComponents.ENCHANTMENT_GLINT_OVERRIDE, true)); // Make all fire resistant items have a glint
}
  • Constructor Details

    • ModifyDefaultComponentsEvent

      @Internal public ModifyDefaultComponentsEvent()
  • Method Details

    • modify

      public void modify(ItemLike item, Consumer<DataComponentPatch.Builder> patch)
      Patches the default components of the given item.
      Parameters:
      item - the item to modify the default components for
      patch - the patch to apply
    • modifyMatching

      public void modifyMatching(Predicate<? super Item> predicate, Consumer<DataComponentPatch.Builder> patch)
      Patches the default components of all items matching the given predicate.

      If this method is used to modify components based on the item's current default components, the event listener should use the lowest priority so that other mods' modifications are already applied.

      Parameters:
      predicate - the item filter
      patch - the patch to apply
    • getAllItems

      public Stream<Item> getAllItems()
      Returns all registered items.
      Returns:
      all registered items