damage people in area?

Discussion in 'Plugin Development' started by Infinity_Crafter, Sep 25, 2013.

Thread Status:
Not open for further replies.
  1. emm.. i want to make plugin that let you hit few players/mobs in area , for example : if il use axe il damage all people in front of me and will cap it to X people... how can i do that? i tried damage all players/mobs in radius... but i want that only mobs in front of me ... can anyone give me example code for getting players/mobs location in front of you?
     
  2. Offline

    chris4315

    You'd listen for the PlayerInteractEvent, check if the item in the Player's hand is whatever type of axe, and get the mob in front of the player and damage it.
     
    Infinity_Crafter likes this.
  3. Offline

    blablubbabc

    Code:java
    1. private final int maxTargets = 10; // > 0
    2. private final double maxRange = 15.0D;
    3. private final double maxAngleCos = Math.cos(Math.toRadians(60)); // an angle between 0 and 90 degree
    4.  
    5. private Set<LivingEntity> findTargets(Player player) {
    6. Set<LivingEntity> targets = new HashSet<LivingEntity>();
    7. if (player != null) {
    8. Vector playerLocation = player.getEyeLocation().toVector();
    9. Vector playerViewDirection = player.getEyeLocation().getDirection().normalize();
    10. for (Entity entity : player.getNearbyEntities(maxRange, maxRange, maxRange)) {
    11. if (entity instanceof LivingEntity && !entity.equals(player)) {
    12. LivingEntity livingEntity = (LivingEntity) entity;
    13.  
    14. // check angle to target:
    15. Vector toTarget = livingEntity.getEyeLocation().toVector().subtract(playerLocation).normalize();
    16. double dotProduct = toTarget.dot(playerViewDirection);
    17. if (dotProduct >= maxAngleCos) {
    18. targets.add(livingEntity);
    19. if (targets.size() >= maxTargets) break;
    20. }
    21. }
    22. }
    23. }
    24. return targets;
    25. }
     
  4. Offline

    Alshain01

    You would have to do a player location analysis.

    Grab all the players in the chunk, look at the original damager's location and compass direction and see who's location in that chunk fits within your given direction and desired angle of splash damage. You will be monitoring the EntityDamageByEntity event and checking the event.getEntity() instanceof Player and event.getDamager() instanceof Player.
     
  5. blablubbabc thx for the code ;) but , i didnt understand a few things, how would i damage the mobs? target.damage()?

    chris4315 as i told , i done that before but with radius and because of that i asked for example code

    Alshain01 thx will try doing that... But i think its too complicated to me (to make it alone) so thats why i ask for example code ;) so i could understand
     
  6. Offline

    blablubbabc

    This method only gives you the living entities in front of your (in a certain angle even).
    So on your event handler (where you want to trigger it; in the interact or one of the damage events for example) you do something like:

    for (LivingEntity target : findTargets(player)) target.damage(3.0D); // or whatever
     
  7. seems like its doesnt work :( im trying to make it on axe but the axe doesnt make anyting , also im not getting any errors :S
     
  8. Offline

    blablubbabc

    maybe show your complete code then? I can't help you without anything..
     
  9. Offline

    mattrick

    Did you register your events?
     
  10. Offline

    mattrick

Thread Status:
Not open for further replies.

Share This Page