Skip to content

Commit

Permalink
Update for method rename and builder -> factory
Browse files Browse the repository at this point in the history
  • Loading branch information
dualspiral committed May 3, 2021
1 parent 24ae78e commit 527f3ba
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,14 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.command.CommandCompletion;

import java.util.Objects;

public final class SpongeCommandCompletionBuilder implements CommandCompletion.Builder {

private @Nullable String completion;
private @Nullable Component tooltip;

@Override
public CommandCompletion.Builder completion(final @NonNull String completion) {
this.completion = Objects.requireNonNull(completion);
if (this.completion.isEmpty()) {
throw new IllegalArgumentException("Completions cannot be empty");
}
return this;
}
public final class SpongeCommandCompletionFactory implements CommandCompletion.Factory {

@Override
public CommandCompletion.Builder tooltip(final @Nullable Component tooltip) {
this.tooltip = tooltip;
return this;
}

@Override
public CommandCompletion build() {
if (this.completion == null) {
throw new IllegalStateException("A non-empty completion must be supplied");
@NonNull
public CommandCompletion completion(final @NonNull String completion, final @Nullable Component tooltip) {
if (completion == null || completion.isEmpty()) {
throw new IllegalStateException("The completion may not be null or empty.");
}
return new SpongeCommandCompletion(this.completion, this.tooltip);
}

@Override
public CommandCompletion.Builder reset() {
this.completion = null;
this.tooltip = null;
return this;
return new SpongeCommandCompletion(completion, tooltip);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void setCommandManager(final SpongeCommandManager commandManager) {
}

@Override
public List<CommandCompletion> suggestions(final @NonNull CommandCause cause, final ArgumentReader.@NonNull Mutable arguments) {
public List<CommandCompletion> complete(final @NonNull CommandCause cause, final ArgumentReader.@NonNull Mutable arguments) {
final SpongeCommandDispatcher dispatcher = this.getCachedDispatcher();
final String input = arguments.remaining();
final ParseResults<CommandSourceStack> parseResults = dispatcher.parse((StringReader) arguments, (CommandSourceStack) cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ private void prettyPrintThrowableError(final Throwable thr, final String command
}

@Override
public List<CommandCompletion> suggest(final @NonNull String arguments) {
public List<CommandCompletion> complete(final @NonNull String arguments) {
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.addContext(EventContextKeys.COMMAND, arguments);
final String[] splitArg = arguments.split(" ", 2);
Expand All @@ -485,7 +485,7 @@ public List<CommandCompletion> suggest(final @NonNull String arguments) {
return Collections.emptyList();
}

return mapping.registrar().suggestions(CommandCause.create(), mapping, command, splitArg[1]);
return mapping.registrar().complete(CommandCause.create(), mapping, command, splitArg[1]);
}

return this.commandMappings.keySet()
Expand All @@ -499,25 +499,25 @@ public List<CommandCompletion> suggest(final @NonNull String arguments) {
}

@Override
public <T extends Subject & Audience> List<CommandCompletion> suggest(
public <T extends Subject & Audience> List<CommandCompletion> complete(
final @NonNull T subjectReceiver,
final @NonNull String arguments) {
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.addContext(EventContextKeys.SUBJECT, subjectReceiver);
frame.addContext(EventContextKeys.AUDIENCE, subjectReceiver);
return this.suggest(arguments);
return this.complete(arguments);
}
}

@Override
public List<CommandCompletion> suggest(
public List<CommandCompletion> complete(
final @NonNull Subject subject,
final @NonNull Audience receiver,
final @NonNull String arguments) {
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.addContext(EventContextKeys.SUBJECT, subject);
frame.addContext(EventContextKeys.AUDIENCE, receiver);
return this.suggest(arguments);
return this.complete(arguments);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ private Tuple<CommandMapping, LiteralCommandNode<CommandSourceStack>> registerIn
}

@Override
public List<CommandCompletion> suggestions(
public List<CommandCompletion> complete(
final @NonNull CommandCause cause,
final @NonNull CommandMapping mapping,
final @NonNull String command,
final @NonNull String arguments) {

final CompletableFuture<Suggestions> suggestionsCompletableFuture =
this.dispatcher.getCompletionSuggestions(
this.dispatcher.parse(this.createCommandString(command, arguments), (CommandSourceStack) cause, true));
// TODO: Fix so that we keep suggestions in the Mojang format?
return suggestionsCompletableFuture.join().getList().stream().map(SpongeCommandCompletion::from).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private SpongeCommandManager commandManager() {
}

@Override
public List<CommandCompletion> suggestions(
public List<CommandCompletion> complete(
final @NonNull CommandCause cause,
final @NonNull CommandMapping mapping,
final @NonNull String command,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public CommandResult process(final CommandCause cause, final CommandMapping mapp
}

@Override
public List<CommandCompletion> suggestions(final CommandCause cause, final CommandMapping mapping, final String command, final String arguments) throws CommandException {
public List<CommandCompletion> complete(final CommandCause cause, final CommandMapping mapping, final String command, final String arguments) throws CommandException {
final Command.Raw commandToExecute = this.commands.get(mapping);
if (commandToExecute.canExecute(cause)) {
return commandToExecute.suggestions(cause, new SpongeStringReader(arguments));
return commandToExecute.complete(cause, new SpongeStringReader(arguments));
}
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
import org.spongepowered.common.ban.SpongeBanBuilder;
import org.spongepowered.common.block.SpongeBlockSnapshotBuilder;
import org.spongepowered.common.block.SpongeBlockStateBuilder;
import org.spongepowered.common.command.SpongeCommandCompletionBuilder;
import org.spongepowered.common.command.SpongeCommandCompletionFactory;
import org.spongepowered.common.command.SpongeParameterizedCommandBuilder;
import org.spongepowered.common.command.parameter.SpongeParameterKeyBuilder;
import org.spongepowered.common.command.parameter.flag.SpongeFlagBuilder;
Expand Down Expand Up @@ -354,7 +354,6 @@ public void registerDefaultBuilders() {
.register(MapColor.Builder.class, SpongeMapColorBuilder::new)
.register(MapDecoration.Builder.class, SpongeMapDecorationBuilder::new)
.register(MapCanvas.Builder.class, SpongeMapCanvasBuilder::new)
.register(CommandCompletion.Builder.class, SpongeCommandCompletionBuilder::new)
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.spongepowered.api.adventure.Audiences;
import org.spongepowered.api.adventure.SpongeComponents;
import org.spongepowered.api.command.CommandCause;
import org.spongepowered.api.command.CommandCompletion;
import org.spongepowered.api.command.parameter.Parameter;
import org.spongepowered.api.command.parameter.managed.standard.VariableValueParameters;
import org.spongepowered.api.command.registrar.tree.CommandTreeNode;
Expand Down Expand Up @@ -90,6 +91,7 @@
import org.spongepowered.common.advancement.criterion.SpongeOrCriterion;
import org.spongepowered.common.adventure.AudiencesFactory;
import org.spongepowered.common.adventure.SpongeAdventure;
import org.spongepowered.common.command.SpongeCommandCompletionFactory;
import org.spongepowered.common.command.manager.SpongeCommandCauseFactory;
import org.spongepowered.common.command.parameter.SpongeParameterFactory;
import org.spongepowered.common.command.parameter.managed.factory.SpongeVariableValueParametersFactory;
Expand Down Expand Up @@ -232,6 +234,7 @@ public void registerDefaultFactories() {
.registerFactory(ChunkGenerator.Factory.class, new SpongeChunkGeneratorFactory())
.registerFactory(ItemStackComparators.Factory.class, new SpongeItemStackComparatorFactory())
.registerFactory(Favicon.Factory.class, new SpongeFavicon.FactoryImpl())
.registerFactory(CommandCompletion.Factory.class, new SpongeCommandCompletionFactory())
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public abstract class ServerGamePacketListenerImplMixin implements ConnectionHol
if (mapping.registrar().canExecute(cause, mapping)) {
try {
final SuggestionsBuilder builder = new SuggestionsBuilder(rawCommand, rawCommand.lastIndexOf(" ") + 1);
mapping.registrar().suggestions(cause, mapping, command[0], command[1])
mapping.registrar().complete(cause, mapping, command[0], command[1])
.forEach(completion -> builder.suggest(completion.completion(),
completion.tooltip().map(SpongeAdventure::asVanilla).orElse(null)));
this.connection.send(new ClientboundCommandSuggestionsPacket(packet.getId(), builder.build()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ public void onRegisterCommand(final RegisterCommandEvent<Command.Parameterized>
(context, currentInput) -> Sponge.pluginManager().plugins().stream()
.filter(pc -> pc.instance() instanceof LoadableModule)
.filter(x -> x.metadata().id().startsWith(currentInput))
.map(x -> CommandCompletion.builder().completion(x.metadata().id())
.tooltip(Component.text(x.metadata().name().orElseGet(() -> x.metadata().id())))
.build())
.map(x -> CommandCompletion.of(x.metadata().id(), Component.text(x.metadata().name().orElseGet(() -> x.metadata().id()))))
.collect(Collectors.toList())).build();
final Command.Parameterized enableCommand = Command.builder().addParameter(pluginKey)
.executor(context -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class ClientSuggestionsRawCommandTest implements Command.Raw {
}

@Override
public List<CommandCompletion> suggestions(@NonNull final CommandCause cause, final ArgumentReader.@NonNull Mutable arguments) throws CommandException {
public List<CommandCompletion> complete(@NonNull final CommandCause cause, final ArgumentReader.@NonNull Mutable arguments) throws CommandException {
// This should not get hit
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public CommandResult process(final @NonNull CommandCause cause, final ArgumentRe
}

@Override
public List<CommandCompletion> suggestions(final @NonNull CommandCause cause, final ArgumentReader.@NonNull Mutable arguments) throws CommandException {
public List<CommandCompletion> complete(final @NonNull CommandCause cause, final ArgumentReader.@NonNull Mutable arguments) throws CommandException {
if (arguments.remaining().endsWith(" ")) {
return this.suggestions;
}
Expand Down

0 comments on commit 527f3ba

Please sign in to comment.