| 1 | package net.minecraft.commands;
|
| 2 |
|
| 3 | import com.google.common.base.CharMatcher;
|
| 4 | import com.google.common.base.Strings;
|
| 5 | import com.google.common.collect.Lists;
|
| 6 | import com.mojang.brigadier.Message;
|
| 7 | import com.mojang.brigadier.context.CommandContext;
|
| 8 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 9 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 10 | import java.util.Collection;
|
| 11 | import java.util.Collections;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Locale;
|
| 14 | import java.util.Set;
|
| 15 | import java.util.concurrent.CompletableFuture;
|
| 16 | import java.util.function.Consumer;
|
| 17 | import java.util.function.Function;
|
| 18 | import java.util.function.Predicate;
|
| 19 | import java.util.stream.Stream;
|
| 20 | import net.minecraft.core.Holder;
|
| 21 | import net.minecraft.core.HolderLookup;
|
| 22 | import net.minecraft.core.Registry;
|
| 23 | import net.minecraft.core.RegistryAccess;
|
| 24 | import net.minecraft.resources.Identifier;
|
| 25 | import net.minecraft.resources.ResourceKey;
|
| 26 | import net.minecraft.server.permissions.PermissionSetSupplier;
|
| 27 | import net.minecraft.tags.TagKey;
|
| 28 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 29 | import net.minecraft.world.level.Level;
|
| 30 |
|
| 31 | public interface SharedSuggestionProvider extends PermissionSetSupplier {
|
| 32 | CharMatcher MATCH_SPLITTER = CharMatcher.anyOf("._/");
|
| 33 |
|
| 34 | Collection<String> getOnlinePlayerNames();
|
| 35 |
|
| 36 | default Collection<String> getCustomTabSuggestions() {
|
| 37 | return this.getOnlinePlayerNames();
|
| 38 | }
|
| 39 |
|
| 40 | default Collection<String> getSelectedEntities() {
|
| 41 | return Collections.emptyList();
|
| 42 | }
|
| 43 |
|
| 44 | Collection<String> getAllTeams();
|
| 45 |
|
| 46 | Stream<Identifier> getAvailableSounds();
|
| 47 |
|
| 48 | Stream<Identifier> getAvailablePostEffects();
|
| 49 |
|
| 50 | CompletableFuture<Suggestions> customSuggestion(CommandContext<?> context);
|
| 51 |
|
| 52 | default Collection<SharedSuggestionProvider.TextCoordinates> getRelevantCoordinates() {
|
| 53 | return Collections.singleton(SharedSuggestionProvider.TextCoordinates.DEFAULT_GLOBAL);
|
| 54 | }
|
| 55 |
|
| 56 | default Collection<SharedSuggestionProvider.TextCoordinates> getAbsoluteCoordinates() {
|
| 57 | return Collections.singleton(SharedSuggestionProvider.TextCoordinates.DEFAULT_GLOBAL);
|
| 58 | }
|
| 59 |
|
| 60 | Set<ResourceKey<Level>> levels();
|
| 61 |
|
| 62 | RegistryAccess registryAccess();
|
| 63 |
|
| 64 | FeatureFlagSet enabledFeatures();
|
| 65 |
|
| 66 | default <E> void suggestRegistryElements(
|
| 67 | final HolderLookup<E> registry, final SharedSuggestionProvider.ElementSuggestionType elements, final SuggestionsBuilder builder, final Predicate<E> filter
|
| 68 | ) {
|
| 69 | if (elements.shouldSuggestTags()) {
|
| 70 | suggestResource(registry.listTagIds().map(TagKey::location), builder, "#");
|
| 71 | }
|
| 72 |
|
| 73 | if (elements.shouldSuggestElements()) {
|
| 74 | suggestResource(registry.listElements().filter(holder -> filter.test(holder.value())).map(Holder.Reference::key).map(ResourceKey::identifier), builder);
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | static <S, E> CompletableFuture<Suggestions> listSuggestions(
|
| 79 | final CommandContext<S> context,
|
| 80 | final SuggestionsBuilder builder,
|
| 81 | final ResourceKey<? extends Registry<E>> registryKey,
|
| 82 | final SharedSuggestionProvider.ElementSuggestionType type
|
| 83 | ) {
|
| 84 | return listSuggestions(context, builder, registryKey, type, var0 -> true);
|
| 85 | }
|
| 86 |
|
| 87 | static <S, E> CompletableFuture<Suggestions> listSuggestions(
|
| 88 | final CommandContext<S> context,
|
| 89 | final SuggestionsBuilder builder,
|
| 90 | final ResourceKey<? extends Registry<E>> registryKey,
|
| 91 | final SharedSuggestionProvider.ElementSuggestionType type,
|
| 92 | final Predicate<E> filter
|
| 93 | ) {
|
| 94 | return context.getSource() instanceof SharedSuggestionProvider suggestionProvider
|
| 95 | ? suggestionProvider.suggestRegistryElements(registryKey, type, builder, context, filter)
|
| 96 | : builder.buildFuture();
|
| 97 | }
|
| 98 |
|
| 99 | <E> CompletableFuture<Suggestions> suggestRegistryElements(
|
| 100 | final ResourceKey<? extends Registry<E>> key,
|
| 101 | final SharedSuggestionProvider.ElementSuggestionType elements,
|
| 102 | final SuggestionsBuilder builder,
|
| 103 | final CommandContext<?> context,
|
| 104 | final Predicate<E> filter
|
| 105 | );
|
| 106 |
|
| 107 | static <T> void filterResources(final Iterable<T> values, final String contents, final Function<T, Identifier> converter, final Consumer<T> consumer) {
|
| 108 | boolean hasNamespace = contents.indexOf(58) > -1;
|
| 109 |
|
| 110 | for (T value : values) {
|
| 111 | Identifier id = converter.apply(value);
|
| 112 | if (hasNamespace) {
|
| 113 | String name = id.toString();
|
| 114 | if (matchesSubStr(contents, name)) {
|
| 115 | consumer.accept(value);
|
| 116 | }
|
| 117 | } else if (matchesSubStr(contents, id.getNamespace()) || matchesSubStr(contents, id.getPath())) {
|
| 118 | consumer.accept(value);
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | static <T> void filterResources(
|
| 124 | final Iterable<T> values, final String contents, final String prefix, final Function<T, Identifier> converter, final Consumer<T> consumer
|
| 125 | ) {
|
| 126 | if (contents.isEmpty()) {
|
| 127 | values.forEach(consumer);
|
| 128 | } else {
|
| 129 | String commonPrefix = Strings.commonPrefix(contents, prefix);
|
| 130 | if (!commonPrefix.isEmpty()) {
|
| 131 | String strippedContents = contents.substring(commonPrefix.length());
|
| 132 | filterResources(values, strippedContents, converter, consumer);
|
| 133 | }
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | static CompletableFuture<Suggestions> suggestResource(final Iterable<Identifier> values, final SuggestionsBuilder builder, final String prefix) {
|
| 138 | String contents = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 139 | filterResources(values, contents, prefix, t -> t, v -> builder.suggest(prefix + v));
|
| 140 | return builder.buildFuture();
|
| 141 | }
|
| 142 |
|
| 143 | static CompletableFuture<Suggestions> suggestResource(final Stream<Identifier> values, final SuggestionsBuilder builder, final String prefix) {
|
| 144 | return suggestResource(values::iterator, builder, prefix);
|
| 145 | }
|
| 146 |
|
| 147 | static CompletableFuture<Suggestions> suggestResource(final Iterable<Identifier> values, final SuggestionsBuilder builder) {
|
| 148 | String contents = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 149 | filterResources(values, contents, t -> t, v -> builder.suggest(v.toString()));
|
| 150 | return builder.buildFuture();
|
| 151 | }
|
| 152 |
|
| 153 | static <T> CompletableFuture<Suggestions> suggestResource(
|
| 154 | final Iterable<T> values, final SuggestionsBuilder builder, final Function<T, Identifier> id, final Function<T, Message> tooltip
|
| 155 | ) {
|
| 156 | String contents = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 157 | filterResources(values, contents, id, v -> builder.suggest(id.apply(v).toString(), tooltip.apply(v)));
|
| 158 | return builder.buildFuture();
|
| 159 | }
|
| 160 |
|
| 161 | static CompletableFuture<Suggestions> suggestResource(final Stream<Identifier> values, final SuggestionsBuilder builder) {
|
| 162 | return suggestResource(values::iterator, builder);
|
| 163 | }
|
| 164 |
|
| 165 | static <T> CompletableFuture<Suggestions> suggestResource(
|
| 166 | final Stream<T> values, final SuggestionsBuilder builder, final Function<T, Identifier> id, final Function<T, Message> tooltip
|
| 167 | ) {
|
| 168 | return suggestResource(values::iterator, builder, id, tooltip);
|
| 169 | }
|
| 170 |
|
| 171 | static CompletableFuture<Suggestions> suggestCoordinates(
|
| 172 | final String currentInput,
|
| 173 | final Collection<SharedSuggestionProvider.TextCoordinates> allSuggestions,
|
| 174 | final SuggestionsBuilder builder,
|
| 175 | final Predicate<String> validator
|
| 176 | ) {
|
| 177 | List<String> result = Lists.newArrayList();
|
| 178 | if (Strings.isNullOrEmpty(currentInput)) {
|
| 179 | for (SharedSuggestionProvider.TextCoordinates coordinate : allSuggestions) {
|
| 180 | String fullValue = coordinate.x + " " + coordinate.y + " " + coordinate.z;
|
| 181 | if (validator.test(fullValue)) {
|
| 182 | result.add(coordinate.x);
|
| 183 | result.add(coordinate.x + " " + coordinate.y);
|
| 184 | result.add(fullValue);
|
| 185 | }
|
| 186 | }
|
| 187 | } else {
|
| 188 | String[] fields = currentInput.split(" ");
|
| 189 | if (fields.length == 1) {
|
| 190 | for (SharedSuggestionProvider.TextCoordinates coordinate : allSuggestions) {
|
| 191 | String fullValue = fields[0] + " " + coordinate.y + " " + coordinate.z;
|
| 192 | if (validator.test(fullValue)) {
|
| 193 | result.add(fields[0] + " " + coordinate.y);
|
| 194 | result.add(fullValue);
|
| 195 | }
|
| 196 | }
|
| 197 | } else if (fields.length == 2) {
|
| 198 | for (SharedSuggestionProvider.TextCoordinates coordinate : allSuggestions) {
|
| 199 | String fullValue = fields[0] + " " + fields[1] + " " + coordinate.z;
|
| 200 | if (validator.test(fullValue)) {
|
| 201 | result.add(fullValue);
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | return suggest(result, builder);
|
| 208 | }
|
| 209 |
|
| 210 | static CompletableFuture<Suggestions> suggest2DCoordinates(
|
| 211 | final String currentInput,
|
| 212 | final Collection<SharedSuggestionProvider.TextCoordinates> allSuggestions,
|
| 213 | final SuggestionsBuilder builder,
|
| 214 | final Predicate<String> validator
|
| 215 | ) {
|
| 216 | List<String> result = Lists.newArrayList();
|
| 217 | if (Strings.isNullOrEmpty(currentInput)) {
|
| 218 | for (SharedSuggestionProvider.TextCoordinates coordinate : allSuggestions) {
|
| 219 | String fullValue = coordinate.x + " " + coordinate.z;
|
| 220 | if (validator.test(fullValue)) {
|
| 221 | result.add(coordinate.x);
|
| 222 | result.add(fullValue);
|
| 223 | }
|
| 224 | }
|
| 225 | } else {
|
| 226 | String[] fields = currentInput.split(" ");
|
| 227 | if (fields.length == 1) {
|
| 228 | for (SharedSuggestionProvider.TextCoordinates coordinate : allSuggestions) {
|
| 229 | String fullValue = fields[0] + " " + coordinate.z;
|
| 230 | if (validator.test(fullValue)) {
|
| 231 | result.add(fullValue);
|
| 232 | }
|
| 233 | }
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | return suggest(result, builder);
|
| 238 | }
|
| 239 |
|
| 240 | static CompletableFuture<Suggestions> suggest(final Iterable<String> values, final SuggestionsBuilder builder) {
|
| 241 | String lowerPrefix = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 242 |
|
| 243 | for (String name : values) {
|
| 244 | if (matchesSubStr(lowerPrefix, name.toLowerCase(Locale.ROOT))) {
|
| 245 | builder.suggest(name);
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 | return builder.buildFuture();
|
| 250 | }
|
| 251 |
|
| 252 | static CompletableFuture<Suggestions> suggest(final Stream<String> values, final SuggestionsBuilder builder) {
|
| 253 | String lowerPrefix = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 254 | values.filter(v -> matchesSubStr(lowerPrefix, v.toLowerCase(Locale.ROOT))).forEach(builder::suggest);
|
| 255 | return builder.buildFuture();
|
| 256 | }
|
| 257 |
|
| 258 | static CompletableFuture<Suggestions> suggest(final String[] values, final SuggestionsBuilder builder) {
|
| 259 | String lowerPrefix = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 260 |
|
| 261 | for (String name : values) {
|
| 262 | if (matchesSubStr(lowerPrefix, name.toLowerCase(Locale.ROOT))) {
|
| 263 | builder.suggest(name);
|
| 264 | }
|
| 265 | }
|
| 266 |
|
| 267 | return builder.buildFuture();
|
| 268 | }
|
| 269 |
|
| 270 | static <T> CompletableFuture<Suggestions> suggest(
|
| 271 | final Iterable<T> values, final SuggestionsBuilder builder, final Function<T, String> toString, final Function<T, Message> tooltip
|
| 272 | ) {
|
| 273 | String lowerPrefix = builder.getRemaining().toLowerCase(Locale.ROOT);
|
| 274 |
|
| 275 | for (T value : values) {
|
| 276 | String name = toString.apply(value);
|
| 277 | if (matchesSubStr(lowerPrefix, name.toLowerCase(Locale.ROOT))) {
|
| 278 | builder.suggest(name, tooltip.apply(value));
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | return builder.buildFuture();
|
| 283 | }
|
| 284 |
|
| 285 | static boolean matchesSubStr(final String pattern, final String input) {
|
| 286 | int index = 0;
|
| 287 |
|
| 288 | while (!input.startsWith(pattern, index)) {
|
| 289 | int indexOfSplitter = MATCH_SPLITTER.indexIn(input, index);
|
| 290 | if (indexOfSplitter < 0) {
|
| 291 | return false;
|
| 292 | }
|
| 293 |
|
| 294 | index = indexOfSplitter + 1;
|
| 295 | }
|
| 296 |
|
| 297 | return true;
|
| 298 | }
|
| 299 |
|
| 300 | enum ElementSuggestionType {
|
| 301 | TAGS,
|
| 302 | ELEMENTS,
|
| 303 | ALL;
|
| 304 |
|
| 305 | public boolean shouldSuggestTags() {
|
| 306 | return this == TAGS || this == ALL;
|
| 307 | }
|
| 308 |
|
| 309 | public boolean shouldSuggestElements() {
|
| 310 | return this == ELEMENTS || this == ALL;
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | class TextCoordinates {
|
| 315 | public static final SharedSuggestionProvider.TextCoordinates DEFAULT_LOCAL = new SharedSuggestionProvider.TextCoordinates("^", "^", "^");
|
| 316 | public static final SharedSuggestionProvider.TextCoordinates DEFAULT_GLOBAL = new SharedSuggestionProvider.TextCoordinates("~", "~", "~");
|
| 317 | public final String x;
|
| 318 | public final String y;
|
| 319 | public final String z;
|
| 320 |
|
| 321 | public TextCoordinates(final String x, final String y, final String z) {
|
| 322 | this.x = x;
|
| 323 | this.y = y;
|
| 324 | this.z = z;
|
| 325 | }
|
| 326 | }
|
| 327 | }
|
| 328 |
|