How to make a command with arguments in a Minecraft mod
On 1.13 and newer a command’s arguments are Brigadier argument nodes: Commands.argument(“count”, IntegerArgumentType.integer(1, 64)) for a number, EntityArgument.player() for a player, chained with .then(…). In .executes you read them back by name (IntegerArgumentType.getInteger, EntityArgument.getPlayer); the game suggests and checks them as players type. On 1.12.2 a command gets its arguments as an array of strings.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Start from Commands.literal(“name”) (on 1.12.2, a CommandBase subclass).
- Add each argument with .then(Commands.argument(name, type)).
- In .executes, read the arguments by name and do the work.
- Return how many things it did (1 or more for success).
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| item | minecraft:emerald |
The item the command gives. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric 1.20 to 1.20.6
Section titled “Fabric 1.20 to 1.20.6”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's onInitialize. public static void register() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> build(dispatcher)); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(() -> Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside onInitialize:
GiftCommand.register();Fabric 1.21 to 1.21.1
Section titled “Fabric 1.21 to 1.21.1”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's onInitialize. public static void register() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> build(dispatcher)); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.parse("minecraft:emerald")), count)); context.getSource().sendSuccess(() -> Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside onInitialize:
GiftCommand.register();NeoForge 1.21 to 1.21.1
Section titled “NeoForge 1.21 to 1.21.1”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.RegisterCommandsEvent;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(GiftCommand::onRegisterCommands); }
private static void onRegisterCommands(RegisterCommandsEvent event) { build(event.getDispatcher()); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.parse("minecraft:emerald")), count)); context.getSource().sendSuccess(() -> Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside <init>:
GiftCommand.register();NeoForge 1.20.2 to 1.20.6
Section titled “NeoForge 1.20.2 to 1.20.6”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.RegisterCommandsEvent;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(GiftCommand::onRegisterCommands); }
private static void onRegisterCommands(RegisterCommandsEvent event) { build(event.getDispatcher()); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(() -> Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside <init>:
GiftCommand.register();Fabric 1.21.2 to 1.21.10
Section titled “Fabric 1.21.2 to 1.21.10”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's onInitialize. public static void register() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> build(dispatcher)); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.getValue(ResourceLocation.parse("minecraft:emerald")), count)); context.getSource().sendSuccess(() -> Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside onInitialize:
GiftCommand.register();NeoForge 1.21.2 to 1.21.10
Section titled “NeoForge 1.21.2 to 1.21.10”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.RegisterCommandsEvent;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(GiftCommand::onRegisterCommands); }
private static void onRegisterCommands(RegisterCommandsEvent event) { build(event.getDispatcher()); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.getValue(ResourceLocation.parse("minecraft:emerald")), count)); context.getSource().sendSuccess(() -> Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside <init>:
GiftCommand.register();NeoForge 1.21.11 to 26.3
Section titled “NeoForge 1.21.11 to 26.3”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
Since 1.21.11 permissions are objects: source.permissions().hasPermission(new Permission.HasCommandLevel(PermissionLevel.byId(n))).
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.server.permissions.Permission;import net.minecraft.server.permissions.PermissionLevel;import net.minecraft.resources.Identifier;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.RegisterCommandsEvent;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(GiftCommand::onRegisterCommands); }
private static void onRegisterCommands(RegisterCommandsEvent event) { build(event.getDispatcher()); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.permissions().hasPermission(new Permission.HasCommandLevel(PermissionLevel.byId(2)))) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:emerald")), count)); context.getSource().sendSuccess(() -> Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside <init>:
GiftCommand.register();Fabric 1.21.11 to 26.3
Section titled “Fabric 1.21.11 to 26.3”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.server.permissions.Permission;import net.minecraft.server.permissions.PermissionLevel;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.Identifier;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's onInitialize. public static void register() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> build(dispatcher)); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.permissions().hasPermission(new Permission.HasCommandLevel(PermissionLevel.GAMEMASTERS))) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:emerald")), count)); context.getSource().sendSuccess(() -> Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside onInitialize:
GiftCommand.register();Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import net.minecraft.network.chat.TextComponent;import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.Registry;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.RegisterCommandsEvent;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(GiftCommand::onRegisterCommands); }
private static void onRegisterCommands(RegisterCommandsEvent event) { build(event.getDispatcher()); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(new TextComponent("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside <init>:
GiftCommand.register();Forge 1.19 to 1.19.2
Section titled “Forge 1.19 to 1.19.2”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.Registry;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.RegisterCommandsEvent;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(GiftCommand::onRegisterCommands); }
private static void onRegisterCommands(RegisterCommandsEvent event) { build(event.getDispatcher()); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside <init>:
GiftCommand.register();Forge 1.19.3 to 1.19.4
Section titled “Forge 1.19.3 to 1.19.4”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.RegisterCommandsEvent;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(GiftCommand::onRegisterCommands); }
private static void onRegisterCommands(RegisterCommandsEvent event) { build(event.getDispatcher()); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside <init>:
GiftCommand.register();Forge 1.20 to 1.20.1, NeoForge 1.20.1
Section titled “Forge 1.20 to 1.20.1, NeoForge 1.20.1”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.RegisterCommandsEvent;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(GiftCommand::onRegisterCommands); }
private static void onRegisterCommands(RegisterCommandsEvent event) { build(event.getDispatcher()); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(() -> Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside <init>:
GiftCommand.register();Fabric 1.19.3 to 1.19.4
Section titled “Fabric 1.19.3 to 1.19.4”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's onInitialize. public static void register() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> build(dispatcher)); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside onInitialize:
GiftCommand.register();Fabric 1.19 to 1.19.2
Section titled “Fabric 1.19 to 1.19.2”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.Registry;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's onInitialize. public static void register() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> build(dispatcher)); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(Component.literal("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside onInitialize:
GiftCommand.register();Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import net.minecraft.util.text.StringTextComponent;
import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.command.CommandSource;import net.minecraft.command.Commands;import net.minecraft.command.arguments.EntityArgument;import net.minecraftforge.registries.ForgeRegistries;import net.minecraft.util.ResourceLocation;import net.minecraft.entity.player.ServerPlayerEntity;import net.minecraft.item.ItemStack;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.RegisterCommandsEvent;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(GiftCommand::onRegisterCommands); }
private static void onRegisterCommands(RegisterCommandsEvent event) { build(event.getDispatcher()); }
private static void build(CommandDispatcher<CommandSource> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayerEntity player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.inventory.add(new ItemStack(ForgeRegistries.ITEMS.getValue(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(new StringTextComponent("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside <init>:
GiftCommand.register();Fabric 1.17 to 1.18.2
Section titled “Fabric 1.17 to 1.18.2”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import net.minecraft.network.chat.TextComponent;import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.Registry;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's onInitialize. public static void register() { CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> build(dispatcher)); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.getInventory().add(new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(new TextComponent("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside onInitialize:
GiftCommand.register();Fabric 1.16.5
Section titled “Fabric 1.16.5”Uses:
Commands.argumentIntegerArgumentType.integerEntityArgument.playerEntityArgument.getPlayer
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import net.minecraft.network.chat.TextComponent;import com.mojang.brigadier.CommandDispatcher;import com.mojang.brigadier.arguments.IntegerArgumentType;import net.minecraft.commands.CommandSourceStack;import net.minecraft.commands.Commands;import net.minecraft.commands.arguments.EntityArgument;import net.minecraft.core.Registry;import net.minecraft.network.chat.Component;import net.minecraft.resources.ResourceLocation;import net.minecraft.server.level.ServerPlayer;import net.minecraft.world.item.ItemStack;import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2).public final class GiftCommand { private GiftCommand() { }
// Called from the mod's onInitialize. public static void register() { CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> build(dispatcher)); }
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) { dispatcher.register(Commands.literal("gift") .requires(source -> source.hasPermission(2)) .then(Commands.argument("player", EntityArgument.player()) .then(Commands.argument("count", IntegerArgumentType.integer(1, 64)) .executes(context -> { ServerPlayer player = EntityArgument.getPlayer(context, "player"); int count = IntegerArgumentType.getInteger(context, "count"); player.inventory.add(new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:emerald")), count)); context.getSource().sendSuccess(new TextComponent("Gave " + count + " to ").append(player.getName()), true); return count; })))); }}In your main mod class, inside onInitialize:
GiftCommand.register();Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
CommandBase.getPlayerCommandBase.parseIntCommandBase.getTabCompletionsCommandBase.notifyCommandListener
1.12.2 commands get their arguments as an array of strings; CommandBase has the helpers that read them (getPlayer, parseInt).
src/main/java/com/example/examplemod/command/GiftCommand.java
package com.example.examplemod.command;
import com.example.examplemod.ExampleMod;import java.util.List;import javax.annotation.Nullable;import net.minecraft.command.CommandBase;import net.minecraft.command.CommandException;import net.minecraft.command.CommandHandler;import net.minecraft.command.ICommandSender;import net.minecraft.entity.player.EntityPlayerMP;import net.minecraft.item.ItemStack;import net.minecraft.server.MinecraftServer;import net.minecraft.util.math.BlockPos;import net.minecraft.util.ResourceLocation;import net.minecraftforge.event.world.WorldEvent;import net.minecraftforge.fml.common.Mod;import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;import net.minecraftforge.fml.common.registry.ForgeRegistries;
// /gift <player> <count>: gives a player some minecraft:emerald; operators only (permission 2). Arguments are strings: CommandBase has helpers to read them.@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)public class GiftCommand extends CommandBase { @Override public String getName() { return "gift"; }
@Override public String getUsage(ICommandSender sender) { return "/gift <player> <count>"; }
@Override public int getRequiredPermissionLevel() { return 2; }
@Override public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { if (args.length < 2) { throw new net.minecraft.command.WrongUsageException(getUsage(sender)); } EntityPlayerMP player = getPlayer(server, sender, args[0]); int count = parseInt(args[1], 1, 64); player.addItemStackToInventory(new ItemStack(ForgeRegistries.ITEMS.getValue(new ResourceLocation("minecraft:emerald")), count)); notifyCommandListener(sender, this, "Gave " + count + " to " + player.getName()); }
@Override public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) { return args.length == 1 ? getListOfStringsMatchingLastWord(args, server.getOnlinePlayerNames()) : super.getTabCompletions(server, sender, args, targetPos); }
// FMLServerStartingEvent only reaches the @Mod class, so this handler registers the command when the overworld loads. @SubscribeEvent public static void onWorldLoad(WorldEvent.Load event) { if (!event.getWorld().isRemote && event.getWorld().provider.getDimension() == 0) { ((CommandHandler) event.getWorld().getMinecraftServer().getCommandManager()).registerCommand(new GiftCommand()); } }}Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/commands/arguments
- https://docs.neoforged.net/docs/misc/commands
Related how-tos
Section titled “Related how-tos”- A chat command
- Run code every tick
- A command that runs on the client
- A block that gives off light
- A creative tab of your own
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.