Jump to content

[1.16.x] Get Entity from Wildcard EntityType


Recommended Posts

For an EntityType<?> I can use getClass(), but the methods available are generic, since it's a wildcard. I would like the specific entity class in order to retrieve attributes and check a few other data. Since methods like getAttribute() aren't directly available, this can (probably?) be accomplished with:

if (MobEntity.class.isAssignableFrom(entityType.getClass())) {
   EntityType<? extends MobEntity> entityType1 = (EntityType<? extends MobEntity>) entityType;

   AttributeModifierMap.MutableAttribute attributeMap = null;
   Method[] methods = entityType1.getClass().getMethods();
   for (Method method : methods) {
      if (method.getReturnType() == AttributeModifierMap.MutableAttribute.class) {
         attributeMap = (AttributeModifierMap.MutableAttribute) method.getDefaultValue();

However this seems over-complicated. Alternatively I can cast ? to type T with a helper method, and then would somehow need to instantiate it:

T entity = new T(entityType1, world) {};

But obviously T can't be used in that manner. Am I overthinking this?

Edited by urbanxx001
Link to comment
Share on other sites

Grab the data on first call instead of immediately. Then you'll have an instance of the entity to grab everything from. The entity type is used to create a singleton from the entity. If you needed a specific attribute map of a type, then you should grab that from GlobalEntityTypeAttributes and calculate there. What is the use-case for this?

  • Thanks 1
Link to comment
Share on other sites

Thank you so much, I'll use GlobalEntityTypeAttributes then. The EntityType (a set of them actually) isn't being grabbed from an Entity in the world, but instead from the ForgeRegistry. It's being used for a database. I may still need the actual class, as I need to check if IAngerable is extended by it (to determine if mobs are Neutral or not; isPeacefulCreature only checks if the mob is peaceful or hostile). 

Edited by urbanxx001
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.

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.


  • Create New...

Important Information

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