Skip to content

Commit

Permalink
Fix completion bugs introduced by the ValueParameter.Simple change
Browse files Browse the repository at this point in the history
Operator parameter now works
  • Loading branch information
dualspiral committed Apr 11, 2021
1 parent e78ff0f commit db2aa92
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 64 deletions.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,4 @@ public interface OperationArgumentAccessor {
throw new UntransformedAccessorError();
}

@Accessor("ERROR_DIVIDE_BY_ZERO")
static SimpleCommandExceptionType accessor$ERROR_DIVIDE_BY_ZERO() {
throw new UntransformedAccessorError();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ default ArgumentParseException modifyExceptionMessage(final SpongeStringReader r

boolean doesNotRead();

default boolean hasClientNativeCompletions() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@
/**
* For use with ArgumentTypes in the base game
*/
public final class StandardCatalogedArgumentParser<S, T> extends StandardArgumentParser<S, T> implements ResourceKeyedValueParameter<T> {
public final class ClientNativeArgumentParser<S, T> extends StandardArgumentParser<S, T> implements ResourceKeyedValueParameter<T> {

public static <T> StandardCatalogedArgumentParser<T, T> createIdentity(final ResourceKey key, final ArgumentType<T> type) {
return new StandardCatalogedArgumentParser<>(key, type, (reader, c, x) -> x);
public static <T> ClientNativeArgumentParser<T, T> createIdentity(final ResourceKey key, final ArgumentType<T> type) {
return new ClientNativeArgumentParser<>(key, type, (reader, c, x) -> x);
}

public static <S, T> StandardCatalogedArgumentParser<S, T> createConverter(
public static <S, T> ClientNativeArgumentParser<S, T> createConverter(
final ResourceKey key,
final ArgumentType<S> type,
final StandardArgumentParser.Converter<S, T> converter) {
return new StandardCatalogedArgumentParser<>(key, type, converter);
return new ClientNativeArgumentParser<>(key, type, converter);
}

// ---

private final ResourceKey key;

private StandardCatalogedArgumentParser(
private ClientNativeArgumentParser(
final ResourceKey key,
final ArgumentType<S> type,
final StandardArgumentParser.Converter<S, T> converter) {
Expand All @@ -63,4 +63,9 @@ public ResourceKey key() {
return this.key;
}

@Override
public boolean hasClientNativeCompletions() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@
*/
package org.spongepowered.common.command.brigadier.argument;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.commands.CommandSourceStack;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.command.CommandCause;
import org.spongepowered.api.command.exception.ArgumentParseException;
import org.spongepowered.api.command.parameter.ArgumentReader;
import org.spongepowered.api.command.parameter.CommandContext;
import org.spongepowered.api.command.parameter.Parameter;
import org.spongepowered.api.command.parameter.managed.ValueParameter;
import org.spongepowered.api.command.parameter.managed.standard.ResourceKeyedValueParameter;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.command.brigadier.context.SpongeCommandContextBuilder;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public abstract class ResourceKeyedArgumentValueParser<T> extends AbstractArgumentParser<T> implements ResourceKeyedValueParameter<T>,
ValueParameter.Simple<T> {
Expand All @@ -59,8 +67,47 @@ public final Optional<? extends T> parseValue(final Parameter.@NonNull Key<? sup

@Override
@NonNull
public final List<String> complete(final @NonNull CommandContext context, final @NonNull String currentInput) {
public List<String> complete(final @NonNull CommandContext context, final @NonNull String currentInput) {
return this.complete(context.cause(), currentInput);
}

// Used when the context is important because we pass through to a child "listSuggestions"
public static abstract class ClientNativeCompletions<T> extends ResourceKeyedArgumentValueParser<T> {

public ClientNativeCompletions(final ResourceKey key) {
super(key);
}

@Override
public final @NonNull List<String> complete(final @NonNull CommandCause cause, final @NonNull String currentInput) {
final CommandDispatcher<CommandSourceStack> dispatcher = SpongeCommon.getServer().getCommands().getDispatcher();
final SpongeCommandContextBuilder builder = new SpongeCommandContextBuilder(
dispatcher,
(CommandSourceStack) cause,
dispatcher.getRoot(),
0
);
return this.complete(builder, currentInput);
}

@Override
@NonNull
public final List<String> complete(final @NonNull CommandContext context, final @NonNull String currentInput) {
final com.mojang.brigadier.context.CommandContext<?> c;
if (context instanceof CommandContext.Builder) {
c = (com.mojang.brigadier.context.CommandContext<?>) ((CommandContext.Builder) context).build(currentInput);
} else {
c = (com.mojang.brigadier.context.CommandContext<?>) context;
}
final SuggestionsBuilder builder = new SuggestionsBuilder(c.getInput(), c.getRange().getStart());
this.listSuggestions(c, builder);
return builder.build().getList().stream().map(Suggestion::getText).collect(Collectors.toList());
}

@Override
public final boolean hasClientNativeCompletions() {
return true;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.spongepowered.api.command.parameter.managed.ValueParameterModifier;
import org.spongepowered.api.command.parameter.managed.ValueUsage;
import org.spongepowered.common.command.brigadier.argument.ArgumentParser;
import org.spongepowered.common.command.brigadier.argument.StandardCatalogedArgumentParser;
import org.spongepowered.common.command.parameter.SpongeParameterKey;

// We use the ArgumentBuilder primarily for setting redirects properly.
Expand All @@ -45,6 +44,13 @@ public final class SpongeArgumentCommandNodeBuilder<T> extends ArgumentBuilder<C
private final @Nullable ValueUsage usage;
private final @Nullable ValueParameterModifier<T> modifier;

private static @Nullable ValueCompleter filterNativeCompleters(final ArgumentParser<?> parser, final ValueCompleter completer) {
if (parser == completer && parser.hasClientNativeCompletions()) {
return null;
}
return completer;
}

public SpongeArgumentCommandNodeBuilder(
final SpongeParameterKey<? super T> key,
final ArgumentParser<T> type,
Expand All @@ -54,7 +60,7 @@ public SpongeArgumentCommandNodeBuilder(
final @Nullable String suffix) {
this.key = key;
this.type = type;
this.completer = type == completer && type instanceof StandardCatalogedArgumentParser ? null : completer;
this.completer = SpongeArgumentCommandNodeBuilder.filterNativeCompleters(type, completer);
this.modifier = modifier;
this.usage = usage;
this.suffix = suffix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.kyori.adventure.text.Component;
Expand All @@ -37,19 +36,15 @@
import org.spongepowered.api.command.CommandCause;
import org.spongepowered.api.command.exception.ArgumentParseException;
import org.spongepowered.api.command.parameter.ArgumentReader;
import org.spongepowered.api.command.parameter.CommandContext;
import org.spongepowered.api.command.parameter.Parameter;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.common.command.brigadier.argument.ResourceKeyedArgumentValueParser;
import org.spongepowered.common.profile.SpongeGameProfile;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

public final class SpongeGameProfileValueParameter extends ResourceKeyedArgumentValueParser<GameProfile> {
public final class SpongeGameProfileValueParameter extends ResourceKeyedArgumentValueParser.ClientNativeCompletions<GameProfile> {

private final GameProfileArgument argument = GameProfileArgument.gameProfile();

Expand All @@ -65,14 +60,6 @@ public CompletableFuture<Suggestions> listSuggestions(
return this.argument.listSuggestions(context, builder);
}

@Override
@NonNull
public List<String> complete(final @NonNull CommandCause context, @NonNull final String currentInput) {
final SuggestionsBuilder builder = new SuggestionsBuilder(currentInput, 0);
this.listSuggestions((com.mojang.brigadier.context.CommandContext<?>) context, builder);
return builder.build().getList().stream().map(Suggestion::getText).collect(Collectors.toList());
}

@Override
public Optional<? extends GameProfile> parseValue(
final @NonNull CommandCause cause, final ArgumentReader.@NonNull Mutable reader) throws ArgumentParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ImmutableStringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.kyori.adventure.text.Component;
Expand All @@ -51,7 +49,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

public final class SpongeOperatorValueParameter extends ResourceKeyedArgumentValueParser<Operator> {
public final class SpongeOperatorValueParameter extends ResourceKeyedArgumentValueParser.ClientNativeCompletions<Operator> {

private final OperationArgument operationArgument = OperationArgument.operation();
private Map<String, Operator> operators;
Expand Down Expand Up @@ -92,14 +90,8 @@ public Optional<? extends Operator> parseValue(final CommandCause commandCause,
}

@Override
public List<String> complete(final CommandCause context, final String currentInput) {
final SuggestionsBuilder builder = new SuggestionsBuilder(currentInput, 0);
this.listSuggestions((com.mojang.brigadier.context.CommandContext<?>) context, builder);
return builder.build().getList().stream().map(Suggestion::getText).collect(Collectors.toList());
}

@Override
public CompletableFuture<Suggestions> listSuggestions(final CommandContext<?> context, final SuggestionsBuilder builder) {
public CompletableFuture<Suggestions> listSuggestions(final com.mojang.brigadier.context.CommandContext<?> context,
final SuggestionsBuilder builder) {
return this.operationArgument.listSuggestions(context, builder);
}

Expand Down
Loading

0 comments on commit db2aa92

Please sign in to comment.