Modding 1.4.7: Basic Mob Part 6: Spawn Egg

In this tutorial I will show you how to add an Entity Spawn egg for your mob. The code for this is done in your mod file. This is the file that I will be starting with.


package tutorial;

import net.minecraft.entity.EnumCreatureType;
import net.minecraft.world.biome.BiomeGenBase;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.EntityRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = “Tutorial_Tutorialmod”, name = “Tutorial”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial 
{
@SidedProxy(clientSide = “tutorial.ClientProxyTutorial”, serverSide = “tutorial.CommonProxyTutorial”)
public static CommonProxyTutorial proxy;

@Init
public void load(FMLInitializationEvent event)
{
proxy.registerRenderThings();

EntityRegistry.registerModEntity(EntityTutorial.class, “Tutorial”, 1, this, 80, 3, true);

EntityRegistry.addSpawn(EntityTutorial.class, 10, 2, 4, EnumCreatureType.monster, BiomeGenBase.desert, BiomeGenBase.desertHills, BiomeGenBase.forest);

LanguageRegistry.instance().addStringLocalization(“entity.Tutorial_Tutorialmod.Tutorial.name”, “Tutorial”);
}
}


To add your Entity to the vanilla spawner eggs without changing the core files you will need to add several things. The first one is a variable somewhere at the top like this.


static int startEntityId = 300;


This number is used to get an unique id for your entity egg.

Now you will have to add this method. I suggest placing this below the load method, but before the end of the class.


public static int getUniqueEntityId() 
{
do 
{
startEntityId++;

while (EntityList.getStringFromID(startEntityId) != null);

return startEntityId;
}


The mob eggs in Minecraft use an id to keep track of which mob they will spawn. If this id is already taken this method will add 1 to the startEntityId. It will keep doing this untill it finds an unused number.

The second method you will need to add is this one.


public static void registerEntityEgg(Class<? extends Entity> entity, int primaryColor, int secondaryColor) 
{
int id = getUniqueEntityId();
EntityList.IDtoClassMapping.put(id, entity);
EntityList.entityEggs.put(id, new EntityEggInfo(id, primaryColor, secondaryColor));
}


This code will add your mob egg to the game. As you can see there are 3 parameters. The first is the Entity that will be added. The second and third are for the colors.

The last line of code is the one that is actually used to add the egg. If you launch without this code nothing wil happen. You need to add this code to the load method.


registerEntityEgg(EntityTutorial.class, 0xffffff, 0x000000);


This line will run the registerEntityEgg method with the specified entity and colors.
Again. The first parameter is the entity that you want to add. The second is the color for the biggest part of the egg and the third parameter is for the spots on the eggs.
To get a color you want you need to start your color with 0x. This will make sure that java reads the number (with letters) right. The numbers/letters after that are the actual color. If you want to make a color yourself you can take a look at the ColorPicker website. On the top you can see the number of the color you created on there.

The whole file should now look something like this.


package tutorial;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityEggInfo;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.world.biome.BiomeGenBase;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.EntityRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = “Tutorial_Tutorialmod”, name = “Tutorial”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial 
{
@SidedProxy(clientSide = “tutorial.ClientProxyTutorial”, serverSide = “tutorial.CommonProxyTutorial”)
public static CommonProxyTutorial proxy;

static int startEntityId = 300;

@Init
public void load(FMLInitializationEvent event)
{
proxy.registerRenderThings();

EntityRegistry.registerModEntity(EntityTutorial.class, “Tutorial”, 1, this, 80, 3, true);

EntityRegistry.addSpawn(EntityTutorial.class, 10, 2, 4, EnumCreatureType.monster, BiomeGenBase.desert, BiomeGenBase.desertHills, BiomeGenBase.forest);

LanguageRegistry.instance().addStringLocalization(“entity.Tutorial_Tutorialmod.Tutorial.name”, “Tutorial”);

registerEntityEgg(EntityTutorial.class, 0xffffff, 0x000000);
}

public static int getUniqueEntityId() 
{
do 
{
startEntityId++;

while (EntityList.getStringFromID(startEntityId) != null);

return startEntityId;
}

public static void registerEntityEgg(Class<? extends Entity> entity, int primaryColor, int secondaryColor) 
{
int id = getUniqueEntityId();
EntityList.IDtoClassMapping.put(id, entity);
EntityList.entityEggs.put(id, new EntityEggInfo(id, primaryColor, secondaryColor));
}
}


You can add more than one entity like this by just copying the new code in the load and changing the entity and colors.

In the next tutorial I will show you how to create a Render file for you mob.
The source code will be available at the end of the last Entity tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">