Skip to content

Commit

Permalink
New system replacing Value Resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Oct 6, 2024
1 parent e25c73a commit 3e9d16e
Show file tree
Hide file tree
Showing 85 changed files with 900 additions and 544 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* within the application, typically in a context like a game server or
* chat system where audiences are dynamically determined.
*
* @param <S> the type of the source from which the {@link Audience} can be derived
* @param <S> the valueType of the source from which the {@link Audience} can be derived
*/
@SuppressWarnings("unchecked")
public interface AdventureProvider<S> {
Expand Down
20 changes: 10 additions & 10 deletions brigadier/src/main/java/dev/velix/imperat/ArgumentTypeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@

/**
* A resolver that specifies {@link ArgumentType}
* for each parameter depending on the parameter's type.
* for each parameter depending on the parameter's valueType.
*/
@FunctionalInterface
public interface ArgumentTypeResolver {

/**
* Creates a {@link ArgumentTypeResolver} that will return the same
* argument type for all parameters that match a specific type
* argument valueType for all parameters that match a specific valueType
*
* @param type Type to check for
* @param argumentType The argument type to return
* @param argumentType The argument valueType to return
* @return The resolver factory
*/
static @NotNull ArgumentTypeResolver forType(Class<?> type, ArgumentType<?> argumentType) {
return parameter -> parameter.type() == type ? argumentType : null;
return parameter -> parameter.valueType() == type ? argumentType : null;
}

/**
* Creates a {@link ArgumentTypeResolver} that will return the same
* argument type for all parameters that match or extend a specific type
* argument valueType for all parameters that match or extend a specific valueType
*
* @param type Type to check for
* @param argumentType The argument type to return
* @param argumentType The argument valueType to return
* @return The resolver factory
*/
static @NotNull ArgumentTypeResolver forHierarchyType(Class<?> type, ArgumentType<?> argumentType) {
return parameter -> {
var token = TypeWrap.of(parameter.type());
var token = TypeWrap.of(parameter.valueType());
var token2 = TypeWrap.of(type);
return parameter.type() == type || token.isSupertypeOf(token2) ? argumentType : null;
return parameter.valueType() == type || token.isSupertypeOf(token2) ? argumentType : null;
};
}

/**
* Returns the argument type for the given parameter.
* Returns the argument valueType for the given parameter.
* If unknown, it returns null
*
* @param parameter Parameter to create for
* @return The argument type
* @return The argument valueType
*/
@Nullable
ArgumentType<?> resolveArgType(@NotNull CommandParameter<?> parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ public <T> void registerArgumentResolver(
return argumentTypeResolver.resolveArgType(flagParameter);
}

return param.type() == flagParameter.flagData().inputType()
return param.valueType() == flagParameter.flagData().inputType()
? argumentTypeResolver.resolveArgType(param) : null;
}
return TypeUtility.matches(param.type(), type) ? argumentTypeResolver.resolveArgType(param) : null;
return TypeUtility.matches(param.valueType(), type) ? argumentTypeResolver.resolveArgType(param) : null;
});
}

Expand Down
16 changes: 8 additions & 8 deletions brigadier/src/main/java/dev/velix/imperat/BrigadierManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* A class that manages parsing {@link Command}
* into brigadier {@link CommandNode}
*
* @param <S> the command-source type
* @param <S> the command-source valueType
*/
public sealed interface BrigadierManager<S extends Source> permits BaseBrigadierManager {

Expand All @@ -26,24 +26,24 @@ public sealed interface BrigadierManager<S extends Source> permits BaseBrigadier
S wrapCommandSource(Object commandSource);

/**
* Registers the argument type to its class type
* Registers the argument valueType to its class valueType
*
* @param type the type to register to the value-type obj
* @param argumentTypeResolver the value type resolver
* @param <T> the type parameter for the type.
* @param type the valueType to register to the value-valueType obj
* @param argumentTypeResolver the value valueType resolver
* @param <T> the valueType parameter for the valueType.
*/
<T> void registerArgumentResolver(Class<T> type, ArgumentTypeResolver argumentTypeResolver);

/**
* Registers the argument type resolver
* Registers the argument valueType resolver
*
* @param argumentTypeResolver the value type resolver
* @param argumentTypeResolver the value valueType resolver
*/
void registerArgumentResolver(ArgumentTypeResolver argumentTypeResolver);


/**
* Fetches the argument type from the parameter
* Fetches the argument valueType from the parameter
*
* @param parameter the parameter
* @return the {@link ArgumentType} for the {@link CommandParameter}
Expand Down
4 changes: 2 additions & 2 deletions bukkit/src/main/java/dev/velix/imperat/BukkitImperat.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ public static Class<? extends Entity> getSelectedEntity(@NotNull Type selectorTy
}

/**
* Wraps the sender into a built-in command-sender type
* Wraps the sender into a built-in command-sender valueType
*
* @param sender the sender's actual value
* @return the wrapped command-sender type
* @return the wrapped command-sender valueType
*/
@Override
public BukkitSource wrapSender(Object sender) {
Expand Down
2 changes: 1 addition & 1 deletion bukkit/src/main/java/dev/velix/imperat/BukkitSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public String name() {
}

/**
* @return The original command sender type instance
* @return The original command sender valueType instance
*/
@Override
public CommandSender origin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DefaultArgTypeResolvers {

if (parameter.isNumeric()) {
NumericRange range = parameter.asNumeric().getRange();
return numeric(parameter.type(), range);
return numeric(parameter.valueType(), range);
}

return null;
Expand All @@ -34,8 +34,8 @@ class DefaultArgTypeResolvers {

//TODO add entity selector
/*public static final ArgumentTypeResolver ENTITY_SELECTOR = parameter -> {
Class<? extends Entity> type = BukkitImperat.getSelectedEntity(parameter.getType());
if (Player.class.isAssignableFrom(type)) // EntitySelector<Player>
Class<? extends Entity> valueType = BukkitImperat.getSelectedEntity(parameter.getType());
if (Player.class.isAssignableFrom(valueType)) // EntitySelector<Player>
return MULTI_PLAYER;
return MULTI_ENTITY;
};*/
Expand All @@ -52,7 +52,7 @@ private static ArgumentType<? extends Number> numeric(
} else if (TypeUtility.matches(type, double.class)) {
return DoubleArgumentType.doubleArg(getMin(range), getMax(range));
} else {
throw new IllegalArgumentException("Unsupported numeric type: " + type);
throw new IllegalArgumentException("Unsupported numeric valueType: " + type);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public static void ensureSetup() {
}

/**
* Checks if this argument type is supported in this Minecraft version
* Checks if this argument valueType is supported in this Minecraft version
*
* @return If this is supported
*/
Expand All @@ -290,7 +290,7 @@ public boolean isSupported() {
}

/**
* Checks if this argument type requires parameters
* Checks if this argument valueType requires parameters
*
* @return If this requires parameters
*/
Expand All @@ -299,34 +299,34 @@ public boolean requiresParameters() {
}

/**
* Returns the argument type represented by this enum value, otherwise
* Returns the argument valueType represented by this enum value, otherwise
* throws an exception
*
* @param <T> The argument type
* @return The argument type
* @param <T> The argument valueType
* @return The argument valueType
* @throws IllegalArgumentException if not supported in this version
* @throws IllegalArgumentException if this argument requires arguments. See {@link #create(Object...)}
*/
public @NotNull <T> ArgumentType<T> get() {
if (argumentConstructor == null)
throw new IllegalArgumentException("Argument type '" + name().toLowerCase() + "' is not available on this version.");
throw new IllegalArgumentException("Argument valueType '" + name().toLowerCase() + "' is not available on this version.");
if (argumentType != null)
return (ArgumentType<T>) argumentType;
throw new IllegalArgumentException("This argument type requires " + parameters.length + " parameter(s) of type(s) " +
throw new IllegalArgumentException("This argument valueType requires " + parameters.length + " parameter(s) of valueType(s) " +
Arrays.stream(parameters).map(Class::getName).collect(Collectors.joining(", ")) + ". Use #create() instead.");
}

/**
* Creates an instance of this argument type
* Creates an instance of this argument valueType
*
* @param arguments Arguments to construct the argument type with
* @param arguments Arguments to construct the argument valueType with
* @param <T> The argument ttype
* @return The created argument type.
* @return The created argument valueType.
* @throws IllegalArgumentException if not supported in this version
*/
public @NotNull <T> ArgumentType<T> create(Object... arguments) {
if (argumentConstructor == null) {
throw new IllegalArgumentException("Argument type '" + name().toLowerCase() + "' is not available on this version.");
throw new IllegalArgumentException("Argument valueType '" + name().toLowerCase() + "' is not available on this version.");
}

if (argumentType != null && arguments.length == 0) {
Expand All @@ -341,28 +341,28 @@ public boolean requiresParameters() {
}

/**
* Returns the argument type represented by this enum value, wrapped
* Returns the argument valueType represented by this enum value, wrapped
* inside an {@link Optional}
*
* @param <T> The argument type
* @return The argument type optional
* @param <T> The argument valueType
* @return The argument valueType optional
* @throws IllegalArgumentException if this argument requires arguments. See {@link #createIfPresent(Object...)}
*/
public @NotNull <T> Optional<ArgumentType<T>> getIfPresent() {
if (argumentConstructor == null)
return Optional.empty();
if (argumentType != null)
return Optional.of((ArgumentType<T>) argumentType);
throw new IllegalArgumentException("This argument type requires " + parameters.length + " parameter(s) of type(s) " +
throw new IllegalArgumentException("This argument valueType requires " + parameters.length + " parameter(s) of valueType(s) " +
Arrays.stream(parameters).map(Class::getName).collect(Collectors.joining(", ")) + ". Use #create() instead.");
}

/**
* Creates an instance of this argument type, wrapped in an optional.
* Creates an instance of this argument valueType, wrapped in an optional.
*
* @param arguments Arguments to construct the argument type with
* @param arguments Arguments to construct the argument valueType with
* @param <T> The argument ttype
* @return The created argument type optional.
* @return The created argument valueType optional.
*/
public @NotNull <T> Optional<ArgumentType<T>> createIfPresent(Object... arguments) {
if (argumentConstructor == null) {
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/dev/velix/imperat/Imperat.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* It also caches the settings that the user can
* change or modify in the api.
*
* @param <S> the command sender type
* @param <S> the command sender valueType
*/
@ApiStatus.AvailableSince("1.0.0")
public non-sealed interface Imperat<S extends Source> extends
Expand Down Expand Up @@ -71,21 +71,21 @@ public non-sealed interface Imperat<S extends Source> extends
void setAnnotationParser(AnnotationParser<S> parser);

/**
* Registers a type of annotations so that it can be
* detected by {@link AnnotationReader} , it's useful as it allows that type of annotation
* Registers a valueType of annotations so that it can be
* detected by {@link AnnotationReader} , it's useful as it allows that valueType of annotation
* to be recognized as a true Imperat-related annotation to be used in something like checking if a
* {@link CommandParameter} is annotated and checks for the annotations it has.
*
* @param type the type of annotation
* @param type the valueType of annotation
*/
void registerAnnotations(Class<? extends Annotation>... type);

/**
* Registers annotation replacer
*
* @param type the type to replace the annotation by
* @param type the valueType to replace the annotation by
* @param replacer the replacer
* @param <A> the type of annotation to replace
* @param <A> the valueType of annotation to replace
*/
<A extends Annotation> void registerAnnotationReplacer(
final Class<A> type,
Expand Down
Loading

0 comments on commit 3e9d16e

Please sign in to comment.