Getting the distance between two players

Discussion in 'Plugin Development' started by Xp10d3, Feb 25, 2021.

  1. Offline

    Xp10d3

    So basically I'm creating a very very very very bare minimum anti-reach plugin and want to get the distance between two players. The best way I thought of was checking the distance between two players using the distance formula, but is there any better way to do this? Are there any downsides to using this method?

    Code:
    Code:java
    1.  
    2. package reach.endran.eltik;
    3.  
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6.  
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    11.  
    12. public class PlayerListeners implements Listener {
    13.  
    14. // Get's the data + time for logging purposes.
    15. Date now = new Date();
    16. SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    17.  
    18. private Core core;
    19.  
    20. public PlayerListeners(Core core) {
    21. this.core = core;
    22. }
    23.  
    24. @EventHandler
    25. public void onHit(EntityDamageByEntityEvent event) {
    26. Player player = (Player) event.getDamager();
    27.  
    28. Player entity = (Player) event.getEntity();
    29.  
    30. // If the damager damages the damaged...
    31. if (((event.getDamager() instanceof Player)) && ((event.getEntity() instanceof Player))) {
    32. double distance = Math.sqrt(Math.pow(((player.getLocation().getX() - entity.getLocation().getX())), 2) + Math.pow(((player.getLocation().getY() - entity.getLocation().getY())), 2) + Math.pow(((player.getLocation().getZ() - entity.getLocation().getZ())), 2));
    33. player.sendMessage("You hit " + entity.getName() + " with a reach of " + distance);
    34. core.otherLog("[" + format.format(now) + "] " + player.getName() + " hit " + entity.getName() + " with a reach of " + distance);
    35. }
    36. }
    37.  
    38. }
    39.  
    40.  
     
  2. Online

    Kars

    No and no.
     
    Xp10d3 likes this.
  3. Offline

    Xp10d3

    Oh. Okay well thank you lmao
     
  4. Online

    CraftCreeper6

Share This Page