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.
public final class BroadCastCommand { public static void register(CommandDispatcher<ServerCommandSource> dispatcher){ dispatcher.register(literal("broadcast") .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. .then(argument("color", ColorArgumentType.color()) .then(argument("message", greedyString()) .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. } final Text text = Text.literal(message).formatted(formatting); source.getMinecraftServer().getPlayerManager().broadcastChatMessage(text, MessageType.CHAT, source.getPlayer().getUuid()); return Command.SINGLE_SUCCESS; // Success } }
In your initializer:
public class ExampleMod implements ModInitializer{ @Override public void onInitialize() { ... CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> register(dispatcher)); } }
In the examples below, registrations in the callbacks are ommited.
First the basic code where we register “giveMeDiamond” as a literal and then an executes
block to tell the dispatcher which method to run.
public static LiteralCommandNode register(CommandDispatcher<ServerCommandSource> dispatcher) { // You can also return a LiteralCommandNode for use with possible redirects return dispatcher.register(literal("giveMeDiamond") .executes(ctx -> giveDiamond(ctx))); }
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.
public static int giveDiamond(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException { final ServerCommandSource source = ctx.getSource(); 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:
if(!player.inventory.insertStack(new ItemStack(Items.DIAMOND))){ throw new CommandException(Text.translatable("commands.giveMeDiamond.isfull")); } source.sendFeedback(() -> Text.translatable("commands.giveMeDiamond.success", self.getDisplayName()), true); return 1; }
…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.
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { dispatcher.register(literal("antioch") .then(required("location", BlockPosArgumentType.blockPos() .executes(ctx -> antioch(ctx.getSource(), BlockPosArgument.getBlockPos(ctx, "location"))))) .executes(ctx -> antioch(ctx.getSource(), null))); }
Then the creation and messages behind the joke.
public static int antioch(ServerCommandSource source, BlockPos blockPos) throws CommandSyntaxException { if(blockPos == null) { // 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. // This class is used as an example and actually doesn't exist yet. blockPos = LocationUtil.calculateCursorOrThrow(source, source.getRotation()); } final TntEntity tnt = new TntEntity(source.getWorld(), blockPos.getX(), blockPos.getY(), blockPos.getZ(), null); tnt.setFuse(3); 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); source.getServer().getPlayerManager().broadcastChatMessage(Text.literal("who being naughty in My sight, shall snuff it.", MessageType.CHAT, UUID.randomUUID()).formatted(Formatting.RED), false); source.getWorld().spawnEntity(tnt); return 1; }