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 co.aikar.commands.apachecommonslang.ApacheCommonsLangUtil;
027import org.jetbrains.annotations.NotNull;
028
029import java.util.ArrayList;
030import java.util.Arrays;
031import java.util.Collection;
032import java.util.Collections;
033import java.util.HashMap;
034import java.util.List;
035import java.util.Locale;
036import java.util.Map;
037import java.util.function.Supplier;
038import java.util.stream.Collectors;
039import java.util.stream.IntStream;
040
041
042@SuppressWarnings({"WeakerAccess", "UnusedReturnValue"})
043public class CommandCompletions<C extends CommandCompletionContext> {
044    private static final String DEFAULT_ENUM_ID = "@__defaultenum__";
045    private final CommandManager manager;
046    // TODO: use a CompletionProvider that can return a delegated Id or provide values such as enum support
047    private Map<String, CommandCompletionHandler> completionMap = new HashMap<>();
048    private Map<Class, String> defaultCompletions = new HashMap<>();
049
050    public CommandCompletions(CommandManager manager) {
051        this.manager = manager;
052        registerStaticCompletion("empty", Collections.emptyList());
053        registerStaticCompletion("nothing", Collections.emptyList());
054        registerStaticCompletion("timeunits", Arrays.asList("minutes", "hours", "days", "weeks", "months", "years"));
055        registerAsyncCompletion("range", (c) -> {
056            String config = c.getConfig();
057            if (config == null) {
058                return Collections.emptyList();
059            }
060            final String[] ranges = ACFPatterns.DASH.split(config);
061            int start;
062            int end;
063            if (ranges.length != 2) {
064                start = 0;
065                end = ACFUtil.parseInt(ranges[0], 0);
066            } else {
067                start = ACFUtil.parseInt(ranges[0], 0);
068                end = ACFUtil.parseInt(ranges[1], 0);
069            }
070            return IntStream.rangeClosed(start, end).mapToObj(Integer::toString).collect(Collectors.toList());
071        });
072    }
073
074    /**
075     * Registr a completion handler to provide command completions based on the user input.
076     *
077     * @param id
078     * @param handler
079     * @return
080     */
081    public CommandCompletionHandler registerCompletion(String id, CommandCompletionHandler<C> handler) {
082        return this.completionMap.put(prepareCompletionId(id), handler);
083    }
084
085    /**
086     * Registr a completion handler to provide command completions based on the user input.
087     * This handler is declared to be safe to be executed asynchronously.
088     * <p>
089     * Not all platforms support this, so if the platform does not support asynchronous execution,
090     * your handler will be executed on the main thread.
091     * <p>
092     * Use this anytime your handler does not need to access state that is not considered thread safe.
093     * <p>
094     * Use context.isAsync() to determine if you are async or not.
095     *
096     * @param id
097     * @param handler
098     * @return
099     */
100    public CommandCompletionHandler registerAsyncCompletion(String id, AsyncCommandCompletionHandler<C> handler) {
101        return this.completionMap.put(prepareCompletionId(id), handler);
102    }
103
104    /**
105     * Register a static list of command completions that will never change.
106     * Like @CommandCompletion, values are | (PIPE) separated.
107     * <p>
108     * Example: foo|bar|baz
109     *
110     * @param id
111     * @param list
112     * @return
113     */
114    public CommandCompletionHandler registerStaticCompletion(String id, String list) {
115        return registerStaticCompletion(id, ACFPatterns.PIPE.split(list));
116    }
117
118    /**
119     * Register a static list of command completions that will never change
120     *
121     * @param id
122     * @param completions
123     * @return
124     */
125    public CommandCompletionHandler registerStaticCompletion(String id, String[] completions) {
126        return registerStaticCompletion(id, Arrays.asList(completions));
127    }
128
129    /**
130     * Register a static list of command completions that will never change. The list is obtained from the supplier
131     * immediately as part of this method call.
132     *
133     * @param id
134     * @param supplier
135     * @return
136     */
137    public CommandCompletionHandler registerStaticCompletion(String id, Supplier<Collection<String>> supplier) {
138        return registerStaticCompletion(id, supplier.get());
139    }
140
141    /**
142     * Register a static list of command completions that will never change
143     *
144     * @param id
145     * @param completions
146     * @return
147     */
148    public CommandCompletionHandler registerStaticCompletion(String id, Collection<String> completions) {
149        return registerAsyncCompletion(id, x -> completions);
150    }
151
152    /**
153     * Registers a completion handler such as @players to default apply to all command parameters of the specified types
154     * <p>
155     * This enables automatic completion support for parameters without manually defining it for custom objects
156     *
157     * @param id
158     * @param classes
159     */
160    public void setDefaultCompletion(String id, Class... classes) {
161        // get completion with specified id
162        id = prepareCompletionId(id);
163        CommandCompletionHandler completion = completionMap.get(id);
164
165        if (completion == null) {
166            // Throw something because no completion with specified id
167            throw new IllegalStateException("Completion not registered for " + id);
168        }
169
170        for (Class clazz : classes) {
171            defaultCompletions.put(clazz, id);
172        }
173    }
174
175    @NotNull
176    private static String prepareCompletionId(String id) {
177        return (id.startsWith("@") ? "" : "@") + id.toLowerCase(Locale.ENGLISH);
178    }
179
180    @NotNull
181    List<String> of(RegisteredCommand cmd, CommandIssuer sender, String[] args, boolean isAsync) {
182        String[] completions = ACFPatterns.SPACE.split(cmd.complete);
183        final int argIndex = args.length - 1;
184
185        String input = args[argIndex];
186
187        String completion = argIndex < completions.length ? completions[argIndex] : null;
188        if (completion == null || "*".equals(completion)) {
189            completion = findDefaultCompletion(cmd, args);
190        }
191
192        if (completion == null && completions.length > 0) {
193            String last = completions[completions.length - 1];
194            if (last.startsWith("repeat@")) {
195                completion = last;
196            } else if (argIndex >= completions.length && cmd.parameters[cmd.parameters.length - 1].consumesRest) {
197                completion = last;
198            }
199        }
200
201        if (completion == null) {
202            return Collections.singletonList(input);
203        }
204
205        return getCompletionValues(cmd, sender, completion, args, isAsync);
206    }
207
208    String findDefaultCompletion(RegisteredCommand cmd, String[] args) {
209        int i = 0;
210        for (CommandParameter param : cmd.parameters) {
211            if (param.canConsumeInput() && ++i == args.length) {
212                Class type = param.getType();
213                while (type != null) {
214                    String completion = this.defaultCompletions.get(type);
215                    if (completion != null) {
216                        return completion;
217                    }
218                    type = type.getSuperclass();
219                }
220                if (param.getType().isEnum()) {
221                    CommandOperationContext ctx = CommandManager.getCurrentCommandOperationContext();
222                    //noinspection unchecked
223                    ctx.enumCompletionValues = ACFUtil.enumNames((Class<? extends Enum<?>>) param.getType());
224                    return DEFAULT_ENUM_ID;
225                }
226                break;
227            }
228        }
229        return null;
230    }
231
232    List<String> getCompletionValues(RegisteredCommand command, CommandIssuer sender, String completion, String[] args, boolean isAsync) {
233        if (DEFAULT_ENUM_ID.equals(completion)) {
234            CommandOperationContext<?> ctx = CommandManager.getCurrentCommandOperationContext();
235            return ctx.enumCompletionValues;
236        }
237        boolean repeat = completion.startsWith("repeat@");
238        if (repeat) {
239            completion = completion.substring(6);
240        }
241        completion = manager.getCommandReplacements().replace(completion);
242
243        List<String> allCompletions = new ArrayList<>();
244        String input = args.length > 0 ? args[args.length - 1] : "";
245
246        for (String value : ACFPatterns.PIPE.split(completion)) {
247            String[] complete = ACFPatterns.COLONEQUALS.split(value, 2);
248            CommandCompletionHandler handler = this.completionMap.get(complete[0].toLowerCase(Locale.ENGLISH));
249            if (handler != null) {
250                if (isAsync && !(handler instanceof AsyncCommandCompletionHandler)) {
251                    ACFUtil.sneaky(new SyncCompletionRequired());
252                    return null;
253                }
254                String config = complete.length == 1 ? null : complete[1];
255                CommandCompletionContext context = manager.createCompletionContext(command, sender, input, config, args);
256
257                try {
258                    //noinspection unchecked
259                    Collection<String> completions = handler.getCompletions(context);
260
261                    //Handle completions with more than one word:
262                    if (!repeat && completions != null
263                            && command.parameters[command.parameters.length - 1].consumesRest
264                            && args.length > ACFPatterns.SPACE.split(command.complete).length) {
265                        String start = String.join(" ", args);
266                        completions = completions.stream()
267                                .map(s -> {
268                                    if (s != null && s.split(" ").length >= args.length && ApacheCommonsLangUtil.startsWithIgnoreCase(s, start)) {
269                                        String[] completionArgs = s.split(" ");
270                                        return String.join(" ", Arrays.copyOfRange(completionArgs, args.length - 1, completionArgs.length));
271                                    } else {
272                                        return s;
273                                    }
274                                }).collect(Collectors.toList());
275                    }
276
277                    if (completions != null) {
278                        allCompletions.addAll(completions);
279                        continue;
280                    }
281                    //noinspection ConstantIfStatement,ConstantConditions
282                    if (false) { // Hack to fool compiler. since its sneakily thrown.
283                        throw new CommandCompletionTextLookupException();
284                    }
285                } catch (CommandCompletionTextLookupException ignored) {
286                    // This should only happen if some other feedback error occured.
287                } catch (Exception e) {
288                    command.handleException(sender, Arrays.asList(args), e);
289                }
290                // Something went wrong in lookup, fall back to input
291                return Collections.singletonList(input);
292            } else {
293                // Plaintext value
294                allCompletions.add(value);
295            }
296        }
297        return allCompletions;
298    }
299
300    public interface CommandCompletionHandler<C extends CommandCompletionContext> {
301        Collection<String> getCompletions(C context) throws InvalidCommandArgument;
302    }
303
304    public interface AsyncCommandCompletionHandler<C extends CommandCompletionContext> extends CommandCompletionHandler<C> {
305    }
306
307    public static class SyncCompletionRequired extends RuntimeException {
308    }
309
310}