Class ItemBuilder

java.lang.Object
valorless.rarespawns.builders.ItemBuilder

public class ItemBuilder extends Object
ItemBuilder is responsible for constructing ItemStack objects from configuration files. It supports a wide range of item properties, including custom model data, enchantments, attributes, NBT data, tags, special components, and more. Items are loaded from the plugin's 'items' folder and cached for efficient access.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static HashMap<String,valorless.valorlessutils.config.Config>
     
    protected static Boolean
    Indicates whether the items have been fully loaded and are ready for use.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.bukkit.inventory.ItemStack
    Creates an ItemStack object from a configuration file.
    static org.bukkit.Color
    Converts a hex color code to a Bukkit Color object.
    static Boolean
    Returns whether the items are fully loaded and ready for use.
    static List<String>
    Retrieves the names of all files (excluding their extensions) in the 'items' folder of the plugin's data directory.
    static ItemData
    prepareData(valorless.valorlessutils.config.Config config, String id)
    Prepares all necessary item data from the configuration for a specific item ID.
    static ItemData
    prepareSharedData(valorless.valorlessutils.config.Config config, String id)
    Prepares shared item data from the configuration.
    static void
    Reloads all item data files from the plugin's items directory.
    static void
    setTextureValue(@NotNull org.bukkit.inventory.ItemStack item, @NotNull String value)
    Sets the texture value for a player head item, using either Spigot API or reflection for older versions.
    static org.bukkit.inventory.ItemStack
    Builds an ItemStack from the provided ItemData object, applying all properties and components.
    static org.bukkit.inventory.ItemStack
    updateItem(org.bukkit.entity.Player player, org.bukkit.inventory.ItemStack item)
    Updates (rebuilds) a RareSpawns item from its current configuration if the item's definition allows updating (config key: update-item = true).

    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 items have been fully loaded and are ready for use. Set to true once all items are loaded and cached.
    • dataFiles

      protected static HashMap<String,valorless.valorlessutils.config.Config> dataFiles
  • Constructor Details

    • ItemBuilder

      public ItemBuilder()
  • Method Details

    • isReady

      public static Boolean isReady()
      Returns whether the items are fully loaded and ready for use.
      Returns:
      true if items are ready; false otherwise.
    • reload

      public static void reload()
      Reloads all item data files from the plugin's items directory.

      Behavior:

      • On first call, populates dataFiles with every discovered item config.
      • On subsequent calls, clears/replaces existing entries to pick up changes.
      • Logs total count and elapsed load time for diagnostics.
      • Delegates to ItemCache.reload() after data files are refreshed.
      • Invalid or unreadable files are logged and skipped.
      This method is safe to invoke multiple times (e.g., for /reload or admin commands).
    • build

      public static org.bukkit.inventory.ItemStack build(String id)
      Creates an ItemStack object from a configuration file. This method constructs an ItemStack with attributes such as material, name, lore, enchantments, attributes, item flags, durability, NBT data, and custom tags based on the provided configuration. It ensures that the item has all the necessary properties defined.
      Parameters:
      id - The unique identifier for the item (filename without extension).
      Returns:
      The fully constructed ItemStack object.
    • sharedBuilder

      public static org.bukkit.inventory.ItemStack sharedBuilder(ItemData data)
      Builds an ItemStack from the provided ItemData object, applying all properties and components.
      Parameters:
      data - The ItemData object containing all item properties.
      Returns:
      The constructed ItemStack.
    • prepareData

      public static ItemData prepareData(valorless.valorlessutils.config.Config config, String id)
      Prepares all necessary item data from the configuration for a specific item ID. This method gathers shared data as well as enchants and attributes if they exist in the config.
      Parameters:
      config - The configuration object containing the item properties.
      id - The unique identifier for the item in the configuration.
      Returns:
      An ItemData object populated with all relevant properties, including randomized enchants and attributes if applicable.
    • prepareSharedData

      public static ItemData prepareSharedData(valorless.valorlessutils.config.Config config, String id)
      Prepares shared item data from the configuration. This includes basic properties such as material, name, lore, amount, durability, custom model data, NBT data, tags, special components (food, tool, playable), and spawner settings.
      Parameters:
      config - The configuration object containing item properties.
      id - The unique identifier for the item in the configuration.
      Returns:
      An ItemData object populated with all shared item properties.
    • hexToColor

      public static org.bukkit.Color hexToColor(String hex)
      Converts a hex color code to a Bukkit Color object.
      Parameters:
      hex - The hex color code, e.g., "#FF5733".
      Returns:
      A Bukkit Color object representing the hex color.
    • setTextureValue

      public static void setTextureValue(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull String value)
      Sets the texture value for a player head item, using either Spigot API or reflection for older versions.
      Parameters:
      item - The ItemStack to apply the texture to.
      value - The base64-encoded texture value.
    • listItems

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

      public static org.bukkit.inventory.ItemStack updateItem(org.bukkit.entity.Player player, org.bukkit.inventory.ItemStack item)
      Updates (rebuilds) a RareSpawns item from its current configuration if the item's definition allows updating (config key: update-item = true).

      Decision flow:

      • If item is null or not recognized as a RareSpawns item, the original reference is returned.
      • Resolves the rare ID from the item's stored metadata.
      • If no definition is found or the definition's update flag is false, the original is returned.
      • Builds a fresh ItemStack from the latest config via build(String).
      • If the rebuilt stack is similar to the original, no change is made.
      • If different, logs a debug message (includes player name) and returns the updated stack.

      Preservation notes:

      • Durability / custom runtime mutations applied after initial creation are not copied; the item is fully rebuilt.
      • If you need to preserve mutable runtime state (e.g., damage, enchant changes), capture invalid input: '&' reapply it manually.

      Performance: Intended for occasional refresh (e.g., on pickup, join, migration). Avoid calling every tick.

      Parameters:
      player - The player context prompting the update (used for debug logging).
      item - The existing ItemStack to evaluate and potentially update.
      Returns:
      The updated ItemStack if an update occurred; otherwise the original reference.