Interface PreferenceService
- All Superinterfaces:
Service
Service for managing player preferences with persistent storage.
Preferences are typed values that can be stored per-player and automatically
serialized/deserialized using registered PreferenceTypes.
Usage
// Define a preference
Preference<String> languagePref = new Preference<>() {
Plugin plugin() { return MyPlugin.getInstance(); }
String displayName() { return "language"; }
String getDefault() { return "en"; }
PreferenceType<String> type() { return PreferenceTypes.STRING; }
};
// Use it directly
PreferenceService service = Mint.PREFERENCE_SERVICE.get();
String lang = service.get(playerId, languagePref);
service.set(playerId, languagePref, "es");
- Since:
- 1.3
-
Method Summary
Modifier and TypeMethodDescription<T> Tget(UUID playerId, Preference<T> preference) Gets a player's preference value, or the default if not set.Gets all registered plugin namespaces.Collection<Preference<?>> Gets all registered preferences.Collection<Preference<?>> getPreferencesByPlugin(String pluginNamespace) Gets all registered preferences for a specific plugin namespace.<T> Optional<PreferenceType<T>> Gets a registeredPreferenceTypefor the given class.<T> voidregister(Preference<T> preference) Registers a preference.<T> voidregisterType(PreferenceType<T> type) Registers aPreferenceTypefor serializing/deserializing preference values.<T> voidset(UUID playerId, Preference<T> preference, T value) Sets a player's preference value.
-
Method Details
-
registerType
@AvailableSince("1.3") <T> void registerType(PreferenceType<T> type) throws IllegalArgumentException Registers aPreferenceTypefor serializing/deserializing preference values.The type is registered using its
PreferenceType.typeKey()which captures the full generic type information at runtime.- Type Parameters:
T- the type of value- Parameters:
type- the type to register- Throws:
IllegalArgumentException- if a type for the same class is already registered
-
getType
Gets a registeredPreferenceTypefor the given class.This searches for a type compatible with the specified class. Common types like String, Integer, Boolean, etc. are available by default.
- Type Parameters:
T- the type- Parameters:
clazz- the class to find a type for- Returns:
- optional containing the type if found
-
get
Gets a player's preference value, or the default if not set.- Type Parameters:
T- the preference value type- Parameters:
playerId- the player's UUIDpreference- the preference to get- Returns:
- the preference value, or default if not set
-
set
Sets a player's preference value.- Type Parameters:
T- the preference value type- Parameters:
playerId- the player's UUIDpreference- the preference to setvalue- the value to set
-
register
Registers a preference.Preferences are automatically grouped by type (booleans, then integers, then floats/doubles, then strings, then enums, then others). Within each type group, preferences are ordered by registration order.
- Type Parameters:
T- the preference value type- Parameters:
preference- the preference to register
-
getPreferences
Gets all registered preferences.- Returns:
- unmodifiable collection of registered preferences
-
getPreferencesByPlugin
Gets all registered preferences for a specific plugin namespace.- Parameters:
pluginNamespace- the plugin namespace to filter by- Returns:
- unmodifiable collection of preferences for the plugin
-
getPluginNamespaces
Gets all registered plugin namespaces.- Returns:
- unmodifiable set of plugin namespace identifiers
-