Getting the Distance Between Two Players

Discussion in 'Plugin Development' started by JacknDaBox42, Dec 14, 2013.

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

    JacknDaBox42

    I am creating a headshot kit for my server, and am in a bit of a rut... The kit works and all, but as of right now it is too overpowered. I wish to make it so that the headshot only works if the player is at least 50 blocks away from the target. Also I would like it for the player to see how many blocks away they shot the player from. Anyone got any ideas or tips? Here is the code so far.
    Code:java
    1. @EventHandler
    2. public void onHeadshot(EntityDamageByEntityEvent e) {
    3. if(e.getCause() != DamageCause.PROJECTILE)
    4. return;
    5.  
    6. Projectile proj = (Projectile) e.getDamager();
    7. if(!(proj.getShooter() instanceof Player))
    8. return;
    9. Entity shot = e.getEntity();
    10.  
    11. double y = proj.getLocation().getY();
    12. double shotY = shot.getLocation().getY();
    13. boolean headshot = y - shotY > 1.35d;
    14.  
    15. if(headshot) {
    16. e.setDamage(e.getDamage() * 20);
    17. StringBuilder message = new StringBuilder(headshot ? "§7You got a headshot on§2" : "");
    18. if(shot instanceof Player) {
    19. message.append(" " + ((Player) shot).getName());
    20. } else {
    21. message.append(" " + shot.getType().getName());
    22. }
    23. ((Player) proj.getShooter()).sendMessage(message.toString());
    24. }
    25. }
     
  2. Offline

    NathanWolf

    float distance = player1.getLocation().getDistance(player2.getLocation());

    :D
     
  3. Offline

    JacknDaBox42

    NathanWolf
    Thanks man. But There is a few errors this is what I have so far. There is an error under line 16 at .getDistance. Also, how would I make a check to see if they are 50 blocks away?
    Code:java
    1. @EventHandler
    2. public void onHeadshot(EntityDamageByEntityEvent e) {
    3. if(e.getCause() != DamageCause.PROJECTILE)
    4. return;
    5. if (!plugin.playerWithin(l1, l2, e.getEntity().getLocation())) {
    6. Projectile proj = (Projectile) e.getDamager();
    7. Player p = (Player)proj.getShooter();
    8. if(!(proj.getShooter() instanceof Player))
    9. return;
    10. Player shot =(Player) e.getEntity();
    11.  
    12. double y = proj.getLocation().getY();
    13. double shotY = shot.getLocation().getY();
    14. boolean headshot = y - shotY > 1.35d;
    15.  
    16. float distance = p.getLocation().getDistance(shot.getLocation());
    17. if(headshot) {
    18. e.setDamage(e.getDamage() * 20);
    19. StringBuilder message = new StringBuilder(headshot ? "§2" + p.getName() + " §7has gotten a headshot on§2" : "");
    20. if(shot instanceof Player) {
    21. message.append(" " + ((Player) shot).getName());
    22. } else {
    23. message.append(" " + shot.getType().getName());
    24. }
    25. Bukkit.broadcastMessage(message.toString());
    26.  
    27. }
    28. }
    29. }
     
  4. Offline

    WinX64

    There's no getDistance() method for the Location class, there's distance():
    Code:java
    1. double distance = p.getLocation().distance(shot.getLocation());


    This code should work:
    Code:java
    1. @EventHandler
    2. public void onHeadshot(EntityDamageByEntityEvent e) {
    3. if(e.getCause() != DamageCause.PROJECTILE)
    4. return;
    5.  
    6. Projectile proj = (Projectile) e.getDamager();
    7.  
    8. if (!(proj.getShooter() instanceof Player) || !(e.getEntity() instanceof LivingEntity))
    9. return;
    10.  
    11. Player shooter = (Player)proj.getShooter();
    12. LivingEntity shot = (LivingEntity)e.getEntity();
    13.  
    14. double y = proj.getLocation().getY();
    15. double shotY = shot.getLocation().getY();
    16. boolean headshot = y - shotY > 1.35d;
    17.  
    18. double distance = shooter.getLocation().distance(shot.getLocation());
    19.  
    20. if (headshot && distance > 25) {
    21. e.setDamage(e.getDamage() * 20);
    22. StringBuilder message = new StringBuilder("§2" + shooter.getName() + " §7has gotten a headshot on§2");
    23. if(shot instanceof Player)
    24. message.append(" " + ((Player) shot).getName());
    25. else
    26. message.append(" " + shot.getType().getName());
    27.  
    28. Bukkit.broadcastMessage(message.toString());
    29. }
    30. }


    JacknDaBox42
     
Thread Status:
Not open for further replies.

Share This Page