Jump to content

[1.7.2]What is proper entity registration (conflicting tutorials confusion)?


Recommended Posts

Posted

Okay, I'm getting confused between the use of the EntityRegistry.registerGlobalEntityID() and the EntityRegistry.registerModEntity() methods for entity registration because I looked at four tutorials that give conflicting advice...

 

A tutorial here (http://www.minecraftforge.net/wiki/How_to_register_a_mob_entity#Registering_an_Entity) indicates to only use the registerGlobalEntityID().  It says:

  Quote
Put the following in your mod file's load() method:

EntityRegistry.registerGlobalEntityID(MyEntity.class, "MyEntityName", myentityid)

Where MyEntity is the name of your entity's class, "MyEntityName" is the name of the entity, and myentityid will be the number Minecraft uses to identify the mob. You can get a unique, unused entity id by calling EntityRegistry.findGlobalUniqueEntityId() instead of myentityid.

A tutorial here (http://www.minecraftforum.net/topic/1417041-mod-entity-problem/page__st__140#entry18822284) says to only use registerModEntity().  It says:

  Quote
There is only one method for registering and adding tracking.

EntityRegistry.registerModEntity(Class<? extends Entity> entityClass, String entityName, int id, Object mod, int trackingRange, int updateFrequency, boolean sendsVelocityUpdates) : void

At tutorial here (http://www.minecraftforum.net/topic/2389683-172-forge-add-new-block-item-entity-ai-creative-tab-language-localization-block-textures-side-textures/) says to use both methods, but to use the same entityID for both.  It says:

  Quote
EntityRegistry.registerGlobalEntityID(entityClass, name, entityID);

EntityRegistry.registerModEntity(entityClass, name, entityID, instance, 64, 1, true);

EntityList.entityEggs.put(Integer.valueOf(entityID), new EntityList.EntityEggInfo(entityID, primaryColor, secondaryColor));

A message here (http://www.minecraftforum.net/topic/2564566-172spawning-my-entity-on-top-of-another-one-crashes-game/)indicates to use both methods, but use different ID in each:

  Quote
You also need to register your entity as a mod entity, using a mod-specific id. I do so using a static int modEntityIndex that I just iterate each time, and a custom helper method that I use for entities that have spawn eggs:

public static void registerEntity(Class entityClass, String name, int primaryColor, int secondaryColor) { int id = EntityRegistry.findGlobalUniqueEntityId(); EntityRegistry.registerGlobalEntityID(entityClass, name, id, primaryColor, secondaryColor); EntityRegistry.registerModEntity(entityClass, name, ++modEntityIndex, ZSSMain.instance, 80, 3, false); }

So which way is correct?  The weird thing is they all kinda work -- I tried all three and can spawn the entities -- but I assume there is some invisible bad stuff going on if you do it the wrong way.

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

Posted

Well, the first one is marked as outdated, so I'd ignore that.

 

It looks to be 1.6 or 1.5, but could be earlier.

 

Second one is 1.4 so DEFINITELY ignore that

 

Third one looks right to me

 

Fourth... couldn't really be bothered looking through that one all that much tbh, but it seemed more focused on other things and wasn't exactly a tutorial

Posted

An example from my WIP mod:

 

EntityRegistry.registerGlobalEntityID(EntityBullet.class, "Bullet", EntityRegistry.findGlobalUniqueEntityId());
EntityRegistry.registerModEntity(EntityBullet.class, "Bullet", Config.entBulletId, Steamcraft.instance, 64, 20, true); 

 

If you don't know what each of the values do in the registerModEntity, please read the Forge javadocs.

Posted
  On 4/14/2014 at 12:38 AM, MrArcane111 said:

An example from my WIP mod:

 

EntityRegistry.registerGlobalEntityID(EntityBullet.class, "Bullet", EntityRegistry.findGlobalUniqueEntityId());
EntityRegistry.registerModEntity(EntityBullet.class, "Bullet", Config.entBulletId, Steamcraft.instance, 64, 20, true); 

 

If you don't know what each of the values do in the registerModEntity, please read the Forge javadocs.

 

Thanks.  That is in line with case #4 (use both but different ID numbers).  I do know about the parameters (distance to render, frequency of updates, velocity updates).

 

@Jacknoshima

  Quote
Fourth... couldn't really be bothered looking through that one all that much tbh, but it seemed more focused on other things and wasn't exactly a tutorial

 

You didn't have to look at the link because I quoted the specific advice.  Note that the advice was from CoolAlias who I greatly respect.  The key difference is that the id is different in the two registrations.  In particular this meshes with the explanation in the second link that indicates that the mod entity registry is meant to be unique id within your mod -- doesn't need to be globally unique.

 

I think method 4 (CoolAlias' suggestion, and backed up by McArcane) is technically correct, but 3 happens to work because if your global id is unique then your mob entity id will be unique too so no real problem created.

 

  Quote
It looks to be 1.6 or 1.5, but could be earlier.

 

Second one is 1.4 so DEFINITELY ignore that

 

I can't quite ignore these because even though they are older they each have a registration method that is different, and now we're supposed to use both?    I understand if there is a newer method that replaces an older one, but it seems strange to me that the new method would be to combine both older methods.

 

Also, the explanation in the second link is quite interesting and makes sense to me -- that the mod id is meant to get away from the trouble of the mod programmer worrying about globally unique id.  While there is a method to find a globally unique id, there is another limitation with the global entity list related to it being limited to 255 entities (due to a byte cast on the id in the packets).  If I look at the related mob spawn packet methods in 1.7.2 there is still a byte cast, so seems like that limitation is still present.

 

Lastly, the weird thing to me is that ALL these methods kinda work when I try them.  I tried all the scenarios, and in every case the entities are created, spawn, act on their AI, have NBT and restore from saves, etc.  I would understand it better if skipping either of the registries prevented the spawn, since then it would imply that you need them.  Just strange that it is hard to notice any difference in behavior between the cases.

 

I guess I'll do some experiments to figure out what behavior is explicitly enabled by each registry.

 

 

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

Posted

Hi again,

 

This issue was quite confusing to me as well, to be honest. In fact, I was under the impression that global entity IDs were not supposed to be used at all, and only to use registerModEntity. registerModEntity can take any ID, as the IDs are associated with your mod specifically, so I just start at zero and iterate up. This is what is used to track your entity. All mod entities should be registered this way, including projectiles, mobs, item entities, etc.

 

Global entity ID is only necessary when registering an entity with a spawn egg, and needs to get a unique global entity ID from the EntityRegistry, otherwise your spawn eggs may conflict with those added by other mods. So if your entity is going to have a spawn egg, you need to register it globally, in addition to the registerModEntity.

 

That's at least how I've come to understand it after working with it some, but the issue is far from 100% clear even after looking through Forge and what little documentation is available.

 

If anyone who knows with absolute certainty can corroborate or correct my experiences, that would be wonderful. xD

 

Cheers,

coolAlias

 

 

Posted

You don't need to have a global entity id at all.

The #registerModEntity is the right one.

  Quote

Also, the explanation in the second link is quite interesting and makes sense to me -- that the mod id is meant to get away from the trouble of the mod programmer worrying about globally unique id.  While there is a method to find a globally unique id, there is another limitation with the global entity list related to it being limited to 255 entities (due to a byte cast on the id in the packets).  If I look at the related mob spawn packet methods in 1.7.2 there is still a byte cast, so seems like that limitation is still present.

This is totally correct.

 

Also, while it doesn't give you a vanilla egg,  it doesn't stop you from making one.

The lazy step would be to register a global entity id and add it to the vanilla egg list. (but then the local id registration is far less interesting)

Another solution is to create your own egg, like any item (see ItemMonsterPlacer, though you'll need to replace the global entity id lookup with either entity name or local id).

Another solution is to use events and spawn the entity yourself.

Posted
  On 4/14/2014 at 2:43 AM, GotoLink said:

You don't need to have a global entity id at all.

The #registerModEntity is the right one.

  Quote

Also, the explanation in the second link is quite interesting and makes sense to me -- that the mod id is meant to get away from the trouble of the mod programmer worrying about globally unique id.  While there is a method to find a globally unique id, there is another limitation with the global entity list related to it being limited to 255 entities (due to a byte cast on the id in the packets).  If I look at the related mob spawn packet methods in 1.7.2 there is still a byte cast, so seems like that limitation is still present.

This is totally correct.

 

Also, while it doesn't give you a vanilla egg,  it doesn't stop you from making one.

The lazy step would be to register a global entity id and add it to the vanilla egg list. (but then the local id registration is far less interesting)

Another solution is to create your own egg, like any item (see ItemMonsterPlacer, though you'll need to replace the global entity id lookup with either entity name or local id).

Another solution is to use events and spawn the entity yourself.

 

I've been experimenting and like you and CoolAlias said it seems that vanilla eggs definitely require a globalID.  I like the idea of only worrying about mod-specific ID since it seems like better programming practice. 

 

But in addition to not having spawn eggs (I wouldn't mind making my own if that was only thing), it seems that the natural spawn registrations also use globalID.  So you'd have to make new natural spawning methods too?

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

Posted
  Quote
But in addition to not having spawn eggs (I wouldn't mind making my own if that was only thing), it seems that the natural spawn registrations also use globalID.  So you'd have to make new natural spawning methods too?

 

No. Entities are registered always within the EntityList. The registerModEntity just ignores the IDtoClassMapping and the classToIDMapping.

Natural spawns always grab the entities from the class supplied by the EntityRegistry.addSpawn method.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

  Quote

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted
  On 4/14/2014 at 6:14 AM, SanAndreasP said:

No. Entities are registered always within the EntityList. The registerModEntity just ignores the IDtoClassMapping and the classToIDMapping.

 

But something is wrong because I don't see the modEntityID registrations ever show up in EntityList.  I created a method to print out the EntityList after I registerEntities and it doesn't show any entries for my entities.  Here is the EntityList after my registration (none of my entities are listed):

 

  Reveal hidden contents

 

And here is my code for the registration.  Main class preInit():

 

  Reveal hidden contents

 

And my commonProxy class:

 

  Reveal hidden contents

 

 

However, if I change the CommonProxy to use globalID then I get the following EntityList readout that shows my entities nicely registered:

 

  Reveal hidden contents

 

Where my CommonProxy was now:

 

  Reveal hidden contents

 

 

So I really don't see the modEntityID registration adding my entities to the EntityList.  Or does it do it at some other range beyond id = 255?

 

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

Posted

registerModEntity doesn't add entities to the ID mapping in EntityList, but it does add them by name and class. The following is a snippet of the registration code:

String entityModName = String.format("%s.%s", mc.getModId(), entityName);
EntityList.classToStringMapping.put(entityClass, entityModName);
EntityList.stringToClassMapping.put(entityModName, entityClass);

 

If you print out the entity list using the string to class map, with "modid.entityName", you will surely see your entities. The ID to class mapping is only used by spawn eggs (which is why the mod entity index and global ID can be different).

 

My question is why haven't either Mojang or Forge team altered the vanilla ID to class map to allow everyone to use the ItemMonsterPlacer? Sure, it may be lazy to not make our own, but it's also good coding practice to not reinvent the wheel. Why copy/paste when you should be able to use something directly, or even through inheritance? It just seems bizarre to be moving toward a modding API but leave something as widely used as this in such a state.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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