Class ModifyDefaultComponentsEvent
- All Implemented Interfaces:
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, componentTypes) -> componentTypes.contains(DataComponents.FIRE_RESISTANT), builder -> builder
.set(DataComponents.ENCHANTMENT_GLINT_OVERRIDE, true)); // Make all fire-resistant items have a glint
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA alternate version ofDataComponentInitializers.Initializerwhich receives the item whose components being initialized.static interfaceEvaluates a condition on anItemand its associatedDataComponentGetterprior to binding components. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<Item, ModifyDefaultComponentsEvent.Initializer> private final List<com.mojang.datafixers.util.Pair<ModifyDefaultComponentsEvent.ItemWithComponentsPredicate, ModifyDefaultComponentsEvent.Initializer>> -
Constructor Summary
ConstructorsConstructorDescriptionModifyDefaultComponentsEvent(Map<Item, ModifyDefaultComponentsEvent.Initializer> modifiersByItem, List<com.mojang.datafixers.util.Pair<ModifyDefaultComponentsEvent.ItemWithComponentsPredicate, ModifyDefaultComponentsEvent.Initializer>> modifiersByPredicate) -
Method Summary
Modifier and TypeMethodDescriptionvoidmodify(ItemLike item, Consumer<DataComponentMap.Builder> patch) Deprecated, for removal: This API element is subject to removal in a future version.voidmodify(ItemLike item, ModifyDefaultComponentsEvent.Initializer patch) Patches the default components of the given item.voidmodifyMatching(ModifyDefaultComponentsEvent.ItemWithComponentsPredicate predicate, Consumer<DataComponentMap.Builder> patch) Deprecated, for removal: This API element is subject to removal in a future version.UsemodifyMatching(ItemWithComponentsPredicate, Initializer)instead, which provides more contextual information.voidmodifyMatching(ModifyDefaultComponentsEvent.ItemWithComponentsPredicate predicate, ModifyDefaultComponentsEvent.Initializer patch) Patches the default components of all items matching the given predicate based on item and/or its currently applied default components.
-
Field Details
-
modifiersByItem
-
modifiersByPredicate
private final List<com.mojang.datafixers.util.Pair<ModifyDefaultComponentsEvent.ItemWithComponentsPredicate, ModifyDefaultComponentsEvent.Initializer>> modifiersByPredicate
-
-
Constructor Details
-
ModifyDefaultComponentsEvent
@Internal public ModifyDefaultComponentsEvent(Map<Item, ModifyDefaultComponentsEvent.Initializer> modifiersByItem, List<com.mojang.datafixers.util.Pair<ModifyDefaultComponentsEvent.ItemWithComponentsPredicate, ModifyDefaultComponentsEvent.Initializer>> modifiersByPredicate)
-
-
Method Details
-
modify
Patches the default components of the given item.- Parameters:
item- the item to modify the default components forpatch- the patch to apply
-
modify
@Deprecated(forRemoval=true, since="26.1.2") public void modify(ItemLike item, Consumer<DataComponentMap.Builder> patch) Deprecated, for removal: This API element is subject to removal in a future version.Usemodify(ItemLike, Initializer)instead, which provides more contextual information.Patches the default components of the given item.- Parameters:
item- the item to modify the default components forpatch- the patch to apply
-
modifyMatching
public void modifyMatching(ModifyDefaultComponentsEvent.ItemWithComponentsPredicate predicate, ModifyDefaultComponentsEvent.Initializer patch) Patches the default components of all items matching the given predicate based on item and/or its currently applied default components.
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 and its current default components filterpatch- the patch to apply
-
modifyMatching
@Deprecated(forRemoval=true, since="26.1.2") public void modifyMatching(ModifyDefaultComponentsEvent.ItemWithComponentsPredicate predicate, Consumer<DataComponentMap.Builder> patch) Deprecated, for removal: This API element is subject to removal in a future version.UsemodifyMatching(ItemWithComponentsPredicate, Initializer)instead, which provides more contextual information.Patches the default components of all items matching the given predicate based on item and/or its currently applied default components.
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 and its current default components filterpatch- the patch to apply
-
modify(ItemLike, Initializer)instead, which provides more contextual information.