Skip to content

Commit

Permalink
Removed TypeSuggestionResolver.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Oct 20, 2024
1 parent d22d93d commit 693e771
Show file tree
Hide file tree
Showing 23 changed files with 193 additions and 143 deletions.
3 changes: 2 additions & 1 deletion bukkit/src/main/java/dev/velix/imperat/BukkitImperat.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ private void registerValueResolvers() {
this.registerParamType(Player.class, new ParameterPlayer());
this.registerParamType(OfflinePlayer.class, new ParameterOfflinePlayer());
this.registerParamType(TargetSelector.class, new ParameterTargetSelector());
this.registerParamType(Reflections.getClass("org.bukkit.World"), new ParameterWorld());
var worldClass = Reflections.getClass("org.bukkit.World");
this.registerParamType(worldClass, new ParameterWorld(worldClass));
}

public void applyBrigadier() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.command.parameters.type.BaseParameterType;
import dev.velix.imperat.context.ExecutionContext;
import dev.velix.imperat.context.SuggestionContext;
import dev.velix.imperat.context.internal.CommandInputStream;
import dev.velix.imperat.exception.ImperatException;
import dev.velix.imperat.exception.UnknownOfflinePlayerException;
import dev.velix.imperat.exception.UnknownPlayerException;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.util.TypeWrap;
import net.minecraft.server.v1_13_R2.MinecraftServer;
import org.bukkit.Bukkit;
Expand All @@ -19,6 +21,8 @@
import org.jetbrains.annotations.Nullable;
import org.spigotmc.SpigotConfig;

import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;

public class ParameterOfflinePlayer extends BaseParameterType<BukkitSource, OfflinePlayer> {
Expand Down Expand Up @@ -69,5 +73,18 @@ public boolean matchesInput(String input, CommandParameter<BukkitSource> paramet
return input.length() <= 16;
}

private final static class PlayerSuggestionResolver implements SuggestionResolver<BukkitSource> {

/**
* @param context the context for suggestions
* @param parameter the parameter of the value to complete
* @return the auto-completed suggestions of the current argument
*/
@Override
public Collection<String> autoComplete(SuggestionContext<BukkitSource> context, CommandParameter<BukkitSource> parameter) {
return Arrays.stream(Bukkit.getOfflinePlayers())
.map(OfflinePlayer::getName)
.toList();
}
}
}
27 changes: 27 additions & 0 deletions bukkit/src/main/java/dev/velix/imperat/type/ParameterPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.command.parameters.type.BaseParameterType;
import dev.velix.imperat.context.ExecutionContext;
import dev.velix.imperat.context.SuggestionContext;
import dev.velix.imperat.context.internal.CommandInputStream;
import dev.velix.imperat.exception.ImperatException;
import dev.velix.imperat.exception.UnknownPlayerException;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.util.TypeWrap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

public class ParameterPlayer extends BaseParameterType<BukkitSource, Player> {

private final PlayerSuggestionResolver SUGGESTION_RESOLVER = new PlayerSuggestionResolver();
public ParameterPlayer() {
super(TypeWrap.of(Player.class));
}
Expand Down Expand Up @@ -43,4 +48,26 @@ public boolean matchesInput(String input, CommandParameter<BukkitSource> paramet
return input.length() <= 16;
}

/**
* Returns the suggestion resolver associated with this parameter type.
*
* @return the suggestion resolver for generating suggestions based on the parameter type.
*/
@Override
public SuggestionResolver<BukkitSource> getSuggestionResolver() {
return SUGGESTION_RESOLVER;
}

private final static class PlayerSuggestionResolver implements SuggestionResolver<BukkitSource> {

/**
* @param context the context for suggestions
* @param parameter the parameter of the value to complete
* @return the auto-completed suggestions of the current argument
*/
@Override
public Collection<String> autoComplete(SuggestionContext<BukkitSource> context, CommandParameter<BukkitSource> parameter) {
return Bukkit.getOnlinePlayers().stream().map(Player::getName).toList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import dev.velix.imperat.context.internal.CommandInputStream;
import dev.velix.imperat.exception.ImperatException;
import dev.velix.imperat.exception.SourceException;
import dev.velix.imperat.resolvers.TypeSuggestionResolver;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.selector.EntityCondition;
import dev.velix.imperat.selector.SelectionParameterInput;
import dev.velix.imperat.selector.SelectionType;
Expand All @@ -31,14 +31,14 @@ public final class ParameterTargetSelector extends BaseParameterType<BukkitSourc
private final static char PARAMETER_START = '[';
private final static char PARAMETER_END = ']';

private final TypeSuggestionResolver<BukkitSource, TargetSelector> suggestionResolver;
private final SuggestionResolver<BukkitSource> suggestionResolver;

public ParameterTargetSelector() {
super(TypeWrap.of(TargetSelector.class));
SelectionType.TYPES.stream()
.filter(type -> type != SelectionType.UNKNOWN)
.map(SelectionType::id)
.forEach((id) -> suggestions.add(String.valueOf(SelectionType.MENTION_CHARACTER + id)));
.forEach((id) -> suggestions.add(SelectionType.MENTION_CHARACTER + id));
suggestionResolver = new TargetSelectorSuggestionResolver();
}

Expand Down Expand Up @@ -135,19 +135,11 @@ private static <V> void operateFields(List<SelectionParameterInput<?>> inputPara
* @return the suggestion resolver for generating suggestions based on the parameter type.
*/
@Override
public TypeSuggestionResolver<BukkitSource, TargetSelector> getSuggestionResolver() {
public SuggestionResolver<BukkitSource> getSuggestionResolver() {
return suggestionResolver;
}

private final class TargetSelectorSuggestionResolver implements TypeSuggestionResolver<BukkitSource, TargetSelector> {

/**
* @return the valueType that is specific for these suggestions resolving
*/
@Override
public @NotNull TypeWrap<TargetSelector> getType() {
return TypeWrap.of(TargetSelector.class);
}
private final class TargetSelectorSuggestionResolver implements SuggestionResolver<BukkitSource> {

/**
* @param context the context for suggestions
Expand Down
37 changes: 35 additions & 2 deletions bukkit/src/main/java/dev/velix/imperat/type/ParameterWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
import dev.velix.imperat.context.internal.CommandInputStream;
import dev.velix.imperat.exception.ImperatException;
import dev.velix.imperat.exception.UnknownWorldException;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.util.TypeWrap;
import dev.velix.imperat.util.reflection.Reflections;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

public class ParameterWorld extends BaseParameterType<BukkitSource, World> {

Expand All @@ -26,14 +30,17 @@ public class ParameterWorld extends BaseParameterType<BukkitSource, World> {
try {
assert WORLD_CLASS != null;
GET_WORLDS = Bukkit.class.getMethod("getWorlds");
GET_WORLDS.setAccessible(true);

GET_NAME = WORLD_CLASS.getMethod("getName");
GET_NAME.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}

public ParameterWorld() {
super(TypeWrap.of(World.class));
public ParameterWorld(Class<?> type) {
super(TypeWrap.of((Class<World>) type));
}

@Override
Expand All @@ -51,4 +58,30 @@ public boolean matchesInput(String input, CommandParameter<BukkitSource> paramet
return super.matchesInput(input, parameter);
}

/**
* Returns the suggestion resolver associated with this parameter type.
*
* @return the suggestion resolver for generating suggestions based on the parameter type.
*/
@Override
@SuppressWarnings("unchecked")
public SuggestionResolver<BukkitSource> getSuggestionResolver() {
try {
return SuggestionResolver.plain(((List<Object>) GET_WORLDS.invoke(null))
.stream()
.map((obj) -> {
try {
return (String) GET_NAME.invoke(obj);
} catch (IllegalAccessException | InvocationTargetException e) {
return null;
}
})
.filter(Objects::nonNull)
.toList()
);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.command.parameters.type.BaseParameterType;
import dev.velix.imperat.context.ExecutionContext;
import dev.velix.imperat.context.SuggestionContext;
import dev.velix.imperat.context.internal.CommandInputStream;
import dev.velix.imperat.exception.ImperatException;
import dev.velix.imperat.exception.UnknownPlayerException;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.util.TypeWrap;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

public final class ParameterProxiedPlayer extends BaseParameterType<BungeeSource, ProxiedPlayer> {

private final ProxiedPlayerSuggestionResolver PROXIED_PLAYER_SUGGESTION_RESOLVER = new ProxiedPlayerSuggestionResolver();
public ParameterProxiedPlayer() {
super(TypeWrap.of(ProxiedPlayer.class));
}
Expand Down Expand Up @@ -46,4 +51,29 @@ public boolean matchesInput(String input, CommandParameter<BungeeSource> paramet
return ProxyServer.getInstance().getPlayer(input) != null;
}

/**
* Returns the suggestion resolver associated with this parameter type.
*
* @return the suggestion resolver for generating suggestions based on the parameter type.
*/
@Override
public SuggestionResolver<BungeeSource> getSuggestionResolver() {
return PROXIED_PLAYER_SUGGESTION_RESOLVER;
}

private final static class ProxiedPlayerSuggestionResolver implements SuggestionResolver<BungeeSource> {

/**
* @param context the context for suggestions
* @param parameter the parameter of the value to complete
* @return the auto-completed suggestions of the current argument
*/
@Override
public Collection<String> autoComplete(SuggestionContext<BungeeSource> context, CommandParameter<BungeeSource> parameter) {
return ProxyServer.getInstance().getPlayers().stream()
.map(ProxiedPlayer::getName)
.toList();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ default <T> ContextResolver<S, T> getContextResolver(CommandParameter<S> command
if (parameterSpecificResolver == null) {
var resolverByType = getSuggestionResolverByType(parameter.valueType());
if (resolverByType != null) return resolverByType;
else return SuggestionResolver.type(String.class, Collections.singletonList(parameter.format()));
else return SuggestionResolver.plain(Collections.singletonList(parameter.format()));
} else
return parameterSpecificResolver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import dev.velix.imperat.command.processors.CommandPreProcessor;
import dev.velix.imperat.context.Source;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.resolvers.TypeSuggestionResolver;
import dev.velix.imperat.supplier.OptionalValueSupplier;
import dev.velix.imperat.util.TypeUtility;
import dev.velix.imperat.util.TypeWrap;
Expand Down Expand Up @@ -441,22 +440,19 @@ private <T> CommandParameter<S> loadParameter(
Suggest suggestAnnotation = element.getAnnotation(Suggest.class);
SuggestionProvider suggestionProvider = element.getAnnotation(SuggestionProvider.class);

TypeSuggestionResolver<S, ?> suggestionResolver = null;
SuggestionResolver<S> suggestionResolver = null;

if (suggestAnnotation != null) {
suggestionResolver = SuggestionResolver.type(
parameterTypeWrap,
suggestionResolver = SuggestionResolver.plain(
imperat.replacePlaceholders(suggestAnnotation.value())
);
} else if (suggestionProvider != null) {
String suggestionResolverName = imperat.replacePlaceholders(suggestionProvider.value().toLowerCase());
var namedResolver = imperat.getNamedSuggestionResolver(
suggestionResolverName
);
if (namedResolver != null && !(namedResolver instanceof TypeSuggestionResolver<?, ?>))
throw new UnsupportedOperationException("Named suggestion resolvers must be of valueType `TypeSuggestionResolver` and make sure the valueType matches that of the parameter's");
else if (namedResolver != null)
suggestionResolver = (TypeSuggestionResolver<S, ?>) namedResolver;
if (namedResolver != null)
suggestionResolver = namedResolver;
else {
throw new IllegalStateException("Unregistered named suggestion resolver : " + suggestionResolverName);
}
Expand Down Expand Up @@ -494,12 +490,12 @@ else if (namedResolver != null)
if (flag != null) {
String[] flagAliases = flag.value();
if (suggestAnnotation != null) {
suggestionResolver = SuggestionResolver.type(TypeWrap.of(parameter.getParameterizedType()), imperat.replacePlaceholders(suggestAnnotation.value()));
suggestionResolver = SuggestionResolver.plain(imperat.replacePlaceholders(suggestAnnotation.value()));
}

return AnnotationParameterDecorator.decorate(
CommandParameter.flag(name, type)
.suggestForInputValue((TypeSuggestionResolver<S, T>) suggestionResolver)
.suggestForInputValue(suggestionResolver)
.aliases(getAllExceptFirst(flagAliases))
.flagDefaultInputValue(optionalValueSupplier)
.description(desc)
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/dev/velix/imperat/command/CommandImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import dev.velix.imperat.exception.ImperatException;
import dev.velix.imperat.help.PaginatedHelpTemplate;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.resolvers.TypeSuggestionResolver;
import dev.velix.imperat.util.ImperatDebugger;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -210,8 +209,8 @@ public FlagParameter<S> asFlagParameter() {
* @return the {@link SuggestionResolver} for a resolving suggestion
*/
@Override
public @Nullable <T> TypeSuggestionResolver<S, T> getSuggestionResolver() {
return (TypeSuggestionResolver<S, T>) suggestionResolver;
public @Nullable SuggestionResolver<S> getSuggestionResolver() {
return suggestionResolver;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import dev.velix.imperat.command.parameters.type.ParameterTypes;
import dev.velix.imperat.context.Source;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.resolvers.TypeSuggestionResolver;
import dev.velix.imperat.supplier.OptionalValueSupplier;
import dev.velix.imperat.util.Preconditions;
import dev.velix.imperat.util.TypeWrap;
Expand All @@ -34,7 +33,7 @@ static <S extends Source, T> CommandParameter<S> of(
boolean optional,
boolean greedy,
@NotNull OptionalValueSupplier<T> valueSupplier,
@Nullable TypeSuggestionResolver<S, ?> suggestionResolver
@Nullable SuggestionResolver<S> suggestionResolver
) {
Preconditions.notNull(name, "name");
Preconditions.notNull(type, "type");
Expand Down Expand Up @@ -236,11 +235,10 @@ default AnnotatedParameter<S> asAnnotated() {
* Fetches the suggestion resolver linked to this
* command parameter.
*
* @param <T> the valueType of value to be resolved
* @return the {@link SuggestionResolver} for a resolving suggestion
*/
@Nullable
<T> TypeSuggestionResolver<S, T> getSuggestionResolver();
SuggestionResolver<S> getSuggestionResolver();

/**
* Formats the usage parameter*
Expand Down
Loading

0 comments on commit 693e771

Please sign in to comment.