tutorial:command_suggestions
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:command_suggestions [2021/09/17 04:49] – external edit 127.0.0.1 | tutorial:command_suggestions [2023/11/18 12:06] (current) – solidblock | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ====== Command Suggestions ====== | ====== Command Suggestions ====== | ||
| - | Brigadier allows specification of custom suggestions for arguments. In Minecraft, these suggestions are sent to the client as a user is typing out the command. | + | Brigadier allows specification of custom suggestions for arguments. In Minecraft, these suggestions are sent to the client as a user is typing out the command. Besides, some argument types can have their default suggestion providers calculated directly in client, which is not focused in this article. |
| ===== Suggestion Providers ===== | ===== Suggestion Providers ===== | ||
| - | A '' | + | A '' |
| Suggestions can be contextual since a suggestion provider gives you access to the current command context. | Suggestions can be contextual since a suggestion provider gives you access to the current command context. | ||
| Line 14: | Line 13: | ||
| ===== An example suggestion provider ===== | ===== An example suggestion provider ===== | ||
| - | For example | + | For example |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | class AttributeSuggestionProvider implements SuggestionProvider< | + | public |
| @Override | @Override | ||
| public CompletableFuture< | public CompletableFuture< | ||
| Line 29: | Line 28: | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | class AttributeSuggestionProvider implements SuggestionProvider< | + | public |
| @Override | @Override | ||
| public CompletableFuture< | public CompletableFuture< | ||
| Identifier entityTypeId = context.getArgument(" | Identifier entityTypeId = context.getArgument(" | ||
| - | EntityType<?> | + | EntityType<?> |
| + | // For versions before 1.19.3, use '' | ||
| | | ||
| if (!DefaultAttributeContainer.hasDefinitionFor(entityType)) { | if (!DefaultAttributeContainer.hasDefinitionFor(entityType)) { | ||
| - | | + | |
| } | } | ||
| | | ||
| DefaultAttributeContainer attributeContainer = DefaultAttributeRegistry.get(entityType); | DefaultAttributeContainer attributeContainer = DefaultAttributeRegistry.get(entityType); | ||
| // You will need mixin to get the ' | // You will need mixin to get the ' | ||
| - | for (EntityAttribute attribute : attributeContainer.instances().keySet()) { | + | for (EntityAttribute attribute : attributeContainer.instances.keySet()) { |
| - | Identifier attributeId = Registry.ATTRIBUTE.getId(attribute); | + | Identifier attributeId = Registries.ATTRIBUTE.getId(attribute); |
| - | if (attributeId != null) { | + | if (attributeId != null && CommandSource.shouldSuggest(builder.getRemaining(), |
| ... | ... | ||
| </ | </ | ||
| + | |||
| + | The check of '' | ||
| In order to have a suggestion appear on the client, you must add the suggestion to the builder. This can be done through one of the '' | In order to have a suggestion appear on the client, you must add the suggestion to the builder. This can be done through one of the '' | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | for (EntityAttribute attribute : attributeContainer.instances().keySet()) { | + | |
| - | Identifier attributeId = Registry.ATTRIBUTE.getId(attribute); | + | Identifier attributeId = Registries.ATTRIBUTE.getId(attribute); |
| - | if (attributeId != null) { | + | if (attributeId != null && CommandSource.shouldSuggest(builder.getRemaining(), |
| - | | + | |
| - | } | + | } |
| - | } | + | } |
| </ | </ | ||
| Line 63: | Line 65: | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | return builder.buildFuture(); | + | |
| </ | </ | ||
| Line 69: | Line 71: | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | class AttributeSuggestionProvider implements SuggestionProvider< | + | public |
| @Override | @Override | ||
| public CompletableFuture< | public CompletableFuture< | ||
| Identifier entityTypeId = context.getArgument(" | Identifier entityTypeId = context.getArgument(" | ||
| - | EntityType<?> | + | EntityType<?> |
| | | ||
| if (!DefaultAttributeContainer.hasDefinitionFor(entityType)) { | if (!DefaultAttributeContainer.hasDefinitionFor(entityType)) { | ||
| - | | + | |
| } | } | ||
| | | ||
| DefaultAttributeContainer attributeContainer = DefaultAttributeRegistry.get(entityType); | DefaultAttributeContainer attributeContainer = DefaultAttributeRegistry.get(entityType); | ||
| // You will need mixin to get the ' | // You will need mixin to get the ' | ||
| - | for (EntityAttribute attribute : attributeContainer.instances().keySet()) { | + | for (EntityAttribute attribute : attributeContainer.instances.keySet()) { |
| - | Identifier attributeId = Registry.ATTRIBUTE.getId(attribute); | + | Identifier attributeId = Registries.ATTRIBUTE.getId(attribute); |
| - | if (attributeId != null) { | + | if (attributeId != null && CommandSource.shouldSuggest(builder.getRemaining(), |
| builder.suggest(attributeId.toString()); | builder.suggest(attributeId.toString()); | ||
| } | } | ||
| Line 109: | Line 111: | ||
| ^ Type ^ Field/ | ^ Type ^ Field/ | ||
| - | | Summonable entities | SuggestionProviders.SUMMONABLE_ENTITIES | | + | | Summonable entities | '' |
| - | | Available sounds | + | | Available sounds |
| - | | Loot Tables | + | | Loot Tables |
| - | | Biomes | + | | Biomes |
| ===== Utilities in CommandSource ===== | ===== Utilities in CommandSource ===== | ||
| - | '' | + | '' |
| - | Many of the utility methods involve returning completed suggestions from a '' | + | |
| + | In the example above, we can simplify it to: | ||
| + | <code java> | ||
| + | DefaultAttributeContainer attributeContainer = DefaultAttributeRegistry.get(entityType); | ||
| + | // You will need mixin to get the ' | ||
| + | return CommandSource.suggestMatching(Iterables.transform(attributeContainer.instances.keySet(), | ||
| + | |||
| + | // you can also use stream: | ||
| + | return CommandSource.suggestMatching(attributeContainer.instances.keySet().stream().map(Identifier:: | ||
| + | </ | ||
| + | |||
| + | ===== Builder offsets ===== | ||
| + | Most cases the suggestions appear from the beginning of the argument, such as the example above. However, sometimes suggestions appear in the middle of the argument. For example, in a block state argument, you input '' | ||
| + | |||
| + | In this case, you should use '' | ||
| ===== Other command tutorials ===== | ===== Other command tutorials ===== | ||
| - | [[tutorial: | + | For other command tutorials, see the sidebar. |
tutorial/command_suggestions.1631854159.txt.gz · Last modified: 2021/09/17 04:49 by 127.0.0.1