Interface SuggestionProviderFactory

All Known Implementing Classes:
EntitySelectorResolver.SelectorSuggestionFactory

public interface SuggestionProviderFactory
Creates a SuggestionProvider for the given type of parameter. These are most useful in the following cases:
  • Creating a suggestion provider for only a specific type of parameters, for example those with a specific annotation
  • Creating suggestion providers for a common interface or class
Example: We want to create a suggestion provider for parameters that have a custom annotation

 @Target(ElementType.PARAMETER)
 @Retention(RetentionPolicy.RUNTIME)
 public @interface WithinRadius {
      double value();
 }

 public final class WithinRadiusSuggestionFactory implements SuggestionProviderFactory {

     @Override public @Nullable SuggestionProvider create(@NotNull CommandParameter parameter) {
         if (parameter.getType() != Player.class) return null;
         WithinRadius radius = parameter.getAnnotation(WithinRadius.class);
         if (radius == null) return null;
         return (args, sender, command) -> {
             return ((BukkitCommandActor) actor).requirePlayer().getNearbyEntities(
                radius.value(), radius.value(), radius.value()
             )
             .stream().filter(entity -> entity instanceof Player)
             .map(Player::getName).collect(Collectors.toList());
         };
     }
 }
 

Note that SuggestionProviderFactoryies must be registered with AutoCompleter.registerSuggestionFactory(SuggestionProviderFactory)

  • Method Details

    • createSuggestionProvider

      @Nullable @Nullable SuggestionProvider createSuggestionProvider(@NotNull @NotNull CommandParameter parameter)
      Creates a SuggestionProvider for the given parameter. If this parameter is not applicable, null should be returned.
      Parameters:
      parameter - Parameter to create for
      Returns:
      The suggestion provider for the parameter, or null if not applicable.
    • forType

      static SuggestionProviderFactory forType(Class<?> type, SuggestionProvider provider)
      Creates a SuggestionProviderFactory that will return the same provider for all parameters that match a specific type
      Parameters:
      type - Type to check for
      provider - The provider to use
      Returns:
      The provider factory
    • forHierarchyType

      static SuggestionProviderFactory forHierarchyType(Class<?> type, SuggestionProvider provider)
      Creates a SuggestionProviderFactory that will return the same provider for all parameters that match or extend a specific type
      Parameters:
      type - Type to check for
      provider - The provider to use
      Returns:
      The provider factory