Class AudienceResolver

java.lang.Object
dev.ua.theroer.magicutils.platform.AudienceResolver

public final class AudienceResolver extends Object
Normalizes "any sender-like object" into an Audience.

Plugins receive different sender types from different code paths (Bukkit Player, Velocity CommandSource, raw CommandSender, an existing Audience, or null). This utility unifies that decoding so callers can write Messages.send(player, key) without re-implementing the type branching dance every time.

Resolution order:

  1. null → null.
  2. Audience instance → returned as is.
  3. Each registered factory is tried in registration order; the first non-null result wins.
  4. Reflective fallback: if the object exposes a getUniqueId() returning UUID, a synthetic Audience carrying just that id is returned.

Platform bootstraps (Bukkit/Velocity/Bungee/Fabric/NeoForge) register a factory at start-up that knows how to wrap their native types into proper Audience implementations. Callers should never need to know which platform they are on.

  • Method Details

    • registerFactory

      public static void registerFactory(Function<Object,Audience> factory)
      Registers a factory that wraps platform-specific objects (e.g. Bukkit Player, Velocity CommandSource) into an Audience.

      Factories receive the raw object and may return null when they cannot handle it. Resolution stops at the first non-null result.

      Parameters:
      factory - non-null factory
    • unregisterFactory

      public static void unregisterFactory(Function<Object,Audience> factory)
      Removes a previously registered factory.
      Parameters:
      factory - factory to remove
    • resolve

      @Nullable public static @Nullable Audience resolve(@Nullable @Nullable Object who)
      Resolves any object into an Audience. Returns null when no resolution is possible.
      Parameters:
      who - Audience, platform sender, player-like object, or null
      Returns:
      audience or null
    • extractUuid

      @Nullable public static @Nullable UUID extractUuid(@Nullable @Nullable Object who)
      Extracts a UUID from any object without constructing an Audience. Useful for code paths that only need to look up a player language preference.
      Parameters:
      who - Audience, player-like object, or null
      Returns:
      UUID or null