Solved EntityDamageByEntityEvent/MoveEvent set target of the damager

Discussion in 'Spigot Help' started by Chromecube, Jun 10, 2017.

Thread Status:
Not open for further replies.
  1. Hey Guys,

    I am stuck at a problem and hope that you can help me! I want to implement an "Away from Keyboard" mode where mobs ignore the AFK-player. I already did that, here is my code:
    Code (Java):

    @EventHandler
       public void OnEntityTarget(EntityTargetEvent e) {
         if(e.getTarget().getType() == EntityType.PLAYER) {
           Player p = (Player)e.getTarget();
           if(Variables.afklist.contains(p.getUniqueId())) {
             e.setCancelled(true);
             return;
           }
           if(Variables.godlist.contains(p.getUniqueId())) {
             e.setCancelled(true);
             return;
           }
           Bukkit.getLogger().info(Variables.serverprefix + e.getEntity().getName() + " targets Player " + p.getName() + "!");
         }
       }
     
    But there is one problem: Entities that already targetted the player wont stop doing that. How can I stop them?

    Thanks for your help,

    Chromecube
     
  2. Hey,

    You could try making use of this event,

    EntityTargetLivingEntityEvent "Called when an entity targets a LivingEntity and can only be the target" tho I'm not sure If this will work for you.

    Also you can also disable any health damage using this event,
    • EntityDamageByEntityEvent - Called when entity is hurt by another entity
    I've tried to help you out with my limited knowledge, let us know If you figured it out :)

    P.S Another idea is that If it detects the player as afk, It would despawn/put the mob 20-30 blocks away from the player so It's target would change
     
  3. Thanks for your reply! I did it now like this:

    Code (Java):

    @EventHandler
       public void OnPlayerDamage(EntityDamageByEntityEvent e) {
            if(Variables.afklist.contains(p.getUniqueId())) {
             if(!(e.getDamager().getType() == EntityType.PLAYER))
               e.getDamager().remove();
             e.setCancelled(true);
           }
    }

     
     
    • Friendly Friendly x 1
Thread Status:
Not open for further replies.