001/*
002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
003 *
004 *  Permission is hereby granted, free of charge, to any person obtaining
005 *  a copy of this software and associated documentation files (the
006 *  "Software"), to deal in the Software without restriction, including
007 *  without limitation the rights to use, copy, modify, merge, publish,
008 *  distribute, sublicense, and/or sell copies of the Software, and to
009 *  permit persons to whom the Software is furnished to do so, subject to
010 *  the following conditions:
011 *
012 *  The above copyright notice and this permission notice shall be
013 *  included in all copies or substantial portions of the Software.
014 *
015 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
018 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
019 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
020 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
021 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022 */
023
024package co.aikar.commands;
025
026import com.google.common.collect.ImmutableList;
027import com.google.common.collect.Lists;
028import org.jetbrains.annotations.NotNull;
029
030import java.util.Collection;
031import java.util.HashMap;
032import java.util.List;
033import java.util.Map;
034import java.util.stream.Collectors;
035import java.util.stream.IntStream;
036
037
038@SuppressWarnings({"WeakerAccess", "UnusedReturnValue"})
039public class CommandCompletions <C extends CommandCompletionContext> {
040    private final CommandManager manager;
041    private Map<String, CommandCompletionHandler> completionMap = new HashMap<>();
042
043    public CommandCompletions(CommandManager manager) {
044        this.manager = manager;
045        registerCompletion("nothing", c -> ImmutableList.of());
046        registerCompletion("range", (c) -> {
047            String config = c.getConfig();
048            if (config == null) {
049                return ImmutableList.of();
050            }
051            final String[] ranges = ACFPatterns.DASH.split(config);
052            int start;
053            int end;
054            if (ranges.length != 2) {
055                start = 0;
056                end = ACFUtil.parseInt(ranges[0], 0);
057            } else {
058                start = ACFUtil.parseInt(ranges[0], 0);
059                end = ACFUtil.parseInt(ranges[1], 0);
060            }
061            return IntStream.rangeClosed(start, end).mapToObj(Integer::toString).collect(Collectors.toList());
062        });
063        registerCompletion("timeunits", (c) -> ImmutableList.of("minutes", "hours", "days", "weeks", "months", "years"));
064    }
065
066    public CommandCompletionHandler registerCompletion(String id, CommandCompletionHandler<C> handler) {
067        return this.completionMap.put("@" + id.toLowerCase(), handler);
068    }
069
070    @NotNull
071    List<String> of(CommandOperationContext commandOperationContext, RegisteredCommand command, CommandIssuer sender, String[] completionInfo, String[] args) {
072        final int argIndex = args.length - 1;
073
074        String input = args[argIndex];
075        final String completion = argIndex < completionInfo.length ? completionInfo[argIndex] : null;
076        if (completion == null) {
077            return ImmutableList.of(input);
078        }
079
080        return getCompletionValues(command, sender, completion, args);
081    }
082
083    @NotNull
084    List<String> getCompletionValues(RegisteredCommand command, CommandIssuer sender, String completion, String[] args) {
085        completion = manager.getCommandReplacements().replace(completion);
086
087        List<String> allCompletions = Lists.newArrayList();
088        String input = args.length > 0 ? args[args.length - 1] : "";
089
090        for (String value : ACFPatterns.PIPE.split(completion)) {
091            String[] complete = ACFPatterns.COLONEQUALS.split(value, 2);
092            CommandCompletionHandler handler = this.completionMap.get(complete[0].toLowerCase());
093            if (handler != null) {
094                String config = complete.length == 1 ? null : complete[1];
095                CommandCompletionContext context = manager.createCompletionContext(command, sender, input, config, args);
096
097                try {
098                    //noinspection unchecked
099                    Collection<String> completions = handler.getCompletions(context);
100                    if (completions != null) {
101                        allCompletions.addAll(completions);
102                        continue;
103                    }
104                    //noinspection ConstantIfStatement,ConstantConditions
105                    if (false) { // Hack to fool compiler. since its sneakily thrown.
106                        throw new CommandCompletionTextLookupException();
107                    }
108                } catch (CommandCompletionTextLookupException ignored) {
109                    // This should only happen if some other feedback error occured.
110                } catch (Exception e) {
111                    command.handleException(sender, Lists.newArrayList(args), e);
112                }
113                // Something went wrong in lookup, fall back to input
114                return ImmutableList.of(input);
115            } else {
116                // Plaintext value
117                allCompletions.add(value);
118            }
119        }
120        return allCompletions;
121    }
122
123    public interface CommandCompletionHandler <C extends CommandCompletionContext> {
124        Collection<String> getCompletions(C context) throws InvalidCommandArgument;
125    }
126
127}