Class PlayerLoader

java.lang.Object
studio.magemonkey.fabled.data.io.PlayerLoader

public class PlayerLoader extends Object
Cache of loaded PlayerAccounts, keyed by player UUID.

The cache is used from the server thread, from async unload tasks and from the auto-save thread. Loading and unloading are compound sequences around file or database I/O, so they are made mutually exclusive per player by lock stripes, while unrelated players still load and save in parallel. A thread never holds more than one stripe: the I/O layer fires Bukkit events, and a listener that asks for another player's data runs without taking a second stripe rather than risking a lock-ordering deadlock.

An unload that runs some time after the player quits is tracked by a ticket, so a rejoin in the meantime can cancel it and keep the cached accounts, instead of the unload saving and evicting them from under the new session.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    cancelPendingUnload(org.bukkit.OfflinePlayer player)
    Cancels the player's pending unload, for a player who rejoins before it has run.
    Retrieves a snapshot of every cached player's accounts.
    getPlayerAccounts(org.bukkit.OfflinePlayer player)
    Retrieves the cached accounts for the player, loading them from storage if they are not cached yet.
    static boolean
    hasPlayerAccounts(org.bukkit.OfflinePlayer player)
    Checks whether accounts are currently cached for the player.
    static void
    Loads the accounts of every online player.
    static void
    loadPlayer(org.bukkit.OfflinePlayer player)
    Loads the player's accounts from storage, replacing anything currently cached for them.
    static long
    prepareUnload(org.bukkit.OfflinePlayer player)
    Registers an unload of the player that will run later, such as from an async task after they quit.
    static void
    Saves every cached player's accounts.
    static void
    saveAllPlayerAccounts(boolean clearCache)
    Saves every cached player's accounts, one stripe at a time.
    static void
    unloadPlayer(org.bukkit.OfflinePlayer player)
    Saves the player's accounts and drops them from the cache.
    static void
    unloadPlayer(org.bukkit.OfflinePlayer player, long ticket)
    Saves the player's accounts and drops them from the cache, unless the unload has been cancelled by cancelPendingUnload(OfflinePlayer) or superseded since it was prepared.

    Methods inherited from class java.lang.Object

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

    • PlayerLoader

      public PlayerLoader()
  • Method Details

    • getPlayerAccounts

      public static PlayerAccounts getPlayerAccounts(org.bukkit.OfflinePlayer player)
      Retrieves the cached accounts for the player, loading them from storage if they are not cached yet.

      Cache hits are lock-free, so the server thread never waits on another thread's save. A miss takes the player's stripe and checks again before loading, so a player is loaded once and never while an unload of the same player is in progress. A lookup for a player who has just quit can still return the instance their unload is about to evict.

      Parameters:
      player - player to get the accounts for
      Returns:
      the player's accounts, or null if storage could not produce any
    • loadPlayer

      public static void loadPlayer(org.bukkit.OfflinePlayer player)
      Loads the player's accounts from storage, replacing anything currently cached for them.
      Parameters:
      player - player to load the accounts for
    • unloadPlayer

      public static void unloadPlayer(org.bukkit.OfflinePlayer player)
      Saves the player's accounts and drops them from the cache.

      The built-in backends log and swallow write failures, so the entry is dropped even if the save did not succeed.

      Parameters:
      player - player to unload
    • prepareUnload

      public static long prepareUnload(org.bukkit.OfflinePlayer player)
      Registers an unload of the player that will run later, such as from an async task after they quit. Pass the returned ticket to unloadPlayer(OfflinePlayer, long). Registering a new unload supersedes any earlier one that is still pending.
      Parameters:
      player - player who is going to be unloaded
      Returns:
      the ticket of the unload
    • unloadPlayer

      public static void unloadPlayer(org.bukkit.OfflinePlayer player, long ticket)
      Saves the player's accounts and drops them from the cache, unless the unload has been cancelled by cancelPendingUnload(OfflinePlayer) or superseded since it was prepared.
      Parameters:
      player - player to unload
      ticket - ticket returned by prepareUnload(OfflinePlayer)
    • cancelPendingUnload

      public static boolean cancelPendingUnload(org.bukkit.OfflinePlayer player)
      Cancels the player's pending unload, for a player who rejoins before it has run. This takes the player's stripe, so an unload that is already saving finishes and evicts first, and a later lookup loads the saved data.
      Parameters:
      player - player who rejoined
      Returns:
      true if an unload was cancelled and the accounts it would have evicted are still cached
    • hasPlayerAccounts

      public static boolean hasPlayerAccounts(org.bukkit.OfflinePlayer player)
      Checks whether accounts are currently cached for the player.
      Parameters:
      player - player to check for
      Returns:
      true if the player's accounts are cached
    • saveAllPlayerAccounts

      public static void saveAllPlayerAccounts()
      Saves every cached player's accounts.
    • saveAllPlayerAccounts

      public static void saveAllPlayerAccounts(boolean clearCache)
      Saves every cached player's accounts, one stripe at a time.

      When clearCache is set, each entry is dropped under the stripe that saved it, so a player who joins during the pass stays cached instead of being discarded unsaved.

      Parameters:
      clearCache - whether to drop each player from the cache once saved
    • getAllPlayerAccounts

      public static Map<UUID,PlayerAccounts> getAllPlayerAccounts()
      Retrieves a snapshot of every cached player's accounts. The map is a copy; the accounts in it are the live instances.
      Returns:
      an unmodifiable snapshot of the cache
    • loadAllPlayerAccounts

      public static void loadAllPlayerAccounts()
      Loads the accounts of every online player.