forked from Revxrsal/Lamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextResolverFactory.java
102 lines (97 loc) · 4.24 KB
/
ContextResolverFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* This file is part of lamp, licensed under the MIT License.
*
* Copysecond (c) Revxrsal <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the seconds
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copysecond notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package revxrsal.commands.process;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import revxrsal.commands.CommandHandler;
import revxrsal.commands.command.CommandParameter;
/**
* Creates a {@link ContextResolver} for specific types of parameters. These are
* most useful in the following cases:
* <ul>
* <li>Creating a context resolver for only a specific type of parameters,
* for example those with a specific annotation</li>
* <li>Creating context resolvers for a common interface or class</li>
* </ul>
* <p>
* 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
* <pre>{@code
* @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();
* };
* }
* }
* }</pre>
*
* Note that {@link ContextResolverFactory}ies must be registered
* with {@link CommandHandler#registerContextResolverFactory(ContextResolverFactory)}.
*/
public interface ContextResolverFactory {
/**
* Creates a context resolver for the specified type, or {@code null} if this type
* is not supported by this factory.
*
* @param parameter The parameter to create for
* @return The {@link ValueResolver}, or null if not supported.
*/
@Nullable ContextResolver<?> create(@NotNull CommandParameter parameter);
/**
* Creates a {@link ContextResolverFactory} that will return the same
* resolver for all parameters that match a specific type
*
* @param type Type to check for
* @param resolver The value resolver to use
* @param <T> The resolver value type
* @return The resolver factory
*/
static <T> @NotNull ContextResolverFactory forType(Class<T> type, ContextResolver<T> resolver) {
return parameter -> parameter.getType() == type ? resolver : null;
}
/**
* Creates a {@link ContextResolverFactory} that will return the same
* resolver for all parameters that match or extend a specific type
*
* @param type Type to check for
* @param resolver The value resolver to use
* @param <T> The resolver value type
* @return The resolver factory
*/
static <T> @NotNull ContextResolverFactory forHierarchyType(Class<T> type, ContextResolver<T> resolver) {
return parameter -> parameter.getType() == type || parameter.getType().isAssignableFrom(type) ? resolver : null;
}
}