Interface PreferenceService

All Superinterfaces:
Service

@Experimental @AvailableSince("1.3") public interface PreferenceService extends 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 Details

    • registerType

      @AvailableSince("1.3") <T> void registerType(PreferenceType<T> type) throws IllegalArgumentException
      Registers a PreferenceType for 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

      @AvailableSince("1.3") <T> Optional<PreferenceType<T>> getType(Class<T> clazz)
      Gets a registered PreferenceType for 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

      @AvailableSince("1.3") <T> T get(UUID playerId, Preference<T> preference)
      Gets a player's preference value, or the default if not set.
      Type Parameters:
      T - the preference value type
      Parameters:
      playerId - the player's UUID
      preference - the preference to get
      Returns:
      the preference value, or default if not set
    • set

      @AvailableSince("1.3") <T> void set(UUID playerId, Preference<T> preference, T value)
      Sets a player's preference value.
      Type Parameters:
      T - the preference value type
      Parameters:
      playerId - the player's UUID
      preference - the preference to set
      value - the value to set
    • register

      @AvailableSince("1.3") <T> void register(Preference<T> preference)
      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

      @AvailableSince("1.3") Collection<Preference<?>> getPreferences()
      Gets all registered preferences.
      Returns:
      unmodifiable collection of registered preferences
    • getPreferencesByPlugin

      @AvailableSince("1.3") Collection<Preference<?>> getPreferencesByPlugin(String pluginNamespace)
      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

      @AvailableSince("1.3") Set<String> getPluginNamespaces()
      Gets all registered plugin namespaces.
      Returns:
      unmodifiable set of plugin namespace identifiers