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?