Ally with Entity

Discussion in 'Plugin Development' started by Draesia, Dec 6, 2013.

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

    Draesia

    How would I go about setting a entity as an ally to a player, such that, they won't target them, but will target all other players?
     
  2. Offline

    MrInspector

    Code:java
    1.  
    2. entity.setTamed(player);
    3.  

    Pretty sure.
     
  3. Offline

    Gater12

    Tupay It should accept a boolean value to set either if it is tamed or not. And it's for Tameable entities like Horses, Ocelots, Wolves and to assign the owner, it would be:
    Code:java
    1. entity.setOwner(Player name);
     
    Tupay likes this.
  4. Offline

    Draesia

    That would only work for Wolves, Ocelots and Horses however, my primary use would be Zombies and Skeletons, however they do not implement Tameable. :(
    /ninja'd
     
  5. Offline

    MrInspector

    Forgot it was setOwner *facepalm*

    Ah okay Draesia, sorry that I couldn't help. :p
     
    Gater12 likes this.
  6. Offline

    Wolfey

    I believe you should use EntityTargetEvent, then check if the entity is a player, then write your other code in there.

    Code:java
    1.  
    2. @EventHandler
    3. public void onTarget(EntityTargetEvent e) {
    4. if(e.getTarget() instanceof Player) {
    5. Player target = (Player) e.getTarget();
    6. if(blahblah.contains(target.getName())) {
    7. e.setCancelled(true);
    8. }
    9. }
    10. }
    11.  
     
    Tupay likes this.
  7. Offline

    Draesia

    I haven't tested this, but wouldn't it just keep re-targeting the same player? :O
     
  8. Offline

    Wolfey

    No, if blahblah contains the target, it would cancel the every single time the entity keeps targeting the player, it works, i've tested it out.

    if you want a certain entity to not attack the player, check the entity with e.getEntity();
     
  9. Offline

    Draesia

    I mean, wouldn't it ATTEMPT to retarget the same player, and never attack another player, because it's trying the same player first, if you understand what I mean. I can't test it right now because it would involve 2 players
     
  10. Offline

    Wolfey

    if it re-targets the player, it would cancel it, if that's what you're saying. But if it targets someone else, it wont cancel that.
     
  11. Offline

    Draesia

    If there is 2 players in the entities range, and the closest one is 'allied' with it, it will keep targetting and cancelling that one, since it's closest, it will never even attempt to target the other player, even though if it did, it would actually target it.
     
  12. Offline

    Wolfey

    Oh I see what you're saying. I'm going to test it out right now. If it goes like you said it would, then there's another option called e.setTarget(Entity ent), I can set the target if it doesn't work, but I think it should

    Nope, it works perfectly, try it out.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  13. Offline

    Draesia

    Ok, I will do next time I have multiple people, thankyou :)
     
  14. Offline

    Wolfey

    No problem, glad to help :)

    If you find any bugs, don't hesitate to ask
     
  15. Offline

    Scyntrus

    I personally just created my own entity class that extends one of the original entities and overwrote the findTarget method. I'm not sure you need to do something so complicated, but I found that repeatedly cancelling the entitytargetevent was inefficient and sometimes unreliable.
     
  16. Offline

    Wolfey

    It doesn't even seem like it is constantly targeting the entity, then canceling it. I think it cancels it once and then moves on to another player/entity. However, if it is inefficient, like I said, you can also check the closest player near you and use e.setTarget(Entity e) {} and set the target yourself.
     
  17. Offline

    Scyntrus

    If there are no other players nearby for it to target, it will constantly try to target you.
     
Thread Status:
Not open for further replies.

Share This Page