Class EtherealBags

java.lang.Object
valorless.havenbags.database.EtherealBags

public class EtherealBags extends Object
Manages in-memory storage and persistence for Ethereal Bags.

Responsibilities:

  • Track which bag IDs belong to which player (by UUID).
  • Store serialized bag contents by a composite bag key.
  • Load data on plugin start (init()) and save on shutdown (shutdown()).
  • Track open Ethereal GUIs to close them safely during shutdown.
  • Provide methods to add, remove, update, and query bags and their contents.
  • Normalize bag IDs by stripping or adding the UUID prefix.
  • Check and manage open GUIs for players and bag IDs.
Storage notes:
  • bags: Map keyed by a player's UUID string. Value is a list of raw bag IDs.
  • bagData: Map keyed by a composite string "<uuid>-<bagId>".
  • bagSettings: Map keyed by the same composite string, storing EtherealBagSettings instances.
  • openGUIs: List of currently open EtherealGUI instances.
Data is persisted to plugins/HavenBags/bags/etherealbags.yml using ValorlessUtils Config with JSON-serialized payloads.
  • Field Details

    • config

      protected static valorless.valorlessutils.config.Config config
      Data file configuration representing bags/etherealbags.yml.
    • openGUIs

      public static List<EtherealGUI> openGUIs
      Currently open Ethereal GUIs, used to ensure they are closed before saving on shutdown.
  • Constructor Details

    • EtherealBags

      public EtherealBags()
  • Method Details

    • isOpen

      public static Boolean isOpen(org.bukkit.entity.Player player, String bagId)
      Check if any specific player has a specific bag GUI open.
      Parameters:
      player - Player to check
      bagId - Raw bag identifier
      Returns:
      true if the player has the specified bag GUI open; false otherwise
    • isOpen

      public static Boolean isOpen(String bagId)
      Check if any player has a specific bag GUI open by its composite ID.

      The composite ID is in the format "<uuid>-<bagId>".

      Parameters:
      bagId - Composite bag identifier to check
      Returns:
      true if any player has the specified bag GUI open; false otherwise
    • closeGUI

      public static Boolean closeGUI(org.bukkit.entity.Player player, String bagId)
      Close a specific player's open bag GUI by raw bag ID.
      Parameters:
      player - Player whose GUI to close
      bagId - Raw bag identifier
      Returns:
      true if the GUI was found and closed; false otherwise
    • closeGUI

      public static Boolean closeGUI(String bagId)
      Close any player's open bag GUI by its composite ID.

      The composite ID is in the format "<uuid>-<bagId>".

      Parameters:
      bagId - Composite bag identifier
      Returns:
      true if the GUI was found and closed; false otherwise
    • init

      public static void init()
      Initialize the Ethereal Bags subsystem.

      Ensures storage directories/files exist, loads persisted JSON strings from the YAML config into in-memory maps, clears any tracked open GUIs, and logs load duration.

    • shutdown

      public static void shutdown()
      Persist all bag ownership and contents to disk.

      Closes any tracked open Ethereal GUIs first, then writes JSON strings for both maps to the YAML file and logs save duration.

    • hasBags

      public static Boolean hasBags(UUID uuid)
      Check whether a player owns any bags.
      Parameters:
      uuid - UUID to check
      Returns:
      true if the player has at least one bag; false otherwise
    • hasBag

      public static Boolean hasBag(UUID uuid, String bagId)
      Check if a player owns a specific bag ID.

      This checks for the existence of the composite key "<uuid>-<bagId>" in bagData.

      Parameters:
      uuid - UUID to check
      bagId - Bag identifier to look for
      Returns:
      true if found; false otherwise
    • addBag

      public static Boolean addBag(UUID uuid, String bagId, List<org.bukkit.inventory.ItemStack> contents)
      Add a bag to a player and store initial contents.

      Records the raw bag ID under the player in bags, and stores contents in bagData under the composite key "-".

      Returns false if this player already has the specified bag ID or if the composite key already exists in bagData; otherwise returns true and persists the provided contents.

      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      contents - Initial contents to store (will be saved as-is)
      Returns:
      true if the new bag was created; false if it already exists
    • addBag

      public static Boolean addBag(UUID uuid, String bagId, int size)
      Add a bag to a player with empty initial contents.

      Records the raw bag ID under the player in bags, and stores empty contents in bagData under the composite key "-". The size parameter determines how many rows of 9 slots to create. Empty slots are represented by null entries.

      Returns false if this player already has the specified bag ID or if the composite key already exists in bagData; otherwise returns true and initializes the contents to size * 9 slots.

      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      size - Number of rows (of 9 slots each) for the bag's initial contents
      Returns:
      true if the new bag was created; false if it already exists
    • removeBag

      public static Boolean removeBag(UUID uuid, String bagId)
      Remove a bag ID from a player and delete its stored contents.

      Removes the raw bag ID from bags. Also removes the entry from bagData using the composite key for the player and bag ID. If the player's bag list becomes empty, their entry is removed from bags.

      Parameters:
      uuid - Owner of the bag
      bagId - Bag identifier to remove
      Returns:
      true if the bag existed and was removed; false otherwise
    • updateBagContents

      public static Boolean updateBagContents(UUID uuid, String bagId, List<org.bukkit.inventory.ItemStack> contents)
      Update stored contents for a player's bag.

      Contents are saved under the composite key "<uuid>-<bagId>". No-op if the composite key does not exist.

      Parameters:
      uuid - Owner of the bag
      bagId - Bag identifier
      contents - New contents to store
      Returns:
      true if the bag existed and was updated; false otherwise
    • getBagContentsOrNull

      public static List<org.bukkit.inventory.ItemStack> getBagContentsOrNull(UUID uuid, String bagId)
      Get the contents of a player's bag or null if unknown.

      Builds the composite key from the player UUID and bagId and queries bagData.

      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      Returns:
      List of ItemStack if present; otherwise null
    • getBagContentsOrEmpty

      public static List<org.bukkit.inventory.ItemStack> getBagContentsOrEmpty(UUID uuid, String bagId)
      Get the contents of a player's bag or an empty list if unknown.

      Builds the composite key from the player UUID and bagId and queries bagData.

      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      Returns:
      Bag contents if present; otherwise an empty list
    • getPlayerBags

      public static List<String> getPlayerBags(UUID uuid)
      Get a player's raw bag IDs.
      Parameters:
      uuid - UUID to query
      Returns:
      List of raw bag IDs owned by the player; empty if none
    • getPlayerBagsFormatted

      public static List<String> getPlayerBagsFormatted(UUID uuid)
      Get a player's bag IDs normalized by stripping any "<uuid>-" prefix.

      If IDs are already raw, the list is returned unchanged.

      Parameters:
      uuid - UUID to query
      Returns:
      List of normalized bag IDs
    • getBagSettings

      public static EtherealBagSettings getBagSettings(UUID uuid, String bagId)
      Get the settings of a player's bag or create default settings if unknown.

      Builds the composite key from the player UUID and bagId and queries bagFeatures.
      If no settings exist, a new default EtherealBagSettings instance is created, stored, and returned.

      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      Returns:
      EtherealBagSettings instance if present; otherwise null
    • getBagAutoPickup

      public static String getBagAutoPickup(UUID uuid, String bagId)
      Get the autoPickup setting of a player's bag or "null" if unknown.
      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      Returns:
      autoPickup setting if present; otherwise "null"
    • getBagMagnet

      public static Boolean getBagMagnet(UUID uuid, String bagId)
      Get the magnet setting of a player's bag or false if unknown.
      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      Returns:
      magnet setting if present; otherwise false
    • getBagAutoSort

      public static Boolean getBagAutoSort(UUID uuid, String bagId)
      Get the autoSort setting of a player's bag or false if unknown.
      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      Returns:
      autoSort setting if present; otherwise false
    • stripBagId

      public static String stripBagId(UUID uuid, String bagId)
      Normalize a bag ID by stripping the player's "<uuid>-" prefix if present.
      Parameters:
      uuid - Owner of the bag
      bagId - Raw or composite bag identifier
      Returns:
      The bagId with the player's "<uuid>-" prefix removed, if it existed
    • formatBagId

      public static String formatBagId(UUID uuid, String bagId)
      Format a raw bag ID by adding the player's "<uuid>-" prefix.
      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      Returns:
      The composite bag key "<uuid>-<bagId>"
    • isBagFull

      public static Boolean isBagFull(UUID uuid, String bagId)
      Check if a player's bag is completely full (no empty slots).

      Uses getBagContentsOrNull(UUID, String) to retrieve contents and checks for any null entries representing empty slots.

      Parameters:
      uuid - Owner of the bag
      bagId - Raw bag identifier
      Returns:
      true if the bag exists and has no empty slots; false if it has empty slots or does not exist
    • fromJson

      public static <T> T fromJson(String json, Type type)