I know its terrible practice to edit base classes, thus i am asking anyone out there if i can do something like this... make a mob follow me for an item...such as a chicken follow me with a new item i have created, but the problem is its terrible practice to edit base classes, so I'm wondering how i can get a chicken to follow me for either seeds or my custom item using the EntityAITempt code! I want to thank everyone in advance for helping me out... i would like an example code in your comment but please explain it, i don't want to copy and blindly paste it and not understand what the heck the set of code i have does...
You don't need to edit any base classes for something like this, all you need to do is add a new AI task to the creature on spawn event (EDIT: Or join world, whatever, dunno what the difference is really - don't think it matters for this case), Whov and Vic_ have the right idea - don't dismiss events so quickly!
Here's an example. All this does is make all animals (EntityAnimal) run away from the player:
public void onEntityJoinWorld(EntityJoinWorldEvent event) {
if(!event.entity.worldObj.isRemote) {
if (event.entity instanceof EntityAnimal) {
EntityAnimal entityAnimal = (EntityAnimal) event.entity;
if (entityAnimal.tasks.taskEntries.size() > 0) { // ensure it actually uses the new AI, just in case
entityAnimal.tasks.addTask(6, new EntityAIAvoidEntity(entityAnimal, EntityPlayer.class, 6.0F, 1.5D, 2.0D));
}
}
}
}
You probably can already gather what your addTask command would be like:
entityAnimal.tasks.addTask(3, new EntityAITempt(entityAnimal, 1.0D, Items.yourCustomItem, false));
..and I guess you'd want to check if it's a Chicken or whatever rather than all animals, if that wasn't obvious. You said you wanted explanation, but you mentioned you already know about EntityAITempt so I assume this is pretty simple for you to understand as is
EDIT: Oh incase you were wondering the first int (the "3" in EntityAITempt) is the priority, where lower numbers = higher priority. You wouldn't want to go any lower than 3 in most cases, it varies per animal but I think for all, 2 is mate, 1 is panic, and 0 is swim. Also note that multiple tasks can exist with the same priority, they'll get equal share/chance for that priority "slot". Personally I parse the whole existing task list and inject it with the same priority as Wander task (because it varies per animal, I've snipped the code above) but that's maybe excessive and performance costly, I've not yet tested such a thing. Full source for that whole thing is here if you're interested (the very last method).
As mentioned, you can edit any vanilla classes that provide useful public fields or methods by simply accessing those. Since the AI task list is public, you can just clear the list and then build up your own, including custom AI classes.
Even if the fields or methods you want to use are private or protected, you can use Java reflection to access them.
Further, for some common things that modders may like to do, Forge provides events that allow you to intercept vanilla processing.
Lastly, for changes that can't be done by the above, there is also access transformers (ASM).
So those are the ways you can interact with vanilla classes without modifying them.
Again i want to thank anyone who comments!
Here's an example. All this does is make all animals (EntityAnimal) run away from the player:
You probably can already gather what your addTask command would be like:
..and I guess you'd want to check if it's a Chicken or whatever rather than all animals, if that wasn't obvious. You said you wanted explanation, but you mentioned you already know about EntityAITempt so I assume this is pretty simple for you to understand as is
EDIT: Oh incase you were wondering the first int (the "3" in EntityAITempt) is the priority, where lower numbers = higher priority. You wouldn't want to go any lower than 3 in most cases, it varies per animal but I think for all, 2 is mate, 1 is panic, and 0 is swim. Also note that multiple tasks can exist with the same priority, they'll get equal share/chance for that priority "slot". Personally I parse the whole existing task list and inject it with the same priority as Wander task (because it varies per animal, I've snipped the code above) but that's maybe excessive and performance costly, I've not yet tested such a thing. Full source for that whole thing is here if you're interested (the very last method).
Even if the fields or methods you want to use are private or protected, you can use Java reflection to access them.
Further, for some common things that modders may like to do, Forge provides events that allow you to intercept vanilla processing.
Lastly, for changes that can't be done by the above, there is also access transformers (ASM).
So those are the ways you can interact with vanilla classes without modifying them.