EntityPathfindEvent is expected to fire when a slime pathfinds, as well as other mobs
When a slime hops about, changes direction etc. EntityPathfindEvent does not appear to fire at all.
I am using the following code in a listener to test:
@EventHandler
public void onPathfind(EntityPathfindEvent event )
{
Entity entity = event.getEntity();
BoatBall.getInstance().getLogger().log(Level.INFO, "{0} is pathfinding!", entity.getName());
if (entity instanceof Slime)
{
event.setCancelled(true);
BoatBall.getInstance().getLogger().log(Level.INFO, "{0} pathfinding has been cancelled.", entity.getName());
}
}
Spawn a few slimes:
/summon slime
Which results in the following lines:
[02:42:59 INFO]: [TestPlugin] Chicken is pathfinding!
[02:44:04 INFO]: [TestPlugin] Chicken is pathfinding!
[02:44:07 INFO]: [TestPlugin] Zombie is pathfinding!
[02:44:39 INFO]: [TestPlugin] Cow is pathfinding!
[02:45:33 INFO]: [TestPlugin] Creeper is pathfinding!
[02:45:39 INFO]: [TestPlugin] Chicken is pathfinding!
But no mention of the slimes pathfinding which can be seen hopping about in-game.
TestPlugin (runs the above code)
Paper version git-Paper-1249 (MC: 1.12.2) (Implementing API version 1.12.2-R0.1-SNAPSHOT)
The event is not fired for their general hopping because there is no pathfinding involved in their general hopping. They literally just pick a random direction and hop around. There is no targetEntity to provide, no location to provide, it's doesn't match the typical "Pathfinding" of other entities.
That said they do track players, so once that occurs we should be firing this event in that case.
Argh. So there's no method to block their general hopping about?
I'd turn their AI off, but that prevents all movement (I'm providing my own physics).
Bats also suffer this same behavior. I added a hacky event to my fork to control bats for that :/
Looks like Ghasts as well
I shopped around by looking at Sponge which seems to have a way to listen for a WanderAITask via AITaskEvent, which I assume is the behaviour we are talking about. Do you think adding an event for that would be feasible in Paper?
Switching over to Sponge and learning a whole new plugin API seems a little excessive just to stop slimes jumping about :p
Slime is a little weird. It has a few pathfinders that would qualify as "wandering."
One is PathfinderGoalSlimeRandomDirection where it will turn and face a random direction every 40-100 ticks (2-5 seconds) if its not targeting a player and on the ground.
Another is PathfinderGoalSlimeRandomJump which is named wrong. Its the pathfinder used to make slime swim in water.
Another is PathfinderGoalSlimeIdle which is called when the slime moves forward.
Another is PathfinderGoalSlimeNearestPlayer which is similar to PathfinderGoalSlimeRandomDirection as in it makes the Slime change it's facing direction, but this time it is towards the nearest Player within range.
To put a cancellable event in to make it stop wandering and hopping about seems like it would be rather spammy. Instead, I suggest a new flag to toggle these pathfinders on/off at will. Something like Slime#setWander(boolean).
Another alternative (for those plugins making their own Slime based entities with custom pathfinders) is to simply not call super.r() and just manually set all the pathfinders they want. But this wont help most plugins that rightfully stay away from NMS.
Now, as for the targeting players. From all my testing Slime fires the EntityTargetEvent and EntityTargetLivingEntityEvent just fine. I am able to cancel them both and Slimes just jump around ignoring me like I'm not there. They will still damage when touched, but thats unrelated and expected behavior, imo.
I am working on a PR to add Slime#setWander(boolean) and a new event SlimePathfindEvent. The original EntityPathfindEvent wont work here because it expects a target Location, which Slimes do not have any target location, just direction. They are always moving forward where they are facing and only change directions when either PathfinderGoalSlimeRandomDirection or PathfinderGoalSlimeNearestPlayer pathfinders are triggered.
Most helpful comment
I shopped around by looking at Sponge which seems to have a way to listen for a WanderAITask via AITaskEvent, which I assume is the behaviour we are talking about. Do you think adding an event for that would be feasible in Paper?
Switching over to Sponge and learning a whole new plugin API seems a little excessive just to stop slimes jumping about :p