Skip to content

Command Alias (0.3.0 0.4.2)

Yao Chung Hu edited this page Jan 13, 2022 · 1 revision

Command Alias

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

Create a command alias using COMMAND_ALIAS

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

{
	"commandMode": "COMMAND_ALIAS"
}

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

{
	"commandMode": "COMMAND_ALIAS",
	"command": "tools"
}

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

{
	"commandMode": "COMMAND_ALIAS",
	"command": "tools",
	"message": "Here are some free wooden tools"
}

Let's get the executor's name and bind it into our message. See class tools for more information about execution class tool.

We can get the executor's name using {this::SELF}.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "tools",
	"message": "Here are some free wooden tools, {this::SELF}!"
}
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 execution which takes in an array of command alias objects.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "tools",
	"execution": [],
	"message": "Here are some free wooden tools, {this::SELF}!"
}

We will need to create a command alias object inside the command alias.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "tools",
	"execution": [
		{
			"command": "give {this::SELF} minecraft:wooden_sword"
		}
	],
	"message": "Here are some free wooden tools, {this::SELF}!"
}

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 runs the command(in this case the /give command). This means if the executor doesn't have permission to use the /give command our command alias will error out. Now to get around this issue, we can use SERVER. Using SERVER will imply the internal/dedicated server executes said command alias, 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_ALIAS",
	"command": "tools",
	"execution": [
		{
			"command": "give {this::SELF} minecraft:wooden_sword",
			"type": "SERVER"
		}
	],
	"message": "Here are some free wooden tools, {this::SELF}!"
}

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

{
	"commandMode": "COMMAND_ALIAS",
	"command": "tools",
	"execution": [
		{
			"command": "give {this::SELF} minecraft:wooden_sword",
			"type": "SERVER"
		},
		{
			"command": "give {this::SELF} minecraft:wooden_pickaxe",
			"type": "SERVER"
		},
		{
			"command": "give {this::SELF} minecraft:wooden_axe",
			"type": "SERVER"
		},
		{
			"command": "give {this::SELF} minecraft:wooden_shovel",
			"type": "SERVER"
		}
	],
	"message": "Here are some free wooden tools, {this::SELF}!"
}

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_ALIAS",
	"command": "tools",
	"execution": [
		{
			"command": "give {this::SELF} minecraft:wooden_sword",
			"type": "SERVER",
			"sleep": "1000"
		},
		{
			"command": "give {this::SELF} minecraft:wooden_pickaxe",
			"type": "SERVER",
			"sleep": "1000"
		},
		{
			"command": "give {this::SELF} minecraft:wooden_axe",
			"type": "SERVER",
			"sleep": "1000"
		},
		{
			"command": "give {this::SELF} minecraft:wooden_shovel",
			"type": "SERVER"
		}
	],
	"message": "Here are some free wooden tools, {this::SELF}!"
}

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

{
	"commandMode": "COMMAND_ALIAS",
	"command": "tools",
	"execution": [
		{
			"command": "give {this::SELF} minecraft:wooden_sword",
			"type": "SERVER",
			"message": "Here is a wooden sword",
			"sleep": "1000"
		},
		{
			"command": "give {this::SELF} minecraft:wooden_pickaxe",
			"type": "SERVER",
			"message": "Here is a wooden pickaxe",
			"sleep": "1000"
		},
		{
			"command": "give {this::SELF} minecraft:wooden_axe",
			"type": "SERVER",
			"message": "Here is a wooden axe",
			"sleep": "1000"
		},
		{
			"command": "give {this::SELF} minecraft:wooden_shovel",
			"type": "SERVER",
			"message": "Here is a wooden shovel, {this::SELF@toUpper}!"
		}
	],
	"message": "Here are some free wooden tools, {this::SELF}!"
}
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 say I have a command redirect that isn't working how I would like it to. Let's say I want to redirect /scoreboard objectives setdisplay sidebar using /sidebar.

  • Why doesn't command redirect redirect this? Simply because sidebar is an argument not a literal.
  • How can I get around this? You can get around this by creating an command alias.
  • Is there anything that Command Aliases can do to fix this? At the moment, not really maybe in the future.
{
	"commandMode": "COMMAND_REDIRECT",
	"command": "sidebar",
	"redirectTo": "scoreboard objectives setdisplay sidebar"
}

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

Let's setup our command alias

{
	"commandMode": "COMMAND_ALIAS",
	"command": "sidebar"
}

The command /scoreboard objectives setdisplay sidebar <objective> takes in an "objective" argument. See Argument Types for full list. We will specify our required argument to our command alias.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "sidebar {arg::minecraft:objective}"
}

Now that we have specified our argument type, you will need to assign it a variable name. I will name mines Objective.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "sidebar {arg::minecraft:objective#Objective}"
}

To continue, we will bind our Objective to our execution command.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "sidebar {arg::minecraft:objective#Objective}",
	"execution": [
		{
			"command": "scoreboard objectives setdisplay sidebar {Objective}"
		}
	]
}

The /scoreboard command is using used by mapmakers and server operators. So in this case you don't want executors without permissions to be changing it. So we will be using CLIENT.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "sidebar {arg::minecraft:objective#Objective}",
	"execution": [
		{
			"command": "scoreboard objectives setdisplay sidebar {Objective}",
			"type": "CLIENT"
		}
	]
}

Now let's make a command for staffs team, side note you can set the command permissions with Player Roles by Gegy and in the future LuckPerms.

{
	"command": "staffchat {arg::minecraft:message#message}",
	"execution": [
		{
			"command": "tellraw @a[team=staff] {\"text\":\"[{this::SELF}] {message}\"}",
			"type": "CLIENT"
		}
	]
}

This command alias may work fine on the surface, it does have a problem.

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 because message is not escaped for JSON format. We can get around this by formatting it using @jsonString.

{
	"command": "staffchat {arg::minecraft:message#message}",
	"execution": [
		{
			"command": "tellraw @a[team=staff] {\"text\":\"[{this::SELF}] {message@jsonString}\"}",
			"type": "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_ALIAS",
	"command": "g {arg::minecraft:item_stack#item}",
	"execution": [
		{
			"command": "give {this::SELF} {item}",
			"type": "CLIENT"
		}
	]
}

Assuming we want an optional count argument we specify them within [] instead of {}

{
	"commandMode": "COMMAND_ALIAS",
	"command": "g {arg::item_stack#item} [arg::brigadier:integer#count]",
	"execution": [
		{
			"command": "give {this::SELF} {item} [count]",
			"type": "CLIENT"
		}
	]
}

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

{
	"commandMode": "COMMAND_ALIAS",
	"command": "g {arg::minecraft:item_stack#item} [arg::brigadier:integer#count]",
	"execution": [
		{
			"command": "give {this::SELF} {item} [count]",
			"type": "CLIENT",
			"message": "We gave {this::SELF} [count]x{item}"
		}
	]
}

Examples

Spawn Command

Created by: @amnotbananaama

{
	"commandMode": "COMMAND_ALIAS",
	"command": "spawn",
	"execution": [
		{
			"command": "execute in minecraft:spawn run tp {this::SELF} 0.5 178 -9.5 0 0",
			"type": "SERVER"
		}
	]
}

Broadcast Command

Created by: @amnotbananaama

{
	"commandMode": "COMMAND_ALIAS",
	"command": "broadcast {arg::minecraft:greedy_string#message}",
	"execution": [
		{
			"command": "tellraw @a [\"\",{\"text\":\"[\",\"color\":\"dark_gray\"},{\"text\":\"Broadcast\",\"color\":\"dark_red\",\"bold\": true},{\"text\":\"] \",\"color\":\"dark_gray\"},{\"text\":\"{message@jsonString}\",\"color\":\"dark_red\"}]",
			"type": "SERVER",
			"ignoreOptionalRemoval": true
		}
	]
}

Local Chat Command

Created by: @amnotbananaama

It's a "local chat" command. It sends your message to players within a 200 block sphere of you.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "local {arg::minecraft:greedy_string#message}",
	"execution": [
		{
			"command": "execute at {this::SELF} run tellraw @a[distance=..200] {\"text\":\"[Local] {this::SELF}:{message@jsonString}\",\"color\":\"gray\"}",
			"type": "SERVER",
			"ignoreOptionalRemoval": true
		}
	]
}

Sudo Command

Created by: @amnotbananaama

A sudo command to run commands as another player.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "sudo {arg::minecraft:player#player} {arg::minecraft:greedy_string#command}",
	"execution": [
		{
			"command": "execute as {player} at {player} run {command}",
			"type": "SERVER"
		}
	]
}

HelpOP Command

Create by: @amnotbananaama

HelpOP command, checks for anyone on the team named "staff" online and sends your message to them. If no staff are online it gives you a link to our discord.

{
	"commandMode": "COMMAND_ALIAS",
	"command": "helpop {arg::minecraft:greedy_string#message}",
	"execution": [
		{
			"command": "execute if entity @a[team=Staff] run tellraw @a[team=Staff] [\"\",{\"text\":\"[\",\"color\":\"gold\"},{\"text\":\"HelpOP\",\"color\":\"dark_red\"},{\"text\":\"] \",\"color\":\"gold\"},{\"text\":\"{this::SELF}\",\"color\":\"red\"},{\"text\":\": \",\"color\":\"white\"},{\"text\":\"{message}\",\"color\":\"red\"}]",
			"type": "SERVER",
			"ignoreOptionalRemoval": true
		},
		{
			"command": "execute unless entity @a[team=Staff] run tellraw {this::SELF} [\"\",{\"text\":\"[\",\"color\":\"gold\"},{\"text\":\"HelpOP\",\"color\":\"dark_red\"},{\"text\":\"] \",\"color\":\"gold\"},{\"text\":\"There are currently no staff online!\",\"color\":\"red\"}]",
			"type": "SERVER",
			"sleep": "1000"
		},
		{
			"command": "execute unless entity @a[team=Staff] run tellraw {this::SELF} [\"\",{\"text\":\"[\",\"color\":\"gold\"},{\"text\":\"HelpOP\",\"color\":\"dark_red\"},{\"text\":\"] \",\"color\":\"gold\"},{\"text\":\"You can get help in our discord:\",\"color\":\"red\"},{\"text\":\"\\n\"},{\"text\":\"URL GOES HERE\",\"color\":\"gold\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"URL GOES HERE\"}}]",
			"type": "SERVER"
		}
	]
}
Clone this wiki locally