Commits
md_5 authored 911bde181f1
1 + | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 + | // Licensed under the MIT license. |
3 + | |
1 4 | package com.mojang.brigadier.tree; |
2 5 | |
3 6 | // CHECKSTYLE:OFF |
4 - | import com.google.common.collect.ComparisonChain; |
5 - | import com.google.common.collect.Maps; |
6 - | import com.google.common.collect.Sets; |
7 7 | import com.mojang.brigadier.AmbiguityConsumer; |
8 8 | import com.mojang.brigadier.Command; |
9 9 | import com.mojang.brigadier.RedirectModifier; |
10 10 | import com.mojang.brigadier.StringReader; |
11 11 | import com.mojang.brigadier.builder.ArgumentBuilder; |
12 12 | import com.mojang.brigadier.context.CommandContext; |
13 13 | import com.mojang.brigadier.context.CommandContextBuilder; |
14 14 | import com.mojang.brigadier.exceptions.CommandSyntaxException; |
15 15 | import com.mojang.brigadier.suggestion.Suggestions; |
16 16 | import com.mojang.brigadier.suggestion.SuggestionsBuilder; |
17 17 | |
18 18 | import java.util.Collection; |
19 19 | import java.util.Collections; |
20 + | import java.util.HashSet; |
20 21 | import java.util.LinkedHashMap; |
21 22 | import java.util.Map; |
22 23 | import java.util.Set; |
23 24 | import java.util.concurrent.CompletableFuture; |
24 25 | import java.util.function.Predicate; |
25 - | import java.util.stream.Collectors; |
26 26 | |
27 27 | import net.minecraft.commands.CommandListenerWrapper; // CraftBukkit |
28 28 | |
29 29 | public abstract class CommandNode<S> implements Comparable<CommandNode<S>> { |
30 - | private Map<String, CommandNode<S>> children = Maps.newLinkedHashMap(); |
31 - | private Map<String, LiteralCommandNode<S>> literals = Maps.newLinkedHashMap(); |
32 - | private Map<String, ArgumentCommandNode<S, ?>> arguments = Maps.newLinkedHashMap(); |
30 + | private final Map<String, CommandNode<S>> children = new LinkedHashMap<>(); |
31 + | private final Map<String, LiteralCommandNode<S>> literals = new LinkedHashMap<>(); |
32 + | private final Map<String, ArgumentCommandNode<S, ?>> arguments = new LinkedHashMap<>(); |
33 33 | private final Predicate<S> requirement; |
34 34 | private final CommandNode<S> redirect; |
35 35 | private final RedirectModifier<S> modifier; |
36 36 | private final boolean forks; |
37 37 | private Command<S> command; |
38 38 | // CraftBukkit start |
39 39 | public void removeCommand(String name) { |
40 40 | children.remove(name); |
41 41 | literals.remove(name); |
42 42 | arguments.remove(name); |
100 100 | child.addChild(grandchild); |
101 101 | } |
102 102 | } else { |
103 103 | children.put(node.getName(), node); |
104 104 | if (node instanceof LiteralCommandNode) { |
105 105 | literals.put(node.getName(), (LiteralCommandNode<S>) node); |
106 106 | } else if (node instanceof ArgumentCommandNode) { |
107 107 | arguments.put(node.getName(), (ArgumentCommandNode<S, ?>) node); |
108 108 | } |
109 109 | } |
110 - | |
111 - | children = children.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); |
112 110 | } |
113 111 | |
114 112 | public void findAmbiguities(final AmbiguityConsumer<S> consumer) { |
115 - | Set<String> matches = Sets.newHashSet(); |
113 + | Set<String> matches = new HashSet<>(); |
116 114 | |
117 115 | for (final CommandNode<S> child : children.values()) { |
118 116 | for (final CommandNode<S> sibling : children.values()) { |
119 117 | if (child == sibling) { |
120 118 | continue; |
121 119 | } |
122 120 | |
123 121 | for (final String input : child.getExamples()) { |
124 122 | if (sibling.isValidInput(input)) { |
125 123 | matches.add(input); |
126 124 | } |
127 125 | } |
128 126 | |
129 127 | if (matches.size() > 0) { |
130 128 | consumer.ambiguous(this, child, sibling, matches); |
131 - | matches = Sets.newHashSet(); |
129 + | matches = new HashSet<>(); |
132 130 | } |
133 131 | } |
134 132 | |
135 133 | child.findAmbiguities(consumer); |
136 134 | } |
137 135 | } |
138 136 | |
139 137 | protected abstract boolean isValidInput(final String input); |
140 138 | |
141 139 | |
186 184 | } else { |
187 185 | return arguments.values(); |
188 186 | } |
189 187 | } else { |
190 188 | return arguments.values(); |
191 189 | } |
192 190 | } |
193 191 | |
194 192 | |
195 193 | public int compareTo(final CommandNode<S> o) { |
196 - | return ComparisonChain |
197 - | .start() |
198 - | .compareTrueFirst(this instanceof LiteralCommandNode, o instanceof LiteralCommandNode) |
199 - | .compare(getSortedKey(), o.getSortedKey()) |
200 - | .result(); |
194 + | if (this instanceof LiteralCommandNode == o instanceof LiteralCommandNode) { |
195 + | return getSortedKey().compareTo(o.getSortedKey()); |
196 + | } |
197 + | |
198 + | return (o instanceof LiteralCommandNode) ? 1 : -1; |
201 199 | } |
202 200 | |
203 201 | public boolean isFork() { |
204 202 | return forks; |
205 203 | } |
206 204 | |
207 205 | public abstract Collection<String> getExamples(); |
208 206 | } |