Solved Prevent checking non existing entitiy types

Discussion in 'Spigot Plugin Development' started by MrGeneralQ, Jan 19, 2020.

  1. MrGeneralQ

    MrGeneralQ Previously qintinator

    Good afternoon everyone

    Currently, we need a proper way to check if an entity type exists. Example

    A series of conditions will check if the mob.getType() == EntityType.SOMETHING then execute the code.

    That works, the problem is that we need to prevent that the code will throw an error when the mob is for example a Bee (which would cause an error for server versions lower then 1.15)

    Any help is always much appreciated.

    Best regards,

    Mr Q
  2. SteelPhoenix


    You can always use Enum#valueOf(String). It doesn't look the cleanest but it works.

    Code (Java):
    // Keeping a cached value
    private static final EntityType type;
    static {
        EntityType et = null;
        try {
            et = EntityType.valueOf("SOMETHING");
        } catch (IllegalArgumentException exception) {
            // The type does not exist in this version
        type = et;

    // Checking entitytype
    Entity entity = ...;
    // Entity#getType() cannot return null values so no need to check if the type is null
    if (entity.getType() == type) {
        // Code
  3. MrGeneralQ

    MrGeneralQ Previously qintinator

    But if for any reason the name would change, that would break it again. On the other hand , that looks like the cleanest solution.