NOTE: Foremost I'd like to say that you do need forge, as we will be using the EntityJoinWorld event. If you find how to execute code whenever an entity is spawned without forge, by all means you can use that too. However since forge has this functionality already built in, I will be using Forge.
I will be using particular examples, however you can apply this anywhere. If you want to stop creepers exploding, or make animals attack you, you will need to replace, add, or remove certain entity ai's. Here is how!
What is required in the following examples:
You are going to need an event, which will be fired every time an entity is spawned in the world. Firstly, inside a new class called MyEvents.class, add this:
@SubscribeEvent
public void onEntitySpawn(EntityJoinWorldEvent e) {}
Since only entities extending EntityLiving can support ai, we need a flag:
@SubscribeEvent
public void onEntitySpawn(EntityJoinWorldEvent event)
{
if (event.entity instanceof EntityLiving) doTaskStuff((EntityLiving) event.entity, event);
}
private void doTaskStuff(EntityLiving e, Event ev)
{
//Do our code here!
}
Now, the following examples will show code that MUST ONLY go in "doTaskStuff( ... )"!
Making creepers dormant:
Creepers can be quite the nuisance. If you as a modder would like to change their very behaviour, you're going to need to remove such AI, this means we're going to need to check out which AI to remove. In eclipse, press Ctrl + Shift + T, and type in EntityCreeper, open it up and scroll down a little.
You should see this:
public EntityCreeper(World par1World)
{
super(par1World);
this.tasks.addTask(1, new EntityAISwimming(this));
this.tasks.addTask(2, new EntityAICreeperSwell(this));
this.tasks.addTask(3, new EntityAIAvoidEntity(this, EntityOcelot.class, 6.0F, 1.0D, 1.2D));
this.tasks.addTask(4, new EntityAIAttackOnCollide(this, 1.0D, false));
this.tasks.addTask(5, new EntityAIWander(this, 0.8D));
this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
this.tasks.addTask(6, new EntityAILookIdle(this));
this.targetTasks.addTask(1, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));
this.targetTasks.addTask(2, new EntityAIHurtByTarget(this, false));
}
If you're quick, you'll see 3 relevant tasks we need to remove called "EntityAICreeperSwell", "EntityAIAttackOnCollide", and "EntityAINearestAttackableTarget". However, the third task is located in a list called "targetTasks", while the rest are in "tasks".
We now know which tasks to remove. I like to do it using a for-loop.
Add this in doTaskStuff:
for (Object a : entity.tasks.taskEntries.toArray())
{
EntityAIBase ai = ((EntityAITaskEntry) a).action;
//We put new code here
}
However since there are two lists "tasks" and "targetTasks", we will need two for-loops. Here is the second one, add this after the first bit of code:
for (Object a : entity.targetTasks.taskEntries.toArray())
{
EntityAIBase ai = ((EntityAITaskEntry) a).action;
//We put new code here
}
Now, we can remove the tasks. To check for a certain task, I always use the check:
if (e instanceof EntityChicken && ai instanceof EntityAIMate) e.tasks.removeTask(ai);
For example, this will check for all tasks in the list and remove any that cause the mob to mate with another mob.
In this example, we will adjust the code above to remove the tasks "EntityAICreeperSwell", and "EntityAIAttackOnCollide" using the OR operator. Add this code inside the first for-loop.
if (e instanceof EntityCreeper && (ai instanceof EntityAICreeperSwell || ai instanceof EntityAIAttackOnCollide)) e.tasks.removeTask(ai);
Perfect! But we're not done, we still have to remove "EntityAINearestAttackableTarget".
Since this task is in "targetTasks", we add the line below to our 2nd for-loop.
if (e instanceof EntityCreeper && ai instanceof EntityAINearestAttackableTarget) e.targetTasks.removeTask(ai);
Done!
The result? All creepers will no longer explode or otherwise harm anything ever again.
Making cows attack you:
Because making cows hostile would make milking them so exciting. Similar to the above example but simpler, we will just be adding a new task.
Inside your "doTaskStuff" method, simply add the following lines:
if (e instanceof EntityCow)
{
this.tasks.addTask(2, new EntityAIAttackOnCollide(this, EntityPlayer.class, 1.0D, false));
this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));
}
Aaaand.... Done! That is all.
By combining the removing and adding of ai tasks, one can modify existing ai with their own, improved one. For example, I can remove EntityAIBreakDoor and replace it with EntityAIBreakDoorAndTrapdoor (pretty self explanatory), the possibilities are almost endless.
You now know how to change the ai of creepers, cows, and whatever you want. You can also add your very own ai.
Below is some pretty pointless ai: The mob with this ai will simply jump up VERY high if it is in water.
Inside a new class called EntityAIJumpOutOfWater:
public class EntityAIJumpOutOfWater extends EntityAIBase {
private EntityLiving theEntity;
public EntityAIJumpOutOfWater(EntityLiving par1EntityLiving)
{
this.theEntity = par1EntityLiving;
}
public boolean shouldExecute()
{
return theEntity.isInWater();
}
public boolean continueExecuting()
{
return theEntity.isInWater();
}
public void startExecuting()
{
this.theEntity.motionY = 2;
}
public void resetTask()
{
//Nothing to reset, if you have variables, you can set them to 0, false, "", etc here.
}
public void updateTask()
{
//We have nothing to update, you can't continue jumping, you jump once then fall back down.
}
}
That's all for this tutorial, if you have any inquiries, do tell.
thanks for this tutorial, it really helped me. The way you are explaining things is really good.
There is just a minor mistake in your code, where you are editing the AI of the cow:
this.tasks.addTask(2, new EntityAIAttackOnCollide(this, EntityPlayer.class, 1.0D, false)); ((EntityCow) e).tasks.addTask(4, new EntityAIAttackOnCollide( e, EntityPlayer.class, 1.0D, false));
But your tutorial really helped me out, thanks a lot again!
This also works in 1.8 Forge
Oh, ok No problem. thanks for pointing it out. I'd fix it but MCF has a nasty habit of messing your comments up whenever you try to edit it.
Thank God I finally found something! I am new to Modding, I have a rather advanced question. I am creating a mod, and I want my custom mob to be like a snow golem and a wolf. But I don't want it to drown in water or die in rain. How would I take away these features and give it a few traits of a wolf?
Thank God I finally found something! I am new to Modding, I have a rather advanced question. I am creating a mod, and I want my custom mob to be like a snow golem and a wolf. But I don't want it to drown in water or die in rain. How would I take away these features and give it a few traits of a wolf?
If you're creating a custom mob from scratch, then this tutorial probably won't help very much.
Just add AI to your creature like this:
this.tasks.addTask(PRIORITY, new EntityAIMyTask(this, arg0, arg1, arg...));
I think you would just do the same thing as with the passive creeper, but change it so that it doesn't throw potions rather than blow up (cuz idt they blow up lol).
I want to create a Small mod, that would change the ender dragons attitude, fighting style (more difficult, but still utilizing the original end crystals and such), looks and more. Is there any tips, you could give me on this?
I've mostly studied (in school for 4 years) on modelling and such- I have very minor understanding on coding- but i have watched some YouTube tutorials. (+ some of the basics i learned in school, but its been almost 3 years from it and its hard to remember)
ps. I don't want to make the EDragon Op- I just want to spice up the vanilla experience to add a new things to learn....
NOTE: Foremost I'd like to say that you do need forge, as we will be using the EntityJoinWorld event. If you find how to execute code whenever an entity is spawned without forge, by all means you can use that too. However since forge has this functionality already built in, I will be using Forge.
I will be using particular examples, however you can apply this anywhere. If you want to stop creepers exploding, or make animals attack you, you will need to replace, add, or remove certain entity ai's. Here is how!
What is required in the following examples:
And call it in the main mod class with:
Since only entities extending EntityLiving can support ai, we need a flag:
Now, the following examples will show code that MUST ONLY go in "doTaskStuff( ... )"!
Making creepers dormant:
You should see this:
{
super(par1World);
this.tasks.addTask(1, new EntityAISwimming(this));
this.tasks.addTask(2, new EntityAICreeperSwell(this));
this.tasks.addTask(3, new EntityAIAvoidEntity(this, EntityOcelot.class, 6.0F, 1.0D, 1.2D));
this.tasks.addTask(4, new EntityAIAttackOnCollide(this, 1.0D, false));
this.tasks.addTask(5, new EntityAIWander(this, 0.8D));
this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
this.tasks.addTask(6, new EntityAILookIdle(this));
this.targetTasks.addTask(1, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));
this.targetTasks.addTask(2, new EntityAIHurtByTarget(this, false));
}
If you're quick, you'll see 3 relevant tasks we need to remove called "EntityAICreeperSwell", "EntityAIAttackOnCollide", and "EntityAINearestAttackableTarget". However, the third task is located in a list called "targetTasks", while the rest are in "tasks".
We now know which tasks to remove. I like to do it using a for-loop.
Add this in doTaskStuff:
However since there are two lists "tasks" and "targetTasks", we will need two for-loops. Here is the second one, add this after the first bit of code:
Now, we can remove the tasks. To check for a certain task, I always use the check:
For example, this will check for all tasks in the list and remove any that cause the mob to mate with another mob.
In this example, we will adjust the code above to remove the tasks "EntityAICreeperSwell", and "EntityAIAttackOnCollide" using the OR operator. Add this code inside the first for-loop.
Perfect! But we're not done, we still have to remove "EntityAINearestAttackableTarget".
Since this task is in "targetTasks", we add the line below to our 2nd for-loop.
Done!
The result? All creepers will no longer explode or otherwise harm anything ever again.
Making cows attack you:
Inside your "doTaskStuff" method, simply add the following lines:
Aaaand.... Done! That is all.
You now know how to change the ai of creepers, cows, and whatever you want. You can also add your very own ai.
Below is some pretty pointless ai: The mob with this ai will simply jump up VERY high if it is in water.
Inside a new class called EntityAIJumpOutOfWater:
That's all for this tutorial, if you have any inquiries, do tell.
Do you mean this code still works in 1.8, because I have been trying but without any success.Never mind, I have found a solution.
Oh, ok No problem. thanks for pointing it out. I'd fix it but MCF has a nasty habit of messing your comments up whenever you try to edit it.
Ah, cool.
Yes.
How will you make them explode?
Ahh, ok, a proximity event. Well that explains itself then in the code posted in the tutorial. If you need help, just ask.
If you're creating a custom mob from scratch, then this tutorial probably won't help very much.
Just add AI to your creature like this:
this.tasks.addTask(PRIORITY, new EntityAIMyTask(this, arg0, arg1, arg...));
Can I use this to make a witch not harm other mobs and players
Pls reply I really need to know how to spawn a pasive witch
How would you make it so that both Ocelots and house cats have the behavior of Zombie pigmen?
IE If you attack an Ocelot/Cat, the other ones in that proximity will also attack you?
Can someone pls tell me how to make a pasive witch
I think you would just do the same thing as with the passive creeper, but change it so that it doesn't throw potions rather than blow up (cuz idt they blow up lol).
HEY HOW TO CHANGE PIGLIN BARTERING TRADE
I want to create a Small mod, that would change the ender dragons attitude, fighting style (more difficult, but still utilizing the original end crystals and such), looks and more. Is there any tips, you could give me on this?
I've mostly studied (in school for 4 years) on modelling and such- I have very minor understanding on coding- but i have watched some YouTube tutorials. (+ some of the basics i learned in school, but its been almost 3 years from it and its hard to remember)
ps. I don't want to make the EDragon Op- I just want to spice up the vanilla experience to add a new things to learn....
This is exactly what iv been looking for. Thanks!