User Tools

Site Tools


Sidebar

← Go back to the homepage

Fabric Tutorials

Setup

These pages will help you setup a productive development environment and semi-automated releases.

Basics

These pages are essential must-reads when modding with Fabric, and modding Minecraft in general, if you are new to modding, it is recommended you read the following.

Items

Blocks and Block Entities

Data Generation

World Generation

Commands

These pages will guide you through Mojang's Brigadier library which allows you to create commands with complex arguments and actions.

Events

These pages will guide you through using the many events included in Fabric API, and how to create your own events for you or other mods to use.

Entities

Fluids

Mixins & ASM

These pages will guide you through the usage of SpongePowered's Mixin library, which is a highly complex topic. We recommend you read these pages thoroughly.

Miscellaneous

Yarn

Contribute to Fabric

tutorial:command_examples

Command examples

These examples are available under the “CC Attribution-Noncommercial-Share Alike 4.0 International” license, which is the current license of the other wiki articles.

Broadcast a message

  1. public final class BroadCastCommand {
  2. public static void register(CommandDispatcher<ServerCommandSource> dispatcher){
  3. dispatcher.register(literal("broadcast")
  4. .requires(source -> source.hasPermissionLevel(2)) // Must be a game master to use the command. Command will not show up in tab completion or execute to non operators or any operator that is permission level 1.
  5. .then(argument("color", ColorArgumentType.color())
  6. .then(argument("message", greedyString())
  7. .executes(ctx -> broadcast(ctx.getSource(), getColor(ctx, "color"), getString(ctx, "message")))))); // You can deal with the arguments out here and pipe them into the command.
  8. }
  9.  
  10. public static int broadcast(ServerCommandSource source, Formatting formatting, String message) {
  11. final Text text = Text.literal(message).formatted(formatting);
  12.  
  13. source.getMinecraftServer().getPlayerManager().broadcastChatMessage(text, MessageType.CHAT, source.getPlayer().getUuid());
  14. return Command.SINGLE_SUCCESS; // Success
  15. }
  16. }

In your initializer:

  1. public class ExampleMod implements ModInitializer{
  2. @Override
  3. public void onInitialize() {
  4. ...
  5.  
  6. CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> register(dispatcher));
  7. }
  8. }

In the examples below, registrations in the callbacks are ommited.

/giveMeDiamond

First the basic code where we register “giveMeDiamond” as a literal and then an executes block to tell the dispatcher which method to run.

  1. public static LiteralCommandNode register(CommandDispatcher<ServerCommandSource> dispatcher) { // You can also return a LiteralCommandNode for use with possible redirects
  2. return dispatcher.register(literal("giveMeDiamond")
  3. .executes(ctx -> giveDiamond(ctx)));
  4. }

Then since we only want to give to players, we check if the CommandSource is a player. But we can use getPlayer and do both at the same time and throw an error if the source is not a player.

  1. public static int giveDiamond(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
  2. final ServerCommandSource source = ctx.getSource();
  3.  
  4. final PlayerEntity self = source.getPlayer(); // If not a player than the command ends

Then we add to the player's inventory, with a check to see if the inventory is full:

  1. if(!player.inventory.insertStack(new ItemStack(Items.DIAMOND))){
  2. throw new SimpleCommandExceptionType(Text.translatable("inventory.isfull")).create();
  3. }
  4.  
  5. return 1;
  6. }

Antioch

…lobbest thou thy Holy Hand Grenade of Antioch towards thy foe. who being naughty in My sight, shall snuff it.

Aside from the joke this command summons a primed TNT to a specified location or the location of the sender's cursor.

First create an entry into the CommandDispatcher that takes a literal of antioch with an optional argument of the location to summon the entity at.

  1. public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
  2. dispatcher.register(literal("antioch")
  3. .then(required("location", BlockPosArgumentType.blockPos()
  4. .executes(ctx -> antioch(ctx.getSource(), BlockPosArgument.getBlockPos(ctx, "location")))))
  5. .executes(ctx -> antioch(ctx.getSource(), null)));
  6. }

Then the creation and messages behind the joke.

  1. public static int antioch(ServerCommandSource source, BlockPos blockPos) throws CommandSyntaxException {
  2. if(blockPos == null) {
  3. // For the case of no inputted argument we calculate the cursor position of the player or throw an error if the nearest position is too far or is outside of the world.
  4. // This class is used as an example and actually doesn't exist yet.
  5. blockPos = LocationUtil.calculateCursorOrThrow(source, source.getRotation());
  6. }
  7.  
  8. final TntEntity tnt = new TntEntity(source.getWorld(), blockPos.getX(), blockPos.getY(), blockPos.getZ(), null);
  9. tnt.setFuse(3);
  10.  
  11. source.getServer().getPlayerManager().broadcastChatMessage(Text.literal("...lobbest thou thy Holy Hand Grenade of Antioch towards thy foe", MessageType.CHAT, UUID.randomUUID()).formatted(Formatting.RED), false);
  12. source.getServer().getPlayerManager().broadcastChatMessage(Text.literal("who being naughty in My sight, shall snuff it.", MessageType.CHAT, UUID.randomUUID()).formatted(Formatting.RED), false);
  13. source.getWorld().spawnEntity(tnt);
  14. return 1;
  15. }
tutorial/command_examples.txt · Last modified: 2022/08/08 02:25 (external edit)