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
tutorial:commands [2023/11/18 11:21] solidblocktutorial:commands [2024/02/23 14:22] (current) – Documenting and first draft allen1210
Line 320: Line 320:
 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. 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.
  
 +===== Can I execute command without typing in game? =====
 +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(), pos.getY(), pos.getZ(), 5, false);
 +        if (player != null) {
 +            CommandManager commandManager = Objects.requireNonNull(player.getServer()).getCommandManager();
 +            ServerCommandSource commandSource = player.getServer().getCommandSource();
 +            commandManager.executeWithPrefix(commandSource, command);
 +        }
 +    }
 +</code>
 +
 +First, you need a CommandManager<ServerCommandSource>.
 +Second, you need the ServerCommandSource.
 +Then, u can call some CommandManager public methods like commandeManader.execute. (.execute need ParseResults<ServerCommandSource>)
 +But commandeManader.executeWithPrefix allow you to use String. You can also put the slash (/).
 +
 +So... have fun!
tutorial/commands.txt · Last modified: 2024/02/23 14:22 by allen1210