Hey guys ..Question. im using this code to make my pigs spawn twins and it works fine until i log out when i rejoin it calls spawn entity a second time and doubles the baby pigs . i think its called again when a chunk reloads but not sureis there a method like LivingEntityEvent that only fires when a entity spawns the first time? heres my code for my event handler. thanks in advance sorry for the red font idk what happened
package bigdirty1985.opig.event;
import com.google.common.eventbus.Subscribe;
import bigdirty1985.opig.EntityAI.EntityAIRoot;
import bigdirty1985.opig.Items.ModItems;
import jline.internal.Log;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.ai.EntityAIEatGrass;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.passive.EntityPig;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.event.entity.living.BabyEntitySpawnEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import scala.util.Random;
public class OPigEventHandler {
EntityPig newPig;
@SubscribeEvent
public void pigSpawn(LivingSpawnEvent e) {
Entity ee = e.getEntity();
//EntityLiving el = (EntityLiving) ee;
if (e.getEntity() instanceof EntityPig){
Log.info("Pig Spawned!");
EntityPig ep = (EntityPig) ee;
makePigsHungry((EntityPig) e.getEntity());
if (ep.getGrowingAge() < 0) {
Log.info("Its a Baby!");
if(ep.getGrowingAge() >= -24000 ) {
Log.info("Twins!");
newPig = new EntityPig(ep.getEntityWorld());
newPig.setPosition(ep.posX, ep.posY, ep.posZ);
//newPig.setHealth(8);
newPig.setGrowingAge(-25000);
makePigsHungry(newPig);
ep.getEntityWorld().spawnEntityInWorld(newPig); //That spawns the Entity
}}
}
}
@SubscribeEvent
public void deadPig(LivingDropsEvent event) {
if (event.getEntity() instanceof EntityPig) {
// DEBUG
System.out.println("EntityPig drops event");
ItemStack itemStackToDrop = new ItemStack(ModItems.bacon, new Random().nextInt(3));
event.getDrops().add(new EntityItem(event.getEntity().worldObj, event.getEntity().posX,
event.getEntity().posY, event.getEntity().posZ, itemStackToDrop));
}
}
private void makePigsHungry(EntityPig e) {
e.tasks.addTask(5, new EntityAIRoot(e));
Log.info("Pig is hungry!");
}
}
You should use BabyEntitySpawnEvent if you want to spawn twins only after pigs breed. LivingSpawnEvent you are using, will fire everytime an entity spawns(on chunk load, on world load etc.).
I had to rework allot but I got it fixed for now. Do the vanilla entities reset their ai every time the chunk loads? because it seems like pausing the game is breaking things until I reload. Thanks BTW
Hey guys ..Question. im using this code to make my pigs spawn twins and it works fine until i log out when i rejoin it calls spawn entity a second time and doubles the baby pigs . i think its called again when a chunk reloads but not sureis there a method like LivingEntityEvent that only fires when a entity spawns the first time? heres my code for my event handler. thanks in advance sorry for the red font idk what happened
I currently have three mods.
Dirty's Cotton 1.0.1 for 1.7.10: forum post
OP-ig Make pigs not useless! Curseforge
I'll get a banner one day. I'm busy
Dirty's Hardcore Vanilla Overhaul Mod(Cancelled until further notice): forum post
You should use BabyEntitySpawnEvent if you want to spawn twins only after pigs breed. LivingSpawnEvent you are using, will fire everytime an entity spawns(on chunk load, on world load etc.).
Let me know if that helps.
I had to rework allot but I got it fixed for now. Do the vanilla entities reset their ai every time the chunk loads? because it seems like pausing the game is breaking things until I reload. Thanks BTW
I currently have three mods.
Dirty's Cotton 1.0.1 for 1.7.10: forum post
OP-ig Make pigs not useless! Curseforge
I'll get a banner one day. I'm busy
Dirty's Hardcore Vanilla Overhaul Mod(Cancelled until further notice): forum post