Jump to content

[SOLVED]Mob spawns with spawn egg, but doesn't spawn naturally[SOLUTION INSIDE]


Recommended Posts

Posted

Hey,

    My mob spawns with the help of a spawn egg. But it doesn't spawn naturally. Here is my code:-

 

Tutorial.java (Main file)

 

package tutorial;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityEggInfo;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EnumCreatureType;
import tutorial.entity.EntityUncleTomo;
import tutorial.model.UncleTomo;
import tutorial.render.RenderUncleTomo;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.EntityRegistry;

@Mod(modid = Tutorial.modid, name = "Tutorial", version = "The best version ever.")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial {
    public static final String modid = "AblazeTheBest_TutorialMod";
    
    public static int startEntityId = 300;
    
    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));
    }
    @EventHandler
    public void load(FMLInitializationEvent event)
    {
    	EntityRegistry.registerGlobalEntityID(EntityUncleTomo.class, "Uncle Tomo", 1);
    	EntityRegistry.addSpawn(EntityUncleTomo.class, 100, 100, 200, EnumCreatureType.ambient);
    	EntityRegistry.findGlobalUniqueEntityId();
    	registerEntityEgg(EntityUncleTomo.class, 0x3AD71A, 0xD7D11A);
    	RenderingRegistry.registerEntityRenderingHandler(EntityUncleTomo.class, new RenderUncleTomo(new UncleTomo(), 0.3F));
    }
}

 

 

UncleTomo.java

 

package tutorial.model;

import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.util.MathHelper;

public class UncleTomo extends ModelBase
{
  //fields
    ModelRenderer head;
    ModelRenderer body;
    ModelRenderer rightarm;
    ModelRenderer leftarm;
    ModelRenderer rightleg;
    ModelRenderer leftleg;
    ModelRenderer apronbody;
    ModelRenderer leftapronedge;
    ModelRenderer rightapronedge;

  
  public UncleTomo()
  {
    textureWidth = 128;
    textureHeight = 128;
    
      head = new ModelRenderer(this, 55, 18);
      head.addBox(-4F, -8F, -4F, 8, 8, ;
      head.setRotationPoint(0F, 0F, 0F);
      head.setTextureSize(128, 128);
      head.mirror = true;
      setRotation(head, 0F, 0F, 0F);
      body = new ModelRenderer(this, 58, 52);
      body.addBox(-4F, 0F, -2F, 8, 12, 4);
      body.setRotationPoint(0F, 0F, 0F);
      body.setTextureSize(128, 128);
      body.mirror = true;
      setRotation(body, 0F, 0F, 0F);
      rightarm = new ModelRenderer(this, 40, 36);
      rightarm.addBox(-3F, -2F, -2F, 4, 12, 4);
      rightarm.setRotationPoint(-5F, 2F, 0F);
      rightarm.setTextureSize(128, 128);
      rightarm.mirror = true;
      setRotation(rightarm, 0F, 0F, 0F);
      leftarm = new ModelRenderer(this, 84, 36);
      leftarm.addBox(-1F, -2F, -2F, 4, 12, 4);
      leftarm.setRotationPoint(5F, 2F, 0F);
      leftarm.setTextureSize(128, 128);
      leftarm.mirror = true;
      setRotation(leftarm, 0F, 0F, 0F);
      rightleg = new ModelRenderer(this, 74, 72);
      rightleg.addBox(-2F, 0F, -2F, 4, 12, 4);
      rightleg.setRotationPoint(-2F, 12F, 0F);
      rightleg.setTextureSize(128, 128);
      rightleg.mirror = true;
      setRotation(rightleg, 0F, 0F, 0F);
      leftleg = new ModelRenderer(this, 54, 72);
      leftleg.addBox(-2F, 0F, -2F, 4, 12, 4);
      leftleg.setRotationPoint(2F, 12F, 0F);
      leftleg.setTextureSize(128, 128);
      leftleg.mirror = true;
      setRotation(leftleg, 0F, 0F, 0F);
      apronbody = new ModelRenderer(this, 1, 110);
      apronbody.addBox(0F, 0F, 0F, 8, 10, 1);
      apronbody.setRotationPoint(-4F, 2F, -3F);
      apronbody.setTextureSize(128, 128);
      apronbody.mirror = true;
      setRotation(apronbody, 0F, 0F, 0F);
      leftapronedge = new ModelRenderer(this, 0, 77);
      leftapronedge.addBox(0F, 0F, 0F, 1, 2, 1);
      leftapronedge.setRotationPoint(3F, 0F, -3F);
      leftapronedge.setTextureSize(128, 128);
      leftapronedge.mirror = true;
      setRotation(leftapronedge, 0F, 0F, 0F);
      rightapronedge = new ModelRenderer(this, 0, 68);
      rightapronedge.addBox(0F, 0F, 0F, 1, 2, 1);
      rightapronedge.setRotationPoint(-4F, 0F, -3F);
      rightapronedge.setTextureSize(128, 128);
      rightapronedge.mirror = true;
      setRotation(rightapronedge, 0F, 0F, 0F);
      
  }
  
  public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5)
  {
    super.render(entity, f, f1, f2, f3, f4, f5);
    setRotationAngles(f, f1, f2, f3, f4, f5, entity);
    head.render(f5);
    body.render(f5);
    rightarm.render(f5);
    leftarm.render(f5);
    rightleg.render(f5);
    leftleg.render(f5);
    apronbody.render(f5);
    leftapronedge.render(f5);
    rightapronedge.render(f5);
  }
  
  private void setRotation(ModelRenderer model, float x, float y, float z)
  {
    model.rotateAngleX = x;
    model.rotateAngleY = y;
    model.rotateAngleZ = z;
  }
  
  public void setRotationAngles(float f, float f1, float f2, float f3, float f4, float f5, Entity entity)
  {
  this.head.rotateAngleY = f3 / (180F / (float)Math.PI);
  this.head.rotateAngleX = f4 / (170F / (float)Math.PI);
  
  this.leftleg.rotateAngleX = MathHelper.cos(f * 0.6662F) * 2.0F * f1 * 0.5F;
  this.leftleg.rotateAngleY = 0.0F;
  
  this.rightleg.rotateAngleX = MathHelper.cos(f * 0.6662F + (float)Math.PI) * 1.4F * f1;
  this.rightleg.rotateAngleY = 0.0F;
  
  this.leftarm.rotateAngleX = MathHelper.cos(f * 0.6662F) * 2.0F * f1 * 0.5F;
  this.leftarm.rotateAngleZ = 0.0F;
  
  this.rightarm.rotateAngleX = MathHelper.cos(f * 0.6663F + (float)Math.PI) * 2.0F * f1 * 0.5F;
  this.rightarm.rotateAngleZ = 0.0F;
  }

}

 

 

EntityUncleTomo.java

 

package tutorial.entity;

import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.world.World;

public class EntityUncleTomo extends EntityMob{

public EntityUncleTomo(World par1World) {
	super(par1World);
	this.experienceValue = 0;
	this.tasks.addTask(0, new EntityAISwimming(this));
	this.tasks.addTask(6, new EntityAIWander(this, 0.5D));
	this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
        this.tasks.addTask(7, new EntityAILookIdle(this));
}

public boolean isAIEnabled(){
	return true;
}

protected String getLivingSound()
    {
        return "mob.villager.idle";
    }

    /**
     * Returns the sound this mob makes when it is hurt.
     */
    protected String getHurtSound()
    {
        return "mob.villager.hit";
    }

    /**
     * Returns the sound this mob makes on death.
     */
    protected String getDeathSound()
    {
        return "mob.villager.death";
    } 
    
    protected void playStepSound(int par1, int par2, int par3, int par4){
    	this.playSound("mob.zombie.step", 0.15F, 1.0F);
    }
    
    protected int getDropItemId()
    {
        return Item.potato.itemID;
    }
    
    protected void dropRareDrop(int par1)
    {
    	int rand = (int)(Math.random() * 4);
    	switch(rand) {
    	case 0:
    		this.dropItem(Item.appleRed.itemID, 1);
    		break;
    	case 1:
    		this.dropItem(Item.appleRed.itemID, 1);
    		break;
    	case 2:
    		this.dropItem(Item.appleGold.itemID, 1);
    		break;
    	case 3:
    		this.dropItem(Item.appleGold.itemID, 1);
    		break;
    	}
    }
    
    
}

 

 

RenderUncleTomo.java

 

package tutorial.render;

import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.entity.RenderLiving;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.util.ResourceLocation;
import tutorial.Tutorial;
import tutorial.entity.EntityUncleTomo;
import tutorial.model.UncleTomo;

public class RenderUncleTomo extends RenderLiving {

protected UncleTomo model;

public RenderUncleTomo(ModelBase par1ModelBase, float par2){
	super(par1ModelBase, par2);
	model = ((UncleTomo)mainModel);
}

public void renderUncleTomo(EntityUncleTomo entity, double par2, double par4, double par6, float par8, float par9){
	super.doRenderLiving(entity, par2, par4, par6, par8, par9);
}

public void doRenderLiving(EntityLiving par1EntityLiving, double par2, double par4, double par6, float par8, float par9){
	renderUncleTomo((EntityUncleTomo)par1EntityLiving, par2, par4, par6, par8, par9);
}

@Override
public void doRender(Entity entity, double d0, double d1, double d2, float f, float f1) {
	renderUncleTomo((EntityUncleTomo)entity, d0, d1, d2, f, f1);
}

@Override
protected ResourceLocation getEntityTexture(Entity entity) {
	return new ResourceLocation(Tutorial.modid + ":textures/mobs/UncleTomo.png");
}

}

 

 

Please help if you can.

 

Regards,

Ablaze.

 

 

Add me on Skype: AblazeTheBest. Send a message saying "#HeyAblaze"

 

Currently: Making a mod!

Posted

Hi

 

Do you mean - they don't spawn randomly?

 

Have a look in WorldServer.spawnRandomCreature -

    public SpawnListEntry spawnRandomCreature(EnumCreatureType par1EnumCreatureType, int par2, int par3, int par4)
    {
        List list = this.getChunkProvider().getPossibleCreatures(par1EnumCreatureType, par2, par3, par4);
        list = ForgeEventFactory.getPotentialSpawns(this, par1EnumCreatureType, par2, par3, par4, list);
        return list != null && !list.isEmpty() ? (SpawnListEntry)WeightedRandom.getRandomItem(this.rand, list) : null;
    }

 

 

It does a call to ForgeEventFactory.getPotentialSpawns (WorldEvent.PotentialSpawns) which I think will let you add your own creatures to the list of creatures provided by the vanilla code.

 

-TGG

Posted

Hi

 

Do you mean - they don't spawn randomly?

 

Have a look in WorldServer.spawnRandomCreature -

    public SpawnListEntry spawnRandomCreature(EnumCreatureType par1EnumCreatureType, int par2, int par3, int par4)
    {
        List list = this.getChunkProvider().getPossibleCreatures(par1EnumCreatureType, par2, par3, par4);
        list = ForgeEventFactory.getPotentialSpawns(this, par1EnumCreatureType, par2, par3, par4, list);
        return list != null && !list.isEmpty() ? (SpawnListEntry)WeightedRandom.getRandomItem(this.rand, list) : null;
    }

 

 

It does a call to ForgeEventFactory.getPotentialSpawns (WorldEvent.PotentialSpawns) which I think will let you add your own creatures to the list of creatures provided by the vanilla code.

 

-TGG

 

I don't quite understand what you mean by 'spawn randomly.' Let me give you an example.

 

The Zombie:-

It can spawn in two ways.

 

1. Right click any block with a Zombie Spawn egg and it will spawn.

2. It spawns naturally on a difficulty of Easy or above.

 

Similarly, my mob:-

 

Uncle Tomo:-

It should spawn in two ways:-

 

1. Right click any block with an Uncle Tomo spawn egg and it will spawn. (This method works)

2. It spawns naturally on a difficulty of Easy or above. (This isn't working! It is not spawning naturally!)

 

Regards,

Ablaze.

Add me on Skype: AblazeTheBest. Send a message saying "#HeyAblaze"

 

Currently: Making a mod!

Posted

Hi

 

Keen, that's what I thought you meant.

 

You can get them to spawn naturally using the Forge event WorldEvent.PotentialSpawns

 

Have you used Forge events before?

A bit of an introduction here - http://www.minecraftforum.net/topic/1419836-131-forge-4x-events-howto/

 

-TGG

 

-TGG

 

I've never used events before. I was reading the article you gave me, but I can't understand much. :( :(

Add me on Skype: AblazeTheBest. Send a message saying "#HeyAblaze"

 

Currently: Making a mod!

Posted

Hi

 

yeah it's a bit cryptic.  Let's see if I can't adapt a bit of code I've used before...

 

public class MyEventHandler {

  @ForgeSubscribe
  public void addMyCreature(WorldEvent.PotentialSpawns event) {
    World world = event.world;
    int xposition = event.x;
    int yposition = event.y;
    int zposition = event.z;
    EnumCreatureType creatureType = event.type;
    List<SpawnListEntry> listOfSpawnableCreatures = event.list;

    final int SPAWNWEIGHT = 5;  // the higher the number, the more likely this creature will spawn
    final int MINIMUMNUMBERTOSPAWN = 1;
    final int MAXIMUMNUMBERTOSPAWN = 4;

    switch (creatureType) {
      case monster: {
        SpawnListEntry myNewCreatureSpawn = new SpawnListEntry(MyCreature.class, SPAWNWEIGHT, MINIMUMNUMBERTOSPAWN, MAXIMUMNUMBERTOSPAWN);
        listOfSpawnableCreatures.add(myNewCreatureSpawn);
        break;
      }
      case creature:
      case waterCreature:
      case ambient:
      default:  
    }
  }
}

In my mod's base class

  @EventHandler
  public void load(FMLInitializationEvent event) {
    MinecraftForge.EVENT_BUS.register(new MyEventHandler());
  }

 

I haven't actually run that code, but it should be pretty close to what you need.

 

-TGG

Posted

Thanks so much!

 

:D

 

Regards,

Ablaze.

Add me on Skype: AblazeTheBest. Send a message saying "#HeyAblaze"

 

Currently: Making a mod!

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.