Class StorageManager

java.lang.Object
me.tayebyassine.homestead.storage.StorageManager

public final class StorageManager extends Object
Central manager for all region storages.

Handles loading, saving, and caching of SharedStorage instances, and coordinates open StorageMenu views so they stay synchronised when the underlying storage changes.

  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    closeAllMenus(long regionId)
    Force-close all open menus for the given region.
    static void
    closePlayerMenus(org.bukkit.entity.Player player)
    Force-close all menus owned by the given player.
    createStorage(long regionId, int size)
    Create a new empty storage for the given region and persist it.
    static void
    deleteStorage(long regionId)
    Delete the storage for the given region from both the cache and the persisted file, and close all open menus.
    getExistingStorage(long regionId)
    Get the cached storage for the given region without loading from disk.
    getStorage(long regionId)
    Get the storage for the given region, creating it from the persisted file if it is not yet cached.
    static boolean
    hasStorage(long regionId)
    Check whether a storage exists for the given region, either in the in-memory cache or on disk.
    static void
    Initialize the storage manager with the plugin instance and load the persisted storage file.
    static void
    registerMenu(long regionId, StorageMenu menu)
    Register an open StorageMenu so it receives updates when the underlying storage changes.
    static void
    replaceStorage(long regionId, SharedStorage newStorage)
    Replace the storage for the given region with a new instance and refresh all open menus.
    static void
    Persist all cached storages to disk synchronously.
    static void
    saveStorage(long regionId)
    Persist the storage for the given region to disk asynchronously.
    static void
    unregisterMenu(long regionId, StorageMenu menu)
    Unregister a StorageMenu so it no longer receives updates.
    static void
    updateAllMenus(long regionId)
    Refresh the full display of all open menus for the given region.
    static void
    updateSlot(long regionId, int slot)
    Update a single slot across all open menus for the given region.

    Methods inherited from class Object

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

    • init

      public static void init()
      Initialize the storage manager with the plugin instance and load the persisted storage file.
    • getStorage

      public static SharedStorage getStorage(long regionId)
      Get the storage for the given region, creating it from the persisted file if it is not yet cached.
      Parameters:
      regionId - the unique region ID
      Returns:
      the shared storage instance, never null
    • getExistingStorage

      public static SharedStorage getExistingStorage(long regionId)
      Get the cached storage for the given region without loading from disk.
      Parameters:
      regionId - the unique region ID
      Returns:
      the cached storage, or null if not yet loaded
    • hasStorage

      public static boolean hasStorage(long regionId)
      Check whether a storage exists for the given region, either in the in-memory cache or on disk.
      Parameters:
      regionId - the unique region ID
      Returns:
      true if a storage is present
    • createStorage

      public static SharedStorage createStorage(long regionId, int size)
      Create a new empty storage for the given region and persist it.
      Parameters:
      regionId - the unique region ID
      size - the inventory size (must be a valid Bukkit size)
      Returns:
      the newly created storage
    • replaceStorage

      public static void replaceStorage(long regionId, SharedStorage newStorage)
      Replace the storage for the given region with a new instance and refresh all open menus.
      Parameters:
      regionId - the unique region ID
      newStorage - the replacement storage
    • deleteStorage

      public static void deleteStorage(long regionId)
      Delete the storage for the given region from both the cache and the persisted file, and close all open menus.
      Parameters:
      regionId - the unique region ID
    • saveStorage

      public static void saveStorage(long regionId)
      Persist the storage for the given region to disk asynchronously.
      Parameters:
      regionId - the unique region ID
    • saveAll

      public static void saveAll()
      Persist all cached storages to disk synchronously.

      Called during plugin shutdown to ensure no data is lost.

    • registerMenu

      public static void registerMenu(long regionId, StorageMenu menu)
      Register an open StorageMenu so it receives updates when the underlying storage changes.
      Parameters:
      regionId - the unique region ID
      menu - the menu to register
    • unregisterMenu

      public static void unregisterMenu(long regionId, StorageMenu menu)
      Unregister a StorageMenu so it no longer receives updates.
      Parameters:
      regionId - the unique region ID
      menu - the menu to unregister
    • updateAllMenus

      public static void updateAllMenus(long regionId)
      Refresh the full display of all open menus for the given region.
      Parameters:
      regionId - the unique region ID
    • updateSlot

      public static void updateSlot(long regionId, int slot)
      Update a single slot across all open menus for the given region.
      Parameters:
      regionId - the unique region ID
      slot - the inventory slot index
    • closeAllMenus

      public static void closeAllMenus(long regionId)
      Force-close all open menus for the given region. Typically called when the storage is being deleted.
      Parameters:
      regionId - the unique region ID
    • closePlayerMenus

      public static void closePlayerMenus(org.bukkit.entity.Player player)
      Force-close all menus owned by the given player. Called when the player disconnects.
      Parameters:
      player - the player whose menus to close