1.14.4 How to check if player is at edge of radius?

Discussion in 'Spigot Plugin Development' started by kinukin, Feb 2, 2020.

  1. I'm trying to check if the player is at the edge of a radius. How can I do that?
    Code (Text):
    public void spawnGoblin(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    Location location = new Location(Bukkit.getWorld("world"), -4.762, 89, -19.306);
    if(p.getLocation().distance(location) == 10) {
  2. FrostedSnowman

    Resource Staff

    Location::distance returns the distance calculated as a double. The chances it's exactly 10, are unlikely. You'll need to round the distance calculated.

    Other things to consider here:
    • This will be called every time a player moves, including any changes in yaw or pitch. You may want to only run your code when a player has moved a full block.
    • Use Location::distanceSquared for better performance over using Math::sqrt that Location::distance internally uses. You'll just need to square your radius (10 = 10*10) to account for this change.