Class EntityCreator

java.lang.Object
valorless.rarespawns.builders.EntityCreator

public class EntityCreator extends Object
EntityCreator is responsible for creating, updating, and managing custom entities in the RareSpawns plugin. It loads entity configurations, applies custom properties, handles entity updates, boss bars, nameplates, inventory, attributes, and more. It also provides utility methods for entity selection, spawning, and conversion.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static List<org.bukkit.entity.LivingEntity>
    List of all living rare entities currently managed by the plugin.
    List of all custom nameplates for rare entities.
    protected static Boolean
    Indicates whether the entity data has been fully loaded and is ready for use.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Adds a new entity to the collection with the provided ID.
    static void
    createNameplate(org.bukkit.entity.LivingEntity entity, EntityData data)
    Creates a custom nameplate for a rare entity if it doesn't already exist.
    static List<org.bukkit.inventory.ItemStack>
    Builds the drop table for an entity from a list of item strings.
    Returns a clone of the entities map containing all custom entity data.
    Retrieves all entities that belong to the specified spawn group id.
    Retrieves all entities that belong to the specified spawn group.
    static EntityData
    Retrieves entity data by its unique ID.
    static SpawnGroup
    Finds a spawn group by its identifier (case-insensitive).
    Gets all configured spawn groups currently loaded.
    static org.bukkit.entity.Wolf.Variant
    getWolfVariant(String variantName)
     
    static boolean
    isPotentiallyHostile(org.bukkit.entity.LivingEntity mob)
    Checks if a mob is potentially hostile (used for AI and targeting logic).
    static Boolean
    Returns whether the entity data is fully loaded and ready for use.
    static List<String>
    Retrieves the names of all files (excluding their extensions) in the 'entities' folder of the plugin's data directory.
    static org.bukkit.inventory.ItemStack
    parseDropItem(String itemString)
    Parses a drop-table item definition into an ItemStack, applying an optional drop chance.
    static org.bukkit.inventory.ItemStack
    parseItem(String itemString, boolean randomize)
    Parses an item definition string into an ItemStack for equipment or inventories.
    static String
    Picks a random rare entity ID based on weighted probabilities.
    static String
    Picks a random rare entity ID within the specified spawn group using weighted selection.
    static void
    Reloads spawn groups and entity definitions from configuration and disk.
    static void
    setEntityAsBaby(org.bukkit.entity.Entity entity, Boolean baby)
    Sets an entity as a baby or adult if supported.
    static void
    setEntityAttribute(org.bukkit.entity.LivingEntity entity, org.bukkit.attribute.Attribute attribute, double value)
    Sets an attribute value for a living entity.
    static void
    setInventory(org.bukkit.entity.LivingEntity entity, InventoryData inv)
    Sets the inventory for a living entity using InventoryData.
    static org.bukkit.Location
    spawnAboveGround(org.bukkit.entity.LivingEntity entity, EntityData data)
    Spawns an entity at a safe above-ground location, and teleports passengers if present.
    static org.bukkit.entity.LivingEntity
    spawnRare(String id, org.bukkit.Location location)
    Spawns a configured rare entity by its unique ID at the given location and initializes it.
    static void
    updateEntity(org.bukkit.entity.LivingEntity entity, String id, boolean New)
    Updates a living entity with the properties from its EntityData, by ID.
    static void
    updateEntity(org.bukkit.entity.LivingEntity entity, EntityData data, boolean New)
    Updates a living entity with the properties from its EntityData.

    Methods inherited from class java.lang.Object

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

    • ready

      protected static Boolean ready
      Indicates whether the entity data has been fully loaded and is ready for use. Set to true once all entity data is loaded from configuration files.
    • allEntities

      public static List<org.bukkit.entity.LivingEntity> allEntities
      List of all living rare entities currently managed by the plugin.
    • nameplates

      public static List<RareEntityNameplate> nameplates
      List of all custom nameplates for rare entities.
  • Constructor Details

    • EntityCreator

      public EntityCreator()
  • Method Details

    • isReady

      public static Boolean isReady()
      Returns whether the entity data is fully loaded and ready for use.
      Returns:
      true if entity data is ready; false otherwise
    • getEntities

      public static HashMap<String,EntityData> getEntities()
      Returns a clone of the entities map containing all custom entity data.
      Returns:
      A cloned HashMap of entity IDs to EntityData.
    • getEntitiesByGroup

      public static List<EntityData> getEntitiesByGroup(String group)
      Retrieves all entities that belong to the specified spawn group id. Matching is case-insensitive.
      Parameters:
      group - the spawn group identifier
      Returns:
      list of EntityData in the group (empty if none)
    • getEntitiesByGroup

      public static List<EntityData> getEntitiesByGroup(SpawnGroup group)
      Retrieves all entities that belong to the specified spawn group.
      Parameters:
      group - the SpawnGroup to match against
      Returns:
      list of EntityData in the group (empty if none)
    • getSpawnGroups

      public static List<SpawnGroup> getSpawnGroups()
      Gets all configured spawn groups currently loaded. Returns the live backing list; do not modify.
      Returns:
      list of SpawnGroup entries
    • getSpawnGroup

      public static SpawnGroup getSpawnGroup(String group)
      Finds a spawn group by its identifier (case-insensitive).
      Parameters:
      group - the group identifier
      Returns:
      the matching SpawnGroup, or null if not found
    • getEntity

      public static EntityData getEntity(String id)
      Retrieves entity data by its unique ID.
      Parameters:
      id - The unique ID of the entity.
      Returns:
      The EntityData associated with the ID, or null if not found.
    • addEntity

      public static boolean addEntity(String id, EntityData entity) throws Exception
      Adds a new entity to the collection with the provided ID. Validates the ID and entity before adding.
      Parameters:
      id - The unique ID of the entity to be added.
      entity - The EntityData that represents the entity.
      Returns:
      True if the entity was successfully added; false if the ID already exists or if an error occurs.
      Throws:
      Exception - If the ID is null or empty.
    • reload

      public static void reload()
      Reloads spawn groups and entity definitions from configuration and disk.

      This method:

      • Rebuilds the in-memory list of SpawnGroup entries from the main config (keys: spawn-chance and spawn-groups), inserting a default group when missing.
      • Loads all YAML files under the plugin data folder entities/ and creates new EntityData entries or updates existing ones via prepareData(Config, String).
      • Logs errors per invalid entry, as well as total entities processed and elapsed time.
      If entity definitions already exist in memory, this performs a best-effort in-place update without clearing previous state; a full server restart is recommended to avoid potential inconsistencies.

      Side effects:

      • Mutates static collections: spawnGroups and entities.
      • Emits log messages for progress and issues.
      This method should be invoked on the server main thread.
    • updateEntity

      public static void updateEntity(org.bukkit.entity.LivingEntity entity, String id, boolean New)
      Updates a living entity with the properties from its EntityData, by ID.
      Parameters:
      entity - The entity to update.
      id - The unique ID of the entity.
      New - Whether the entity is newly spawned.
    • updateEntity

      public static void updateEntity(org.bukkit.entity.LivingEntity entity, EntityData data, boolean New)
      Updates a living entity with the properties from its EntityData.
      Parameters:
      entity - The entity to update.
      data - The EntityData containing all properties.
      New - Whether the entity is newly spawned.
    • isPotentiallyHostile

      public static boolean isPotentiallyHostile(org.bukkit.entity.LivingEntity mob)
      Checks if a mob is potentially hostile (used for AI and targeting logic).
      Parameters:
      mob - The living entity to check.
      Returns:
      True if the mob is considered hostile, false otherwise.
    • createNameplate

      public static void createNameplate(org.bukkit.entity.LivingEntity entity, EntityData data)
      Creates a custom nameplate for a rare entity if it doesn't already exist.
      Parameters:
      entity - The entity to attach the nameplate to.
      data - The EntityData for the entity.
    • generateDropTable

      public static List<org.bukkit.inventory.ItemStack> generateDropTable(EntityData data)
      Builds the drop table for an entity from a list of item strings. Supports randomization and chance.
      Parameters:
      items - List of item strings in format "item:amount[:chance]".
      Returns:
      List of ItemStack objects representing the drop table.
    • setInventory

      public static void setInventory(org.bukkit.entity.LivingEntity entity, InventoryData inv)
      Sets the inventory for a living entity using InventoryData.
      Parameters:
      entity - The entity to set inventory for.
      inv - The InventoryData object containing item slots.
    • setEntityAttribute

      public static void setEntityAttribute(org.bukkit.entity.LivingEntity entity, org.bukkit.attribute.Attribute attribute, double value)
      Sets an attribute value for a living entity.
      Parameters:
      entity - The entity to set the attribute for.
      attribute - The Attribute to set.
      value - The value to set.
    • pickRandomRareEntity

      public static String pickRandomRareEntity()
      Picks a random rare entity ID based on weighted probabilities. Only entities with positive weight are considered.
      Returns:
      The selected entity ID, or null if none found.
    • pickRandomRareEntityByGroup

      public static String pickRandomRareEntityByGroup(SpawnGroup group)
      Picks a random rare entity ID within the specified spawn group using weighted selection. Only entities with positive weight are considered.
      Parameters:
      group - the SpawnGroup to filter candidates by
      Returns:
      the selected entity ID, or null if none are eligible
    • setEntityAsBaby

      public static void setEntityAsBaby(org.bukkit.entity.Entity entity, Boolean baby)
      Sets an entity as a baby or adult if supported.
      Parameters:
      entity - The entity to modify.
      baby - True to set as baby, false for adult.
    • spawnAboveGround

      public static org.bukkit.Location spawnAboveGround(org.bukkit.entity.LivingEntity entity, EntityData data)
      Spawns an entity at a safe above-ground location, and teleports passengers if present.
      Parameters:
      entity - The entity to spawn.
      data - The EntityData for the entity.
      Returns:
      True if the entity was successfully spawned, false otherwise.
    • getWolfVariant

      public static org.bukkit.entity.Wolf.Variant getWolfVariant(String variantName)
    • listEntities

      public static List<String> listEntities()
      Retrieves the names of all files (excluding their extensions) in the 'entities' folder of the plugin's data directory.
      Returns:
      A List of file names without extensions.
    • spawnRare

      public static org.bukkit.entity.LivingEntity spawnRare(String id, org.bukkit.Location location)
      Spawns a configured rare entity by its unique ID at the given location and initializes it.

      This will:

      Parameters:
      id - the unique rare ID as defined in entities/configs
      location - the world location to spawn at
      Returns:
      the spawned LivingEntity, or null if the ID is invalid
    • parseItem

      public static org.bukkit.inventory.ItemStack parseItem(String itemString, boolean randomize)
      Parses an item definition string into an ItemStack for equipment or inventories.

      Format:

      • id:amount
      • id:min-max — the amount is chosen randomly in the inclusive range
      If the string starts with nexo:, the item is built via NexoItems; otherwise it tries Bukkit Material first, then falls back to custom item builders.

      Parameters:
      itemString - the item descriptor string
      randomize - when true, uses ItemBuilder (randomized) for custom IDs; otherwise uses ItemCreator
      Returns:
      the resulting ItemStack; Material.AIR if parsing fails
    • parseDropItem

      public static org.bukkit.inventory.ItemStack parseDropItem(String itemString)
      Parses a drop-table item definition into an ItemStack, applying an optional drop chance.

      Format:

      • id:amount
      • id:min-max — amount is randomized in the inclusive range
      • id:amount:chance — the item is returned only if the chance check passes
      • id:min-max:chance — the item is returned only if the chance check passes
      If a chance token is provided, this method returns null when the roll fails.

      Parameters:
      itemString - the drop-table descriptor string
      Returns:
      the built ItemStack to drop, or null if the chance roll fails