[TUTORIAL] Advanced coding: custom spawn eggs and names

Started by MeatyMaker00 on

Topic category: User side tutorials

Joined Dec 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[TUTORIAL] Advanced coding: custom spawn eggs and names

I leave this more as a future reference for myself, but in the offchance it is useful to someone else here it is.

 

Let's say you make yourself a custom mob and you want a spawn egg for it. Easy enough, just tick a box and you're done!

But what if you don't like the icon for the spawn egg and instead want a custom spawn egg?

So you start testing with armor stands and vanilla spawn eggs and discover things like SPAWN_TYPE and the like. And let's say you actually manage to implement the entity spawning (without doing so inside a block!)

Aaaand then you rename a spawn egg to XxSniperEggxX for kicks and giggles only to realize that the creeper got a custom name.

So, in code, you put down:

if (itemstack.hasCustomName)
{
  entityToSpawn.setCustomName(itemstack.getDisplayName());
}

And the creeper's name will be [XxSniperEggxX]

Why the brackets? ChatGPT will tell you it's Forge's fault, and will start inventing classes.

Save yourself the headache and use HoverName instead:

entityToSpawn.setCustomName(itemstack.getHoverName());

Don't forget to add this to the item extension for the spawn egg so dispensers can use them!