Class DistanceUtil

java.lang.Object
me.deecaad.core.utils.DistanceUtil

public final class DistanceUtil extends Object
This final utility class outlines static methods involving the visible distance around a player, as well as methods to send packets to players so long as the packet's location is within the player's viewing distance.
  • Method Summary

    Modifier and Type
    Method
    Description
    static List<org.bukkit.entity.Player>
    getPlayersInRange(@NotNull org.bukkit.Location origin)
     
    static List<org.bukkit.entity.Player>
    getPlayersInRange(@NotNull org.bukkit.Location origin, double min, double max)
    Returns the entities withing range
    static int
    Returns the default viewing distance of worlds, defined by the vanilla minecraft server's server.properties file.
    static int
    getRange(@NotNull org.bukkit.World world)
    Returns the viewing distance of the given world.
    static void
    sendPacket(@NotNull org.bukkit.Location origin, Object... packets)
    Sends the given packet to all players who can see the given Location.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getRange

      public static int getRange()
      Returns the default viewing distance of worlds, defined by the vanilla minecraft server's server.properties file. Generally, this method should not be used. Use instead: getRange(World)
      Returns:
      The non-negative viewing distance, in blocks.
    • getRange

      public static int getRange(@NotNull @NotNull org.bukkit.World world)
      Returns the viewing distance of the given world.
      Parameters:
      world - Which world to pull the viewing distance from.
      Returns:
      The non-negative viewing distance, in blocks.
    • getPlayersInRange

      public static List<org.bukkit.entity.Player> getPlayersInRange(@NotNull @NotNull org.bukkit.Location origin)
    • getPlayersInRange

      public static List<org.bukkit.entity.Player> getPlayersInRange(@NotNull @NotNull org.bukkit.Location origin, double min, double max)
      Returns the entities withing range
      Parameters:
      origin - The coordinates that from where entities are taken
      min - How far away from the origin must the player be
      max - How close to the origin must the player be
      Returns:
      The entities withing range of view distance from origin
    • sendPacket

      public static void sendPacket(@NotNull @NotNull org.bukkit.Location origin, Object... packets)
      Sends the given packet to all players who can see the given Location. The distance that a player can see is defined by getRange(World).

      Note that sending packets with too long of a range can cause client sided performance issues. This has been a problem for a long time in the notchian server, so Spigot added entity tracking range, limiting how far the server would send clients packets. It is a good practice to limit how far away you send your packet.

       
           Location origin = /* not shown */;
           Object packet = /* not shown */;
           int distance = Math.min(DistanceUtils.getRange(), 50);
           DistanceUtils.sendPacket(origin, packet, distance)
       
       
      Parameters:
      origin - The coordinates that the packet is being spawned at.
      packets - The packets to send to players in view.