Package valorless.rarespawns.builders
Class ItemBuilder
java.lang.Object
valorless.rarespawns.builders.ItemBuilder
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.bukkit.inventory.ItemStackCreates an ItemStack object from a configuration file.static org.bukkit.ColorhexToColor(String hex) Converts a hex color code to a Bukkit Color object.static BooleanisReady()Returns whether the items are fully loaded and ready for use.Retrieves the names of all files (excluding their extensions) in the 'items' folder of the plugin's data directory.static ItemDataprepareData(valorless.valorlessutils.config.Config config, String id) Prepares all necessary item data from the configuration for a specific item ID.static ItemDataprepareSharedData(valorless.valorlessutils.config.Config config, String id) Prepares shared item data from the configuration.static voidreload()Reloads all item data files from the plugin's items directory.static voidsetTextureValue(@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.ItemStacksharedBuilder(ItemData data) Builds an ItemStack from the provided ItemData object, applying all properties and components.static org.bukkit.inventory.ItemStackupdateItem(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).
-
Field Details
-
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
-
-
Constructor Details
-
ItemBuilder
public ItemBuilder()
-
-
Method Details
-
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.
-
build
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.
-
prepareData
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.
-
hexToColor
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
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
itemis 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
updateflag is false, the original is returned. - Builds a fresh ItemStack from the latest config via
build(String). - If the rebuilt stack is
similarto 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.
- If
-