Class PreferenceProcessor
java.lang.Object
tk.airshipcraft.commonlib.configuration.impl.PreferenceProcessor
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
convertStringToFieldType
(String value, Class<?> type) Converts a string value to the specified field type.static void
load
(org.bukkit.entity.Player player, IPlayerPreference preference, org.bukkit.configuration.file.FileConfiguration config) Loads player preferences from a configuration file into anIPlayerPreference
instance.static void
loadFromDatabase
(org.bukkit.entity.Player player, IPlayerPreference preference, Connection connection) Loads player preferences from a MySQL database into anIPlayerPreference
instance.static void
save
(org.bukkit.entity.Player player, IPlayerPreference preference, org.bukkit.configuration.file.FileConfiguration config) Saves player preferences from anIPlayerPreference
instance to a configuration file.static void
saveToDatabase
(org.bukkit.entity.Player player, IPlayerPreference preference, Connection connection) Saves player preferences from anIPlayerPreference
instance to a MySQL database.
-
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 anIPlayerPreference
instance. This method uses reflection to dynamically assign values to fields annotated withPlayerPref
based on the stored config.- Parameters:
player
- The player whose preferences should be loaded.preference
- TheIPlayerPreference
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 anIPlayerPreference
instance to a configuration file. This method reflects through fields annotated withPlayerPref
and saves their values in the config.- Parameters:
player
- The player whose preferences should be saved.preference
- TheIPlayerPreference
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 anIPlayerPreference
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
- TheIPlayerPreference
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 anIPlayerPreference
instance to a MySQL database. This method updates the database with the current values of fields annotated withPlayerPref
.- Parameters:
player
- The player whose preferences should be saved.preference
- TheIPlayerPreference
object containing the fields to be saved.connection
- The database connection.
-
convertStringToFieldType
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.
-