How to use FindNearestAttackableTarget

Hi, I’m trying to rebuild an old favorite plugin of mine and was curious how would would use the FindNearestAttackableTargetAITask and TargetAITask ? i Believe these are the two that allow for control of the range a ‘creature’ can see and then the second one i believe allows it to attack…if not can someone point me in the correct direction. pretty much i want to expand the range of what a creature can sense a player from and then have them turn around and hunt the player down.

As far as I’m aware the EntityAI API is still in the works.

You will be able to do it soon though.

TL;DR: To set the range, use NMS:

((EntityLiving) entity).getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(100)

Where 100 is the follow range you want.

The first phase of the AI API is in the latest SpongeAPI, and fully implemented. However, there are still many Vanilla AI tasks which need to be exposed. If there’s a particular task you want to be able to access, feel free to open an issue on SpongeAPI. The second phase (pathfinding and other fun stuff) will be coming later.

In answer to your question, TargetAITask is the base interface for all target tasks (you’ll notice that FindNearestAttackableTargetAITask extends it).

A target task is, in theory, only responsible for setting the target of an entity (accessible through TargetLivingData). The actual logic of attacking (hitting with a sword, shooting an arrow, etc), is handled by tasks such as AttackLivingTask.

One of the goals of the AI API is to modify builtin tasks as little as possible, to preserve Vanilla and mod compatibility. Plugins can always re-implement tasks, and add new features, by extending AbstractAITask and creating their own task.

At the moment, it’s not possible to change the range (using the API) of any task extending TargetAITask, until the Attributes API is written and implemented. However, you can use some NMS for now to accomplish this:

((EntityLiving) entity).getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(100)

Where 100 is the range you want to set. For reference, the default value is 32.

TL;DR: To set the range, use NMS:

whatis this?

and how would i encorporate this with say a creeper? :slight_smile:

NMS stands for net.minecraft.server. All it means is casting to the underlying Minecraft object.

If you’re running your plugin from your IDE (which you should, as it’s much easier than building a jar to test changes), then you can add SpongeForge as a dependency of your plugin. This will give you access to the Minecraft sources, which you can use with the code in my previous post.

ok…i have maven dependancies with sponge api loaded under it…how would i add spongeforge?

I think you need to add the maven repository:

…i use eclipse…do i add that into the pom file? obviously for Maven