Get Entity by name

I wonder if there is a method to see if an entity is registered into the game. For example, i want to know if an entity called “Dummy” is registered, meaning there is an entity with that name or that string in it’s id in vanilla minecraft or in some mods. Is there a way to do this?

The GameRegistry contains all of this information and can be obtained with Sponge.getRegistry(). From there, you can use methods such as #getType and #getAllOf. For example, Sponge.getRegistry().getType(EntityType.class, "minecraft:blaze") returns EntityTypes.BLAZE.

If you’re looking for more complex actions with names, you’ll have to filter things out manually. The easiest way to do this is with the Stream API, which might look something like:

String name; //should be lowercase
Sponge.getRegistry().getAllOf(EntityType.class).stream()
    .map(CatelogType::getId) //maps to the id
    .filter(id -> id.contains(name)) //filters out ids without the name
    .collect(Collectors.toList());

You’ll have to address multiple matches (or none) in whatever way works best for your use case.

As a final note, this can be done for anything that extends CatelogType, such as EntityType, ItemType, etc.

1 Like

Thank you, that worked like a charm :smiley:

1 Like