Solved Force player to attack entity

Discussion in 'Spigot Plugin Development' started by NoComment_, Apr 17, 2017.

  1. Hello !

    How can I force a player to attack entity (for exemple player.attackEntity(entity) ?
    I sought on Spigot and Bukkit but the only thing I found is how to force an entity to attack an other entity but not a player :/

  2. Well, for that you'd have to take control of the player. Which you can't.
  3. Interesting problem. You could fake it and send the required animation and interact events, then manually damage the entity to force Bukkit to send its damage events. Bukkit would know the difference, but all other plugins would not and ultimately it would have the same effect. Might look a little odd client-side, though.

    Edit: Spelling. TIL "evens" isn't just a proper noun.
    • Agree Agree x 1
  4. That sure would look weird!

    EDIT: Founds this: Which is a plugin allowing controlling of movement etc. Might wanna look into that.

    Just leaving this here:
    • Funny Funny x 1
  5. I can try to look into it.
  6. You could possibly have an arrow shoot out of the player and damage the entity. Hitting won't work, as you wouldn't get the animation.
    • Agree Agree x 1
  7. It just sets the victims to SPECTATOR, and disguises the controller as the victim.
    • Agree Agree x 1
  8. Wait, is that what you were doing? Just a simple control plugin? You know my plugin TC++ has a /control command and is FOSS on Github, right?
  9. I wanted to when a player press the left button (to attack), all players will be hits
  10. Next time, please
  11. Not sure which version was it added, but in 1.16 there's an EntityPlayer#attack method in NMS.

    You could use
    Code (Java):
    for (Entity entity : ListOfEntities /*get nearby entities*/)
    ((CraftPlayer) p).getHandle().attack(((CraftEntity) entity).getHandle());

    Important: this will break pretty much every version change. You can use reflection, but sometimes methods' names are completely different than in other versions.
    Also, if you want to see the animation, use Player#swingMainHand or Player#swingOffHand
    And make sure that the weapon cooldown is reset after the hits:
    Code (Java):
    ((CraftPlayer) p).getHandle().resetAttackCooldown();
    #14 NooTChhYT, Mar 21, 2021
    Last edited: Mar 23, 2021