Class PreferenceProcessor

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

public class PreferenceProcessor extends Object
Provides static utility methods to process player preferences using reflection. This class handles loading and saving player preferences to and from various data stores (e.g., files, databases), by dynamically interacting with fields annotated with PlayerPref. The use of reflection allows for a flexible and extensible approach to preference management.
Since:
2023-11-20
Version:
1.0.0
Author:
notzune
  • Constructor Details

    • PreferenceProcessor

      public PreferenceProcessor()
  • Method Details

    • load

      public static void load(org.bukkit.entity.Player player, IPlayerPreference preference, org.bukkit.configuration.file.FileConfiguration config)
      Loads player preferences from a configuration file into an IPlayerPreference instance. This method uses reflection to dynamically assign values to fields annotated with PlayerPref based on the stored config.
      Parameters:
      player - The player whose preferences should be loaded.
      preference - The IPlayerPreference object containing the fields to be loaded.
      config - The configuration file from which to load the preferences.
    • save

      public static void save(org.bukkit.entity.Player player, IPlayerPreference preference, org.bukkit.configuration.file.FileConfiguration config)
      Saves player preferences from an IPlayerPreference instance to a configuration file. This method reflects through fields annotated with PlayerPref and saves their values in the config.
      Parameters:
      player - The player whose preferences should be saved.
      preference - The IPlayerPreference object containing the fields to be saved.
      config - The configuration file to which to save the preferences.
    • loadFromDatabase

      public static void loadFromDatabase(org.bukkit.entity.Player player, IPlayerPreference preference, Connection connection)
      Loads player preferences from a MySQL database into an IPlayerPreference instance. This method queries the database for each annotated field's value and assigns it dynamically.
      Parameters:
      player - The player whose preferences should be loaded.
      preference - The IPlayerPreference object containing the fields to be loaded.
      connection - The database connection.
    • saveToDatabase

      public static void saveToDatabase(org.bukkit.entity.Player player, IPlayerPreference preference, Connection connection)
      Saves player preferences from an IPlayerPreference instance to a MySQL database. This method updates the database with the current values of fields annotated with PlayerPref.
      Parameters:
      player - The player whose preferences should be saved.
      preference - The IPlayerPreference object containing the fields to be saved.
      connection - The database connection.
    • convertStringToFieldType

      public static Object convertStringToFieldType(String value, Class<?> type)
      Converts a string value to the specified field type. This utility method facilitates the conversion of database string values to their appropriate field types.
      Parameters:
      value - The string value to convert.
      type - The type to convert the value to.
      Returns:
      The converted object, appropriately typed according to the specified class.