Interface ContextResolverFactory


public interface ContextResolverFactory
Creates a ContextResolver for specific types of parameters. These are most useful in the following cases:
  • Creating a context resolver for only a specific type of parameters, for example those with a specific annotation
  • Creating context resolvers for a common interface or class

Example: We want to register a special context resolver for org.bukkit.Locations that are annotated with a specific annotation, in which the argument will be fed with the player's target location


 @Target(ElementType.PARAMETER)
 @Retention(RetentionPolicy.RUNTIME)
 public @interface LookingLocation {

 }

 public final class LookingLocationFactory implements ContextResolverFactory {

     @Override public @Nullable ContextResolver<?> create(@NotNull CommandParameter parameter) {
         if (parameter.getType() != Location.class) return null;
         if (!parameter.hasAnnotation(LookingLocation.class)) return null;
         return (actor, p, command) -> {
             Player player = ((BukkitCommandActor) actor).requirePlayer();
             return player.getTargetBlock(null, 200).getLocation();
         };
     }
 }
 
Note that ContextResolverFactoryies must be registered with CommandHandler.registerContextResolverFactory(ContextResolverFactory).
  • Method Details

    • create

      @Nullable @Nullable ContextResolver<?> create(@NotNull @NotNull CommandParameter parameter)
      Creates a context resolver for the specified type, or null if this type is not supported by this factory.
      Parameters:
      parameter - The parameter to create for
      Returns:
      The ValueResolver, or null if not supported.
    • forType

      @NotNull static <T> @NotNull ContextResolverFactory forType(Class<T> type, ContextResolver<T> resolver)
      Creates a ContextResolverFactory that will return the same resolver for all parameters that match a specific type
      Type Parameters:
      T - The resolver value type
      Parameters:
      type - Type to check for
      resolver - The value resolver to use
      Returns:
      The resolver factory
    • forHierarchyType

      @NotNull static <T> @NotNull ContextResolverFactory forHierarchyType(Class<T> type, ContextResolver<T> resolver)
      Creates a ContextResolverFactory that will return the same resolver for all parameters that match or extend a specific type
      Type Parameters:
      T - The resolver value type
      Parameters:
      type - Type to check for
      resolver - The value resolver to use
      Returns:
      The resolver factory