I been trying for a couple of days now to get a village guard-like mob to spawn in a village that generates in the game. I started by using a forge event "PopulateChunkEvent", but it doesn't seem to be working right.
'
@SubscribeEvent
public void onPopulateChunk(PopulateChunkEvent.Populate event){
FMLLog.getLogger().info("Spawn Watch");
if(event.hasVillageGenerated){
EntityVillageWatch.spawnWatch(event.world);
}
}
'
Registering the class into the forge EVENTBUS:
'
@EventHandler
public void preInit(FMLPreInitializationEvent event){
CivItems.init();
CivBlocks.init();
CivItems.register();
CivBlocks.register();
//EntityRegisterCiv.addSpawns();
MinecraftForge.EVENT_BUS.register(new spawnWatch());
}
'
My spawnWatch function:
'
public EntityVillageWatch(World worldIn, int x, int y, int z)
{
super(worldIn);
this.posX = x;
this.posY = y;
this.posZ = z;
this.setSize(1.4F, 2.9F);
((PathNavigateGround)this.getNavigator()).func_179690_a(true);
this.tasks.addTask(1, new EntityAIAttackOnCollide(this, 1.0D, true));
this.tasks.addTask(2, new EntityAIMoveTowardsTarget(this, 0.9D, 32.0F));
this.tasks.addTask(3, new EntityAIMoveThroughVillage(this, 0.6D, true));
this.tasks.addTask(4, new EntityAIMoveTowardsRestriction(this, 1.0D));
this.tasks.addTask(6, new EntityAIWander(this, 0.6D));
this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
this.tasks.addTask(8, new EntityAILookIdle(this));
this.targetTasks.addTask(1, new EntityAIGuardVillage(this));
this.targetTasks.addTask(2, new EntityAIHurtByTarget(this, false, new Class[0]));
this.targetTasks.addTask(3, new EntityVillageWatch.AINearestAttackableTargetNonCreeper(this, EntityLiving.class, 10, false, true, IMob.field_175450_e));
}
public static void spawnWatch(World worldIn){
int i = 0;
int numWatch = r.nextInt(20)+21;
List villages = worldIn.getVillageCollection().getVillageList();
while(villages.iterator().hasNext()){
int numWatchSpawned = 0;
Village village = (Village) villages.get(i);
BlockPos center = village.getCenter();
int radius = village.getVillageRadius();
while(numWatchSpawned < numWatch){
worldIn.spawnEntityInWorld(new EntityVillageWatch(worldIn, center.getX() + r.nextInt(radius), center.getY()+r.nextInt(radius), center.getZ()+r.nextInt(radius)));
numWatchSpawned++;
}
i++;
}
}
'
Any suggestions or problems you see with this code? Any help is greatly appreciated.