Help with EntityTargetEvent

Discussion in 'Plugin Development' started by Horsey, May 20, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Horsey

    Hi there :D
    I'm having a little trouble with the EntityTargetEvent. When a Illager/Zombie tries to target a villager, I set the event to cancelled, but the villager still runs away. Maybe something wrong with my code?
    Code:java
    1.  
    2. package me.horsey.duel;
    3.  
    4. import org.bukkit.entity.Entity;
    5. import org.bukkit.entity.Evoker;
    6. import org.bukkit.entity.Villager;
    7. import org.bukkit.entity.Vindicator;
    8. import org.bukkit.entity.Zombie;
    9. import org.bukkit.entity.ZombieVillager;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityTargetEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener {
    16.  
    17. public void onEnable(){
    18. getServer().getPluginManager().registerEvents(this,this);
    19. }
    20. //I'm a horse
    21.  
    22. public void onDisable(){
    23. }
    24.  
    25. @EventHandler
    26. public void onTarget(EntityTargetEvent event){
    27.  
    28. Entity targeter = event.getEntity();
    29. Entity target = event.getTarget();
    30.  
    31. if (targeter instanceof Vindicator || targeter instanceof Evoker || targeter instanceof Zombie || targeter instanceof ZombieVillager){
    32.  
    33. if (target instanceof Villager){
    34. event.setCancelled(true);
    35.  
    36. }
    37.  
    38. }
    39.  
    40. }
    41.  
    42. }
    43.  

    Anyway, if the event is supposed to work this way, maybe someone can suggest a way to do it by editing the Villager class or something?
     
  2. Offline

    Horsey

    Bump?
     
  3. Online

    timtower Administrator Administrator Moderator

    @Horsey Is the running away even depending on being a target then?
     
  4. Offline

    Horsey

    @timtower Yeah it is, the villagers run away from the zombies until they get out of range, then they go back to walking around normally.
     
  5. Online

    timtower Administrator Administrator Moderator

    @HorseyBut is that because the villager is the target of the zombie or that the villages knows about the zombie?
     
  6. Offline

    Zombie_Striker

    @Horsey
    The villager's AI is independent of if they are being targeted. If you look at the NMS code, the pathfiner goal for the villager just checks if there is a zombie near them, not if they are being targeted. In order to disable villagers from running away, you have to remove are edit that pathfinding goal.
     
  7. Offline

    Horsey

    @timtower @Zombie_Striker
    Ah I see what you guys mean, the villager runs away without being targeted... Anyone of you know a good tut to change pathfinding goals?
     
Thread Status:
Not open for further replies.

Share This Page