I'm trying to Make pigs produce multiple offspring and modify the AI on spawn as well.The problem is that i cant get it to spawn a baby, and the adult that i was able to spawn had no AI at all. the pigs that spawn naturally are using my AI. anyway here's the code. thanks in advance.
package bigdirty1985.opig.event;
import com.google.common.eventbus.Subscribe;
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.fml.common.eventhandler.SubscribeEvent;
import scala.util.Random;
public class OPigEventHandler {
EntityPig newPig;
@SubscribeEvent
public void pigSpawn(EntityJoinWorldEvent 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(), e);
if (e.getEntity() instanceof EntityAgeable) {
Log.info("Its a Baby!");
if(ep.getHealth() != 8) {
Log.info("Twins!");
newPig = new EntityPig(ep.getEntityWorld());
newPig.setPosition(ep.posX, ep.posY, ep.posZ);
newPig.setHealth(8);
newPig.createChild(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, EntityJoinWorldEvent event) {
e.tasks.addTask(1, new EntityAIEatGrass(e));
Log.info("Pig is hungry!");
}
}
First of all, there is an event for baby's spawning called BabyEntitySpawnedEvent. You should use that instead of the JoinWorldEvent.
In your code also I think you're:
getting the different pig instances mixed up.
You're only adding the AI to pig that is making the babies, but you never call the makePigHungry() method on the baby pig.
Then you're only creating one child pig, and you're making that the baby for the that joined the world (possibly born). I don't think that is what you wanted to do.
Also your logic for declaring it a baby is wrong. All pig entities are instanceof EntityAgeable. That doesn't mean it is a child, just means it has an age. In fact it will always be true since you've already checked that it is an EntityPig. Instead I think there is an isChild() method.
Also, I'm not sure why you're checking if the healt is !=8. What is that for? All pigs will have same health when born.
Anyway, switching to the BabyEntitySpawnedEvent should make your code a bit simpler because you won't have to check whether it is a child. But you'll still need to take care about which pig is which and apply the AI to all of them.
Thanks for the response but i figured it out myself. BabyEntitySpawned would have been so much easier lol.but thats not what i did...
I was playing with the health to differentiate my babies from the vanilla babies so that i wouldn't create a PigOcalypse but i instead used the growing age to keep from spawning more . I may change my method later to clean things up but this works for now.Thanks again!
P.S. isChild i believe just returns a boolean. an entity is a child if its GrowingAge is less than 0.
package bigdirty1985.opig.event;
import com.google.common.eventbus.Subscribe;
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.fml.common.eventhandler.SubscribeEvent;
import scala.util.Random;
public class OPigEventHandler {
EntityPig newPig;
@SubscribeEvent
public void pigSpawn(EntityJoinWorldEvent 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(1, new EntityAIEatGrass(e));
Log.info("Pig is hungry!");
}
}
I'm trying to Make pigs produce multiple offspring and modify the AI on spawn as well.The problem is that i cant get it to spawn a baby, and the adult that i was able to spawn had no AI at all. the pigs that spawn naturally are using my AI. anyway here's the code. thanks in advance.
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
First of all, there is an event for baby's spawning called BabyEntitySpawnedEvent. You should use that instead of the JoinWorldEvent.
In your code also I think you're:
Anyway, switching to the BabyEntitySpawnedEvent should make your code a bit simpler because you won't have to check whether it is a child. But you'll still need to take care about which pig is which and apply the AI to all of them.
Thanks for the response but i figured it out myself. BabyEntitySpawned would have been so much easier lol.but thats not what i did...
I was playing with the health to differentiate my babies from the vanilla babies so that i wouldn't create a PigOcalypse but i instead used the growing age to keep from spawning more . I may change my method later to clean things up but this works for now.Thanks again!
P.S. isChild i believe just returns a boolean. an entity is a child if its GrowingAge is less than 0.
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