Make any mob hostile/attack players

Discussion in 'Plugin Development' started by Ne0nx3r0, Mar 3, 2013.

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

    Ne0nx3r0

    I'm not very familiar with how to do this, what's the best way to make chickens/golems/pig zombies/etc. constantly attack any nearby player?

    My best guess so far is to set a task that constantly finds the nearest player and uses entity.setTarget(), but this feels inefficient and I was wondering if there's a way to set it's behavior to hostile.
     
  2. make custom entities and make them interact on players blahblah...ot just do it the way u said..(that way seems legit)
     
  3. Offline

    Ne0nx3r0

    Haven't had any luck; setTarget doesn't seem to make the mob hostile, and it doesn't even really seem to work all that reliably, even in a repeating task.
     
  4. Offline

    Cybermaxke

    I am working on a easy extended entities api, but I haven't finished the target/attacking ai yet.
    I will tag once finished, if you want. ;)
     
  5. Offline

    Ne0nx3r0

    Yeah definitely let me know. I've been trying to sort this out for a few weeks now.
     
  6. Even if the entity won't lose the target, chickens will neither go towards the target nor deal damage to it. This is because of the way the movement and attacking works. Every entity has goals which it would like to fulfill and each of them has a different priority. Most, actually kinda every one, of them is a movement goal including some that let the entity move towards their target. Zombies, creepers, etc. all have such a goal to make them move towards their target, but entities such as a pig or cow don't have them (why should they? they don't need that normally). Neither do those entities have a goal which makes use of their attack if they had any. However, this is not something bukkit did, you have to use NMS stuff for this.
    How can you use that? If you take a look at the EntityLiving class you'll find a field called 'goalSelector'. This selector holds all those goals in it and selects which one it should use. You want to get the instance of this selector for your attacking entity. The field is protected so you could directly access it if you make your own entity. Then you can use its a() method to add the attacking and movement goal and you're good to go! I'm not entirely sure if setTarget will still bug out, but it should work properly.

    Goals you might need:
    PathfinderGoalMoveTowardsTarget
    PathfinderGoalArrowAttack
    PathfinderGoalLeapAtTarget
    PathfinderGoalMeleeAttack
    PathfinderGoalOceloteAttack
    PathfinderGoalSwell

    Or use a library. Either use like CitizensAPI or RemoteEntities.
     
  7. Offline

    Ne0nx3r0

    Well, I'm not very familiar with NMS code diving, but it looks like you've basically laid the whole thing out for me. I'll give this a shot, thanks!

    kumpelblase2 After re-reading your post to try it out I just noticed your library, it looks cool.

    Basically all I want to do is make passive mobs spawned through this plugin attack players instead of act passive.

    What action/movement desires, or how would you recommend I set up passive mobs to attack players using your API?

    I haven't been able to test it yet, but so far I've got:
    Code:java
    1. rEntity.getMind().addMovementDesire(new DesireMoveToTarget(rEntity, 8F), 1);
    2.  
    3. rEntity.getMind().addActionDesire(new DesireAttackOnCollide(rEntity, EntityHuman.class, true), 1);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. You don't need MoveToTarget as AttackOnCollide does not only do the attacking part but also movement. Because of that, it also belongs to the movement desires, too. As a hint: you don't need to pass over the NMS class, you can use the bukkit one as well (so you can use it without craftbukkit and NMS).
     
  9. Offline

    Ne0nx3r0

    kumpelblase2

    I'm sorry I can't seem to get this to work; the mobs just stand around and stare. At one point I got them to attack, but only the already aggressive mobs (not chickens for example, they would just move towards the player) but only every 10-20 seconds or so.

    Here's essentially what I'm trying, in different premutations:
    Code:java
    1. RemoteEntity rEntity = plugin.entityManager.createEntity(RemoteEntityType.valueOf(entityType.getName()), location, false);
    2.  
    3. entity = rEntity.getBukkitEntity();
    4.  
    5. //rEntity.getMind().addActionDesire(new DesireFindAttackingTarget(rEntity, 30, false, true, true),1);
    6. //rEntity.getMind().addActionDesire(new DesireFindNearestTarget(rEntity, EntityHuman.class, 30, false, true, 100),1);
    7. rEntity.getMind().addActionDesire(new DesireAttackOnCollide(rEntity, HumanEntity.class, true), 1);


    How would you for example make a chicken or cow attack a player?
     
  10. Offline

    Vexil

    How did you get this to work?
     
  11. bump
     
Thread Status:
Not open for further replies.

Share This Page