-
Notifications
You must be signed in to change notification settings - Fork 380
/
CommandDispatcher.java
705 lines (653 loc) · 34 KB
/
CommandDispatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package com.mojang.brigadier;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.SuggestionContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* The core command dispatcher, for registering, parsing, and executing commands.
*
* @param <S> a custom "source" type, such as a user or originator of a command
*/
public class CommandDispatcher<S> {
/**
* The string required to separate individual arguments in an input string
*
* @see #ARGUMENT_SEPARATOR_CHAR
*/
public static final String ARGUMENT_SEPARATOR = " ";
/**
* The char required to separate individual arguments in an input string
*
* @see #ARGUMENT_SEPARATOR
*/
public static final char ARGUMENT_SEPARATOR_CHAR = ' ';
private static final String USAGE_OPTIONAL_OPEN = "[";
private static final String USAGE_OPTIONAL_CLOSE = "]";
private static final String USAGE_REQUIRED_OPEN = "(";
private static final String USAGE_REQUIRED_CLOSE = ")";
private static final String USAGE_OR = "|";
private final RootCommandNode<S> root;
private final Predicate<CommandNode<S>> hasCommand = new Predicate<CommandNode<S>>() {
@Override
public boolean test(final CommandNode<S> input) {
return input != null && (input.getCommand() != null || input.getChildren().stream().anyMatch(hasCommand));
}
};
private ResultConsumer<S> consumer = (c, s, r) -> {
};
/**
* Create a new {@link CommandDispatcher} with the specified root node.
*
* <p>This is often useful to copy existing or pre-defined command trees.</p>
*
* @param root the existing {@link RootCommandNode} to use as the basis for this tree
*/
public CommandDispatcher(final RootCommandNode<S> root) {
this.root = root;
}
/**
* Creates a new {@link CommandDispatcher} with an empty command tree.
*/
public CommandDispatcher() {
this(new RootCommandNode<>());
}
/**
* Utility method for registering new commands.
*
* <p>This is a shortcut for calling {@link RootCommandNode#addChild(CommandNode)} after building the provided {@code command}.</p>
*
* <p>As {@link RootCommandNode} can only hold literals, this method will only allow literal arguments.</p>
*
* @param command a literal argument builder to add to this command tree
* @return the node added to this tree
*/
public LiteralCommandNode<S> register(final LiteralArgumentBuilder<S> command) {
final LiteralCommandNode<S> build = command.build();
root.addChild(build);
return build;
}
/**
* Sets a callback to be informed of the result of every command.
*
* @param consumer the new result consumer to be called
*/
public void setConsumer(final ResultConsumer<S> consumer) {
this.consumer = consumer;
}
/**
* Parses and executes a given command.
*
* <p>This is a shortcut to first {@link #parse(StringReader, Object)} and then {@link #execute(ParseResults)}.</p>
*
* <p>It is recommended to parse and execute as separate steps, as parsing is often the most expensive step, and easiest to cache.</p>
*
* <p>If this command returns a value, then it successfully executed something. If it could not parse the command, or the execution was a failure,
* then an exception will be thrown. Most exceptions will be of type {@link CommandSyntaxException}, but it is possible that a {@link RuntimeException}
* may bubble up from the result of a command. The meaning behind the returned result is arbitrary, and will depend
* entirely on what command was performed.</p>
*
* <p>If the command passes through a node that is {@link CommandNode#isFork()} then it will be 'forked'.
* A forked command will not bubble up any {@link CommandSyntaxException}s, and the 'result' returned will turn into
* 'amount of successful commands executes'.</p>
*
* <p>After each and any command is ran, a registered callback given to {@link #setConsumer(ResultConsumer)}
* will be notified of the result and success of the command. You can use that method to gather more meaningful
* results than this method will return, especially when a command forks.</p>
*
* @param input a command string to parse & execute
* @param source a custom "source" object, usually representing the originator of this command
* @return a numeric result from a "command" that was performed
* @throws CommandSyntaxException if the command failed to parse or execute
* @throws RuntimeException if the command failed to execute and was not handled gracefully
* @see #parse(String, Object)
* @see #parse(StringReader, Object)
* @see #execute(ParseResults)
* @see #execute(StringReader, Object)
*/
public int execute(final String input, final S source) throws CommandSyntaxException {
return execute(new StringReader(input), source);
}
/**
* Parses and executes a given command.
*
* <p>This is a shortcut to first {@link #parse(StringReader, Object)} and then {@link #execute(ParseResults)}.</p>
*
* <p>It is recommended to parse and execute as separate steps, as parsing is often the most expensive step, and easiest to cache.</p>
*
* <p>If this command returns a value, then it successfully executed something. If it could not parse the command, or the execution was a failure,
* then an exception will be thrown. Most exceptions will be of type {@link CommandSyntaxException}, but it is possible that a {@link RuntimeException}
* may bubble up from the result of a command. The meaning behind the returned result is arbitrary, and will depend
* entirely on what command was performed.</p>
*
* <p>If the command passes through a node that is {@link CommandNode#isFork()} then it will be 'forked'.
* A forked command will not bubble up any {@link CommandSyntaxException}s, and the 'result' returned will turn into
* 'amount of successful commands executes'.</p>
*
* <p>After each and any command is ran, a registered callback given to {@link #setConsumer(ResultConsumer)}
* will be notified of the result and success of the command. You can use that method to gather more meaningful
* results than this method will return, especially when a command forks.</p>
*
* @param input a command string to parse & execute
* @param source a custom "source" object, usually representing the originator of this command
* @return a numeric result from a "command" that was performed
* @throws CommandSyntaxException if the command failed to parse or execute
* @throws RuntimeException if the command failed to execute and was not handled gracefully
* @see #parse(String, Object)
* @see #parse(StringReader, Object)
* @see #execute(ParseResults)
* @see #execute(String, Object)
*/
public int execute(final StringReader input, final S source) throws CommandSyntaxException {
final ParseResults<S> parse = parse(input, source);
return execute(parse);
}
/**
* Executes a given pre-parsed command.
*
* <p>If this command returns a value, then it successfully executed something. If the execution was a failure,
* then an exception will be thrown.
* Most exceptions will be of type {@link CommandSyntaxException}, but it is possible that a {@link RuntimeException}
* may bubble up from the result of a command. The meaning behind the returned result is arbitrary, and will depend
* entirely on what command was performed.</p>
*
* <p>If the command passes through a node that is {@link CommandNode#isFork()} then it will be 'forked'.
* A forked command will not bubble up any {@link CommandSyntaxException}s, and the 'result' returned will turn into
* 'amount of successful commands executes'.</p>
*
* <p>After each and any command is ran, a registered callback given to {@link #setConsumer(ResultConsumer)}
* will be notified of the result and success of the command. You can use that method to gather more meaningful
* results than this method will return, especially when a command forks.</p>
*
* @param parse the result of a successful {@link #parse(StringReader, Object)}
* @return a numeric result from a "command" that was performed.
* @throws CommandSyntaxException if the command failed to parse or execute
* @throws RuntimeException if the command failed to execute and was not handled gracefully
* @see #parse(String, Object)
* @see #parse(StringReader, Object)
* @see #execute(String, Object)
* @see #execute(StringReader, Object)
*/
public int execute(final ParseResults<S> parse) throws CommandSyntaxException {
if (parse.getReader().canRead()) {
if (parse.getExceptions().size() == 1) {
throw parse.getExceptions().values().iterator().next();
} else if (parse.getContext().getRange().isEmpty()) {
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(parse.getReader());
} else {
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(parse.getReader());
}
}
int result = 0;
int successfulForks = 0;
boolean forked = false;
boolean foundCommand = false;
final String command = parse.getReader().getString();
final CommandContext<S> original = parse.getContext().build(command);
List<CommandContext<S>> contexts = Collections.singletonList(original);
ArrayList<CommandContext<S>> next = null;
while (contexts != null) {
final int size = contexts.size();
for (int i = 0; i < size; i++) {
final CommandContext<S> context = contexts.get(i);
final CommandContext<S> child = context.getChild();
if (child != null) {
forked |= context.isForked();
if (child.hasNodes()) {
foundCommand = true;
final RedirectModifier<S> modifier = context.getRedirectModifier();
if (modifier == null) {
if (next == null) {
next = new ArrayList<>(1);
}
next.add(child.copyFor(context.getSource()));
} else {
try {
final Collection<S> results = modifier.apply(context);
if (!results.isEmpty()) {
if (next == null) {
next = new ArrayList<>(results.size());
}
for (final S source : results) {
next.add(child.copyFor(source));
}
}
} catch (final CommandSyntaxException ex) {
consumer.onCommandComplete(context, false, 0);
if (!forked) {
throw ex;
}
}
}
}
} else if (context.getCommand() != null) {
foundCommand = true;
try {
final int value = context.getCommand().run(context);
result += value;
consumer.onCommandComplete(context, true, value);
successfulForks++;
} catch (final CommandSyntaxException ex) {
consumer.onCommandComplete(context, false, 0);
if (!forked) {
throw ex;
}
}
}
}
contexts = next;
next = null;
}
if (!foundCommand) {
consumer.onCommandComplete(original, false, 0);
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(parse.getReader());
}
return forked ? successfulForks : result;
}
/**
* Parses a given command.
*
* <p>The result of this method can be cached, and it is advised to do so where appropriate. Parsing is often the
* most expensive step, and this allows you to essentially "precompile" a command if it will be ran often.</p>
*
* <p>If the command passes through a node that is {@link CommandNode#isFork()} then the resulting context will be marked as 'forked'.
* Forked contexts may contain child contexts, which may be modified by the {@link RedirectModifier} attached to the fork.</p>
*
* <p>Parsing a command can never fail, you will always be provided with a new {@link ParseResults}.
* However, that does not mean that it will always parse into a valid command. You should inspect the returned results
* to check for validity. If its {@link ParseResults#getReader()} {@link StringReader#canRead()} then it did not finish
* parsing successfully. You can use that position as an indicator to the user where the command stopped being valid.
* You may inspect {@link ParseResults#getExceptions()} if you know the parse failed, as it will explain why it could
* not find any valid commands. It may contain multiple exceptions, one for each "potential node" that it could have visited,
* explaining why it did not go down that node.</p>
*
* <p>When you eventually call {@link #execute(ParseResults)} with the result of this method, the above error checking
* will occur. You only need to inspect it yourself if you wish to handle that yourself.</p>
*
* @param command a command string to parse
* @param source a custom "source" object, usually representing the originator of this command
* @return the result of parsing this command
* @see #parse(StringReader, Object)
* @see #execute(ParseResults)
* @see #execute(String, Object)
*/
public ParseResults<S> parse(final String command, final S source) {
return parse(new StringReader(command), source);
}
/**
* Parses a given command.
*
* <p>The result of this method can be cached, and it is advised to do so where appropriate. Parsing is often the
* most expensive step, and this allows you to essentially "precompile" a command if it will be ran often.</p>
*
* <p>If the command passes through a node that is {@link CommandNode#isFork()} then the resulting context will be marked as 'forked'.
* Forked contexts may contain child contexts, which may be modified by the {@link RedirectModifier} attached to the fork.</p>
*
* <p>Parsing a command can never fail, you will always be provided with a new {@link ParseResults}.
* However, that does not mean that it will always parse into a valid command. You should inspect the returned results
* to check for validity. If its {@link ParseResults#getReader()} {@link StringReader#canRead()} then it did not finish
* parsing successfully. You can use that position as an indicator to the user where the command stopped being valid.
* You may inspect {@link ParseResults#getExceptions()} if you know the parse failed, as it will explain why it could
* not find any valid commands. It may contain multiple exceptions, one for each "potential node" that it could have visited,
* explaining why it did not go down that node.</p>
*
* <p>When you eventually call {@link #execute(ParseResults)} with the result of this method, the above error checking
* will occur. You only need to inspect it yourself if you wish to handle that yourself.</p>
*
* @param command a command string to parse
* @param source a custom "source" object, usually representing the originator of this command
* @return the result of parsing this command
* @see #parse(String, Object)
* @see #execute(ParseResults)
* @see #execute(String, Object)
*/
public ParseResults<S> parse(final StringReader command, final S source) {
final CommandContextBuilder<S> context = new CommandContextBuilder<>(this, source, root, command.getCursor());
return parseNodes(root, command, context);
}
private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader originalReader, final CommandContextBuilder<S> contextSoFar) {
final S source = contextSoFar.getSource();
Map<CommandNode<S>, CommandSyntaxException> errors = null;
List<ParseResults<S>> potentials = null;
final int cursor = originalReader.getCursor();
for (final CommandNode<S> child : node.getRelevantNodes(originalReader)) {
if (!child.canUse(source)) {
continue;
}
final CommandContextBuilder<S> context = contextSoFar.copy();
final StringReader reader = new StringReader(originalReader);
try {
try {
child.parse(reader, context);
} catch (final RuntimeException ex) {
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherParseException().createWithContext(reader, ex.getMessage());
}
if (reader.canRead()) {
if (reader.peek() != ARGUMENT_SEPARATOR_CHAR) {
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherExpectedArgumentSeparator().createWithContext(reader);
}
}
} catch (final CommandSyntaxException ex) {
if (errors == null) {
errors = new LinkedHashMap<>();
}
errors.put(child, ex);
reader.setCursor(cursor);
continue;
}
context.withCommand(child.getCommand());
if (reader.canRead(child.getRedirect() == null ? 2 : 1)) {
reader.skip();
if (child.getRedirect() != null) {
final CommandContextBuilder<S> childContext = new CommandContextBuilder<>(this, source, child.getRedirect(), reader.getCursor());
final ParseResults<S> parse = parseNodes(child.getRedirect(), reader, childContext);
context.withChild(parse.getContext());
return new ParseResults<>(context, parse.getReader(), parse.getExceptions());
} else {
final ParseResults<S> parse = parseNodes(child, reader, context);
if (potentials == null) {
potentials = new ArrayList<>(1);
}
potentials.add(parse);
}
} else {
if (potentials == null) {
potentials = new ArrayList<>(1);
}
potentials.add(new ParseResults<>(context, reader, Collections.emptyMap()));
}
}
if (potentials != null) {
if (potentials.size() > 1) {
potentials.sort((a, b) -> {
if (!a.getReader().canRead() && b.getReader().canRead()) {
return -1;
}
if (a.getReader().canRead() && !b.getReader().canRead()) {
return 1;
}
if (a.getExceptions().isEmpty() && !b.getExceptions().isEmpty()) {
return -1;
}
if (!a.getExceptions().isEmpty() && b.getExceptions().isEmpty()) {
return 1;
}
return 0;
});
}
return potentials.get(0);
}
return new ParseResults<>(contextSoFar, originalReader, errors == null ? Collections.emptyMap() : errors);
}
/**
* Gets all possible executable commands following the given node.
*
* <p>You may use {@link #getRoot()} as a target to get all usage data for the entire command tree.</p>
*
* <p>The returned syntax will be in "simple" form: {@code <param>} and {@code literal}. "Optional" nodes will be
* listed as multiple entries: the parent node, and the child nodes.
* For example, a required literal "foo" followed by an optional param "int" will be two nodes:</p>
* <ul>
* <li>{@code foo}</li>
* <li>{@code foo <int>}</li>
* </ul>
*
* <p>The path to the specified node will <b>not</b> be prepended to the output, as there can theoretically be many
* ways to reach a given node. It will only give you paths relative to the specified node, not absolute from root.</p>
*
* @param node target node to get child usage strings for
* @param source a custom "source" object, usually representing the originator of this command
* @param restricted if true, commands that the {@code source} cannot access will not be mentioned
* @return array of full usage strings under the target node
*/
public String[] getAllUsage(final CommandNode<S> node, final S source, final boolean restricted) {
final ArrayList<String> result = new ArrayList<>();
getAllUsage(node, source, result, "", restricted);
return result.toArray(new String[result.size()]);
}
private void getAllUsage(final CommandNode<S> node, final S source, final ArrayList<String> result, final String prefix, final boolean restricted) {
if (restricted && !node.canUse(source)) {
return;
}
if (node.getCommand() != null) {
result.add(prefix);
}
if (node.getRedirect() != null) {
final String redirect = node.getRedirect() == root ? "..." : "-> " + node.getRedirect().getUsageText();
result.add(prefix.isEmpty() ? node.getUsageText() + ARGUMENT_SEPARATOR + redirect : prefix + ARGUMENT_SEPARATOR + redirect);
} else if (!node.getChildren().isEmpty()) {
for (final CommandNode<S> child : node.getChildren()) {
getAllUsage(child, source, result, prefix.isEmpty() ? child.getUsageText() : prefix + ARGUMENT_SEPARATOR + child.getUsageText(), restricted);
}
}
}
/**
* Gets the possible executable commands from a specified node.
*
* <p>You may use {@link #getRoot()} as a target to get usage data for the entire command tree.</p>
*
* <p>The returned syntax will be in "smart" form: {@code <param>}, {@code literal}, {@code [optional]} and {@code (either|or)}.
* These forms may be mixed and matched to provide as much information about the child nodes as it can, without being too verbose.
* For example, a required literal "foo" followed by an optional param "int" can be compressed into one string:</p>
* <ul>
* <li>{@code foo [<int>]}</li>
* </ul>
*
* <p>The path to the specified node will <b>not</b> be prepended to the output, as there can theoretically be many
* ways to reach a given node. It will only give you paths relative to the specified node, not absolute from root.</p>
*
* <p>The returned usage will be restricted to only commands that the provided {@code source} can use.</p>
*
* @param node target node to get child usage strings for
* @param source a custom "source" object, usually representing the originator of this command
* @return array of full usage strings under the target node
*/
public Map<CommandNode<S>, String> getSmartUsage(final CommandNode<S> node, final S source) {
final Map<CommandNode<S>, String> result = new LinkedHashMap<>();
final boolean optional = node.getCommand() != null;
for (final CommandNode<S> child : node.getChildren()) {
final String usage = getSmartUsage(child, source, optional, false);
if (usage != null) {
result.put(child, usage);
}
}
return result;
}
private String getSmartUsage(final CommandNode<S> node, final S source, final boolean optional, final boolean deep) {
if (!node.canUse(source)) {
return null;
}
final String self = optional ? USAGE_OPTIONAL_OPEN + node.getUsageText() + USAGE_OPTIONAL_CLOSE : node.getUsageText();
final boolean childOptional = node.getCommand() != null;
final String open = childOptional ? USAGE_OPTIONAL_OPEN : USAGE_REQUIRED_OPEN;
final String close = childOptional ? USAGE_OPTIONAL_CLOSE : USAGE_REQUIRED_CLOSE;
if (!deep) {
if (node.getRedirect() != null) {
final String redirect = node.getRedirect() == root ? "..." : "-> " + node.getRedirect().getUsageText();
return self + ARGUMENT_SEPARATOR + redirect;
} else {
final Collection<CommandNode<S>> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList());
if (children.size() == 1) {
final String usage = getSmartUsage(children.iterator().next(), source, childOptional, childOptional);
if (usage != null) {
return self + ARGUMENT_SEPARATOR + usage;
}
} else if (children.size() > 1) {
final Set<String> childUsage = new LinkedHashSet<>();
for (final CommandNode<S> child : children) {
final String usage = getSmartUsage(child, source, childOptional, true);
if (usage != null) {
childUsage.add(usage);
}
}
if (childUsage.size() == 1) {
final String usage = childUsage.iterator().next();
return self + ARGUMENT_SEPARATOR + (childOptional ? USAGE_OPTIONAL_OPEN + usage + USAGE_OPTIONAL_CLOSE : usage);
} else if (childUsage.size() > 1) {
final StringBuilder builder = new StringBuilder(open);
int count = 0;
for (final CommandNode<S> child : children) {
if (count > 0) {
builder.append(USAGE_OR);
}
builder.append(child.getUsageText());
count++;
}
if (count > 0) {
builder.append(close);
return self + ARGUMENT_SEPARATOR + builder.toString();
}
}
}
}
}
return self;
}
/**
* Gets suggestions for a parsed input string on what comes next.
*
* <p>As it is ultimately up to custom argument types to provide suggestions, it may be an asynchronous operation,
* for example getting in-game data or player names etc. As such, this method returns a future and no guarantees
* are made to when or how the future completes.</p>
*
* <p>The suggestions provided will be in the context of the end of the parsed input string, but may suggest
* new or replacement strings for earlier in the input string. For example, if the end of the string was
* {@code foobar} but an argument preferred it to be {@code minecraft:foobar}, it will suggest a replacement for that
* whole segment of the input.</p>
*
* @param parse the result of a {@link #parse(StringReader, Object)}
* @return a future that will eventually resolve into a {@link Suggestions} object
*/
public CompletableFuture<Suggestions> getCompletionSuggestions(final ParseResults<S> parse) {
return getCompletionSuggestions(parse, parse.getReader().getTotalLength());
}
public CompletableFuture<Suggestions> getCompletionSuggestions(final ParseResults<S> parse, int cursor) {
final CommandContextBuilder<S> context = parse.getContext();
final SuggestionContext<S> nodeBeforeCursor = context.findSuggestionContext(cursor);
final CommandNode<S> parent = nodeBeforeCursor.parent;
final int start = Math.min(nodeBeforeCursor.startPos, cursor);
final String fullInput = parse.getReader().getString();
final String truncatedInput = fullInput.substring(0, cursor);
@SuppressWarnings("unchecked") final CompletableFuture<Suggestions>[] futures = new CompletableFuture[parent.getChildren().size()];
int i = 0;
for (final CommandNode<S> node : parent.getChildren()) {
CompletableFuture<Suggestions> future = Suggestions.empty();
try {
future = node.listSuggestions(context.build(truncatedInput), new SuggestionsBuilder(truncatedInput, start));
} catch (final CommandSyntaxException ignored) {
}
futures[i++] = future;
}
final CompletableFuture<Suggestions> result = new CompletableFuture<>();
CompletableFuture.allOf(futures).thenRun(() -> {
final List<Suggestions> suggestions = new ArrayList<>();
for (final CompletableFuture<Suggestions> future : futures) {
suggestions.add(future.join());
}
result.complete(Suggestions.merge(fullInput, suggestions));
});
return result;
}
/**
* Gets the root of this command tree.
*
* <p>This is often useful as a target of a {@link com.mojang.brigadier.builder.ArgumentBuilder#redirect(CommandNode)},
* {@link #getAllUsage(CommandNode, Object, boolean)} or {@link #getSmartUsage(CommandNode, Object)}.
* You may also use it to clone the command tree via {@link #CommandDispatcher(RootCommandNode)}.</p>
*
* @return root of the command tree
*/
public RootCommandNode<S> getRoot() {
return root;
}
/**
* Finds a valid path to a given node on the command tree.
*
* <p>There may theoretically be multiple paths to a node on the tree, especially with the use of forking or redirecting.
* As such, this method makes no guarantees about which path it finds. It will not look at forks or redirects,
* and find the first instance of the target node on the tree.</p>
*
* <p>The only guarantee made is that for the same command tree and the same version of this library, the result of
* this method will <b>always</b> be a valid input for {@link #findNode(Collection)}, which should return the same node
* as provided to this method.</p>
*
* @param target the target node you are finding a path for
* @return a path to the resulting node, or an empty list if it was not found
*/
public Collection<String> getPath(final CommandNode<S> target) {
final List<List<CommandNode<S>>> nodes = new ArrayList<>();
addPaths(root, nodes, new ArrayList<>());
for (final List<CommandNode<S>> list : nodes) {
if (list.get(list.size() - 1) == target) {
final List<String> result = new ArrayList<>(list.size());
for (final CommandNode<S> node : list) {
if (node != root) {
result.add(node.getName());
}
}
return result;
}
}
return Collections.emptyList();
}
/**
* Finds a node by its path
*
* <p>Paths may be generated with {@link #getPath(CommandNode)}, and are guaranteed (for the same tree, and the
* same version of this library) to always produce the same valid node by this method.</p>
*
* <p>If a node could not be found at the specified path, then {@code null} will be returned.</p>
*
* @param path a generated path to a node
* @return the node at the given path, or null if not found
*/
public CommandNode<S> findNode(final Collection<String> path) {
CommandNode<S> node = root;
for (final String name : path) {
node = node.getChild(name);
if (node == null) {
return null;
}
}
return node;
}
/**
* Scans the command tree for potential ambiguous commands.
*
* <p>This is a shortcut for {@link CommandNode#findAmbiguities(AmbiguityConsumer)} on {@link #getRoot()}.</p>
*
* <p>Ambiguities are detected by testing every {@link CommandNode#getExamples()} on one node verses every sibling
* node. This is not fool proof, and relies a lot on the providers of the used argument types to give good examples.</p>
*
* @param consumer a callback to be notified of potential ambiguities
*/
public void findAmbiguities(final AmbiguityConsumer<S> consumer) {
root.findAmbiguities(consumer);
}
private void addPaths(final CommandNode<S> node, final List<List<CommandNode<S>>> result, final List<CommandNode<S>> parents) {
final List<CommandNode<S>> current = new ArrayList<>(parents);
current.add(node);
result.add(current);
for (final CommandNode<S> child : node.getChildren()) {
addPaths(child, result, current);
}
}
}