EntityTargetLivingEntityEvent with condition

Discussion in 'Plugin Development' started by ToastHelmi, Aug 11, 2013.

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

    ToastHelmi

    Hey I want zombies only have a target when the condition is true
    my code works fine but i have one problem
    if a zombie set his target and the condition true but now the condition changes and its false how can i update the target
    at the moment i have folowing code

    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void onZombieTargetPlayer(EntityTargetLivingEntityEvent event)
    3. {
    4. if ((event.getEntity() instanceof Zombie)
    5. && event.getTarget() instanceof Player)
    6. {
    7. final Player p = (Player) event.getTarget();
    8. final Zombie z = (Zombie) event.getEntity();
    9.  
    10. List<Entity> nearBys = z.getNearbyEntities(z.getLocation().getX(),
    11. z.getLocation().getY(), z.getLocation().getZ());
    12.  
    13. int maxWanted = 0;
    14. for (Entity e : nearBys)
    15. {
    16. if (e instanceof Player)
    17. {
    18. Player NBP = (Player) e;
    19. if ((int)getMetadata(NBP, "WantedLevel") > maxWanted)
    20. {
    21. System.out.println("4 targed changed");
    22. maxWanted = (int)getMetadata(NBP, "WantedLevel");
    23.  
    24. event.setTarget(NBP);
    25.  
    26. }
    27. }
    28. }
    29.  
    30. if ((int)getMetadata((Player)event.getTarget(),"WantedLevel") <= 0)
    31. {
    32. event.setCancelled(true);
    33. }
    34. }
    35. }
     
  2. Offline

    valon750

    } else {
    event.setCancelled(true);
    return;
    }

    That SHOULD do it, at least that's what worked when I was testing it with creepers a few weeks back.
     
  3. Offline

    ToastHelmi

    valon750
    are you sure i mean the target is already set isnt it?

    i tryed it and it doesent work after the zombie has set me as target and than i change the condition to false he still wants to atack me
     
  4. Offline

    valon750

    ToastHelmi

    When tested with a simple..

    if(event.getTarget() instanceof Player){
    event.setCancelled(true);
    return;
    }

    It cancelled the event just fine.
     
  5. Offline

    ToastHelmi

    valon750
    i think you think i have a other proplem :D
    the event is also cancelled fine without return;

    my problem is a zombie is searching his target when the condition is true
    zombietarget = player

    but now the condition turns into false
    but still

    zombietarget = player

    but what i want is
    zombietarget = null or zombietarget = new player

    if the zombie have a target it will not call a
    EntityTargetLivingEntityEvent
    again

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page