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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • i managed to fix it by reinstalling the modpack and re-add all the extra mods I've had previously.
    • Ah, it appears I spoke too soon, I still need a little help here. I now have the forceloading working reliably.  However, I've realized it's not always the first tick that loads the entity.  I've seen it take anywhere from 2-20ish to actually go through, in which time my debugging has revealed that the chunk is loaded, but during which time calling  serverLevelIn.getEntity(uuidIn) returns a null result.  I suspect this has to do with queuing and how entities are loaded into the game.  While not optimal, it's acceptable, and I don't think there's a whole ton I can do to avoid it. However, my concern is that occasionally teleporting an entity in this manner causes a lag spike.  It's not every time and gives the appearance of being correlated with when other chunks are loading in.  It's also not typically a long spike, but can last a second or two, which is less than ideal.  The gist of how I'm summoning is here (although I've omitted some parts that weren't relevant.  The lag occurs before the actual summon so I'm pretty confident it's the loading, and not the actual summon call). ChunkPos chunkPos = new ChunkPos(entityPosIn); if (serverLevelIn.areEntitiesLoaded(chunkPos.toLong())) { boolean isSummoned = // The method I'm using for actual summoning is called here. Apart from a few checks, the bulk of it is shown later on. if (isSummoned) { // Code that runs here just notifies the player of the summon, clears it from the queue, and removes the forceload } } else { // I continue forcing the chunk until the summon succeeds, to make sure it isn't inadvertently cleared ForgeChunkManager.forceChunk(serverLevelIn, MODID, summonPosIn, chunkPos.x, chunkPos.z, true, true); } The summon code itself uses serverLevelIn.getEntity(uuidIn) to retrieve the entity, and moves it as such.  It is then moved thusly: if (entity.isAlive()) { entity.moveTo(posIn.getX(), posIn.getY(), posIn.getZ()); serverLevelIn.playSound(null, entity, SoundEvents.ENDERMAN_TELEPORT, SoundSource.NEUTRAL, 1.0F, 1.0F); return true; } I originally was calling .getEntity() more frequently and didn't have the check for whether or not entities were loaded in place to prevent unnecessary code calls, but even with those safety measures in place, the lag still persists.  Could this just be an issue with 1.18's lack of optimization in certain areas?  Is there anything I can do to mitigate it?  Is there a performance boosting mod I could recommend alongside my own to reduce the chunk loading lag? At the end of the day, it does work, and I'm putting measures in place to prevent players from abusing the system to cause lag (i.e. each player can only have one queued summon at a time-- trying to summon another replaces the first call).  It's also not an unacceptable level of lag, IMO, given the infrequency of such calls, and the fact that I'm providing the option to toggle off the feature if server admins don't want it used.  However, no amount of lag is ideal, so if possible I'd love to find a more elegant solution-- or at least a mod recommendation to help improve it. Thanks!
    • When i start my forge server its on but when i try to join its come a error Internal Exception: java.lang.OutOfMemoryError: Requested array size exceeds VM limit Server infos: Linux Minecraft version 1.20.1 -Xmx11G -Xms8G
    • Also add the latest.log from your logs-folder
  • Topics

×
×
  • Create New...

Important Information

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