Provide utility to retrieve Entity class from EntityType
vi945786 opened this issue · 8 comments
I’ve noticed there isn’t currently a way in Fabric to get the Entity class from an EntityType.
It would be really helpful if Fabric provided a simple utility method or API to retrieve the Entity class of an EntityType.
For example, looking at the EntityType class definition:
public class EntityType<T extends Entity> implements ToggleableFeature, TypeFilter<Entity, T> {
...
public static final EntityType<BoatEntity> ACACIA_BOAT;
public static final EntityType<ChestBoatEntity> ACACIA_CHEST_BOAT;
public static final EntityType<AllayEntity> ALLAY;
...
Currently, at runtime it is possible to get the ACACIA_BOAT EntityType, but not the entity class from that EntityType (which is BoatEntity).
Trivial use-case example: A config screen which needs to determine which entities are living entities (by checking which of the EntityTypes' entity classes extend LivingEntity).
it doesn't look like the entity type currently contains this information, and I think our policy on these small enhancements is usually for them to be automatic and require no extra registration from modders
Unfortunately there is no way to do this in a generic way, you need to create the entity and then use instanceof.
If you only need the vanilla entity types (maybe in datagen, for example), then you can get them via reflection like this, however it won't include any modded entity types.
Is there a more fitting repository to suggest this on?
Thanks for the quick replies!
We can't accept the suggestion as it is impossible to implement. Unlike NeoForge, in Fabric API we can't rule out mods registering entities without using Fabric API.
Is there a more fitting repository to suggest this on? Thanks for the quick replies!
I don't think so. it's just generally not possible without creating the entity in question
I wrote this, which while not perfect and fails in some cases/mods, generally works and provides the data.
So feel free to copy it if you really need that
https://github.com/Patbox/polymer/blob/dev/1.21.6/polymer-common/src/main/java/eu/pb4/polymer/common/impl/entity/InternalEntityHelpers.java