Skip to content

Command Custom (0.4.0 0.6.0)

Yao Chung Hu edited this page Jun 15, 2022 · 1 revision

Command Custom

Note: This is still a work in progress, binding and formatting functions will change in the future.

When using command mode COMMAND_CUSTOM, it creates a new command that can run one or more existing commands with required/optional arguments passthrough.

Create a custom command using COMMAND_CUSTOM

First, we need to set our command mode to COMMAND_CUSTOM.

{
	"commandMode": "COMMAND_CUSTOM"
}

Secondly, we define our custom command object.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
    }
}

Now that we have set our command mode, we can define our new command.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "tools"
	}
}

Let's send a message to the command executor(the player/console that runs the command).

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "tools"
		"message": "Here are some free wooden tools"
	}
}

Let's get the executor's name and bind it to our message.

We can get the executor's name using $executor_name().

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "tools",
		"message": "Here are some free wooden tools, $executor_name()!"
	}
}
Scenario
  1. Player123 runs /tools

  2. Output will return Here are some free wooden tools, Player123!

Now let's give the player a wooden sword. This can be done by using the field actions which takes in an array of command action objects.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "tools",
		"actions": [],
		"message": "Here are some free wooden tools, $executor_name()!"
	}
}

We will need to create a command action object inside the custom command.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "tools",
		"actions": [
			{
				"command": "give $executor_name() minecraft:wooden_sword"
			}
		],
		"message": "Here are some free wooden tools, $executor_name()!"
	}
}

Before we go ahead and run the command. We need to define the type of execution between CLIENT and SERVER. Using CLIENT will imply that the executors run the command(in this case the /give command). This means if the executor doesn't have permission to use the /give command our custom command will error out. Now to get around this issue, we can use SERVER. Using SERVER will imply the internal/dedicated server executes said the custom command, not the executor. In this case for our /tools command, we will need to use SERVER since the players don't have access to the /give command.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "tools",
		"actions": [
			{
				"command": "give $executor_name() minecraft:wooden_sword",
				"commandType": "SERVER"
			}
		],
		"message": "Here are some free wooden tools, $executor_name()!"
	}
}

Now let's give the player all the wooden tools.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "tools",
		"actions": [
			{
				"command": "give $executor_name() minecraft:wooden_sword",
				"commandType": "SERVER"
			},
			{
				"command": "give $executor_name() minecraft:wooden_pickaxe",
				"commandType": "SERVER"
			},
			{
				"command": "give $executor_name() minecraft:wooden_axe",
				"commandType": "SERVER"
			},
			{
				"command": "give $executor_name() minecraft:wooden_shovel",
				"commandType": "SERVER"
			}
		],
		"message": "Here are some free wooden tools, $executor_name()!"
	}
}

We can also add a delay between commands using sleep. Sleeps in milliseconds~~, you can also specify an execution class tool on it.~~

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "tools",
		"message": "Here are some free wooden tools, $executor_name()!",
		"actions": [
			{
				"command": "give $executor_name() minecraft:wooden_sword",
				"commandType": "SERVER",
				"sleep": "1000"
			},
			{
				"command": "give $executor_name() minecraft:wooden_pickaxe",
				"commandType": "SERVER",
				"sleep": "1000"
			},
			{
				"command": "give $executor_name() minecraft:wooden_axe",
				"commandType": "SERVER",
				"sleep": "1000"
			},
			{
				"command": "give $executor_name() minecraft:wooden_shovel",
				"commandType": "SERVER"
			}
		]
	}
}

You can also add a message in the command action object. Which also binds execution class tools objects.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "tools",
		"message": "Here are some free wooden tools, $executor_name()!",
		"actions": [
			{
				"command": "give $executor_name() minecraft:wooden_sword",
				"commandType": "SERVER",
				"sleep": "1000",
				"message": "Here is a wooden sword"
			},
			{
				"command": "give $executor_name() minecraft:wooden_pickaxe",
				"commandType": "SERVER",
				"sleep": "1000",
				"message": "Here is a wooden pickaxe"
			},
			{
				"command": "give $executor_name() minecraft:wooden_axe",
				"commandType": "SERVER",
				"sleep": "1000",
				"message": "Here is a wooden axe"
			},
			{
				"command": "give $executor_name() minecraft:wooden_shovel",
				"commandType": "SERVER",
				"message": "Here is a wooden shovel, $executor_name()!"
			}
		]
	}
}
Scenario
  1. Player123 runs /tools
  2. Drops wooden sword, and prints Here is a wooden sword; sleeps for 1 second
  3. Drops wooden pickaxe, and prints Here is a wooden pickaxe; sleeps for 1 second
  4. Drops wooden axe, and prints Here is a wooden axe; sleeps for 1 second
  5. Drops wooden shovel, and prints Here is a wooden shovel, Player123!
  6. Final output Here are some free wooden tools, Player123!

Notice we also play with string formatting. This is useful in certain cases like /tellraw, but I will explain this further down.

Now let's make a command for the staff team, you can set the command permissions level from 1 - 4.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "staffchat",
		"permission": 1,
		"message": "Try using /staffchat <Message>",
		"children": []
	}
}

Let's create a child component for /staffchat, the child component will be the type of argument or literal.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "staffchat",
		"permission": 1,
		"message": "Try using /staffchat <Message>",
		"children": [
			{
				"child": "message",
				"type": "argument",
				"argumentType": "minecraft:greedy_string"
			}
		]
	}
}

A required/optional argument is determined by whether the message/actions field is empty or not. Let's add our command action.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "staffchat",
		"permission": 1,
		"message": "Try using /staffchat <Message>",
		"children": [
			{
				"child": "message",
				"type": "argument",
				"argumentType": "minecraft:greedy_string",
				"actions": [
					{
						"command": "tellraw @a[team=staff] {\"text\":\"[$executor_name()] {{message}}\"}",
						"commandType": "CLIENT"
					}
				]
			}
		]
	}
}
Scenario
  1. Player123 runs /staffchat Hello staffs
  2. Output will return [Player123] Hello staffs
  3. Player321 runs /staffchat PlayerXYZ might be "cheating"
  4. Output will error out.

The reason this happens is that message is not escaped for JSON format. We can get around this by formatting it using @jsonString.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "staffchat",
		"permission": 1,
		"message": "Try using /staffchat <Message>",
		"children": [
			{
				"child": "message",
				"type": "argument",
				"argumentType": "minecraft:greedy_string",
				"actions": [
					{
						"command": "tellraw @a[team=staff] {\"text\":\"[$executor_name()] {{message@jsonString}}\"}",
						"commandType": "CLIENT"
					}
				]
			}
		]
	}
}

It can be appended either on command at {arg::message#message@jsonString} or appended in execution like done above {message@jsonString}.

There are some cases you will need/want an optional argument. Since command redirects can only do so much. Let's take an invalid command redirect.

{
	"commandMode": "COMMAND_REDIRECT",
	"command": "g",
	"redirectTo": "give @a"
}

Note: The example above is not a valid command redirect.

First, we create our command alias with the required arguments

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "g",
		"permission": 1,
		"message": "Try using /g <item> [count]",
		"children": [
			{
				"child": "item",
				"type": "argument",
				"argumentType": "minecraft:item_stack",
				"actions": [
					{
						"command": "give $executor_name() {{item}}",
						"commandType": "CLIENT"
					}
				]
			}
		]
	}
}

Assuming we want an optional count argument we specify them by creating another child component in the original child component.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "g",
		"permission": 1,
		"message": "Try using /g <item> [count]",
		"children": [
			{
				"child": "item",
				"type": "argument",
				"argumentType": "minecraft:item_stack",
				"actions": [
					{
						"command": "give $executor_name() {{item}}",
						"commandType": "CLIENT"
					}
				],
				"children": [
					{
						"child": "count",
						"type": "argument",
						"argumentType": "brigadier:integer",
						"actions": [
							{
								"command": "give $executor_name() {{item}} {{count}}",
								"commandType": "CLIENT"
							}
						]
					}
				]
			}
		]
	}
}

We can now specify an optional count, and for fun let's add a message.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "g",
		"permission": 1,
		"message": "Try using /g <item> [count]",
		"children": [
			{
				"child": "item",
				"type": "argument",
				"argumentType": "minecraft:item_stack",
				"actions": [
					{
						"command": "give $executor_name() {{item}}",
						"commandType": "CLIENT"
					}
				],
				"children": [
					{
						"child": "count",
						"type": "argument",
						"argumentType": "brigadier:integer",
						"actions": [
							{
								"command": "give $executor_name() {{item}} {{count}}",
								"commandType": "CLIENT",
								"message": "We gave $executor_name() {{count}}x{{item}}"
							}
						]
					}
				]
			}
		]
	}
}

We can create another child component for specifying which player to give instead of giving only to the executor.

{
	"commandMode": "COMMAND_CUSTOM",
	"customCommand": {
		"parent": "g",
		"permission": 1,
		"message": "Try using /g <item> [count]",
		"children": [
			{
				"child": "item",
				"type": "argument",
				"argumentType": "minecraft:item_stack",
				"actions": [
					{
						"command": "give $executor_name() {{item}}",
						"commandType": "CLIENT"
					}
				],
				"children": [
					{
						"child": "count",
						"type": "argument",
						"argumentType": "brigadier:integer",
						"actions": [
							{
								"command": "give $executor_name() {{item}} {{count}}",
								"commandType": "CLIENT",
								"message": "We gave $executor_name() {{count}}x{{item}}"
							}
						]
					}
				]
			},
			{
				"child": "player",
				"type": "argument",
				"argumentType": "minecraft:player",
				"message": "Try using /g <player> <item> [count]",
				"children": [
					{
						"child": "item",
						"type": "argument",
						"argumentType": "minecraft:item_stack",
						"actions": [
							{
								"command": "give {{player}} {{item}}",
								"commandType": "CLIENT"
							}
						],
						"children": [
							{
								"child": "count",
								"type": "argument",
								"argumentType": "brigadier:integer",
								"actions": [
									{
										"command": "give {{player}} {{item}} {{count}}",
										"commandType": "CLIENT",
										"message": "We gave {{player}} {{count}}x{{item}}"
									}
								]
							}
						]
					}
				]
			}
		]
	}
}

Examples

Coming soon

Clone this wiki locally