Code example for adding attributes to items

Started by Kalmeira on

Topic category: Help with Minecraft modding (Java Edition)

Joined Aug 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Code example for adding attributes to items

Since mcreator does not yet support adding attributes to items, I am trying to figure out how to do it with code. If I could get an example, I could probably figure it out from there. To further clarify, I don't mean spawning an item in with a command. 

Also, I have tried using the attributes plugin (nerdy version) but could not get it to work with items. There is virtually no documentation for it and the plugin itself may be not working, hence why I am seeking help with code.

Joined Dec 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
If you add this chunk of…
Sun, 09/22/2024 - 22:29

If you add this chunk of code to (ITEMNAME)Item.java It will add whatever attribute you set it as, you can also change the slot the item has to be put in for the attributes to work.

https://pastebin.com/0sKL1FUs

Joined Aug 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thank you for your help.
Mon, 09/23/2024 - 18:29

Thank you for your help.

Joined Aug 2017
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@Nol_Inuse I couldn't get it…
Thu, 02/20/2025 - 01:31

@Nol_Inuse

I couldn't get it to work. I did add the imports and the code seemingly correctly. Errors and code:

error: method does not override or implement a method from a supertype @Override

error: incompatible types: EquipmentSlot cannot be converted to ItemStack builder.putAll(super.getDefaultAttributeModifiers(equipmentSlot));

error: cannot find symbol builder.put(Attributes.ENTITY_INTERACTION_RANGE, new AttributeModifier(UUID.fromString("1218abdb-72e3-44d6-b690-2867df6199c9"), "Item modifier", 2.0, AttributeModifier.Operation.ADDITION));
^
symbol: variable ADDITION
location: class Operation

error: incompatible types: EquipmentSlot cannot be converted to ItemStack return super.getDefaultAttributeModifiers(equipmentSlot);
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
4 errors

 

Code (at the bottom of the .java class):

@Override
public Multimap < Attribute, AttributeModifier > getDefaultAttributeModifiers(EquipmentSlot equipmentSlot) {
if (equipmentSlot == EquipmentSlot.MAINHAND) {
ImmutableMultimap.Builder < Attribute, AttributeModifier > builder = ImmutableMultimap.builder();
builder.putAll(super.getDefaultAttributeModifiers(equipmentSlot));
// GENERATE A NEW UUID FROM https://www.uuidtools.com/minecraft HAVING THE SAME UUID ON MORE THAN ONE ITEM WILL MAKE PROBLEMS
builder.put(Attributes.ENTITY_INTERACTION_RANGE, new AttributeModifier(UUID.fromString("1218abdb-72e3-44d6-b690-2867df6199c9"), "Item modifier", 2.0, AttributeModifier.Operation.ADDITION));
return builder.build();
}
return super.getDefaultAttributeModifiers(equipmentSlot);
}