tutorial:commands
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:commands [2024/02/23 14:09] – Adding one question in FAQ section allen1210 | tutorial:commands [2025/07/06 07:10] (current) – [Register a basic command] solidblock | ||
---|---|---|---|
Line 3: | Line 3: | ||
====== Creating Commands ====== | ====== Creating Commands ====== | ||
- | Creating commands can allow a mod developer to add functionality that can used through a command. This tutorial will teach you how to register commands, and the general command structure of Brigadier. | + | Creating commands can allow a mod developer to add functionality that can be used through a command. This tutorial will teach you how to register commands, and the general command structure of Brigadier. |
===== What is Brigadier? ===== | ===== What is Brigadier? ===== | ||
Line 105: | Line 105: | ||
</ | </ | ||
- | **Please ensure you import the correct static method.** The method '' | + | **Please ensure you import the correct static method.** The method '' |
In the '' | In the '' | ||
Line 130: | Line 130: | ||
===== Static Imports ===== | ===== Static Imports ===== | ||
- | In the example above, the use of static imports is used for code simplifying. For a literal this would shorten the statement to '' | + | In the example above, the use of static imports is used for code simplifying. For a literal this would shorten the statement to '' |
Below is an example of some static imports: | Below is an example of some static imports: | ||
Line 321: | Line 321: | ||
===== Can I execute command without typing in game? ===== | ===== Can I execute command without typing in game? ===== | ||
- | Yes! You can. | + | Yes! You can. Before trying the next code, take note it works on Fabric 0.91.6+1.20.2 and it was not tested in other versions. |
+ | Here is the code example | ||
+ | |||
+ | <code java> | ||
+ | private void vanillaCommandByPlayer(World world, BlockPos pos, String command) { | ||
+ | PlayerEntity player = world.getClosestPlayer(pos.getX(), | ||
+ | if (player != null) { | ||
+ | CommandManager commandManager = Objects.requireNonNull(player.getServer()).getCommandManager(); | ||
+ | ServerCommandSource commandSource = player.getServer().getCommandSource(); | ||
+ | commandManager.executeWithPrefix(commandSource, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | First, you need a CommandManager< | ||
+ | Second, you need the ServerCommandSource. | ||
+ | Then, u can call some CommandManager public methods like commandeManader.execute. (.execute need ParseResults< | ||
+ | But commandeManader.executeWithPrefix allow you to use String. You can also put the slash (/). | ||
+ | |||
+ | So... have fun! |
tutorial/commands.1708697382.txt.gz · Last modified: 2024/02/23 14:09 by allen1210