How do I prevent Wolves from retaliating to attacked owners? Code inside.

Discussion in 'Plugin Development' started by ninwa, Apr 12, 2011.

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

    ninwa

    I use this code when a damage event is recognized, more specifically when one player hits another player. As you can guess, it's not working. Despite my best efforts, the wolf still attacks the other player. Any ideas?

    Code:
            List<Entity> nearby = attacker.getNearbyEntities(40, 40, 40);
                for(int i = 0; i < nearby.size(); i++){
                    if( nearby.get(i) instanceof Wolf ){
                        Wolf w = (Wolf) nearby.get(i);
                        w.setAngry(false);
                        w.setTarget(null);
                    }
                }
    
    My only guess as to why is that the wolf-attack event, or however it's triggered, is actually called after the damage event calls are ran.

    I've tried setting the event register for ENTITY_DAMAGE to Highest, also.
     
  2. Offline

    Cartan12

    Try it with the onEntityTargetEvent.
    Something like:
    Code:
    if ((event.getTarget instanceof Player) && (event.getEntity instanceof Wolf)) event.setCancelled(true);
     
  3. Offline

    ninwa

    By my tests, oddly, an EntityTargetEvent is only actually triggered when the wolf STOPS targeting the player. I'm not sure why, maybe a bug.
     
  4. Offline

    Mixcoatl

    This event is triggered when the mobile's target changes.
    You're probably looking for the onEntityDamage event.
     
  5. Offline

    ninwa

    Could you elaborate? From what I know, a wolfs target is his owner, unless he's angry, then it's his attacking target. If I checked for EntityTargetEvent I should be able to see when a wolf becomes angry, and set him back to his previous target and set him to not angry. The problem with this is I have no easy way to know his previous target. Minecraft knows, but bukkit doesn't let me access this easily.

    The only way I have stopped a wolf from attacking is after it has already attacked. In onEntityDamage I can watch the attacker and attacked, and if a wolf attacks a player I can set it's angry status to false and target to null and it seems to give up. This is not a good solution though, because the player still gets attacked at least once.

    I still don't understand why EntityTargetEvent is only called when a wolf finishes attacking his enemy. It really should be called when he becomes angry and choses an attacking target, too.

    I looked at CraftBukkit and Minecraft wolf code, but I'm not familiar enough with it to know how to fix this.
     
  6. Offline

    Mixcoatl

    The targeting bit is a little crazy. I've not heard of it working correctly. In theory, watching the wolf's target should be sufficient. But perhaps this doesn't even count as a target change in Bukkit.
    What I'm suggesting is to cancel the attack if the target entity is a wolf and belongs to the attacking player.
    My guess is that it's not invoked again because the target hasn't changed. If you attack your own wolf, only its angry flag should change. My guess is there is some magic going on internally to change the wolf's attacking state.
     
  7. Offline

    ninwa

    Albiet clumsily, this worked. The wolf now reaches the player and just stops in its tracks (doesn't attack). I'm halfway there, but for now it'll work until the API for handling wolves and their events becomes a little clearer and/or I become less ignorant about it. :)

    Thank you for your help! :)

    Afterthought: could possibly check wolves onMove event, seeing if it's angry and then cancelling it's pathfinding toward target, but this seems like it'd take up an unneccessary amount of server cpu time.
     
  8. Offline

    Mixcoatl

    I know, right? XD
    Glad you got it (sort of) working!
    Sure, that's possible. I'm not certain you wouldn't have better luck just preventing players from harming their own wolves entirely. I would be willing to bet that, internally, the wolf is no longer considered owned by you once you've damaged it.
     
Thread Status:
Not open for further replies.

Share This Page