Jump to content

Get a list of every entity registered and know if it is a mob?


Recommended Posts

Posted

I'm trying to get a list of every entity in the game and check if it is a mob

I've tried doing something like this

public static Set<ResourceLocation> entityList = EntityList.getEntityNameList();
public static ArrayList<String> mobsList = new ArrayList<String>();

for (ResourceLocation entity : entityList) {
	if (EntityList.getClass(entity) instanceof EntityMob)
    	mobsList.add(entity.toString());
  
  //do things with the arraylist
}

It's obiviously not working since getClass returns a Class and not an Entity

What's a better and working way of doing it?

Posted
  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

The reason your code wasn't working was because instance of is meant for comparing instances. The class you get is not an instance of that class. If it was then you could do things like call the instance methods from it. So as Draco pointed out you need to look at the standard Java ways of doing it. If you want to work with the classes, then you need to use the isAssignableFrom() method with the classes, or conversely you need to create an instance of the class (like call its constructor) then do instanceof.

  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted
1 minute ago, jabelar said:

or conversely you need to create an instance of the class (like call its constructor) then do instanceof.

That's the worst idea. All of those instances would then need to be garbage collected.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)
1 minute ago, Draco18s said:

That's the worst idea. All of those instances would then need to be garbage collected.

For sure it is also a waste of performance I assume. Why go through all the trouble of constructing it? But it is important to teach the reasoning behind why his code didn't work, not just tell someone "copy an answer from Stack Overflow". I wanted the person to understand when instanceof applies and when it doesn't.

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted (edited)
On 30/9/2017 at 5:33 AM, jabelar said:

The reason your code wasn't working was because instance of is meant for comparing instances. The class you get is not an instance of that class. If it was then you could do things like call the instance methods from it. So as Draco pointed out you need to look at the standard Java ways of doing it. If you want to work with the classes, then you need to use the isAssignableFrom() method with the classes, or conversely you need to create an instance of the class (like call its constructor) then do instanceof.

I am missing something
I've tried using isAssignableFrom() but it always returns false for every entity

if (EntityList.getClass(entity).isAssignableFrom(EntityCreature.class))
  //returns false

EDIT: Just found out that using something like EntityCreeper.class works (obiviously only for creepers), but I need for every entity that inherits from EntityCreature
And for some reasons EntityList.getClass("minecraft:lightning_bolt") returns null crashing the game.

 

EDIT2: Managed to make it. I was doing it the wrong way. It was the opposite. Solution in last post if anyone is intrested

Edited by Insane96MCP
Added EDIT2
Posted
3 hours ago, diesieben07 said:

That does not even compile. getClass takes a ResourceLocation, not a String.

Yeah, I meant the resource location for the lightning bolt

3 hours ago, diesieben07 said:

EntityList also already has a method that does this: EntityList.isMatchingName(Entity, ResourceLocation).

does this what?

Posted
On 30/9/2017 at 9:29 AM, Insane96MCP said:

I am missing something
I've tried using isAssignableFrom() but it always returns false for every entity


if (EntityList.getClass(entity).isAssignableFrom(EntityCreature.class))
  //returns false

 

I was doing the wrong way around. The solution is the opposite

if (EntityCreature.class.isAssignableFrom(EntityList.getClass(entity)))
  //returns true if entity is a subtype of EntityCreature

 

Posted
3 hours ago, diesieben07 said:

Checks if an entity matches a given entity name. You should use this method.

D7, I am pretty sure that checks "is this entity 'minecraft:creeper'?" but does not check "is this entity a class that is (or extends) EntityCreeper?" (because of mod-added creepers).

 

The OP wants to detect all mobs, not a specific one.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)
6 minutes ago, diesieben07 said:

I guess I misunderstood based on the code posted then.

If you want to know if a given entity is a mob, use an instanceof check. I don't know why you would ever need to check the entire list of registered entity types, that does not make much sense.

Why they're checking all registered entities, I don't know. I'll give you that, because I agree, it seems odd.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
5 hours ago, gummby8 said:

I have a mob spawner that lists all EntityLiving in the selection gui. But yes I needed to create an instead first and then check with instanceof

 

 

 

23 minutes ago, diesieben07 said:

No. No, you do not.

As already said, I managed to do it. No need to instantiate every mob: 

 

Posted
On 9/29/2017 at 0:35 PM, Draco18s said:

Literally the first reply in this thread. 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)

Okay so I think what you want to do is scan through the list of entities, including other mods which is why you can't just use a fixed list of known mobs, and present them in a GUI.

 

Because you don't know the names of entities from other mods, you can't use the entity names.

 

Using instanceof is logically correct, but is somewhat wasteful -- you're running all the creation code for the entity just to test what it is and then throw it away. However, honestly, if you just did that once during the post-init phase of loading I think that is perfectly fine. Definitely don't do it every time you show the GUI though!

 

However, if you want to do things in a way that is both logically correct and performance-optimized, you will want to just look at the classes as listed in the registry. In Java you can test if one class is a subclass of another by using the isAssignableFrom() method. So if you use EntityMob.class.isAssignableFrom(EntityWhatever.class) it should tell you that EntityWhatever is a mob.

Spoiler

 

 

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

  • 2 years later...
  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.