Package revxrsal.commands.process
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 ContextResolverFactory
ies must be registered
with CommandHandler.registerContextResolverFactory(ContextResolverFactory)
.-
Method Summary
Modifier and TypeMethodDescription@Nullable ContextResolver<?>
create
(@NotNull CommandParameter parameter) Creates a context resolver for the specified type, ornull
if this type is not supported by this factory.static <T> @NotNull ContextResolverFactory
forHierarchyType
(Class<T> type, ContextResolver<T> resolver) Creates aContextResolverFactory
that will return the same resolver for all parameters that match or extend a specific typestatic <T> @NotNull ContextResolverFactory
forType
(Class<T> type, ContextResolver<T> resolver) Creates aContextResolverFactory
that will return the same resolver for all parameters that match a specific type
-
Method Details
-
create
Creates a context resolver for the specified type, ornull
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 aContextResolverFactory
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 forresolver
- The value resolver to use- Returns:
- The resolver factory
-
forHierarchyType
@NotNull static <T> @NotNull ContextResolverFactory forHierarchyType(Class<T> type, ContextResolver<T> resolver) Creates aContextResolverFactory
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 forresolver
- The value resolver to use- Returns:
- The resolver factory
-