Search in sources :

Example 1 with CommandSuggestionEvent

use of com.sk89q.worldedit.event.platform.CommandSuggestionEvent in project FastAsyncWorldEdit by IntellectualSites.

the class CommandWrapper method suggest.

private static CompletableFuture<Suggestions> suggest(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException {
    CommandSuggestionEvent event = new CommandSuggestionEvent(FabricAdapter.adaptPlayer(context.getSource().getPlayer()), builder.getInput());
    WorldEdit.getInstance().getEventBus().post(event);
    List<Substring> suggestions = event.getSuggestions();
    ImmutableList.Builder<Suggestion> result = ImmutableList.builder();
    for (Substring suggestion : suggestions) {
        String suggestionText = suggestion.getSubstring();
        // Ensure there is a space!
        if (suggestion.getStart() == suggestion.getEnd() && suggestion.getEnd() == builder.getInput().length() && !builder.getInput().endsWith(" ") && !builder.getInput().endsWith("\"")) {
            suggestionText = " " + suggestionText;
        }
        result.add(new Suggestion(StringRange.between(suggestion.getStart(), suggestion.getEnd()), suggestionText));
    }
    return CompletableFuture.completedFuture(Suggestions.create(builder.getInput(), result.build()));
}
Also used : Substring(com.sk89q.worldedit.internal.util.Substring) Suggestion(com.mojang.brigadier.suggestion.Suggestion) CommandSuggestionEvent(com.sk89q.worldedit.event.platform.CommandSuggestionEvent) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with CommandSuggestionEvent

use of com.sk89q.worldedit.event.platform.CommandSuggestionEvent in project FastAsyncWorldEdit by IntellectualSites.

the class CommandWrapper method suggest.

private static CompletableFuture<Suggestions> suggest(CommandContext<CommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException {
    CommandSuggestionEvent event = new CommandSuggestionEvent(ForgeAdapter.adaptPlayer(context.getSource().asPlayer()), builder.getInput());
    WorldEdit.getInstance().getEventBus().post(event);
    List<Substring> suggestions = event.getSuggestions();
    ImmutableList.Builder<Suggestion> result = ImmutableList.builder();
    for (Substring suggestion : suggestions) {
        String suggestionText = suggestion.getSubstring();
        // Ensure there is a space!
        if (suggestion.getStart() == suggestion.getEnd() && suggestion.getEnd() == builder.getInput().length() && !builder.getInput().endsWith(" ") && !builder.getInput().endsWith("\"")) {
            suggestionText = " " + suggestionText;
        }
        result.add(new Suggestion(StringRange.between(suggestion.getStart(), suggestion.getEnd()), suggestionText));
    }
    return CompletableFuture.completedFuture(Suggestions.create(builder.getInput(), result.build()));
}
Also used : Substring(com.sk89q.worldedit.internal.util.Substring) Suggestion(com.mojang.brigadier.suggestion.Suggestion) CommandSuggestionEvent(com.sk89q.worldedit.event.platform.CommandSuggestionEvent) ImmutableList(com.google.common.collect.ImmutableList)

Example 3 with CommandSuggestionEvent

use of com.sk89q.worldedit.event.platform.CommandSuggestionEvent in project FastAsyncWorldEdit by IntellectualSites.

the class SpongePlatform method registerCommands.

@Override
public void registerCommands(CommandManager manager) {
    for (Command command : manager.getAllCommands().collect(toList())) {
        CommandAdapter adapter = new CommandAdapter(command) {

            @Override
            public CommandResult process(CommandSource source, String arguments) throws org.spongepowered.api.command.CommandException {
                CommandEvent weEvent = new CommandEvent(SpongeWorldEdit.inst().wrapCommandSource(source), command.getName() + " " + arguments);
                WorldEdit.getInstance().getEventBus().post(weEvent);
                return weEvent.isCancelled() ? CommandResult.success() : CommandResult.empty();
            }

            @Override
            public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<org.spongepowered.api.world.World> targetPosition) throws CommandException {
                CommandSuggestionEvent weEvent = new CommandSuggestionEvent(SpongeWorldEdit.inst().wrapCommandSource(source), command.getName() + " " + arguments);
                WorldEdit.getInstance().getEventBus().post(weEvent);
                return CommandUtil.fixSuggestions(arguments, weEvent.getSuggestions());
            }
        };
        ImmutableList.Builder<String> aliases = ImmutableList.builder();
        aliases.add(command.getName()).addAll(command.getAliases());
        Sponge.getCommandManager().register(SpongeWorldEdit.inst(), adapter, aliases.build());
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) CommandSource(org.spongepowered.api.command.CommandSource) CommandSuggestionEvent(com.sk89q.worldedit.event.platform.CommandSuggestionEvent) Command(org.enginehub.piston.Command) CommandEvent(com.sk89q.worldedit.event.platform.CommandEvent) Nullable(javax.annotation.Nullable) Location(org.spongepowered.api.world.Location)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)3 CommandSuggestionEvent (com.sk89q.worldedit.event.platform.CommandSuggestionEvent)3 Suggestion (com.mojang.brigadier.suggestion.Suggestion)2 Substring (com.sk89q.worldedit.internal.util.Substring)2 CommandEvent (com.sk89q.worldedit.event.platform.CommandEvent)1 Nullable (javax.annotation.Nullable)1 Command (org.enginehub.piston.Command)1 CommandSource (org.spongepowered.api.command.CommandSource)1 Location (org.spongepowered.api.world.Location)1