Skip to content

Conversation

@ChiefArug
Copy link
Member

This extends the ModifyDefaultComponentsEvent to add methods to modify existing components using a UnaryOperator.
This is intended for components that are a collection of things, such as ItemAttributeModifiers, as outright replacing them will discard other peoples changes.

This PR also includes moving ItemAttributeModifiersBuilder from the ItemAttributeModifierEvent to its own class as it is useful for modifying default components as well.

Part of what makes this PR work is making ModifyDefaultComponentsEvent use a custom subclass of DataComponentPatch.Builder that has a base set of component that can be modified by a few new methods added. This is a non breaking change (despite the change in the non-erased signature) as the consumers are still passed a subclass of what they expect, so it would be great if this can be backported as well.

Example usage (can be pasted into NeoForgeMod's constructor):

        modEventBus.addListener((ModifyDefaultComponentsEvent event) -> {
            event.modify(Items.GOLDEN_SWORD, builder -> {
                builder.modify(DataComponents.ATTRIBUTE_MODIFIERS, o -> {
                    var attributes = new ItemAttributeModifiersBuilder(o);
                    attributes.addModifier(Attributes.MAX_HEALTH,
                            new AttributeModifier(Identifier.fromNamespaceAndPath(MOD_ID, "more_health_pls"), 10, AttributeModifier.Operation.ADD_VALUE),
                            EquipmentSlotGroup.HAND);
                    return attributes.build();
                });
            });
        });

        modEventBus.addListener((ModifyDefaultComponentsEvent event) -> {
            event.modify(Items.GOLDEN_SWORD, builder -> {
                builder.modify(DataComponents.ATTRIBUTE_MODIFIERS, o -> {
                    var attributes = new ItemAttributeModifiersBuilder(o);
                    attributes.replaceModifier(Attributes.ATTACK_DAMAGE,
                            new AttributeModifier(Item.BASE_ATTACK_DAMAGE_ID, 7, AttributeModifier.Operation.ADD_VALUE),
                            EquipmentSlotGroup.MAINHAND);
                    return attributes.build();
                });
            });
        });

Result:
image

@neoforged-automation neoforged-automation bot added the 1.21.11 Targeted at Minecraft 1.21.11 label Jan 21, 2026
@neoforged-pr-publishing
Copy link

  • Publish PR to GitHub Packages

@Technici4n
Copy link
Member

Can't we patch in new methods to the vanilla builders instead?

@ChiefArug
Copy link
Member Author

I considered that for the data component patch builder, but the patch surface ended up being kind of large for the extra parameter + some other changes i needed to make. However I did change slightly how I was doing so that it wouldn't be as bad now.

For the item attributes that class is just moved and a javadoc updated to copy a warning over. I haven't looked into how easy it would be to patch the vanilla one instead.

@ChiefArug
Copy link
Member Author

After attempting to merge both builders into their respective vanilla classes neither is very feasible.

ItemAttributeModifiersBuilder uses a different backing data structure to back it to be able to do more than the vanilla one. Additionally I do not think renaming it to ExtendedItemAttributeModifiersBuilder like suggested in the discord is a very good idea due to how lengthy and confusing the name gets. The javadoc covers how its different from the vanilla builder.

ModifyDefaultComponentsEvent.ModifyingBuilder (renamed from just .Builder) is an extension to the vanilla one that allows basing it off an existing set of components. Implementing this into the vanilla one would require even more 'gotchas' added to the javadocs than there already are due to the class having two modes (the vanilla one and the neo added one).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.21.11 Targeted at Minecraft 1.21.11

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants