Getting CreatureType from Creature

Discussion in 'Plugin Development' started by Olof Larsson, Mar 6, 2011.

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

    Olof Larsson

    Hello! Envision this situation. I have a Creature but I do not know which type of creature it is and I want to find the CreatureType. Does enyone know of a good way to do this?

    My first approach was to to use many instanceof checks like this:
    CreatureType creatureType;
    if (creature instanceof Zombie) {
        creatureType = CreatureType.ZOMBIE;
    } else if (creature instanceof Sheep) {
        creatureType = CreatureType.SHEEP;
    } else if .......
    However that will be one loooong statement.

    My current approach is using this function I created:
    public static CreatureType creatureTypeFromEntity(Entity entity) {
        if ( ! (entity instanceof Creature)) {
            return null;
        String name = entity.getClass().getSimpleName();
        name = name.substring(5); // Remove "Craft"
        return CreatureType.fromName(name);
    Is there a better way? Have I missed something?
    If not I would suggest adding this method to bukkit: Creature.getCreatureType()
Thread Status:
Not open for further replies.

Share This Page