Class PreferencesManager

java.lang.Object
tk.airshipcraft.commonlib.configuration.impl.PreferencesManager

public class PreferencesManager extends Object

Manages the preferences of all players on a Minecraft server.

This class acts as a central point for registering, loading, saving, and resetting player preferences.

It uses a thread-safe map to handle concurrent operations, ensuring that player preference data is managed safely in a multi-threaded environment.

Example implementation would be checking if a player has preferences, updating specific preference fields, and saving all preferences during server shutdown.

Example usage:


 // To check if a player has preferences:
 boolean hasPrefs = preferencesManager.hasPreferences(player);

 // To update a specific preference field:
 preferencesManager.updatePreferenceField(player, "townName", "NewTown");

 // To save all preferences on server shutdown:
 preferencesManager.saveAllPreferences();
 
Since:
2023-11-20
Version:
1.0.0
Author:
notzune
  • Constructor Details

    • PreferencesManager

      public PreferencesManager()
  • Method Details

    • registerPreference

      public void registerPreference(UUID playerUuid, IPlayerPreference preference)
      Registers or updates the preference object for a specific player. If a preference object already exists for the player, it will be replaced with the new one.
      Parameters:
      playerUuid - The UUID of the player.
      preference - The player's preference object to be registered or updated.
    • loadPreferences

      public void loadPreferences(org.bukkit.entity.Player player)
      Loads the preferences for a specific player. Should be called when a player logs in or when preferences need to be refreshed.
      Parameters:
      player - The player whose preferences are to be loaded.
    • savePreferences

      public void savePreferences(org.bukkit.entity.Player player)
      Saves the preferences for a specific player. Should be called when a player logs out or when preferences need to be persisted.
      Parameters:
      player - The player whose preferences are to be saved.
    • resetPreferences

      public void resetPreferences(org.bukkit.entity.Player player)
      Resets the preferences for a specific player to their default values. This can be useful when preferences need to be reverted to a known state.
      Parameters:
      player - The player whose preferences are to be reset.
    • describePreferences

      public String describePreferences(org.bukkit.entity.Player player)
      Provides a descriptive string of a player's preferences. Useful for displaying preference information to the player or for administrative purposes.
      Parameters:
      player - The player whose preferences are to be described.
      Returns:
      A string description of the player's preferences, or a message indicating no preferences are registered.
    • getAllPreferences

      public Set<IPlayerPreference> getAllPreferences()
      Retrieves a set of all registered player preferences. This can be useful for administrative or debugging purposes.
      Returns:
      An unmodifiable set containing all registered player preference objects.
    • hasPreferences

      public boolean hasPreferences(org.bukkit.entity.Player player)
      Checks whether a specific player has preferences registered.
      Parameters:
      player - The player to check.
      Returns:
      True if the player has preferences registered, false otherwise.
    • getPreference

      public IPlayerPreference getPreference(org.bukkit.entity.Player player)
      Retrieves the preference object associated with a specific player. This allows direct access to and manipulation of the player's preferences.
      Parameters:
      player - The player whose preference object is to be retrieved.
      Returns:
      The IPlayerPreference object for the player, or null if none is registered.
    • updatePreferenceField

      public void updatePreferenceField(org.bukkit.entity.Player player, String preferenceKey, Object newValue)
      Updates a specific field within a player's preferences. This method can utilize reflection to identify and update fields annotated with @PlayerPref.
      Parameters:
      player - The player whose preference is to be updated.
      preferenceKey - The key identifying the preference field to update.
      newValue - The new value to assign to the preference field.
    • saveAllPreferences

      public void saveAllPreferences()
      Saves all registered player preferences. This method should be invoked during server shutdown to ensure all preferences are persisted.