Class EntityUtils

java.lang.Object
valorless.rarespawns.api.EntityUtils

public class EntityUtils extends Object
Utility class that provides common helper methods for interacting with entities. Includes functions for checking proximity, health percentage, exposure to sunlight, calculating entity bounding box centers, and generating safe circular spawn locations around a given origin.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    formatLocation(org.bukkit.Location loc)
    Formats a Location object into a readable string containing the world name and coordinates.
    Example output: world (100.00, 64.00, 200.00)
    static String
    formatLocation(org.bukkit.Location loc, String format)
    Formats a Location object into a string based on the given format.
    static List<org.bukkit.Location>
    generateSafeCircularSpawnLocations(org.bukkit.Location location, int amount, double radius)
    Generates safe spawn locations arranged in a circle around a given origin location.
    static double
    getAttackSpeed(org.bukkit.entity.Mob mob)
    Returns the weapon attack speed for a Mob by inspecting the attribute modifiers on its main-hand ItemStack.
    static double
    getDamage(org.bukkit.entity.Mob mob)
    Returns the weapon attack damage for a Mob by inspecting the attribute modifiers on its main-hand ItemStack.
    static org.bukkit.Location
    getEntityCenter(org.bukkit.entity.Entity entity)
    Get the center location of an entity's bounding box.
    static double
    getHealthPercentage(org.bukkit.entity.LivingEntity entity)
    Calculate the current health percentage of a living entity.
    static org.bukkit.entity.Player
    getNearestPlayer(org.bukkit.entity.LivingEntity entity)
    Finds the nearest player to the given rare entity.
    static Integer
    getNearestPlayerDistance(org.bukkit.entity.LivingEntity entity)
    Gets the distance to the nearest player from the given rare entity.
    static List<org.bukkit.entity.Player>
    getPlayersInRadius(org.bukkit.entity.LivingEntity entity, double radius)
    Get a list of players within a specific radius around a living entity.
    static boolean
    isExposedToSunlight(org.bukkit.entity.Entity entity)
    Check if an entity is exposed to direct sunlight.
    static boolean
    isLocationSafe(org.bukkit.Location location)
    Checks if the location is safe (not inside a solid block).

    Methods inherited from class java.lang.Object

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

    • EntityUtils

      public EntityUtils()
  • Method Details

    • getPlayersInRadius

      public static List<org.bukkit.entity.Player> getPlayersInRadius(org.bukkit.entity.LivingEntity entity, double radius)
      Get a list of players within a specific radius around a living entity.
      Parameters:
      entity - The entity around which to search for players.
      radius - The radius within which to search for players.
      Returns:
      A list of players within the specified radius.
    • getHealthPercentage

      public static double getHealthPercentage(org.bukkit.entity.LivingEntity entity)
      Calculate the current health percentage of a living entity.
      Parameters:
      entity - The entity whose health percentage is to be calculated.
      Returns:
      The health percentage (0-100) of the entity.
    • isExposedToSunlight

      public static boolean isExposedToSunlight(org.bukkit.entity.Entity entity)
      Check if an entity is exposed to direct sunlight.

      This checks if the entity's current Y position is greater than or equal to the highest block at its X and Z coordinates.

      Parameters:
      entity - The entity to check.
      Returns:
      true if the entity is exposed to sunlight, false otherwise.
    • getEntityCenter

      public static org.bukkit.Location getEntityCenter(org.bukkit.entity.Entity entity)
      Get the center location of an entity's bounding box.

      This method calculates the midpoint of the entity's bounding box in all three dimensions. Useful for effects or precise positioning relative to the entity.

      Parameters:
      entity - The entity whose center location is to be calculated.
      Returns:
      The Location representing the center of the entity's bounding box.
    • generateSafeCircularSpawnLocations

      public static List<org.bukkit.Location> generateSafeCircularSpawnLocations(org.bukkit.Location location, int amount, double radius)
      Generates safe spawn locations arranged in a circle around a given origin location. Ensures each spawn point is not inside a solid block; otherwise, falls back to the origin.
      Parameters:
      location - The center/origin location to spawn around.
      amount - Number of spawn points to generate.
      radius - The radius of the circle.
      Returns:
      List of safe spawn locations.
    • isLocationSafe

      public static boolean isLocationSafe(org.bukkit.Location location)
      Checks if the location is safe (not inside a solid block).
      Parameters:
      location - The location to check.
      Returns:
      True if the location is safe, false otherwise.
    • formatLocation

      public static String formatLocation(org.bukkit.Location loc)
      Formats a Location object into a readable string containing the world name and coordinates.
      Example output: world (100.00, 64.00, 200.00)
      Parameters:
      loc - the location to format
      Returns:
      a formatted string in the form "world (x, y, z)", with coordinates rounded to two decimals
    • formatLocation

      public static String formatLocation(org.bukkit.Location loc, String format)
      Formats a Location object into a string based on the given format.

      The format string can include the following placeholders:

      • %w - Replaced with the world's name
      • %x - Replaced with the X coordinate
      • %y - Replaced with the Y coordinate
      • %z - Replaced with the Z coordinate
      Example usage: "%w - %x %y %z"world - 100.0 64.0 200.0
      Parameters:
      loc - the location to format
      format - the format string containing placeholders
      Returns:
      a string with placeholders replaced by location values
    • getDamage

      public static double getDamage(org.bukkit.entity.Mob mob)
      Returns the weapon attack damage for a Mob by inspecting the attribute modifiers on its main-hand ItemStack.

      Looks up Attribute.ATTACK_DAMAGE and, for version compatibility, falls back to Attribute.GENERIC_ATTACK_DAMAGE. If the item has multiple matching modifiers, the first one encountered is used. If the mob holds no item or the item has no relevant modifiers, this method returns 3.0.

      Note: This reads only the item's attribute modifiers; it does not combine with the mob's base attributes, enchantments, or effects.

      Parameters:
      mob - the mob whose main-hand weapon is inspected
      Returns:
      the attack damage value from the weapon's attribute modifiers, or 3.0 if unavailable
    • getAttackSpeed

      public static double getAttackSpeed(org.bukkit.entity.Mob mob)
      Returns the weapon attack speed for a Mob by inspecting the attribute modifiers on its main-hand ItemStack.

      Looks up Attribute.ATTACK_SPEED and, for version compatibility, falls back to Attribute.GENERIC_ATTACK_SPEED. If multiple matching modifiers exist, the first one encountered is used. If absent, this method returns 1.0.

      Note: Only item attribute modifiers are considered; this does not account for the mob's base attributes or temporary effects.

      Parameters:
      mob - the mob whose main-hand weapon is inspected
      Returns:
      the attack speed value from the weapon's attribute modifiers, or 1.0 if unavailable
    • getNearestPlayer

      public static org.bukkit.entity.Player getNearestPlayer(org.bukkit.entity.LivingEntity entity)
      Finds the nearest player to the given rare entity.
      Parameters:
      entity - The rare entity to check from.
      Returns:
      The nearest Player, or null if none found.
    • getNearestPlayerDistance

      public static Integer getNearestPlayerDistance(org.bukkit.entity.LivingEntity entity)
      Gets the distance to the nearest player from the given rare entity.
      Parameters:
      entity - The rare entity to check from.
      Returns:
      The distance to the nearest player as an integer, or Integer.MAX_VALUE if none found.