Jump to content

[1.19.2] Adding custom attributes to vanilla entities?


NatePlays95

Recommended Posts

Ok, so, I understand that new entities have their attributes set by subscribing to EntityAttributeCreationEvent and all, including custom ones. I got that to work no problems.
However, say I have a new Attribute:

public static final DeferredRegister<Attribute> REGISTRY = DeferredRegister.create(ForgeRegistries.Keys.ATTRIBUTES, MhbMod.MODID);
public static final RegistryObject<Attribute> DEFENSE = registerAttr("defense", () -> new RangedAttribute("attribute.mhb.defense", 0, 0, 1024));
private static <T extends Attribute> RegistryObject<T> registerAttr(final String name, final Supplier<T> sup) { 
  	return REGISTRY.register(name, sup); 
}

And I want to add it to an existing entity type, like EntityType.PLAYER. This is what I had in mind:

@SubscribeEvent
public static void createAttributes(EntityAttributeCreationEvent event){
	event.put(EntityType.PLAYER, 
		AttributeSupplier.builder().add(DEFENSE.get(), 3).build()
	);
}

It seemed really clever, but EntityAttributeCreationEvent itself will thrown an error if any individual entity type has more than one AttributeSupplier.Builder mapped. I also considered accessing the Map inside EntityAttributeCreationEvent and replacing the existing player mapping with my own, but not only is that destructive, but the variable is private anyway so that's dead.

Clearly, this is not the right approach. What else could I do then? Should I add the Attribute later on on events like "entity joining the world / spawning" or "entity tick" so that any player entity can be selected?

Edited by NatePlays95
grammar
Link to comment
Share on other sites

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.