User Tools

Site Tools


tutorial:commands

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:commands [2020/06/14 04:41] – Link to new suggestions page i509vcbtutorial:commands [2020/06/14 05:13] – Refactor FAQ section i509vcb
Line 212: Line 212:
 CommandManager is in ''net.minecraft.server.command'' CommandManager is in ''net.minecraft.server.command''
  
-===== Advanced concepts =====+====== Advanced concepts ======
  
 Below are links to the articles about more complex concepts used in brigadier. Below are links to the articles about more complex concepts used in brigadier.
  
-^ Page                                           ^ Description                              +^ Page                                                           ^ Description                                                                     ^ 
-| [[tutorials:commands:suggestions|Suggestions]] | Suggesting argument values to the client |+| [[tutorials:commands:requirements|Requirements]]               | Only allow users to execute commands in certain scenarios.                      | 
 +| [[tutorials:commands:exceptions  |Exceptions]]                 | Fail execution of a command with a descriptive message and in certain contexts. |                                                                   
 +| [[tutorials:commands:suggestions |Suggestions]]                | Suggesting input to be sent to the client.                                      | 
 +| [[tutorials:commands:redirects_aliases|Redirects (Aliases)]]   | Allow use of aliases to execute commands.                                       | 
 +| [[tutorials:commands:redirects_chaining|Redirects (Chaining)]] | Allow commands to have repeating elements and flags.                            | 
 +| [[tutorials:commands:argument_types|Custom Argument Types]]    | Parse your own arguments and return your own types.                             |
  
-**TODO:** Sections are being moved to sub categories and will be added here as they are moved+**TODO:** Sections are being moved to sub categories and will be added to their respective articles as they are migrated.
  
-===== Suggestions =====+====== Sorry for the mess ======
  
-Suggestions can be provided to the client to recommend what to input into the command.  This is used for Scoreboards and Loot Tables ingame. The game stores these in the SuggestionProviders. A few examples of Minecraft's built in suggestions providers are below +__Currently this article is being migratedso things may be messBelow is are the parts of the article that are yet to be migrated to the new format.__
-<code> +
-SUMMONABLE_ENTITIES +
-AVAILIBLE_SOUNDS +
-ALL_RECIPES +
-ASK_SERVER +
-</code> +
- +
-Loot tables specify their own SuggestionProvider inside ''LootCommand'' for example. +
- +
-The example below is a dynamically changing SuggestionProvider that lists several words for a StringArgumentType to demonstrate how it works: +
-<code java [enable_line_numbers="true"]> +
-public static SuggestionProvider<ServerCommandSource> suggestedStrings() { +
-    return (ctxbuilder) -> getSuggestionsBuilder(builder, /*Access to list here*/); +
-+
-     +
-private static CompletableFuture<Suggestions> getSuggestionsBuilder(SuggestionsBuilder builder, List<String> list) { +
-    String remaining = builder.getRemaining().toLowerCase(Locale.ROOT); +
-         +
-    if(list.isEmpty()) { // If the list is empty then return no suggestions +
-        return Suggestions.empty(); // No suggestions +
-    } +
-         +
-    for (String str : list) { // Iterate through the supplied list +
-        if (str.toLowerCase(Locale.ROOT).startsWith(remaining)) { +
-            builder.suggest(str); // Add every single entry to suggestions list. +
-        } +
-    } +
-    return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions +
-+
-</code> +
- +
-The SuggestionProvider is a functional interface that returns a CompletableFuture containing a list of suggestions. These suggestions are sent to client as a command is typed and can be changed while server is running. The SuggestionProvider provides a CommandContext and a SuggestionBuilder to allow determination of all the suggestionsThe CommandSource can also be taken into account during the suggestion creation process as it is available through the CommandContext. +
- +
-Though remember these are suggestions. The inputted command may not contain an argument you suggested so arguments are parsed without consideration for suggestions. +
- +
-To use the suggestion you would append it right after the argument you want to suggest possible arguments for. This can be any argument and the normal client side exception popups will still work. Note this cannot be applied to literals. +
- +
-<code java [enable_line_numbers="true"]> +
-argument(argumentName, word()) +
-.suggests(CompletionProviders.suggestedStrings()) +
-    .then(/*Rest of the command*/)); +
-</code>+
  
 ===== Requirements ===== ===== Requirements =====
Line 632: Line 595:
 } }
 </file> </file>
-===== FAQ =====+====== FAQ ====== 
 + 
 +===== Why does my command not compile ===== 
 + 
 +There are two immediate possibilities for why this could occur. 
 + 
 +==== Catch or throw a CommandSyntaxException ====
  
-=== What else can I send feedback to the CommandSource? ===+The solution to this issue is to make the run or suggest methods throw a CommandSyntaxException. Don't worry, brigadier will handle the exceptions and output the proper error message.
  
-You use the Text classes (LiteralText, TranslatableText, KeybindText, etc).+==== Issues with generics ====
  
-=== Why does my IDE complain saying that method executed by my command needs to catch or throw a CommandSyntaxException ===+You may have an issue with generic types once in while. Verify you are using ''CommandManager.literal(...)'' or ''CommandManager.argument(...)'' instead ''LiteralArgumentBuilder'' or ''RequiredArgumentBuilder''.
  
-The solution to this is just to make the methods throw a CommandSyntaxException. This is because brigadier handles the exceptions.+===== Can I register client side commands? =====
  
-=== Can I register commands in runtime? ===+Fabric doesn't currently support client side commands. There is a [[https://github.com/CottonMC/ClientCommands|third-party mod]] by the Cotton team that adds this functionality.
  
-You can do this but it is not reccomended. You would get the instance of the CommandManager and add anything you wish to the CommandDispatcher within it.+===== Dark Arts =====
  
-After that you will need to send the command tree to every player again using ''CommandManager.sendCommandTree(ServerPlayerEntity)''+A few things we don't recommend, but are possible.
  
-=== Can I unregister commands in runtime? ===+==== Can I register commands in runtime? ====
  
-You can also do this but it is very unstable and could cause unwanted side effectsLets just say it involves a bunch of Reflection.+You can do this but it is not recommendedYou would get the ''CommandManager'' from the server and add anything commands you wish to it's ''CommandDispatcher''.
  
-Once again you will need to send the command tree to every player again using ''CommandManager.sendCommandTree(ServerPlayerEntity)'' afterwards.+After that you need to send the command tree to every player again using ''sendCommandTree(ServerPlayerEntity)''. This is required because the client locally caches the command tree it receives during login (or when operator packets are sent) for local completions rich error messages.
  
-=== Can I register client side commands? ===+==== Can I unregister commands in runtime====
  
-Well Fabric currently doesn't support this natively but there is a mod by the Cotton team that adds this functionality where the commands do not run on the server and only on the client+You can also do this, however it is much less stable than registering commands and could cause unwanted side effects. To keep things simple, you need to use reflection on brigadier and remove the nodes. After this, you need to send the command tree to every player again using ''sendCommandTree(ServerPlayerEntity)''. If you don't send the updated command tree, the client may think a command still exists, even though the server will fail execution.
-https://github.com/CottonMC/ClientCommands+
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210