ConstructEntityEvent.Pre get Sponge entity class

Hello!

In ConstructEntityEvent.Pre I get the entityClass with event.getTargetType().getEntityClass(). When I wanted to check if the class comes from Monster with entityClass.isAssignableFrom(Monster.class) I discovered while debugging that the entityClass is from NMS. Of course the isAssignableFrom can’t work then.
But how to check if it will be a Monster?

Thanks in advance!

Theoretically, this should still work… As the way NMS is used in sponge is by smashing together NMS bytecode and interfaces. If I’m not mistaken, you simply need to do Monster.class.isAssignableFrom(entityClass)

Documentation for isAssignableFrom:

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

Sorry for bothering you! The problem wasn’t the assignable check, it was a very dumb bug in my code. It wasn’t related to the check.
Anyway, your code works, so I mark it as the correct answer. Thanks :joy:

1 Like