Get entity by name

Discussion in 'Plugin Development' started by Reflxction, Jan 31, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    Reflxction

    Hi,
    I have a small question. I'm making a plugin which gives the ability to choose the entity type through the config. However, I can't seem to figure out how do I get the entity. Apparently EntityType#fromName is deprecated, and I'm not going to add thousands of if statements for each single entity type. I thought about EntityType#valueOf, but wasn't sure if it's the correct one to use. Any help would be much appreciated.

    P.S: If any staff can change my username from xTechno_ to Reflxction (my new name), I'll be thankful.
     
  2. Offline

    Zombie_Striker

    @xTechno_
    1. The fromName method should still work. You should use that to get the entitytype.
    2. If you really don't want to use a deprecated method, EntityType#name() returns the name of the enum value. To return the entitytype, for loop through all the values and compare the names of the values to the value from the config. Once you find a name that matches, return that entity type.
     
  3. Offline

    Reflxction

    @Zombie_Striker
    Alright thanks, so what you mean would be something like this:
    Code:
        public EntityType getEntityByName(String name) {
            for (EntityType type : EntityType.values()) {
                if(type.name().equalsIgnoreCase(name)) {
                    return type;
                }
            }
            return null;
        }
    Is this correct or am I that bad?
     
  4. Offline

    Zombie_Striker

    Reflxction likes this.
Thread Status:
Not open for further replies.

Share This Page